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}
23 {$INCLUDE ../nogl/noGLuses.inc}
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
118 // vertical scrollbar
119 procedure drawVSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
120 // horizontal scrollbar
121 procedure drawHSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
123 class function sbarFilled (wh
: Integer; cur
, min
, max
: Integer): Integer;
124 class function sbarPos (cxy
: Integer; xy
, wh
: Integer; min
, max
: Integer): Integer;
127 procedure glSetScale (ascale
: Single);
128 procedure glSetTrans (ax
, ay
: Single);
129 procedure glSetScaleTrans (ascale
, ax
, ay
: Single);
132 property active
: Boolean read mActive
;
133 property color
: TGxRGBA read mColor write setColor
;
134 property font
: AnsiString read getFont write setFont
;
135 property offset
: TGxOfs read mClipOfs write setClipOfs
;
136 property clip
: TGxRect read mClipRect write setClipRect
; // clipping is unaffected by offset
140 // set active context; `ctx` can be `nil`
141 procedure gxSetContext (ctx
: TGxContext
; ascale
: Single=1.0);
142 procedure gxSetContextNoMatrix (ctx
: TGxContext
);
145 // setup 2D OpenGL mode; will be called automatically in `glInit()`
146 procedure oglSetup2D (winWidth
, winHeight
: Integer; upsideDown
: Boolean=false);
147 procedure oglSetup2DState (); // don't modify viewports and matrices
149 procedure oglDrawCursor ();
150 procedure oglDrawCursorAt (msX
, msY
: Integer);
153 procedure fuiGfxLoadFont (const fontname
: AnsiString; const fontFile
: AnsiString; proportional
: Boolean=false);
154 procedure fuiGfxLoadFont (const fontname
: AnsiString; st
: TStream
; proportional
: Boolean=false);
157 // ////////////////////////////////////////////////////////////////////////// //
159 gGfxDoClear
: Boolean = true;
169 // ////////////////////////////////////////////////////////////////////////// //
170 // returns `false` if the color is transparent
171 // returns `false` if the color is transparent
172 function setupGLColor (constref clr
: TGxRGBA
): Boolean;
174 if (clr
.a
< 255) then
177 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
183 glColor4ub(clr
.r
, clr
.g
, clr
.b
, clr
.a
);
184 result
:= (clr
.a
<> 0);
187 function isScaled (): Boolean;
189 mt
: packed array [0..15] of GLfloat
;
191 glGetFloatv(GL_MODELVIEW_MATRIX
, @mt
[0]);
192 result
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
196 // ////////////////////////////////////////////////////////////////////////// //
197 //TODO: OpenGL framebuffers and shaders state
199 TSavedGLState
= record
202 gltextbinding
: GLint
;
204 //oldfbr, oldfbw: GLint;
205 glvport
: packed array [0..3] of GLint
;
209 constructor Create (dosave
: Boolean);
211 procedure restore ();
214 constructor TSavedGLState
.Create (dosave
: Boolean);
216 FillChar(self
, sizeof(self
), 0);
217 if (dosave
) then save();
220 procedure TSavedGLState
.save ();
222 if (saved
) then raise Exception
.Create('cannot save into already saved OpenGL state');
223 glGetIntegerv(GL_MATRIX_MODE
, @glmatmode
);
224 glGetIntegerv(GL_TEXTURE_BINDING_2D
, @gltextbinding
);
225 glGetIntegerv(GL_VIEWPORT
, @glvport
[0]);
226 //glGetIntegerv(GL_CURRENT_PROGRAM, &oldprg);
227 //glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &oldfbr);
228 //glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfbw);
229 glMatrixMode(GL_PROJECTION
); glPushMatrix();
230 glMatrixMode(GL_MODELVIEW
); glPushMatrix();
231 glMatrixMode(GL_TEXTURE
); glPushMatrix();
232 glMatrixMode(GL_COLOR
); glPushMatrix();
233 glPushAttrib({GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_CURRENT_BIT}GL_ALL_ATTRIB_BITS
); // let's play safe
237 procedure TSavedGLState
.restore ();
239 if (not saved
) then raise Exception
.Create('cannot restore unsaved OpenGL state');
240 glPopAttrib({GL_ENABLE_BIT});
241 glMatrixMode(GL_PROJECTION
); glPopMatrix();
242 glMatrixMode(GL_MODELVIEW
); glPopMatrix();
243 glMatrixMode(GL_TEXTURE
); glPopMatrix();
244 glMatrixMode(GL_COLOR
); glPopMatrix();
245 glMatrixMode(glmatmode
);
246 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, oldfbr);
247 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, oldfbw);
248 glBindTexture(GL_TEXTURE_2D
, gltextbinding
);
249 //if (glHasFunc!"glUseProgram") glUseProgram(oldprg);
250 glViewport(glvport
[0], glvport
[1], glvport
[2], glvport
[3]);
256 curCtx
: TGxContext
= nil;
257 savedGLState
: TSavedGLState
;
260 // ////////////////////////////////////////////////////////////////////////// //
261 // set active context; `ctx` can be `nil`
262 procedure gxSetContextInternal (ctx
: TGxContext
; ascale
: Single; domatrix
: Boolean);
264 mt
: packed array [0..15] of GLfloat
;
266 if (savedGLState
.saved
) then savedGLState
.restore();
268 if (curCtx
<> nil) then
270 curCtx
.onDeactivate();
271 curCtx
.mActive
:= false;
281 oglSetup2D(fuiScrWdt
, fuiScrHgt
);
282 glScalef(ascale
, ascale
, 1.0);
283 ctx
.mScaled
:= (ascale
<> 1.0);
284 ctx
.mScale
:= ascale
;
288 // assume uniform scale
289 glGetFloatv(GL_MODELVIEW_MATRIX
, @mt
[0]);
290 ctx
.mScaled
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
299 procedure gxSetContext (ctx
: TGxContext
; ascale
: Single=1.0); begin gxSetContextInternal(ctx
, ascale
, true); end;
300 procedure gxSetContextNoMatrix (ctx
: TGxContext
); begin gxSetContextInternal(ctx
, 1, false); end;
303 // ////////////////////////////////////////////////////////////////////////// //
305 TScissorSave
= record
308 scxywh
: packed array[0..3] of GLint
;
313 procedure save (enableScissoring
: Boolean);
314 procedure restore ();
316 // set new scissor rect, bounded by the saved scissor rect
317 procedure combineRect (x
, y
, w
, h
: Integer);
321 procedure TScissorSave
.save (enableScissoring
: Boolean);
323 wassc
:= (glIsEnabled(GL_SCISSOR_TEST
) <> 0);
324 if wassc
then glGetIntegerv(GL_SCISSOR_BOX
, @scxywh
[0]) else glGetIntegerv(GL_VIEWPORT
, @scxywh
[0]);
325 //conwritefln('(%d,%d)-(%d,%d)', [scxywh[0], scxywh[1], scxywh[2], scxywh[3]]);
326 if enableScissoring
and (not wassc
) then glEnable(GL_SCISSOR_TEST
);
329 procedure TScissorSave
.restore ();
331 glScissor(scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]);
332 if wassc
then glEnable(GL_SCISSOR_TEST
) else glDisable(GL_SCISSOR_TEST
);
335 procedure TScissorSave
.combineRect (x
, y
, w
, h
: Integer);
336 //var ox, oy, ow, oh: Integer;
338 if (w
< 1) or (h
< 1) then begin glScissor(0, 0, 0, 0); exit
; end;
339 y
:= fuiScrHgt
-(y
+h
);
340 //ox := x; oy := y; ow := w; oh := h;
341 if not intersectRect(x
, y
, w
, h
, scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]) then
343 //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, ')');
344 //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, '>');
345 glScissor(0, 0, 0, 0);
349 glScissor(x
, y
, w
, h
);
354 // ////////////////////////////////////////////////////////////////////////// //
356 TGxBmpFont
= class(TGxFont
)
358 mTexId
: GLuint
; // OpenGL texture id
359 mWidth
: Integer; // <=0: proportional
362 mFreeFontWdt
: Boolean;
363 mFreeFontBmp
: Boolean;
366 procedure oglCreateTexture ();
367 procedure oglDestroyTexture ();
369 procedure initDrawText ();
370 procedure doneDrawText ();
371 function drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
372 function drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
373 function drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer; // return width (not including last empty pixel)
376 constructor Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
377 destructor Destroy (); override;
379 function charWidth (const ch
: AnsiChar): Integer; override;
380 function textWidth (const s
: AnsiString): Integer; override;
384 constructor TGxBmpFont
.Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
386 sign
: packed array [0..7] of AnsiChar;
387 enc
: packed array [0..16] of AnsiChar;
389 wdt
, hgt
, elen
: Integer;
394 mFreeFontBmp
:= true;
395 mFreeFontWdt
:= true;
399 st
.ReadBuffer(sign
[0], 8);
400 if (sign
<> 'FUIFONT0') then raise Exception
.Create('FlexUI: invalid font file signature');
401 // encoding length and width
403 wdt
:= (b
and $0f)+1; // 16 is not supported
404 if (wdt
= 16) then raise Exception
.Create('FlexUI: 16-wdt fonts aren''t supported yet');
405 elen
:= ((b
shr 4) and $0f);
406 if (elen
= 0) then raise Exception
.CreateFmt('FlexUI: invalid font encoding length: %d', [elen
]);
410 if (hgt
< 2) then raise Exception
.CreateFmt('FlexUI: invalid font height: %d', [hgt
]);
412 st
.ReadBuffer(enc
[0], elen
);
413 // check for 'cp1251' here (it can also be 'koi8')
414 if (wdt
<= 8) then fntbwdt
:= 1 else fntbwdt
:= 2;
415 // shift and width table (hi nibble: left shift for proportional print; lo nibble: shifted character width for proportional print)
416 GetMem(mFontWdt
, 256);
417 st
.ReadBuffer(mFontWdt
^, 256);
419 GetMem(mFontBmp
, (hgt
*fntbwdt
)*256);
420 st
.ReadBuffer(mFontBmp
^, (hgt
*fntbwdt
)*256);
423 mBaseLine
:= hgt
-1; //FIXME
424 if (proportional
) then
427 for ch
:= 0 to 255 do
429 for dy
:= 0 to hgt
-1 do
431 if (fntbwdt
= 1) then
433 mFontBmp
[ch
*hgt
+dy
] := mFontBmp
[ch
*hgt
+dy
] shl (mFontWdt
[ch
] shr 4);
437 wrd
:= mFontBmp
[ch
*(hgt
*2)+(dy
*2)]+256*mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1];
438 wrd
:= wrd
shl (mFontWdt
[ch
] shr 4);
439 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+0] := (wrd
and $ff);
440 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1] := ((wrd
shr 16) and $ff);
447 FillChar(mFontWdt
^, 256, wdt
);
452 destructor TGxBmpFont
.Destroy ();
454 if (mFreeFontBmp
) and (mFontBmp
<> nil) then FreeMem(mFontBmp
);
455 if (mFreeFontWdt
) and (mFontWdt
<> nil) then FreeMem(mFontWdt
);
462 mFreeFontWdt
:= false;
463 mFreeFontBmp
:= false;
469 procedure TGxBmpFont
.oglCreateTexture ();
477 x
, y
, dx
, dy
: Integer;
479 GetMem(tex
, TxWidth
*TxHeight
*4);
480 FillChar(tex
^, TxWidth
*TxHeight
*4, 0);
482 for cc
:= 0 to 255 do
486 for dy
:= 0 to mHeight
-1 do
488 if (mWidth
<= 8) then b
:= mFontBmp
[cc
*mHeight
+dy
] else b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+1];
489 //if prop then b := b shl (fontwdt[cc] shr 4);
490 tpp
:= tex
+((y
+dy
)*(TxWidth
*4))+x
*4;
493 if ((b
and $80) <> 0) then
495 tpp
^ := 255; Inc(tpp
);
496 tpp
^ := 255; Inc(tpp
);
497 tpp
^ := 255; Inc(tpp
);
498 tpp
^ := 255; Inc(tpp
);
507 b
:= (b
and $7f) shl 1;
511 b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+0];
514 if ((b
and $80) <> 0) then
516 tpp
^ := 255; Inc(tpp
);
517 tpp
^ := 255; Inc(tpp
);
518 tpp
^ := 255; Inc(tpp
);
519 tpp
^ := 255; Inc(tpp
);
528 b
:= (b
and $7f) shl 1;
534 glGenTextures(1, @mTexId
);
535 if (mTexId
= 0) then raise Exception
.Create('can''t create FlexUI font texture');
537 glBindTexture(GL_TEXTURE_2D
, mTexId
);
538 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
539 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
540 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
541 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
543 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, TxWidth
, TxHeight
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex
);
546 glBindTexture(GL_TEXTURE_2D
, 0);
551 procedure TGxBmpFont
.oglDestroyTexture ();
553 if (mTexId
<> 0) then
555 glDeleteTextures(1, @mTexId
);
561 function TGxBmpFont
.charWidth (const ch
: AnsiChar): Integer;
563 result
:= (mFontWdt
[Byte(ch
)] and $0f);
567 function TGxBmpFont
.textWidth (const s
: AnsiString): Integer;
571 if (Length(s
) > 0) then
574 for ch
in s
do result
+= (mFontWdt
[Byte(ch
)] and $0f)+1;
583 procedure TGxBmpFont
.initDrawText ();
585 glEnable(GL_ALPHA_TEST
);
586 glAlphaFunc(GL_NOTEQUAL
, 0.0);
587 glEnable(GL_TEXTURE_2D
);
588 glBindTexture(GL_TEXTURE_2D
, mTexId
);
592 procedure TGxBmpFont
.doneDrawText ();
594 glDisable(GL_ALPHA_TEST
);
595 glDisable(GL_TEXTURE_2D
);
596 glBindTexture(GL_TEXTURE_2D
, 0);
600 function TGxBmpFont
.drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer;
604 tx
:= (Integer(ch
) mod 16)*16;
605 ty
:= (Integer(ch
) div 16)*16;
607 glTexCoord2f((tx
+0)/256.0, (ty
+0)/256.0); glVertex2i(x
+0, y
+0); // top-left
608 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+0)/256.0); glVertex2i(x
+mWidth
, y
+0); // top-right
609 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+mWidth
, y
+mHeight
); // bottom-right
610 glTexCoord2f((tx
+0)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+0, y
+mHeight
); // bottom-left
612 result
:= (mFontWdt
[Byte(ch
)] and $0f);
616 function TGxBmpFont
.drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer;
619 result
:= drawCharInterim(x
, y
, ch
);
624 function TGxBmpFont
.drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer;
629 if (Length(s
) = 0) then begin result
:= 0; exit
; end;
634 wdt
:= drawCharInterim(x
, y
, ch
)+1;
642 // ////////////////////////////////////////////////////////////////////////// //
644 fontList
: array of TGxBmpFont
= nil;
645 defaultFontName
: AnsiString = 'win14';
648 function strEquCI (const s0
, s1
: AnsiString): Boolean;
653 result
:= (Length(s0
) = Length(s1
));
656 for f
:= 1 to Length(s0
) do
659 if (c0
>= 'a') and (c0
<= 'z') then Dec(c0
, 32); // poor man's `toupper()`
661 if (c1
>= 'a') and (c1
<= 'z') then Dec(c1
, 32); // poor man's `toupper()`
662 if (c0
<> c1
) then begin result
:= false; exit
; end;
668 function getFontByName (const aname
: AnsiString): TGxBmpFont
;
673 if (Length(fontList
) = 0) then raise Exception
.Create('font subsystem not initialized');
674 if (Length(aname
) = 0) or (strEquCI(aname
, 'default')) then fname
:= defaultFontName
else fname
:= aname
;
675 for f
:= 0 to High(fontList
) do
677 result
:= fontList
[f
];
678 if (result
= nil) then continue
;
679 if (strEquCI(result
.name
, fname
)) then exit
;
681 if (fontList
[0] = nil) then raise Exception
.Create('font subsystem not properly initialized');
682 result
:= fontList
[0];
687 procedure deleteFonts ();
691 for f := 0 to High(fontList) do freeAndNil(fontList[f]);
697 procedure fuiGfxLoadFont (const fontname
: AnsiString; const fontFile
: AnsiString; proportional
: Boolean=false);
701 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font '''+fontFile
+'''');
702 st
:= fuiOpenFile(fontFile
);
703 if (st
= nil) then raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
705 fuiGfxLoadFont(fontname
, st
, proportional
);
706 except on e
: Exception
do
708 writeln('FlexUI font loadin error: ', e
.message);
710 raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
719 procedure fuiGfxLoadFont (const fontname
: AnsiString; st
: TStream
; proportional
: Boolean=false);
721 fnt
: TGxBmpFont
= nil;
724 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font');
725 fnt
:= TGxBmpFont
.Create(fontname
, st
, proportional
);
727 for f
:= 0 to High(fontList
) do
729 if (strEquCI(fontList
[f
].name
, fontname
)) then
731 if (fontList
[f
].mTexId
<> 0) then raise Exception
.Create('FlexUI: cannot reload generated font named '''+fontname
+'''');
732 FreeAndNil(fontList
[f
]);
737 SetLength(fontList
, Length(fontList
)+1);
738 fontList
[High(fontList
)] := fnt
;
746 procedure oglInitFonts ();
750 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglCreateTexture();
754 procedure oglDeinitFonts ();
758 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglDestroyTexture();
762 // ////////////////////////////////////////////////////////////////////////// //
763 procedure oglSetup2DState ();
766 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
767 glDisable(GL_LINE_SMOOTH
);
768 glDisable(GL_POLYGON_SMOOTH
);
769 glDisable(GL_POINT_SMOOTH
);
770 glDisable(GL_DEPTH_TEST
);
771 glDisable(GL_TEXTURE_2D
);
772 glDisable(GL_LIGHTING
);
773 glDisable(GL_DITHER
);
774 glDisable(GL_STENCIL_TEST
);
775 glDisable(GL_SCISSOR_TEST
);
776 glDisable(GL_CULL_FACE
);
777 glDisable(GL_ALPHA_TEST
);
779 glClearColor(0, 0, 0, 0);
780 glColor4f(1, 1, 1, 1);
784 procedure oglSetup2D (winWidth
, winHeight
: Integer; upsideDown
: Boolean=false);
786 glViewport(0, 0, winWidth
, winHeight
);
790 glMatrixMode(GL_TEXTURE
);
793 glMatrixMode(GL_COLOR
);
796 glMatrixMode(GL_PROJECTION
);
800 glOrtho(0, winWidth
, 0, winHeight
, -1, 1); // set origin to bottom left
804 glOrtho(0, winWidth
, winHeight
, 0, -1, 1); // set origin to top left
807 glMatrixMode(GL_MODELVIEW
);
812 // ////////////////////////////////////////////////////////////////////////// //
813 {$INCLUDE fui_gfx_gl_cursor.inc}
815 procedure oglDrawCursor (); begin oglDrawCursorAt(fuiMouseX
, fuiMouseY
); end;
818 // ////////////////////////////////////////////////////////////////////////// //
819 constructor TGxContext
.Create ();
822 mColor
:= TGxRGBA
.Create(255, 255, 255);
823 mFont
:= getFontByName('default');
826 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
827 mClipOfs
:= TGxOfs
.Create(0, 0);
831 destructor TGxContext
.Destroy ();
833 if (mActive
) then gxSetContext(nil);
838 function TGxContext
.getFont (): AnsiString;
840 result
:= mFont
.name
;
843 procedure TGxContext
.setFont (const aname
: AnsiString);
845 mFont
:= getFontByName(aname
);
849 procedure TGxContext
.onActivate ();
851 setupGLColor(mColor
);
855 procedure TGxContext
.onDeactivate ();
860 procedure TGxContext
.setColor (const clr
: TGxRGBA
);
863 if (mActive
) then setupGLColor(mColor
);
867 procedure TGxContext
.realizeClip ();
869 sx
, sy
, sw
, sh
: Integer;
871 if (not mActive
) then exit
; // just in case
872 if (mClipRect
.w
<= 0) or (mClipRect
.h
<= 0) then
874 glEnable(GL_SCISSOR_TEST
);
875 glScissor(0, 0, 0, 0);
881 sx
:= trunc(mClipRect
.x
*mScale
);
882 sy
:= trunc(mClipRect
.y
*mScale
);
883 sw
:= trunc(mClipRect
.w
*mScale
);
884 sh
:= trunc(mClipRect
.h
*mScale
);
893 if (not intersectRect(sx
, sy
, sw
, sh
, 0, 0, fuiScrWdt
, fuiScrHgt
)) then
895 glEnable(GL_SCISSOR_TEST
);
896 glScissor(0, 0, 0, 0);
898 else if (sx
= 0) and (sy
= 0) and (sw
= fuiScrWdt
) and (sh
= fuiScrHgt
) then
900 glDisable(GL_SCISSOR_TEST
);
904 glEnable(GL_SCISSOR_TEST
);
905 sy
:= fuiScrHgt
-(sy
+sh
);
906 glScissor(sx
, sy
, sw
, sh
);
912 procedure TGxContext
.resetClip ();
914 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
915 if (mActive
) then realizeClip();
919 procedure TGxContext
.setClipOfs (const aofs
: TGxOfs
);
925 procedure TGxContext
.setClipRect (const aclip
: TGxRect
);
928 if (mActive
) then realizeClip();
932 function TGxContext
.setOffset (constref aofs
: TGxOfs
): TGxOfs
;
939 function TGxContext
.setClip (constref aclip
: TGxRect
): TGxRect
;
943 if (mActive
) then realizeClip();
947 function TGxContext
.combineClip (constref aclip
: TGxRect
): TGxRect
;
950 mClipRect
.intersect(aclip
);
951 if (mActive
) then realizeClip();
955 procedure TGxContext
.line (x1
, y1
, x2
, y2
: Integer);
957 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
959 if (not mScaled
) then
963 glVertex2f(x1
+0.375, y1
+0.375);
964 glVertex2f(x2
+0.375, y2
+0.375);
967 if (x1
<> x2
) or (y1
<> y2
) then
971 glVertex2f(x2
+0.375, y2
+0.375);
983 glVertex2i(x2
+1, y2
+1);
989 procedure TGxContext
.hline (x
, y
, len
: Integer);
991 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
992 if (len
< 1) then exit
;
993 if (not mScaled
) then
997 glVertex2f(x
+0.375, y
+0.375);
998 glVertex2f(x
+len
+0.375, y
+0.375);
1001 else if (mScale
> 1.0) then
1005 glVertex2i(x
+len
, y
);
1006 glVertex2i(x
+len
, y
+1);
1014 while (len
> 0) do begin glVertex2i(x
, y
); Inc(x
); Dec(len
); end;
1020 procedure TGxContext
.vline (x
, y
, len
: Integer);
1022 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1023 if (len
< 1) then exit
;
1024 if (not mScaled
) then
1028 glVertex2f(x
+0.375, y
+0.375);
1029 glVertex2f(x
+0.375, y
+len
+0.375);
1032 else if (mScale
> 1.0) then
1036 glVertex2i(x
, y
+len
);
1037 glVertex2i(x
+1, y
+len
);
1045 while (len
> 0) do begin glVertex2i(x
, y
); Inc(y
); Dec(len
); end;
1051 procedure TGxContext
.rect (x
, y
, w
, h
: Integer);
1053 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1054 if (w
< 0) or (h
< 0) then exit
;
1055 if (w
= 1) and (h
= 1) then
1059 if mScaled
then glVertex2i(x
, y
) else glVertex2f(x
+0.375, y
+0.375);
1064 if (not mScaled
) then
1068 glVertex2i(x
, y
); glVertex2i(x
+w
, y
); // top
1069 glVertex2i(x
, y
+h
-1); glVertex2i(x
+w
, y
+h
-1); // bottom
1070 glVertex2f(x
+0.375, y
+1); glVertex2f(x
+0.375, y
+h
-1); // left
1071 glVertex2f(x
+w
-1+0.375, y
+1); glVertex2f(x
+w
-1+0.375, y
+h
-1); // right
1079 vline(x
+w
-1, y
+1, h
-2);
1085 procedure TGxContext
.fillRect (x
, y
, w
, h
: Integer);
1087 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1088 if (w
< 0) or (h
< 0) then exit
;
1092 glVertex2f(x
+w
, y
+h
);
1098 procedure TGxContext
.darkenRect (x
, y
, w
, h
: Integer; a
: Integer);
1100 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (a
>= 255) then exit
;
1101 if (w
< 0) or (h
< 0) then exit
;
1102 if (a
< 0) then a
:= 0;
1104 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1105 glColor4f(0.0, 0.0, 0.0, a
/255.0);
1109 glVertex2i(x
+w
, y
+h
);
1112 setupGLColor(mColor
);
1116 function TGxContext
.charWidth (const ch
: AnsiChar): Integer;
1118 result
:= mFont
.charWidth(ch
);
1121 function TGxContext
.charHeight (const ch
: AnsiChar): Integer;
1123 result
:= mFont
.height
;
1127 function TGxContext
.textWidth (const s
: AnsiString): Integer;
1129 result
:= mFont
.textWidth(s
);
1132 function TGxContext
.textHeight (const s
: AnsiString): Integer;
1134 result
:= mFont
.height
;
1138 function TGxContext
.drawChar (x
, y
: Integer; const ch
: AnsiChar): Integer; // returns char width
1140 result
:= mFont
.charWidth(ch
);
1141 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1142 TGxBmpFont(mFont
).drawCharInternal(x
, y
, ch
);
1145 function TGxContext
.drawText (x
, y
: Integer; const s
: AnsiString): Integer; // returns text width
1147 result
:= mFont
.textWidth(s
);
1148 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) or (Length(s
) = 0) then exit
;
1149 TGxBmpFont(mFont
).drawTextInternal(x
, y
, s
);
1153 function TGxContext
.iconMarkWidth (ic
: TMarkIcon
): Integer;
1155 {$IFDEF FUI_TEXT_ICONS}
1157 TMarkIcon
.Checkbox
: result
:= textWidth('[x]');
1158 TMarkIcon
.Radiobox
: result
:= textWidth('(*)');
1159 else result
:= textWidth('[x]');
1166 function TGxContext
.iconMarkHeight (ic
: TMarkIcon
): Integer;
1168 {$IFDEF FUI_TEXT_ICONS}
1170 TMarkIcon
.Checkbox
: result
:= textHeight('[x]');
1171 TMarkIcon
.Radiobox
: result
:= textHeight('(*)');
1172 else result
:= textHeight('[x]');
1179 procedure TGxContext
.drawIconMark (ic
: TMarkIcon
; x
, y
: Integer; marked
: Boolean);
1181 {$IFDEF FUI_TEXT_ICONS}
1187 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1188 {$IFDEF FUI_TEXT_ICONS}
1190 TMarkIcon
.Checkbox
: xstr
:= '[x]';
1191 TMarkIcon
.Radiobox
: xstr
:= '(*)';
1196 drawText(x
, y
, xstr
);
1200 drawChar(x
, y
, xstr
[1]);
1201 drawChar(x
+textWidth(xstr
)-charWidth(xstr
[3]), y
, xstr
[3]);
1204 if (ic
= TMarkIcon
.Checkbox
) then
1216 vline(x
+10, y
+1, 5);
1222 if (not marked
) then exit
;
1228 vline(x
+3+f
, y
+1+f
, 1);
1229 vline(x
+7-f
, y
+1+f
, 1);
1245 function TGxContext
.iconWinWidth (ic
: TWinIcon
): Integer;
1247 {$IFDEF FUI_TEXT_ICONS}
1249 TWinIcon
.Close
: result
:= nmax(textWidth('[x]'), textWidth('[#]'));
1250 else result
:= nmax(textWidth('[x]'), textWidth('[#]'));
1257 function TGxContext
.iconWinHeight (ic
: TWinIcon
): Integer;
1259 {$IFDEF FUI_TEXT_ICONS}
1261 TWinIcon
.Close
: result
:= nmax(textHeight('[x]'), textHeight('[#]'));
1262 else result
:= nmax(textHeight('[x]'), textHeight('[#]'));
1269 procedure TGxContext
.drawIconWin (ic
: TWinIcon
; x
, y
: Integer; pressed
: Boolean);
1271 {$IFDEF FUI_TEXT_ICONS}
1278 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1279 {$IFDEF FUI_TEXT_ICONS}
1281 TWinIcon
.Close
: if (pressed
) then xstr
:= '[#]' else xstr
:= '[x]';
1284 wdt
:= nmax(textWidth('[x]'), textWidth('[#]'));
1285 drawChar(x
, y
, xstr
[1]);
1286 drawChar(x
+wdt
-charWidth(xstr
[3]), y
, xstr
[3]);
1287 drawChar(x
+((wdt
-charWidth(xstr
[2])) div 2), y
, xstr
[2]);
1289 if pressed
then rect(x
, y
, 9, 8);
1292 vline(x
+1+f
, y
+f
, 1);
1293 vline(x
+1+6-f
, y
+f
, 1);
1299 procedure TGxContext
.glSetScale (ascale
: Single);
1301 if (ascale
< 0.01) then ascale
:= 0.01;
1303 glScalef(ascale
, ascale
, 1.0);
1305 mScaled
:= (ascale
<> 1.0);
1308 procedure TGxContext
.glSetTrans (ax
, ay
: Single);
1311 glScalef(mScale
, mScale
, 1.0);
1312 glTranslatef(ax
, ay
, 0);
1316 procedure TGxContext
.glSetScaleTrans (ascale
, ax
, ay
: Single);
1319 glTranslatef(ax
, ay
, 0);
1323 // vertical scroll bar
1324 procedure TGxContext
.drawVSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
1328 if (wdt
< 1) or (hgt
< 1) then exit
;
1329 filled
:= sbarFilled(hgt
, cur
, min
, max
);
1331 fillRect(x
, y
, wdt
, filled
);
1333 fillRect(x
, y
+filled
, wdt
, hgt
-filled
);
1337 // horizontal scrollbar
1338 procedure TGxContext
.drawHSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
1342 if (wdt
< 1) or (hgt
< 1) then exit
;
1343 filled
:= sbarFilled(wdt
, cur
, min
, max
);
1345 fillRect(x
, y
, filled
, hgt
);
1347 fillRect(x
+filled
, y
, wdt
-filled
, hgt
);
1351 class function TGxContext
.sbarFilled (wh
: Integer; cur
, min
, max
: Integer): Integer;
1353 if (wh
< 1) then result
:= 0
1354 else if (min
> max
) then result
:= 0
1355 else if (min
= max
) then result
:= wh
1358 if (cur
< min
) then cur
:= min
else if (cur
> max
) then cur
:= max
;
1359 result
:= wh
*(cur
-min
) div (max
-min
);
1364 class function TGxContext
.sbarPos (cxy
: Integer; xy
, wh
: Integer; min
, max
: Integer): Integer;
1366 if (wh
< 1) then begin result
:= 0; exit
; end;
1367 if (min
> max
) then begin result
:= 0; exit
; end;
1368 if (min
= max
) then begin result
:= max
; exit
; end;
1369 if (cxy
< xy
) then begin result
:= min
; exit
; end;
1370 if (cxy
>= xy
+wh
) then begin result
:= max
; exit
; end;
1371 result
:= min
+((max
-min
)*(cxy
-xy
) div wh
);
1372 assert((result
>= min
) and (result
<= max
));
1378 // ////////////////////////////////////////////////////////////////////////// //
1380 procedure oglRestoreMode (doClear: Boolean);
1382 oglSetup2D(fuiScrWdt, fuiScrHgt);
1383 glScissor(0, 0, fuiScrWdt, fuiScrHgt);
1385 glBindTexture(GL_TEXTURE_2D, 0);
1386 glDisable(GL_BLEND);
1387 glDisable(GL_TEXTURE_2D);
1388 glDisable(GL_STENCIL_TEST);
1389 glDisable(GL_SCISSOR_TEST);
1390 glDisable(GL_LIGHTING);
1391 glDisable(GL_DEPTH_TEST);
1392 glDisable(GL_CULL_FACE);
1393 glDisable(GL_LINE_SMOOTH);
1394 glDisable(GL_POINT_SMOOTH);
1397 glColor4f(1, 1, 1, 1);
1401 glClearColor(0, 0, 0, 0);
1402 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_ACCUM_BUFFER_BIT or GL_STENCIL_BUFFER_BIT);
1406 glMatrixMode(GL_MODELVIEW);
1408 //glScalef(4, 4, 1);
1413 //procedure onWinFocus (); begin uiFocus(); end;
1414 //procedure onWinBlur (); begin fuiResetKMState(true); uiBlur(); end;
1416 //procedure onPreRender (); begin oglRestoreMode(gGfxDoClear); end;
1417 procedure onPostRender (); begin oglDrawCursor(); end;
1419 procedure onInit ();
1421 //oglSetup2D(fuiScrWdt, fuiScrHgt);
1422 createCursorTexture();
1426 procedure onDeinit ();
1428 fuiResetKMState(false);
1429 if (curtexid
<> 0) then glDeleteTextures(1, @curtexid
);
1439 // ////////////////////////////////////////////////////////////////////////// //
1441 savedGLState
:= TSavedGLState
.Create(false);
1443 //winFocusCB := onWinFocus;
1444 //winBlurCB := onWinBlur;
1445 //prerenderFrameCB := onPreRender;
1446 postrenderFrameCB
:= onPostRender
;
1447 oglInitCB
:= onInit
;
1448 oglDeinitCB
:= onDeinit
;