1 (* coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 {$INCLUDE ../shared/a_modes.inc}
18 {$DEFINE FUI_TEXT_ICONS}
27 fui_common
, fui_events
;
30 // ////////////////////////////////////////////////////////////////////////// //
39 function charWidth (const ch
: AnsiChar): Integer; virtual; abstract;
40 function textWidth (const s
: AnsiString): Integer; virtual; abstract;
43 property name
: AnsiString read mName
;
44 property height
: Integer read mHeight
;
45 property baseLine
: Integer read mBaseLine
;
65 // for active contexts
72 function getFont (): AnsiString;
73 procedure setFont (const aname
: AnsiString);
75 procedure onActivate ();
76 procedure onDeactivate ();
78 procedure setColor (const clr
: TGxRGBA
);
80 procedure realizeClip (); // setup scissoring
82 procedure setClipOfs (const aofs
: TGxOfs
);
83 procedure setClipRect (const aclip
: TGxRect
);
86 constructor Create ();
87 destructor Destroy (); override;
89 procedure line (x1
, y1
, x2
, y2
: Integer);
90 procedure hline (x
, y
, len
: Integer);
91 procedure vline (x
, y
, len
: Integer);
92 procedure rect (x
, y
, w
, h
: Integer);
93 procedure fillRect (x
, y
, w
, h
: Integer);
94 procedure darkenRect (x
, y
, w
, h
: Integer; a
: Integer);
96 function charWidth (const ch
: AnsiChar): Integer;
97 function charHeight (const ch
: AnsiChar): Integer;
98 function textWidth (const s
: AnsiString): Integer;
99 function textHeight (const s
: AnsiString): Integer;
100 function drawChar (x
, y
: Integer; const ch
: AnsiChar): Integer; // returns char width
101 function drawText (x
, y
: Integer; const s
: AnsiString): Integer; // returns text width
103 function iconMarkWidth (ic
: TMarkIcon
): Integer;
104 function iconMarkHeight (ic
: TMarkIcon
): Integer;
105 procedure drawIconMark (ic
: TMarkIcon
; x
, y
: Integer; marked
: Boolean);
107 function iconWinWidth (ic
: TWinIcon
): Integer;
108 function iconWinHeight (ic
: TWinIcon
): Integer;
109 procedure drawIconWin (ic
: TWinIcon
; x
, y
: Integer; pressed
: Boolean);
111 procedure resetClip ();
113 function setOffset (constref aofs
: TGxOfs
): TGxOfs
; // returns previous offset
114 function setClip (constref aclip
: TGxRect
): TGxRect
; // returns previous clip
116 function combineClip (constref aclip
: TGxRect
): TGxRect
; // returns previous clip
119 procedure glSetScale (ascale
: Single);
120 procedure glSetTrans (ax
, ay
: Single);
121 procedure glSetScaleTrans (ascale
, ax
, ay
: Single);
124 property active
: Boolean read mActive
;
125 property color
: TGxRGBA read mColor write setColor
;
126 property font
: AnsiString read getFont write setFont
;
127 property offset
: TGxOfs read mClipOfs write setClipOfs
;
128 property clip
: TGxRect read mClipRect write setClipRect
; // clipping is unaffected by offset
132 // set active context; `ctx` can be `nil`
133 procedure gxSetContext (ctx
: TGxContext
; ascale
: Single=1.0);
134 procedure gxSetContextNoMatrix (ctx
: TGxContext
);
137 // setup 2D OpenGL mode; will be called automatically in `glInit()`
138 procedure oglSetup2D (winWidth
, winHeight
: Integer; upsideDown
: Boolean=false);
139 procedure oglSetup2DState (); // don't modify viewports and matrices
141 procedure oglDrawCursor ();
142 procedure oglDrawCursorAt (msX
, msY
: Integer);
145 procedure fuiGfxLoadFont (const fontname
: AnsiString; const fontFile
: AnsiString; proportional
: Boolean=false);
146 procedure fuiGfxLoadFont (const fontname
: AnsiString; st
: TStream
; proportional
: Boolean=false);
149 // ////////////////////////////////////////////////////////////////////////// //
151 gGfxDoClear
: Boolean = true;
161 // ////////////////////////////////////////////////////////////////////////// //
162 // returns `false` if the color is transparent
163 // returns `false` if the color is transparent
164 function setupGLColor (constref clr
: TGxRGBA
): Boolean;
166 if (clr
.a
< 255) then
169 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
175 glColor4ub(clr
.r
, clr
.g
, clr
.b
, clr
.a
);
176 result
:= (clr
.a
<> 0);
179 function isScaled (): Boolean;
181 mt
: packed array [0..15] of Double;
183 glGetDoublev(GL_MODELVIEW_MATRIX
, @mt
[0]);
184 result
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
188 // ////////////////////////////////////////////////////////////////////////// //
189 //TODO: OpenGL framebuffers and shaders state
191 TSavedGLState
= record
194 gltextbinding
: GLint
;
196 //oldfbr, oldfbw: GLint;
197 glvport
: packed array [0..3] of GLint
;
201 constructor Create (dosave
: Boolean);
203 procedure restore ();
206 constructor TSavedGLState
.Create (dosave
: Boolean);
208 FillChar(self
, sizeof(self
), 0);
209 if (dosave
) then save();
212 procedure TSavedGLState
.save ();
214 if (saved
) then raise Exception
.Create('cannot save into already saved OpenGL state');
215 glGetIntegerv(GL_MATRIX_MODE
, @glmatmode
);
216 glGetIntegerv(GL_TEXTURE_BINDING_2D
, @gltextbinding
);
217 glGetIntegerv(GL_VIEWPORT
, @glvport
[0]);
218 //glGetIntegerv(GL_CURRENT_PROGRAM, &oldprg);
219 //glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &oldfbr);
220 //glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfbw);
221 glMatrixMode(GL_PROJECTION
); glPushMatrix();
222 glMatrixMode(GL_MODELVIEW
); glPushMatrix();
223 glMatrixMode(GL_TEXTURE
); glPushMatrix();
224 glMatrixMode(GL_COLOR
); glPushMatrix();
225 glPushAttrib({GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_CURRENT_BIT}GL_ALL_ATTRIB_BITS
); // let's play safe
229 procedure TSavedGLState
.restore ();
231 if (not saved
) then raise Exception
.Create('cannot restore unsaved OpenGL state');
232 glPopAttrib({GL_ENABLE_BIT});
233 glMatrixMode(GL_PROJECTION
); glPopMatrix();
234 glMatrixMode(GL_MODELVIEW
); glPopMatrix();
235 glMatrixMode(GL_TEXTURE
); glPopMatrix();
236 glMatrixMode(GL_COLOR
); glPopMatrix();
237 glMatrixMode(glmatmode
);
238 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, oldfbr);
239 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, oldfbw);
240 glBindTexture(GL_TEXTURE_2D
, gltextbinding
);
241 //if (glHasFunc!"glUseProgram") glUseProgram(oldprg);
242 glViewport(glvport
[0], glvport
[1], glvport
[2], glvport
[3]);
248 curCtx
: TGxContext
= nil;
249 savedGLState
: TSavedGLState
;
252 // ////////////////////////////////////////////////////////////////////////// //
253 // set active context; `ctx` can be `nil`
254 procedure gxSetContextInternal (ctx
: TGxContext
; ascale
: Single; domatrix
: Boolean);
256 mt
: packed array [0..15] of Double;
258 if (savedGLState
.saved
) then savedGLState
.restore();
260 if (curCtx
<> nil) then
262 curCtx
.onDeactivate();
263 curCtx
.mActive
:= false;
273 oglSetup2D(fuiScrWdt
, fuiScrHgt
);
274 glScalef(ascale
, ascale
, 1.0);
275 ctx
.mScaled
:= (ascale
<> 1.0);
276 ctx
.mScale
:= ascale
;
280 // assume uniform scale
281 glGetDoublev(GL_MODELVIEW_MATRIX
, @mt
[0]);
282 ctx
.mScaled
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
291 procedure gxSetContext (ctx
: TGxContext
; ascale
: Single=1.0); begin gxSetContextInternal(ctx
, ascale
, true); end;
292 procedure gxSetContextNoMatrix (ctx
: TGxContext
); begin gxSetContextInternal(ctx
, 1, false); end;
295 // ////////////////////////////////////////////////////////////////////////// //
297 TScissorSave
= record
300 scxywh
: packed array[0..3] of GLint
;
305 procedure save (enableScissoring
: Boolean);
306 procedure restore ();
308 // set new scissor rect, bounded by the saved scissor rect
309 procedure combineRect (x
, y
, w
, h
: Integer);
313 procedure TScissorSave
.save (enableScissoring
: Boolean);
315 wassc
:= (glIsEnabled(GL_SCISSOR_TEST
) <> 0);
316 if wassc
then glGetIntegerv(GL_SCISSOR_BOX
, @scxywh
[0]) else glGetIntegerv(GL_VIEWPORT
, @scxywh
[0]);
317 //conwritefln('(%d,%d)-(%d,%d)', [scxywh[0], scxywh[1], scxywh[2], scxywh[3]]);
318 if enableScissoring
and (not wassc
) then glEnable(GL_SCISSOR_TEST
);
321 procedure TScissorSave
.restore ();
323 glScissor(scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]);
324 if wassc
then glEnable(GL_SCISSOR_TEST
) else glDisable(GL_SCISSOR_TEST
);
327 procedure TScissorSave
.combineRect (x
, y
, w
, h
: Integer);
328 //var ox, oy, ow, oh: Integer;
330 if (w
< 1) or (h
< 1) then begin glScissor(0, 0, 0, 0); exit
; end;
331 y
:= fuiScrHgt
-(y
+h
);
332 //ox := x; oy := y; ow := w; oh := h;
333 if not intersectRect(x
, y
, w
, h
, scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]) then
335 //writeln('oops: COMBINE: old=(', ox, ',', oy, ')-(', ox+ow-1, ',', oy+oh-1, '); sci: (', scxywh[0], ',', scxywh[1], ')-(', scxywh[0]+scxywh[2]-1, ',', scxywh[1]+scxywh[3]-1, ')');
336 //writeln('oops: COMBINE: oldx=<', ox, '-', ox+ow-1, '>; oldy=<', oy, ',', oy+oh-1, '> : scix=<', scxywh[0], '-', scxywh[0]+scxywh[2]-1, '>; sciy=<', scxywh[1], '-', scxywh[1]+scxywh[3]-1, '>');
337 glScissor(0, 0, 0, 0);
341 glScissor(x
, y
, w
, h
);
346 // ////////////////////////////////////////////////////////////////////////// //
348 TGxBmpFont
= class(TGxFont
)
350 mTexId
: GLuint
; // OpenGL texture id
351 mWidth
: Integer; // <=0: proportional
354 mFreeFontWdt
: Boolean;
355 mFreeFontBmp
: Boolean;
358 procedure oglCreateTexture ();
359 procedure oglDestroyTexture ();
361 procedure initDrawText ();
362 procedure doneDrawText ();
363 function drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
364 function drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
365 function drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer; // return width (not including last empty pixel)
368 constructor Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
369 destructor Destroy (); override;
371 function charWidth (const ch
: AnsiChar): Integer; override;
372 function textWidth (const s
: AnsiString): Integer; override;
376 constructor TGxBmpFont
.Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
378 sign
: packed array [0..7] of AnsiChar;
379 enc
: packed array [0..16] of AnsiChar;
381 wdt
, hgt
, elen
: Integer;
386 mFreeFontBmp
:= true;
387 mFreeFontWdt
:= true;
391 st
.ReadBuffer(sign
[0], 8);
392 if (sign
<> 'FUIFONT0') then raise Exception
.Create('FlexUI: invalid font file signature');
393 // encoding length and width
395 wdt
:= (b
and $0f)+1; // 16 is not supported
396 if (wdt
= 16) then raise Exception
.Create('FlexUI: 16-wdt fonts aren''t supported yet');
397 elen
:= ((b
shr 4) and $0f);
398 if (elen
= 0) then raise Exception
.CreateFmt('FlexUI: invalid font encoding length: %d', [elen
]);
402 if (hgt
< 2) then raise Exception
.CreateFmt('FlexUI: invalid font height: %d', [hgt
]);
404 st
.ReadBuffer(enc
[0], elen
);
405 // check for 'cp1251' here (it can also be 'koi8')
406 if (wdt
<= 8) then fntbwdt
:= 1 else fntbwdt
:= 2;
407 // shift and width table (hi nibble: left shift for proportional print; lo nibble: shifted character width for proportional print)
408 GetMem(mFontWdt
, 256);
409 st
.ReadBuffer(mFontWdt
^, 256);
411 GetMem(mFontBmp
, (hgt
*fntbwdt
)*256);
412 st
.ReadBuffer(mFontBmp
^, (hgt
*fntbwdt
)*256);
415 mBaseLine
:= hgt
-1; //FIXME
416 if (proportional
) then
419 for ch
:= 0 to 255 do
421 for dy
:= 0 to hgt
-1 do
423 if (fntbwdt
= 1) then
425 mFontBmp
[ch
*hgt
+dy
] := mFontBmp
[ch
*hgt
+dy
] shl (mFontWdt
[ch
] shr 4);
429 wrd
:= mFontBmp
[ch
*(hgt
*2)+(dy
*2)]+256*mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1];
430 wrd
:= wrd
shl (mFontWdt
[ch
] shr 4);
431 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+0] := (wrd
and $ff);
432 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1] := ((wrd
shr 16) and $ff);
439 FillChar(mFontWdt
^, 256, wdt
);
444 destructor TGxBmpFont
.Destroy ();
446 if (mFreeFontBmp
) and (mFontBmp
<> nil) then FreeMem(mFontBmp
);
447 if (mFreeFontWdt
) and (mFontWdt
<> nil) then FreeMem(mFontWdt
);
454 mFreeFontWdt
:= false;
455 mFreeFontBmp
:= false;
461 procedure TGxBmpFont
.oglCreateTexture ();
469 x
, y
, dx
, dy
: Integer;
471 GetMem(tex
, TxWidth
*TxHeight
*4);
472 FillChar(tex
^, TxWidth
*TxHeight
*4, 0);
474 for cc
:= 0 to 255 do
478 for dy
:= 0 to mHeight
-1 do
480 if (mWidth
<= 8) then b
:= mFontBmp
[cc
*mHeight
+dy
] else b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+1];
481 //if prop then b := b shl (fontwdt[cc] shr 4);
482 tpp
:= tex
+((y
+dy
)*(TxWidth
*4))+x
*4;
485 if ((b
and $80) <> 0) then
487 tpp
^ := 255; Inc(tpp
);
488 tpp
^ := 255; Inc(tpp
);
489 tpp
^ := 255; Inc(tpp
);
490 tpp
^ := 255; Inc(tpp
);
499 b
:= (b
and $7f) shl 1;
503 b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+0];
506 if ((b
and $80) <> 0) then
508 tpp
^ := 255; Inc(tpp
);
509 tpp
^ := 255; Inc(tpp
);
510 tpp
^ := 255; Inc(tpp
);
511 tpp
^ := 255; Inc(tpp
);
520 b
:= (b
and $7f) shl 1;
526 glGenTextures(1, @mTexId
);
527 if (mTexId
= 0) then raise Exception
.Create('can''t create FlexUI font texture');
529 glBindTexture(GL_TEXTURE_2D
, mTexId
);
530 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
531 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
532 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
533 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
535 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, TxWidth
, TxHeight
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex
);
538 glBindTexture(GL_TEXTURE_2D
, 0);
543 procedure TGxBmpFont
.oglDestroyTexture ();
545 if (mTexId
<> 0) then
547 glDeleteTextures(1, @mTexId
);
553 function TGxBmpFont
.charWidth (const ch
: AnsiChar): Integer;
555 result
:= (mFontWdt
[Byte(ch
)] and $0f);
559 function TGxBmpFont
.textWidth (const s
: AnsiString): Integer;
563 if (Length(s
) > 0) then
566 for ch
in s
do result
+= (mFontWdt
[Byte(ch
)] and $0f)+1;
575 procedure TGxBmpFont
.initDrawText ();
577 glEnable(GL_ALPHA_TEST
);
578 glAlphaFunc(GL_NOTEQUAL
, 0.0);
579 glEnable(GL_TEXTURE_2D
);
580 glBindTexture(GL_TEXTURE_2D
, mTexId
);
584 procedure TGxBmpFont
.doneDrawText ();
586 glDisable(GL_ALPHA_TEST
);
587 glDisable(GL_TEXTURE_2D
);
588 glBindTexture(GL_TEXTURE_2D
, 0);
592 function TGxBmpFont
.drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer;
596 tx
:= (Integer(ch
) mod 16)*16;
597 ty
:= (Integer(ch
) div 16)*16;
599 glTexCoord2f((tx
+0)/256.0, (ty
+0)/256.0); glVertex2i(x
+0, y
+0); // top-left
600 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+0)/256.0); glVertex2i(x
+mWidth
, y
+0); // top-right
601 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+mWidth
, y
+mHeight
); // bottom-right
602 glTexCoord2f((tx
+0)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+0, y
+mHeight
); // bottom-left
604 result
:= (mFontWdt
[Byte(ch
)] and $0f);
608 function TGxBmpFont
.drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer;
611 result
:= drawCharInterim(x
, y
, ch
);
616 function TGxBmpFont
.drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer;
621 if (Length(s
) = 0) then begin result
:= 0; exit
; end;
626 wdt
:= drawCharInterim(x
, y
, ch
)+1;
634 // ////////////////////////////////////////////////////////////////////////// //
636 fontList
: array of TGxBmpFont
= nil;
637 defaultFontName
: AnsiString = 'win14';
640 function strEquCI (const s0
, s1
: AnsiString): Boolean;
645 result
:= (Length(s0
) = Length(s1
));
648 for f
:= 1 to Length(s0
) do
651 if (c0
>= 'a') and (c0
<= 'z') then Dec(c0
, 32); // poor man's `toupper()`
653 if (c1
>= 'a') and (c1
<= 'z') then Dec(c1
, 32); // poor man's `toupper()`
654 if (c0
<> c1
) then begin result
:= false; exit
; end;
660 function getFontByName (const aname
: AnsiString): TGxBmpFont
;
665 if (Length(fontList
) = 0) then raise Exception
.Create('font subsystem not initialized');
666 if (Length(aname
) = 0) or (strEquCI(aname
, 'default')) then fname
:= defaultFontName
else fname
:= aname
;
667 for f
:= 0 to High(fontList
) do
669 result
:= fontList
[f
];
670 if (result
= nil) then continue
;
671 if (strEquCI(result
.name
, fname
)) then exit
;
673 if (fontList
[0] = nil) then raise Exception
.Create('font subsystem not properly initialized');
674 result
:= fontList
[0];
679 procedure deleteFonts ();
683 for f := 0 to High(fontList) do freeAndNil(fontList[f]);
689 procedure fuiGfxLoadFont (const fontname
: AnsiString; const fontFile
: AnsiString; proportional
: Boolean=false);
693 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font '''+fontFile
+'''');
694 st
:= fuiOpenFile(fontFile
);
695 if (st
= nil) then raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
697 fuiGfxLoadFont(fontname
, st
, proportional
);
698 except on e
: Exception
do
700 writeln('FlexUI font loadin error: ', e
.message);
702 raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
711 procedure fuiGfxLoadFont (const fontname
: AnsiString; st
: TStream
; proportional
: Boolean=false);
713 fnt
: TGxBmpFont
= nil;
716 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font');
717 fnt
:= TGxBmpFont
.Create(fontname
, st
, proportional
);
719 for f
:= 0 to High(fontList
) do
721 if (strEquCI(fontList
[f
].name
, fontname
)) then
723 if (fontList
[f
].mTexId
<> 0) then raise Exception
.Create('FlexUI: cannot reload generated font named '''+fontname
+'''');
724 FreeAndNil(fontList
[f
]);
729 SetLength(fontList
, Length(fontList
)+1);
730 fontList
[High(fontList
)] := fnt
;
738 procedure oglInitFonts ();
742 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglCreateTexture();
746 procedure oglDeinitFonts ();
750 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglDestroyTexture();
754 // ////////////////////////////////////////////////////////////////////////// //
755 procedure oglSetup2DState ();
758 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
759 glDisable(GL_LINE_SMOOTH
);
760 glDisable(GL_POLYGON_SMOOTH
);
761 glDisable(GL_POINT_SMOOTH
);
762 glDisable(GL_DEPTH_TEST
);
763 glDisable(GL_TEXTURE_2D
);
764 glDisable(GL_LIGHTING
);
765 glDisable(GL_DITHER
);
766 glDisable(GL_STENCIL_TEST
);
767 glDisable(GL_SCISSOR_TEST
);
768 glDisable(GL_CULL_FACE
);
769 glDisable(GL_ALPHA_TEST
);
771 glClearColor(0, 0, 0, 0);
772 glColor4f(1, 1, 1, 1);
776 procedure oglSetup2D (winWidth
, winHeight
: Integer; upsideDown
: Boolean=false);
778 glViewport(0, 0, winWidth
, winHeight
);
782 glMatrixMode(GL_TEXTURE
);
785 glMatrixMode(GL_COLOR
);
788 glMatrixMode(GL_PROJECTION
);
792 glOrtho(0, winWidth
, 0, winHeight
, -1, 1); // set origin to bottom left
796 glOrtho(0, winWidth
, winHeight
, 0, -1, 1); // set origin to top left
799 glMatrixMode(GL_MODELVIEW
);
804 // ////////////////////////////////////////////////////////////////////////// //
805 {$INCLUDE fui_gfx_gl_cursor.inc}
807 procedure oglDrawCursor (); begin oglDrawCursorAt(fuiMouseX
, fuiMouseY
); end;
810 // ////////////////////////////////////////////////////////////////////////// //
811 constructor TGxContext
.Create ();
814 mColor
:= TGxRGBA
.Create(255, 255, 255);
815 mFont
:= getFontByName('default');
818 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
819 mClipOfs
:= TGxOfs
.Create(0, 0);
823 destructor TGxContext
.Destroy ();
825 if (mActive
) then gxSetContext(nil);
830 function TGxContext
.getFont (): AnsiString;
832 result
:= mFont
.name
;
835 procedure TGxContext
.setFont (const aname
: AnsiString);
837 mFont
:= getFontByName(aname
);
841 procedure TGxContext
.onActivate ();
843 setupGLColor(mColor
);
847 procedure TGxContext
.onDeactivate ();
852 procedure TGxContext
.setColor (const clr
: TGxRGBA
);
855 if (mActive
) then setupGLColor(mColor
);
859 procedure TGxContext
.realizeClip ();
861 sx
, sy
, sw
, sh
: Integer;
863 if (not mActive
) then exit
; // just in case
864 if (mClipRect
.w
<= 0) or (mClipRect
.h
<= 0) then
866 glEnable(GL_SCISSOR_TEST
);
867 glScissor(0, 0, 0, 0);
873 sx
:= trunc(mClipRect
.x
*mScale
);
874 sy
:= trunc(mClipRect
.y
*mScale
);
875 sw
:= trunc(mClipRect
.w
*mScale
);
876 sh
:= trunc(mClipRect
.h
*mScale
);
885 if (not intersectRect(sx
, sy
, sw
, sh
, 0, 0, fuiScrWdt
, fuiScrHgt
)) then
887 glEnable(GL_SCISSOR_TEST
);
888 glScissor(0, 0, 0, 0);
890 else if (sx
= 0) and (sy
= 0) and (sw
= fuiScrWdt
) and (sh
= fuiScrHgt
) then
892 glDisable(GL_SCISSOR_TEST
);
896 glEnable(GL_SCISSOR_TEST
);
897 sy
:= fuiScrHgt
-(sy
+sh
);
898 glScissor(sx
, sy
, sw
, sh
);
904 procedure TGxContext
.resetClip ();
906 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
907 if (mActive
) then realizeClip();
911 procedure TGxContext
.setClipOfs (const aofs
: TGxOfs
);
917 procedure TGxContext
.setClipRect (const aclip
: TGxRect
);
920 if (mActive
) then realizeClip();
924 function TGxContext
.setOffset (constref aofs
: TGxOfs
): TGxOfs
;
931 function TGxContext
.setClip (constref aclip
: TGxRect
): TGxRect
;
935 if (mActive
) then realizeClip();
939 function TGxContext
.combineClip (constref aclip
: TGxRect
): TGxRect
;
942 mClipRect
.intersect(aclip
);
943 if (mActive
) then realizeClip();
947 procedure TGxContext
.line (x1
, y1
, x2
, y2
: Integer);
949 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
951 if (not mScaled
) then
955 glVertex2f(x1
+0.375, y1
+0.375);
956 glVertex2f(x2
+0.375, y2
+0.375);
959 if (x1
<> x2
) or (y1
<> y2
) then
963 glVertex2f(x2
+0.375, y2
+0.375);
975 glVertex2i(x2
+1, y2
+1);
981 procedure TGxContext
.hline (x
, y
, len
: Integer);
983 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
984 if (len
< 1) then exit
;
985 if (not mScaled
) then
989 glVertex2f(x
+0.375, y
+0.375);
990 glVertex2f(x
+len
+0.375, y
+0.375);
993 else if (mScale
> 1.0) then
997 glVertex2i(x
+len
, y
);
998 glVertex2i(x
+len
, y
+1);
1006 while (len
> 0) do begin glVertex2i(x
, y
); Inc(x
); Dec(len
); end;
1012 procedure TGxContext
.vline (x
, y
, len
: Integer);
1014 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1015 if (len
< 1) then exit
;
1016 if (not mScaled
) then
1020 glVertex2f(x
+0.375, y
+0.375);
1021 glVertex2f(x
+0.375, y
+len
+0.375);
1024 else if (mScale
> 1.0) then
1028 glVertex2i(x
, y
+len
);
1029 glVertex2i(x
+1, y
+len
);
1037 while (len
> 0) do begin glVertex2i(x
, y
); Inc(y
); Dec(len
); end;
1043 procedure TGxContext
.rect (x
, y
, w
, h
: Integer);
1045 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1046 if (w
< 0) or (h
< 0) then exit
;
1047 if (w
= 1) and (h
= 1) then
1051 if mScaled
then glVertex2i(x
, y
) else glVertex2f(x
+0.375, y
+0.375);
1056 if (not mScaled
) then
1060 glVertex2i(x
, y
); glVertex2i(x
+w
, y
); // top
1061 glVertex2i(x
, y
+h
-1); glVertex2i(x
+w
, y
+h
-1); // bottom
1062 glVertex2f(x
+0.375, y
+1); glVertex2f(x
+0.375, y
+h
-1); // left
1063 glVertex2f(x
+w
-1+0.375, y
+1); glVertex2f(x
+w
-1+0.375, y
+h
-1); // right
1071 vline(x
+w
-1, y
+1, h
-2);
1077 procedure TGxContext
.fillRect (x
, y
, w
, h
: Integer);
1079 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1080 if (w
< 0) or (h
< 0) then exit
;
1084 glVertex2f(x
+w
, y
+h
);
1090 procedure TGxContext
.darkenRect (x
, y
, w
, h
: Integer; a
: Integer);
1092 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (a
>= 255) then exit
;
1093 if (w
< 0) or (h
< 0) then exit
;
1094 if (a
< 0) then a
:= 0;
1096 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1097 glColor4f(0.0, 0.0, 0.0, a
/255.0);
1101 glVertex2i(x
+w
, y
+h
);
1104 setupGLColor(mColor
);
1108 function TGxContext
.charWidth (const ch
: AnsiChar): Integer;
1110 result
:= mFont
.charWidth(ch
);
1113 function TGxContext
.charHeight (const ch
: AnsiChar): Integer;
1115 result
:= mFont
.height
;
1119 function TGxContext
.textWidth (const s
: AnsiString): Integer;
1121 result
:= mFont
.textWidth(s
);
1124 function TGxContext
.textHeight (const s
: AnsiString): Integer;
1126 result
:= mFont
.height
;
1130 function TGxContext
.drawChar (x
, y
: Integer; const ch
: AnsiChar): Integer; // returns char width
1132 result
:= mFont
.charWidth(ch
);
1133 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1134 TGxBmpFont(mFont
).drawCharInternal(x
, y
, ch
);
1137 function TGxContext
.drawText (x
, y
: Integer; const s
: AnsiString): Integer; // returns text width
1139 result
:= mFont
.textWidth(s
);
1140 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) or (Length(s
) = 0) then exit
;
1141 TGxBmpFont(mFont
).drawTextInternal(x
, y
, s
);
1145 function TGxContext
.iconMarkWidth (ic
: TMarkIcon
): Integer;
1147 {$IFDEF FUI_TEXT_ICONS}
1149 TMarkIcon
.Checkbox
: result
:= textWidth('[x]');
1150 TMarkIcon
.Radiobox
: result
:= textWidth('(*)');
1151 else result
:= textWidth('[x]');
1158 function TGxContext
.iconMarkHeight (ic
: TMarkIcon
): Integer;
1160 {$IFDEF FUI_TEXT_ICONS}
1162 TMarkIcon
.Checkbox
: result
:= textHeight('[x]');
1163 TMarkIcon
.Radiobox
: result
:= textHeight('(*)');
1164 else result
:= textHeight('[x]');
1171 procedure TGxContext
.drawIconMark (ic
: TMarkIcon
; x
, y
: Integer; marked
: Boolean);
1173 {$IFDEF FUI_TEXT_ICONS}
1179 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1180 {$IFDEF FUI_TEXT_ICONS}
1182 TMarkIcon
.Checkbox
: xstr
:= '[x]';
1183 TMarkIcon
.Radiobox
: xstr
:= '(*)';
1188 drawText(x
, y
, xstr
);
1192 drawChar(x
, y
, xstr
[1]);
1193 drawChar(x
+textWidth(xstr
)-charWidth(xstr
[3]), y
, xstr
[3]);
1196 if (ic
= TMarkIcon
.Checkbox
) then
1208 vline(x
+10, y
+1, 5);
1214 if (not marked
) then exit
;
1220 vline(x
+3+f
, y
+1+f
, 1);
1221 vline(x
+7-f
, y
+1+f
, 1);
1237 function TGxContext
.iconWinWidth (ic
: TWinIcon
): Integer;
1239 {$IFDEF FUI_TEXT_ICONS}
1241 TWinIcon
.Close
: result
:= nmax(textWidth('[x]'), textWidth('[#]'));
1242 else result
:= nmax(textWidth('[x]'), textWidth('[#]'));
1249 function TGxContext
.iconWinHeight (ic
: TWinIcon
): Integer;
1251 {$IFDEF FUI_TEXT_ICONS}
1253 TWinIcon
.Close
: result
:= nmax(textHeight('[x]'), textHeight('[#]'));
1254 else result
:= nmax(textHeight('[x]'), textHeight('[#]'));
1261 procedure TGxContext
.drawIconWin (ic
: TWinIcon
; x
, y
: Integer; pressed
: Boolean);
1263 {$IFDEF FUI_TEXT_ICONS}
1270 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1271 {$IFDEF FUI_TEXT_ICONS}
1273 TWinIcon
.Close
: if (pressed
) then xstr
:= '[#]' else xstr
:= '[x]';
1276 wdt
:= nmax(textWidth('[x]'), textWidth('[#]'));
1277 drawChar(x
, y
, xstr
[1]);
1278 drawChar(x
+wdt
-charWidth(xstr
[3]), y
, xstr
[3]);
1279 drawChar(x
+((wdt
-charWidth(xstr
[2])) div 2), y
, xstr
[2]);
1281 if pressed
then rect(x
, y
, 9, 8);
1284 vline(x
+1+f
, y
+f
, 1);
1285 vline(x
+1+6-f
, y
+f
, 1);
1291 procedure TGxContext
.glSetScale (ascale
: Single);
1293 if (ascale
< 0.01) then ascale
:= 0.01;
1295 glScalef(ascale
, ascale
, 1.0);
1297 mScaled
:= (ascale
<> 1.0);
1300 procedure TGxContext
.glSetTrans (ax
, ay
: Single);
1303 glScalef(mScale
, mScale
, 1.0);
1304 glTranslatef(ax
, ay
, 0);
1308 procedure TGxContext
.glSetScaleTrans (ascale
, ax
, ay
: Single);
1311 glTranslatef(ax
, ay
, 0);
1315 // ////////////////////////////////////////////////////////////////////////// //
1317 procedure oglRestoreMode (doClear: Boolean);
1319 oglSetup2D(fuiScrWdt, fuiScrHgt);
1320 glScissor(0, 0, fuiScrWdt, fuiScrHgt);
1322 glBindTexture(GL_TEXTURE_2D, 0);
1323 glDisable(GL_BLEND);
1324 glDisable(GL_TEXTURE_2D);
1325 glDisable(GL_STENCIL_TEST);
1326 glDisable(GL_SCISSOR_TEST);
1327 glDisable(GL_LIGHTING);
1328 glDisable(GL_DEPTH_TEST);
1329 glDisable(GL_CULL_FACE);
1330 glDisable(GL_LINE_SMOOTH);
1331 glDisable(GL_POINT_SMOOTH);
1334 glColor4f(1, 1, 1, 1);
1338 glClearColor(0, 0, 0, 0);
1339 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_ACCUM_BUFFER_BIT or GL_STENCIL_BUFFER_BIT);
1343 glMatrixMode(GL_MODELVIEW);
1345 //glScalef(4, 4, 1);
1350 //procedure onWinFocus (); begin end;
1351 //procedure onWinBlur (); begin fuiResetKMState(true); end;
1353 //procedure onPreRender (); begin oglRestoreMode(gGfxDoClear); end;
1354 procedure onPostRender (); begin oglDrawCursor(); end;
1356 procedure onInit ();
1358 //oglSetup2D(fuiScrWdt, fuiScrHgt);
1359 createCursorTexture();
1363 procedure onDeinit ();
1365 fuiResetKMState(false);
1366 if (curtexid
<> 0) then glDeleteTextures(1, @curtexid
);
1376 // ////////////////////////////////////////////////////////////////////////// //
1378 savedGLState
:= TSavedGLState
.Create(false);
1380 //winFocusCB := onWinFocus;
1381 //winBlurCB := onWinBlur;
1382 //prerenderFrameCB := onPreRender;
1383 postrenderFrameCB
:= onPostRender
;
1384 oglInitCB
:= onInit
;
1385 oglDeinitCB
:= onDeinit
;