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, version 3 of the License ONLY.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
17 {$DEFINE FUI_TEXT_ICONS}
25 fui_common
, fui_events
;
28 // ////////////////////////////////////////////////////////////////////////// //
37 function charWidth (const ch
: AnsiChar): Integer; virtual; abstract;
38 function textWidth (const s
: AnsiString): Integer; virtual; abstract;
41 property name
: AnsiString read mName
;
42 property height
: Integer read mHeight
;
43 property baseLine
: Integer read mBaseLine
;
46 TGxContext
= class (fui_gfx
.TGxContext
)
50 // for active contexts
57 procedure realizeClip (); // setup scissoring
58 procedure setClipOfs (const aofs
: TGxOfs
); // !!!
61 function setOffset (constref aofs
: TGxOfs
): TGxOfs
; // returns previous offset
62 function setClip (constref aclip
: TGxRect
): TGxRect
; // returns previous clip
65 function getFont (): AnsiString; override;
66 procedure setFont (const aname
: AnsiString); override;
68 procedure onActivate (); override;
69 procedure onDeactivate (); override;
71 function getColor (): TGxRGBA
; override;
72 procedure setColor (const clr
: TGxRGBA
); override;
74 function getClipRect (): TGxRect
; override;
75 procedure setClipRect (const aclip
: TGxRect
); override;
78 constructor Create ();
79 destructor Destroy (); override;
81 procedure line (x1
, y1
, x2
, y2
: Integer); override;
82 procedure hline (x
, y
, len
: Integer); override;
83 procedure vline (x
, y
, len
: Integer); override;
84 procedure rect (x
, y
, w
, h
: Integer); override;
85 procedure fillRect (x
, y
, w
, h
: Integer); override;
86 procedure darkenRect (x
, y
, w
, h
: Integer; a
: Integer); override;
88 function charWidth (const ch
: AnsiChar): Integer; override;
89 function charHeight (const ch
: AnsiChar): Integer; override;
90 function textWidth (const s
: AnsiString): Integer; override;
91 function textHeight (const s
: AnsiString): Integer; override;
92 function drawChar (x
, y
: Integer; const ch
: AnsiChar): Integer; override; // returns char width
93 function drawText (x
, y
: Integer; const s
: AnsiString): Integer; override; // returns text width
95 function iconMarkWidth (ic
: TMarkIcon
): Integer; override;
96 function iconMarkHeight (ic
: TMarkIcon
): Integer; override;
97 procedure drawIconMark (ic
: TMarkIcon
; x
, y
: Integer; marked
: Boolean); override;
99 function iconWinWidth (ic
: TWinIcon
): Integer; override;
100 function iconWinHeight (ic
: TWinIcon
): Integer; override;
101 procedure drawIconWin (ic
: TWinIcon
; x
, y
: Integer; pressed
: Boolean); override;
103 procedure resetClip (); override;
105 function combineClip (constref aclip
: TGxRect
): TGxRect
; override; // returns previous clip
107 // vertical scrollbar
108 procedure drawVSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
); override;
109 // horizontal scrollbar
110 procedure drawHSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
); override;
113 procedure glSetScale (ascale
: Single);
114 procedure glSetTrans (ax
, ay
: Single);
115 procedure glSetScaleTrans (ascale
, ax
, ay
: Single);
118 property color
: TGxRGBA read mColor write setColor
;
119 property offset
: TGxOfs read mClipOfs write setClipOfs
;
120 property clip
: TGxRect read mClipRect write setClipRect
; // clipping is unaffected by offset
124 // setup 2D OpenGL mode; will be called automatically in `glInit()`
125 //procedure oglSetup2D (winWidth, winHeight: Integer; upsideDown: Boolean=false);
126 //procedure oglSetup2DState (); // don't modify viewports and matrices
128 //procedure oglDrawCursor ();
129 //procedure oglDrawCursorAt (msX, msY: Integer);
132 //procedure fuiGfxLoadFont (const fontname: AnsiString; const fontFile: AnsiString; proportional: Boolean=false);
133 //procedure fuiGfxLoadFont (const fontname: AnsiString; st: TStream; proportional: Boolean=false);
136 // ////////////////////////////////////////////////////////////////////////// //
138 gGfxDoClear
: Boolean = true;
144 {$INCLUDE ../nogl/noGLuses.inc}
150 // ////////////////////////////////////////////////////////////////////////// //
151 // returns `false` if the color is transparent
152 // returns `false` if the color is transparent
153 function setupGLColor (constref clr
: TGxRGBA
): Boolean;
155 if (clr
.a
< 255) then
158 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
164 glColor4ub(clr
.r
, clr
.g
, clr
.b
, clr
.a
);
165 result
:= (clr
.a
<> 0);
168 function isScaled (): Boolean;
170 mt
: packed array [0..15] of GLfloat
;
172 glGetFloatv(GL_MODELVIEW_MATRIX
, @mt
[0]);
173 result
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
177 // ////////////////////////////////////////////////////////////////////////// //
178 //TODO: OpenGL framebuffers and shaders state
180 TSavedGLState
= record
183 gltextbinding
: GLint
;
185 //oldfbr, oldfbw: GLint;
186 glvport
: packed array [0..3] of GLint
;
190 constructor Create (dosave
: Boolean);
192 procedure restore ();
195 constructor TSavedGLState
.Create (dosave
: Boolean);
197 FillChar(self
, sizeof(self
), 0);
198 if (dosave
) then save();
201 procedure TSavedGLState
.save ();
203 if (saved
) then raise Exception
.Create('cannot save into already saved OpenGL state');
204 glGetIntegerv(GL_MATRIX_MODE
, @glmatmode
);
205 glGetIntegerv(GL_TEXTURE_BINDING_2D
, @gltextbinding
);
206 glGetIntegerv(GL_VIEWPORT
, @glvport
[0]);
207 //glGetIntegerv(GL_CURRENT_PROGRAM, &oldprg);
208 //glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &oldfbr);
209 //glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfbw);
210 glMatrixMode(GL_PROJECTION
); glPushMatrix();
211 glMatrixMode(GL_MODELVIEW
); glPushMatrix();
212 glMatrixMode(GL_TEXTURE
); glPushMatrix();
213 glMatrixMode(GL_COLOR
); glPushMatrix();
214 glPushAttrib({GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_CURRENT_BIT}GL_ALL_ATTRIB_BITS
); // let's play safe
218 procedure TSavedGLState
.restore ();
220 if (not saved
) then raise Exception
.Create('cannot restore unsaved OpenGL state');
221 glPopAttrib({GL_ENABLE_BIT});
222 glMatrixMode(GL_PROJECTION
); glPopMatrix();
223 glMatrixMode(GL_MODELVIEW
); glPopMatrix();
224 glMatrixMode(GL_TEXTURE
); glPopMatrix();
225 glMatrixMode(GL_COLOR
); glPopMatrix();
226 glMatrixMode(glmatmode
);
227 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, oldfbr);
228 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, oldfbw);
229 glBindTexture(GL_TEXTURE_2D
, gltextbinding
);
230 //if (glHasFunc!"glUseProgram") glUseProgram(oldprg);
231 glViewport(glvport
[0], glvport
[1], glvport
[2], glvport
[3]);
237 savedGLState
: TSavedGLState
;
239 procedure gxGLPreSetContextCallback
;
241 if (savedGLState
.saved
) then savedGLState
.restore();
244 function gxGLCreateContextCallback (): fui_gfx
.TGxContext
;
246 result
:= TGxContext
.Create();
249 // ////////////////////////////////////////////////////////////////////////// //
251 TScissorSave
= record
254 scxywh
: packed array[0..3] of GLint
;
259 procedure save (enableScissoring
: Boolean);
260 procedure restore ();
262 // set new scissor rect, bounded by the saved scissor rect
263 procedure combineRect (x
, y
, w
, h
: Integer);
267 procedure TScissorSave
.save (enableScissoring
: Boolean);
269 wassc
:= (glIsEnabled(GL_SCISSOR_TEST
) <> 0);
270 if wassc
then glGetIntegerv(GL_SCISSOR_BOX
, @scxywh
[0]) else glGetIntegerv(GL_VIEWPORT
, @scxywh
[0]);
271 //conwritefln('(%d,%d)-(%d,%d)', [scxywh[0], scxywh[1], scxywh[2], scxywh[3]]);
272 if enableScissoring
and (not wassc
) then glEnable(GL_SCISSOR_TEST
);
275 procedure TScissorSave
.restore ();
277 glScissor(scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]);
278 if wassc
then glEnable(GL_SCISSOR_TEST
) else glDisable(GL_SCISSOR_TEST
);
281 procedure TScissorSave
.combineRect (x
, y
, w
, h
: Integer);
282 //var ox, oy, ow, oh: Integer;
284 if (w
< 1) or (h
< 1) then begin glScissor(0, 0, 0, 0); exit
; end;
285 y
:= fuiScrHgt
-(y
+h
);
286 //ox := x; oy := y; ow := w; oh := h;
287 if not intersectRect(x
, y
, w
, h
, scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]) then
289 //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, ')');
290 //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, '>');
291 glScissor(0, 0, 0, 0);
295 glScissor(x
, y
, w
, h
);
300 // ////////////////////////////////////////////////////////////////////////// //
302 TGxBmpFont
= class(TGxFont
)
304 mTexId
: GLuint
; // OpenGL texture id
305 mWidth
: Integer; // <=0: proportional
308 mFreeFontWdt
: Boolean;
309 mFreeFontBmp
: Boolean;
312 procedure oglCreateTexture ();
313 procedure oglDestroyTexture ();
315 procedure initDrawText ();
316 procedure doneDrawText ();
317 function drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
318 function drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
319 function drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer; // return width (not including last empty pixel)
322 constructor Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
323 destructor Destroy (); override;
325 function charWidth (const ch
: AnsiChar): Integer; override;
326 function textWidth (const s
: AnsiString): Integer; override;
330 constructor TGxBmpFont
.Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
332 sign
: packed array [0..7] of AnsiChar;
333 enc
: packed array [0..16] of AnsiChar;
335 wdt
, hgt
, elen
: Integer;
340 mFreeFontBmp
:= true;
341 mFreeFontWdt
:= true;
345 st
.ReadBuffer(sign
[0], 8);
346 if (sign
<> 'FUIFONT0') then raise Exception
.Create('FlexUI: invalid font file signature');
347 // encoding length and width
349 wdt
:= (b
and $0f)+1; // 16 is not supported
350 if (wdt
= 16) then raise Exception
.Create('FlexUI: 16-wdt fonts aren''t supported yet');
351 elen
:= ((b
shr 4) and $0f);
352 if (elen
= 0) then raise Exception
.CreateFmt('FlexUI: invalid font encoding length: %d', [elen
]);
356 if (hgt
< 2) then raise Exception
.CreateFmt('FlexUI: invalid font height: %d', [hgt
]);
358 st
.ReadBuffer(enc
[0], elen
);
359 // check for 'cp1251' here (it can also be 'koi8')
360 if (wdt
<= 8) then fntbwdt
:= 1 else fntbwdt
:= 2;
361 // shift and width table (hi nibble: left shift for proportional print; lo nibble: shifted character width for proportional print)
362 GetMem(mFontWdt
, 256);
363 st
.ReadBuffer(mFontWdt
^, 256);
365 GetMem(mFontBmp
, (hgt
*fntbwdt
)*256);
366 st
.ReadBuffer(mFontBmp
^, (hgt
*fntbwdt
)*256);
369 mBaseLine
:= hgt
-1; //FIXME
370 if (proportional
) then
373 for ch
:= 0 to 255 do
375 for dy
:= 0 to hgt
-1 do
377 if (fntbwdt
= 1) then
379 mFontBmp
[ch
*hgt
+dy
] := mFontBmp
[ch
*hgt
+dy
] shl (mFontWdt
[ch
] shr 4);
383 wrd
:= mFontBmp
[ch
*(hgt
*2)+(dy
*2)]+256*mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1];
384 wrd
:= wrd
shl (mFontWdt
[ch
] shr 4);
385 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+0] := (wrd
and $ff);
386 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1] := ((wrd
shr 16) and $ff);
393 FillChar(mFontWdt
^, 256, wdt
);
398 destructor TGxBmpFont
.Destroy ();
400 if (mFreeFontBmp
) and (mFontBmp
<> nil) then FreeMem(mFontBmp
);
401 if (mFreeFontWdt
) and (mFontWdt
<> nil) then FreeMem(mFontWdt
);
408 mFreeFontWdt
:= false;
409 mFreeFontBmp
:= false;
415 procedure TGxBmpFont
.oglCreateTexture ();
423 x
, y
, dx
, dy
: Integer;
425 GetMem(tex
, TxWidth
*TxHeight
*4);
426 FillChar(tex
^, TxWidth
*TxHeight
*4, 0);
428 for cc
:= 0 to 255 do
432 for dy
:= 0 to mHeight
-1 do
434 if (mWidth
<= 8) then b
:= mFontBmp
[cc
*mHeight
+dy
] else b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+1];
435 //if prop then b := b shl (fontwdt[cc] shr 4);
436 tpp
:= tex
+((y
+dy
)*(TxWidth
*4))+x
*4;
439 if ((b
and $80) <> 0) then
441 tpp
^ := 255; Inc(tpp
);
442 tpp
^ := 255; Inc(tpp
);
443 tpp
^ := 255; Inc(tpp
);
444 tpp
^ := 255; Inc(tpp
);
453 b
:= (b
and $7f) shl 1;
457 b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+0];
460 if ((b
and $80) <> 0) then
462 tpp
^ := 255; Inc(tpp
);
463 tpp
^ := 255; Inc(tpp
);
464 tpp
^ := 255; Inc(tpp
);
465 tpp
^ := 255; Inc(tpp
);
474 b
:= (b
and $7f) shl 1;
480 glGenTextures(1, @mTexId
);
481 if (mTexId
= 0) then raise Exception
.Create('can''t create FlexUI font texture');
483 glBindTexture(GL_TEXTURE_2D
, mTexId
);
484 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
485 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
486 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
487 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
489 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, TxWidth
, TxHeight
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex
);
492 glBindTexture(GL_TEXTURE_2D
, 0);
497 procedure TGxBmpFont
.oglDestroyTexture ();
499 if (mTexId
<> 0) then
501 glDeleteTextures(1, @mTexId
);
507 function TGxBmpFont
.charWidth (const ch
: AnsiChar): Integer;
509 result
:= (mFontWdt
[Byte(ch
)] and $0f);
513 function TGxBmpFont
.textWidth (const s
: AnsiString): Integer;
517 if (Length(s
) > 0) then
520 for ch
in s
do result
+= (mFontWdt
[Byte(ch
)] and $0f)+1;
529 procedure TGxBmpFont
.initDrawText ();
531 glEnable(GL_ALPHA_TEST
);
532 glAlphaFunc(GL_NOTEQUAL
, 0.0);
533 glEnable(GL_TEXTURE_2D
);
534 glBindTexture(GL_TEXTURE_2D
, mTexId
);
538 procedure TGxBmpFont
.doneDrawText ();
540 glDisable(GL_ALPHA_TEST
);
541 glDisable(GL_TEXTURE_2D
);
542 glBindTexture(GL_TEXTURE_2D
, 0);
546 function TGxBmpFont
.drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer;
550 tx
:= (Integer(ch
) mod 16)*16;
551 ty
:= (Integer(ch
) div 16)*16;
553 glTexCoord2f((tx
+0)/256.0, (ty
+0)/256.0); glVertex2i(x
+0, y
+0); // top-left
554 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+0)/256.0); glVertex2i(x
+mWidth
, y
+0); // top-right
555 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+mWidth
, y
+mHeight
); // bottom-right
556 glTexCoord2f((tx
+0)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+0, y
+mHeight
); // bottom-left
558 result
:= (mFontWdt
[Byte(ch
)] and $0f);
562 function TGxBmpFont
.drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer;
565 result
:= drawCharInterim(x
, y
, ch
);
570 function TGxBmpFont
.drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer;
575 if (Length(s
) = 0) then begin result
:= 0; exit
; end;
580 wdt
:= drawCharInterim(x
, y
, ch
)+1;
588 // ////////////////////////////////////////////////////////////////////////// //
590 fontList
: array of TGxBmpFont
= nil;
591 defaultFontName
: AnsiString = 'win14';
594 function strEquCI (const s0
, s1
: AnsiString): Boolean;
599 result
:= (Length(s0
) = Length(s1
));
602 for f
:= 1 to Length(s0
) do
605 if (c0
>= 'a') and (c0
<= 'z') then Dec(c0
, 32); // poor man's `toupper()`
607 if (c1
>= 'a') and (c1
<= 'z') then Dec(c1
, 32); // poor man's `toupper()`
608 if (c0
<> c1
) then begin result
:= false; exit
; end;
614 function getFontByName (const aname
: AnsiString): TGxBmpFont
;
619 if (Length(fontList
) = 0) then raise Exception
.Create('font subsystem not initialized');
620 if (Length(aname
) = 0) or (strEquCI(aname
, 'default')) then fname
:= defaultFontName
else fname
:= aname
;
621 for f
:= 0 to High(fontList
) do
623 result
:= fontList
[f
];
624 if (result
= nil) then continue
;
625 if (strEquCI(result
.name
, fname
)) then exit
;
627 if (fontList
[0] = nil) then raise Exception
.Create('font subsystem not properly initialized');
628 result
:= fontList
[0];
633 procedure deleteFonts ();
637 for f := 0 to High(fontList) do freeAndNil(fontList[f]);
643 procedure fuiGfxLoadFont (const fontname
: AnsiString; st
: TStream
; proportional
: Boolean=false);
645 fnt
: TGxBmpFont
= nil;
648 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font');
649 fnt
:= TGxBmpFont
.Create(fontname
, st
, proportional
);
651 for f
:= 0 to High(fontList
) do
653 if (strEquCI(fontList
[f
].name
, fontname
)) then
655 if (fontList
[f
].mTexId
<> 0) then raise Exception
.Create('FlexUI: cannot reload generated font named '''+fontname
+'''');
656 FreeAndNil(fontList
[f
]);
661 SetLength(fontList
, Length(fontList
)+1);
662 fontList
[High(fontList
)] := fnt
;
670 procedure fuiGfxLoadFont (const fontname
: AnsiString; const fontFile
: AnsiString; proportional
: Boolean=false);
674 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font '''+fontFile
+'''');
675 st
:= fuiOpenFile(fontFile
);
676 if (st
= nil) then raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
678 fuiGfxLoadFont(fontname
, st
, proportional
);
679 except on e
: Exception
do
681 writeln('FlexUI font loadin error: ', e
.message);
683 raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
692 procedure oglInitFonts ();
696 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglCreateTexture();
700 procedure oglDeinitFonts ();
704 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglDestroyTexture();
708 // ////////////////////////////////////////////////////////////////////////// //
709 procedure oglSetup2DState ();
712 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
713 glDisable(GL_LINE_SMOOTH
);
714 glDisable(GL_POLYGON_SMOOTH
);
715 glDisable(GL_POINT_SMOOTH
);
716 glDisable(GL_DEPTH_TEST
);
717 glDisable(GL_TEXTURE_2D
);
718 glDisable(GL_LIGHTING
);
719 glDisable(GL_DITHER
);
720 glDisable(GL_STENCIL_TEST
);
721 glDisable(GL_SCISSOR_TEST
);
722 glDisable(GL_CULL_FACE
);
723 glDisable(GL_ALPHA_TEST
);
725 glClearColor(0, 0, 0, 0);
726 glColor4f(1, 1, 1, 1);
730 procedure oglSetup2D (winWidth
, winHeight
: Integer; upsideDown
: Boolean=false);
732 glViewport(0, 0, winWidth
, winHeight
);
736 glMatrixMode(GL_TEXTURE
);
739 glMatrixMode(GL_COLOR
);
742 glMatrixMode(GL_PROJECTION
);
746 glOrtho(0, winWidth
, 0, winHeight
, -1, 1); // set origin to bottom left
750 glOrtho(0, winWidth
, winHeight
, 0, -1, 1); // set origin to top left
753 glMatrixMode(GL_MODELVIEW
);
758 // ////////////////////////////////////////////////////////////////////////// //
759 {$INCLUDE r_fui_gfx_gl_cursor.inc}
761 procedure oglDrawCursor (); begin oglDrawCursorAt(fuiMouseX
, fuiMouseY
); end;
764 // ////////////////////////////////////////////////////////////////////////// //
765 constructor TGxContext
.Create ();
768 mColor
:= TGxRGBA
.Create(255, 255, 255);
769 mFont
:= getFontByName('default');
772 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
773 mClipOfs
:= TGxOfs
.Create(0, 0);
777 destructor TGxContext
.Destroy ();
779 if self
.active
then gxSetContext(nil);
784 function TGxContext
.getFont (): AnsiString;
786 result
:= mFont
.name
;
789 procedure TGxContext
.setFont (const aname
: AnsiString);
791 mFont
:= getFontByName(aname
);
795 procedure TGxContext
.onActivate ();
796 //ascale: Single; domatrix: Boolean;
798 mt
: packed array [0..15] of GLfloat
;
801 // if (domatrix) then
803 // oglSetup2D(fuiScrWdt, fuiScrHgt);
804 // glScalef(ascale, ascale, 1.0);
805 // self.mScaled := (ascale <> 1.0);
806 // self.mScale := ascale;
810 // assume uniform scale
811 glGetFloatv(GL_MODELVIEW_MATRIX
, @mt
[0]);
812 self
.mScaled
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
813 self
.mScale
:= mt
[0];
816 setupGLColor(mColor
);
820 procedure TGxContext
.onDeactivate ();
825 function TGxContext
.getColor (): TGxRGBA
;
830 procedure TGxContext
.setColor (const clr
: TGxRGBA
);
833 if self
.active
then setupGLColor(mColor
);
837 procedure TGxContext
.realizeClip ();
839 sx
, sy
, sw
, sh
: Integer;
841 if not self
.active
then exit
; // just in case
842 if (mClipRect
.w
<= 0) or (mClipRect
.h
<= 0) then
844 glEnable(GL_SCISSOR_TEST
);
845 glScissor(0, 0, 0, 0);
851 sx
:= trunc(mClipRect
.x
*mScale
);
852 sy
:= trunc(mClipRect
.y
*mScale
);
853 sw
:= trunc(mClipRect
.w
*mScale
);
854 sh
:= trunc(mClipRect
.h
*mScale
);
863 if (not intersectRect(sx
, sy
, sw
, sh
, 0, 0, fuiScrWdt
, fuiScrHgt
)) then
865 glEnable(GL_SCISSOR_TEST
);
866 glScissor(0, 0, 0, 0);
868 else if (sx
= 0) and (sy
= 0) and (sw
= fuiScrWdt
) and (sh
= fuiScrHgt
) then
870 glDisable(GL_SCISSOR_TEST
);
874 glEnable(GL_SCISSOR_TEST
);
875 sy
:= fuiScrHgt
-(sy
+sh
);
876 glScissor(sx
, sy
, sw
, sh
);
882 procedure TGxContext
.resetClip ();
884 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
885 if self
.active
then realizeClip();
889 procedure TGxContext
.setClipOfs (const aofs
: TGxOfs
);
895 function TGxContext
.getClipRect (): TGxRect
;
900 procedure TGxContext
.setClipRect (const aclip
: TGxRect
);
903 if self
.active
then realizeClip();
907 function TGxContext
.setOffset (constref aofs
: TGxOfs
): TGxOfs
;
914 function TGxContext
.setClip (constref aclip
: TGxRect
): TGxRect
;
918 if self
.active
then realizeClip();
922 function TGxContext
.combineClip (constref aclip
: TGxRect
): TGxRect
;
925 mClipRect
.intersect(aclip
);
926 if self
.active
then realizeClip();
930 procedure TGxContext
.line (x1
, y1
, x2
, y2
: Integer);
932 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
934 if (not mScaled
) then
938 glVertex2f(x1
+0.375, y1
+0.375);
939 glVertex2f(x2
+0.375, y2
+0.375);
942 if (x1
<> x2
) or (y1
<> y2
) then
946 glVertex2f(x2
+0.375, y2
+0.375);
958 glVertex2i(x2
+1, y2
+1);
964 procedure TGxContext
.hline (x
, y
, len
: Integer);
966 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
967 if (len
< 1) then exit
;
968 if (not mScaled
) then
972 glVertex2f(x
+0.375, y
+0.375);
973 glVertex2f(x
+len
+0.375, y
+0.375);
976 else if (mScale
> 1.0) then
980 glVertex2i(x
+len
, y
);
981 glVertex2i(x
+len
, y
+1);
989 while (len
> 0) do begin glVertex2i(x
, y
); Inc(x
); Dec(len
); end;
995 procedure TGxContext
.vline (x
, y
, len
: Integer);
997 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
998 if (len
< 1) then exit
;
999 if (not mScaled
) then
1003 glVertex2f(x
+0.375, y
+0.375);
1004 glVertex2f(x
+0.375, y
+len
+0.375);
1007 else if (mScale
> 1.0) then
1011 glVertex2i(x
, y
+len
);
1012 glVertex2i(x
+1, y
+len
);
1020 while (len
> 0) do begin glVertex2i(x
, y
); Inc(y
); Dec(len
); end;
1026 procedure TGxContext
.rect (x
, y
, w
, h
: Integer);
1028 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1029 if (w
< 0) or (h
< 0) then exit
;
1030 if (w
= 1) and (h
= 1) then
1034 if mScaled
then glVertex2i(x
, y
) else glVertex2f(x
+0.375, y
+0.375);
1039 if (not mScaled
) then
1043 glVertex2i(x
, y
); glVertex2i(x
+w
, y
); // top
1044 glVertex2i(x
, y
+h
-1); glVertex2i(x
+w
, y
+h
-1); // bottom
1045 glVertex2f(x
+0.375, y
+1); glVertex2f(x
+0.375, y
+h
-1); // left
1046 glVertex2f(x
+w
-1+0.375, y
+1); glVertex2f(x
+w
-1+0.375, y
+h
-1); // right
1054 vline(x
+w
-1, y
+1, h
-2);
1060 procedure TGxContext
.fillRect (x
, y
, w
, h
: Integer);
1062 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1063 if (w
< 0) or (h
< 0) then exit
;
1067 glVertex2f(x
+w
, y
+h
);
1073 procedure TGxContext
.darkenRect (x
, y
, w
, h
: Integer; a
: Integer);
1075 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (a
>= 255) then exit
;
1076 if (w
< 0) or (h
< 0) then exit
;
1077 if (a
< 0) then a
:= 0;
1079 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1080 glColor4f(0.0, 0.0, 0.0, a
/255.0);
1084 glVertex2i(x
+w
, y
+h
);
1087 setupGLColor(mColor
);
1091 function TGxContext
.charWidth (const ch
: AnsiChar): Integer;
1093 result
:= mFont
.charWidth(ch
);
1096 function TGxContext
.charHeight (const ch
: AnsiChar): Integer;
1098 result
:= mFont
.height
;
1102 function TGxContext
.textWidth (const s
: AnsiString): Integer;
1104 result
:= mFont
.textWidth(s
);
1107 function TGxContext
.textHeight (const s
: AnsiString): Integer;
1109 result
:= mFont
.height
;
1113 function TGxContext
.drawChar (x
, y
: Integer; const ch
: AnsiChar): Integer; // returns char width
1115 result
:= mFont
.charWidth(ch
);
1116 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1117 TGxBmpFont(mFont
).drawCharInternal(x
, y
, ch
);
1120 function TGxContext
.drawText (x
, y
: Integer; const s
: AnsiString): Integer; // returns text width
1122 result
:= mFont
.textWidth(s
);
1123 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) or (Length(s
) = 0) then exit
;
1124 TGxBmpFont(mFont
).drawTextInternal(x
, y
, s
);
1128 function TGxContext
.iconMarkWidth (ic
: TMarkIcon
): Integer;
1130 {$IFDEF FUI_TEXT_ICONS}
1132 TMarkIcon
.Checkbox
: result
:= textWidth('[x]');
1133 TMarkIcon
.Radiobox
: result
:= textWidth('(*)');
1134 else result
:= textWidth('[x]');
1141 function TGxContext
.iconMarkHeight (ic
: TMarkIcon
): Integer;
1143 {$IFDEF FUI_TEXT_ICONS}
1145 TMarkIcon
.Checkbox
: result
:= textHeight('[x]');
1146 TMarkIcon
.Radiobox
: result
:= textHeight('(*)');
1147 else result
:= textHeight('[x]');
1154 procedure TGxContext
.drawIconMark (ic
: TMarkIcon
; x
, y
: Integer; marked
: Boolean);
1156 {$IFDEF FUI_TEXT_ICONS}
1162 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1163 {$IFDEF FUI_TEXT_ICONS}
1165 TMarkIcon
.Checkbox
: xstr
:= '[x]';
1166 TMarkIcon
.Radiobox
: xstr
:= '(*)';
1171 drawText(x
, y
, xstr
);
1175 drawChar(x
, y
, xstr
[1]);
1176 drawChar(x
+textWidth(xstr
)-charWidth(xstr
[3]), y
, xstr
[3]);
1179 if (ic
= TMarkIcon
.Checkbox
) then
1191 vline(x
+10, y
+1, 5);
1197 if (not marked
) then exit
;
1203 vline(x
+3+f
, y
+1+f
, 1);
1204 vline(x
+7-f
, y
+1+f
, 1);
1220 function TGxContext
.iconWinWidth (ic
: TWinIcon
): Integer;
1222 {$IFDEF FUI_TEXT_ICONS}
1224 TWinIcon
.Close
: result
:= nmax(textWidth('[x]'), textWidth('[#]'));
1225 else result
:= nmax(textWidth('[x]'), textWidth('[#]'));
1232 function TGxContext
.iconWinHeight (ic
: TWinIcon
): Integer;
1234 {$IFDEF FUI_TEXT_ICONS}
1236 TWinIcon
.Close
: result
:= nmax(textHeight('[x]'), textHeight('[#]'));
1237 else result
:= nmax(textHeight('[x]'), textHeight('[#]'));
1244 procedure TGxContext
.drawIconWin (ic
: TWinIcon
; x
, y
: Integer; pressed
: Boolean);
1246 {$IFDEF FUI_TEXT_ICONS}
1253 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1254 {$IFDEF FUI_TEXT_ICONS}
1256 TWinIcon
.Close
: if (pressed
) then xstr
:= '[#]' else xstr
:= '[x]';
1259 wdt
:= nmax(textWidth('[x]'), textWidth('[#]'));
1260 drawChar(x
, y
, xstr
[1]);
1261 drawChar(x
+wdt
-charWidth(xstr
[3]), y
, xstr
[3]);
1262 drawChar(x
+((wdt
-charWidth(xstr
[2])) div 2), y
, xstr
[2]);
1264 if pressed
then rect(x
, y
, 9, 8);
1267 vline(x
+1+f
, y
+f
, 1);
1268 vline(x
+1+6-f
, y
+f
, 1);
1274 procedure TGxContext
.glSetScale (ascale
: Single);
1276 if (ascale
< 0.01) then ascale
:= 0.01;
1278 glScalef(ascale
, ascale
, 1.0);
1280 mScaled
:= (ascale
<> 1.0);
1283 procedure TGxContext
.glSetTrans (ax
, ay
: Single);
1286 glScalef(mScale
, mScale
, 1.0);
1287 glTranslatef(ax
, ay
, 0);
1291 procedure TGxContext
.glSetScaleTrans (ascale
, ax
, ay
: Single);
1294 glTranslatef(ax
, ay
, 0);
1298 // vertical scroll bar
1299 procedure TGxContext
.drawVSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
1303 if (wdt
< 1) or (hgt
< 1) then exit
;
1304 filled
:= sbarFilled(hgt
, cur
, min
, max
);
1306 fillRect(x
, y
, wdt
, filled
);
1308 fillRect(x
, y
+filled
, wdt
, hgt
-filled
);
1312 // horizontal scrollbar
1313 procedure TGxContext
.drawHSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
1317 if (wdt
< 1) or (hgt
< 1) then exit
;
1318 filled
:= sbarFilled(wdt
, cur
, min
, max
);
1320 fillRect(x
, y
, filled
, hgt
);
1322 fillRect(x
+filled
, y
, wdt
-filled
, hgt
);
1326 // ////////////////////////////////////////////////////////////////////////// //
1328 procedure oglRestoreMode (doClear: Boolean);
1330 oglSetup2D(fuiScrWdt, fuiScrHgt);
1331 glScissor(0, 0, fuiScrWdt, fuiScrHgt);
1333 glBindTexture(GL_TEXTURE_2D, 0);
1334 glDisable(GL_BLEND);
1335 glDisable(GL_TEXTURE_2D);
1336 glDisable(GL_STENCIL_TEST);
1337 glDisable(GL_SCISSOR_TEST);
1338 glDisable(GL_LIGHTING);
1339 glDisable(GL_DEPTH_TEST);
1340 glDisable(GL_CULL_FACE);
1341 glDisable(GL_LINE_SMOOTH);
1342 glDisable(GL_POINT_SMOOTH);
1345 glColor4f(1, 1, 1, 1);
1349 glClearColor(0, 0, 0, 0);
1350 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_ACCUM_BUFFER_BIT or GL_STENCIL_BUFFER_BIT);
1354 glMatrixMode(GL_MODELVIEW);
1356 //glScalef(4, 4, 1);
1361 //procedure onWinFocus (); begin uiFocus(); end;
1362 //procedure onWinBlur (); begin fuiResetKMState(true); uiBlur(); end;
1364 //procedure onPreRender (); begin oglRestoreMode(gGfxDoClear); end;
1365 procedure onPostRender (); begin oglDrawCursor(); end;
1367 procedure onInit ();
1369 //oglSetup2D(fuiScrWdt, fuiScrHgt);
1370 createCursorTexture();
1374 procedure onDeinit ();
1376 fuiResetKMState(false);
1377 if (curtexid
<> 0) then glDeleteTextures(1, @curtexid
);
1387 // ////////////////////////////////////////////////////////////////////////// //
1389 savedGLState
:= TSavedGLState
.Create(false);
1391 //winFocusCB := onWinFocus;
1392 //winBlurCB := onWinBlur;
1393 //prerenderFrameCB := onPreRender;
1394 postrenderFrameCB
:= onPostRender
;
1395 oglInitCB
:= onInit
;
1396 oglDeinitCB
:= onDeinit
;
1398 gxPreSetContextCallback
:= gxGLPreSetContextCallback
;
1399 gxCreateContextCallback
:= gxGLCreateContextCallback
;
1400 gxFuiGfxLoadFontCallback
:= fuiGfxLoadFont
;