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}
32 fui_common
, fui_events
;
35 // ////////////////////////////////////////////////////////////////////////// //
44 function charWidth (const ch
: AnsiChar): Integer; virtual; abstract;
45 function textWidth (const s
: AnsiString): Integer; virtual; abstract;
48 property name
: AnsiString read mName
;
49 property height
: Integer read mHeight
;
50 property baseLine
: Integer read mBaseLine
;
70 // for active contexts
77 function getFont (): AnsiString;
78 procedure setFont (const aname
: AnsiString);
80 procedure onActivate ();
81 procedure onDeactivate ();
83 procedure setColor (const clr
: TGxRGBA
);
85 procedure realizeClip (); // setup scissoring
87 procedure setClipOfs (const aofs
: TGxOfs
);
88 procedure setClipRect (const aclip
: TGxRect
);
91 constructor Create ();
92 destructor Destroy (); override;
94 procedure line (x1
, y1
, x2
, y2
: Integer);
95 procedure hline (x
, y
, len
: Integer);
96 procedure vline (x
, y
, len
: Integer);
97 procedure rect (x
, y
, w
, h
: Integer);
98 procedure fillRect (x
, y
, w
, h
: Integer);
99 procedure darkenRect (x
, y
, w
, h
: Integer; a
: Integer);
101 function charWidth (const ch
: AnsiChar): Integer;
102 function charHeight (const ch
: AnsiChar): Integer;
103 function textWidth (const s
: AnsiString): Integer;
104 function textHeight (const s
: AnsiString): Integer;
105 function drawChar (x
, y
: Integer; const ch
: AnsiChar): Integer; // returns char width
106 function drawText (x
, y
: Integer; const s
: AnsiString): Integer; // returns text width
108 function iconMarkWidth (ic
: TMarkIcon
): Integer;
109 function iconMarkHeight (ic
: TMarkIcon
): Integer;
110 procedure drawIconMark (ic
: TMarkIcon
; x
, y
: Integer; marked
: Boolean);
112 function iconWinWidth (ic
: TWinIcon
): Integer;
113 function iconWinHeight (ic
: TWinIcon
): Integer;
114 procedure drawIconWin (ic
: TWinIcon
; x
, y
: Integer; pressed
: Boolean);
116 procedure resetClip ();
118 function setOffset (constref aofs
: TGxOfs
): TGxOfs
; // returns previous offset
119 function setClip (constref aclip
: TGxRect
): TGxRect
; // returns previous clip
121 function combineClip (constref aclip
: TGxRect
): TGxRect
; // returns previous clip
123 // vertical scrollbar
124 procedure drawVSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
125 // horizontal scrollbar
126 procedure drawHSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
128 class function sbarFilled (wh
: Integer; cur
, min
, max
: Integer): Integer;
129 class function sbarPos (cxy
: Integer; xy
, wh
: Integer; min
, max
: Integer): Integer;
132 procedure glSetScale (ascale
: Single);
133 procedure glSetTrans (ax
, ay
: Single);
134 procedure glSetScaleTrans (ascale
, ax
, ay
: Single);
137 property active
: Boolean read mActive
;
138 property color
: TGxRGBA read mColor write setColor
;
139 property font
: AnsiString read getFont write setFont
;
140 property offset
: TGxOfs read mClipOfs write setClipOfs
;
141 property clip
: TGxRect read mClipRect write setClipRect
; // clipping is unaffected by offset
145 // set active context; `ctx` can be `nil`
146 procedure gxSetContext (ctx
: TGxContext
; ascale
: Single=1.0);
147 procedure gxSetContextNoMatrix (ctx
: TGxContext
);
150 // setup 2D OpenGL mode; will be called automatically in `glInit()`
151 procedure oglSetup2D (winWidth
, winHeight
: Integer; upsideDown
: Boolean=false);
152 procedure oglSetup2DState (); // don't modify viewports and matrices
154 procedure oglDrawCursor ();
155 procedure oglDrawCursorAt (msX
, msY
: Integer);
158 procedure fuiGfxLoadFont (const fontname
: AnsiString; const fontFile
: AnsiString; proportional
: Boolean=false);
159 procedure fuiGfxLoadFont (const fontname
: AnsiString; st
: TStream
; proportional
: Boolean=false);
162 // ////////////////////////////////////////////////////////////////////////// //
164 gGfxDoClear
: Boolean = true;
174 // ////////////////////////////////////////////////////////////////////////// //
175 // returns `false` if the color is transparent
176 // returns `false` if the color is transparent
177 function setupGLColor (constref clr
: TGxRGBA
): Boolean;
179 if (clr
.a
< 255) then
182 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
188 glColor4ub(clr
.r
, clr
.g
, clr
.b
, clr
.a
);
189 result
:= (clr
.a
<> 0);
192 function isScaled (): Boolean;
194 mt
: packed array [0..15] of Double;
197 glGetDoublev(GL_MODELVIEW_MATRIX
, @mt
[0]);
199 result
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
203 // ////////////////////////////////////////////////////////////////////////// //
204 //TODO: OpenGL framebuffers and shaders state
206 TSavedGLState
= record
209 gltextbinding
: GLint
;
211 //oldfbr, oldfbw: GLint;
212 glvport
: packed array [0..3] of GLint
;
216 constructor Create (dosave
: Boolean);
218 procedure restore ();
221 constructor TSavedGLState
.Create (dosave
: Boolean);
223 FillChar(self
, sizeof(self
), 0);
224 if (dosave
) then save();
227 procedure TSavedGLState
.save ();
229 if (saved
) then raise Exception
.Create('cannot save into already saved OpenGL state');
230 glGetIntegerv(GL_MATRIX_MODE
, @glmatmode
);
231 glGetIntegerv(GL_TEXTURE_BINDING_2D
, @gltextbinding
);
232 glGetIntegerv(GL_VIEWPORT
, @glvport
[0]);
233 //glGetIntegerv(GL_CURRENT_PROGRAM, &oldprg);
234 //glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &oldfbr);
235 //glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfbw);
236 glMatrixMode(GL_PROJECTION
); glPushMatrix();
237 glMatrixMode(GL_MODELVIEW
); glPushMatrix();
238 glMatrixMode(GL_TEXTURE
); glPushMatrix();
239 glMatrixMode(GL_COLOR
); glPushMatrix();
241 glPushAttrib({GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_CURRENT_BIT}GL_ALL_ATTRIB_BITS
); // let's play safe
246 procedure TSavedGLState
.restore ();
248 if (not saved
) then raise Exception
.Create('cannot restore unsaved OpenGL state');
250 glPopAttrib({GL_ENABLE_BIT});
252 glMatrixMode(GL_PROJECTION
); glPopMatrix();
253 glMatrixMode(GL_MODELVIEW
); glPopMatrix();
254 glMatrixMode(GL_TEXTURE
); glPopMatrix();
255 glMatrixMode(GL_COLOR
); glPopMatrix();
256 glMatrixMode(glmatmode
);
257 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, oldfbr);
258 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, oldfbw);
259 glBindTexture(GL_TEXTURE_2D
, gltextbinding
);
260 //if (glHasFunc!"glUseProgram") glUseProgram(oldprg);
261 glViewport(glvport
[0], glvport
[1], glvport
[2], glvport
[3]);
267 curCtx
: TGxContext
= nil;
268 savedGLState
: TSavedGLState
;
271 // ////////////////////////////////////////////////////////////////////////// //
272 // set active context; `ctx` can be `nil`
273 procedure gxSetContextInternal (ctx
: TGxContext
; ascale
: Single; domatrix
: Boolean);
275 mt
: packed array [0..15] of Double;
277 if (savedGLState
.saved
) then savedGLState
.restore();
279 if (curCtx
<> nil) then
281 curCtx
.onDeactivate();
282 curCtx
.mActive
:= false;
292 oglSetup2D(fuiScrWdt
, fuiScrHgt
);
293 glScalef(ascale
, ascale
, 1.0);
294 ctx
.mScaled
:= (ascale
<> 1.0);
295 ctx
.mScale
:= ascale
;
299 // assume uniform scale
301 glGetDoublev(GL_MODELVIEW_MATRIX
, @mt
[0]);
303 ctx
.mScaled
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
312 procedure gxSetContext (ctx
: TGxContext
; ascale
: Single=1.0); begin gxSetContextInternal(ctx
, ascale
, true); end;
313 procedure gxSetContextNoMatrix (ctx
: TGxContext
); begin gxSetContextInternal(ctx
, 1, false); end;
316 // ////////////////////////////////////////////////////////////////////////// //
318 TScissorSave
= record
321 scxywh
: packed array[0..3] of GLint
;
326 procedure save (enableScissoring
: Boolean);
327 procedure restore ();
329 // set new scissor rect, bounded by the saved scissor rect
330 procedure combineRect (x
, y
, w
, h
: Integer);
334 procedure TScissorSave
.save (enableScissoring
: Boolean);
337 wassc
:= false; // FIXIT
339 wassc
:= (glIsEnabled(GL_SCISSOR_TEST
) <> 0);
341 if wassc
then glGetIntegerv(GL_SCISSOR_BOX
, @scxywh
[0]) else glGetIntegerv(GL_VIEWPORT
, @scxywh
[0]);
342 //conwritefln('(%d,%d)-(%d,%d)', [scxywh[0], scxywh[1], scxywh[2], scxywh[3]]);
343 if enableScissoring
and (not wassc
) then glEnable(GL_SCISSOR_TEST
);
346 procedure TScissorSave
.restore ();
348 glScissor(scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]);
349 if wassc
then glEnable(GL_SCISSOR_TEST
) else glDisable(GL_SCISSOR_TEST
);
352 procedure TScissorSave
.combineRect (x
, y
, w
, h
: Integer);
353 //var ox, oy, ow, oh: Integer;
355 if (w
< 1) or (h
< 1) then begin glScissor(0, 0, 0, 0); exit
; end;
356 y
:= fuiScrHgt
-(y
+h
);
357 //ox := x; oy := y; ow := w; oh := h;
358 if not intersectRect(x
, y
, w
, h
, scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]) then
360 //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, ')');
361 //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, '>');
362 glScissor(0, 0, 0, 0);
366 glScissor(x
, y
, w
, h
);
371 // ////////////////////////////////////////////////////////////////////////// //
373 TGxBmpFont
= class(TGxFont
)
375 mTexId
: GLuint
; // OpenGL texture id
376 mWidth
: Integer; // <=0: proportional
379 mFreeFontWdt
: Boolean;
380 mFreeFontBmp
: Boolean;
383 procedure oglCreateTexture ();
384 procedure oglDestroyTexture ();
386 procedure initDrawText ();
387 procedure doneDrawText ();
388 function drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
389 function drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
390 function drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer; // return width (not including last empty pixel)
393 constructor Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
394 destructor Destroy (); override;
396 function charWidth (const ch
: AnsiChar): Integer; override;
397 function textWidth (const s
: AnsiString): Integer; override;
401 constructor TGxBmpFont
.Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
403 sign
: packed array [0..7] of AnsiChar;
404 enc
: packed array [0..16] of AnsiChar;
406 wdt
, hgt
, elen
: Integer;
411 mFreeFontBmp
:= true;
412 mFreeFontWdt
:= true;
416 st
.ReadBuffer(sign
[0], 8);
417 if (sign
<> 'FUIFONT0') then raise Exception
.Create('FlexUI: invalid font file signature');
418 // encoding length and width
420 wdt
:= (b
and $0f)+1; // 16 is not supported
421 if (wdt
= 16) then raise Exception
.Create('FlexUI: 16-wdt fonts aren''t supported yet');
422 elen
:= ((b
shr 4) and $0f);
423 if (elen
= 0) then raise Exception
.CreateFmt('FlexUI: invalid font encoding length: %d', [elen
]);
427 if (hgt
< 2) then raise Exception
.CreateFmt('FlexUI: invalid font height: %d', [hgt
]);
429 st
.ReadBuffer(enc
[0], elen
);
430 // check for 'cp1251' here (it can also be 'koi8')
431 if (wdt
<= 8) then fntbwdt
:= 1 else fntbwdt
:= 2;
432 // shift and width table (hi nibble: left shift for proportional print; lo nibble: shifted character width for proportional print)
433 GetMem(mFontWdt
, 256);
434 st
.ReadBuffer(mFontWdt
^, 256);
436 GetMem(mFontBmp
, (hgt
*fntbwdt
)*256);
437 st
.ReadBuffer(mFontBmp
^, (hgt
*fntbwdt
)*256);
440 mBaseLine
:= hgt
-1; //FIXME
441 if (proportional
) then
444 for ch
:= 0 to 255 do
446 for dy
:= 0 to hgt
-1 do
448 if (fntbwdt
= 1) then
450 mFontBmp
[ch
*hgt
+dy
] := mFontBmp
[ch
*hgt
+dy
] shl (mFontWdt
[ch
] shr 4);
454 wrd
:= mFontBmp
[ch
*(hgt
*2)+(dy
*2)]+256*mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1];
455 wrd
:= wrd
shl (mFontWdt
[ch
] shr 4);
456 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+0] := (wrd
and $ff);
457 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1] := ((wrd
shr 16) and $ff);
464 FillChar(mFontWdt
^, 256, wdt
);
469 destructor TGxBmpFont
.Destroy ();
471 if (mFreeFontBmp
) and (mFontBmp
<> nil) then FreeMem(mFontBmp
);
472 if (mFreeFontWdt
) and (mFontWdt
<> nil) then FreeMem(mFontWdt
);
479 mFreeFontWdt
:= false;
480 mFreeFontBmp
:= false;
486 procedure TGxBmpFont
.oglCreateTexture ();
494 x
, y
, dx
, dy
: Integer;
496 GetMem(tex
, TxWidth
*TxHeight
*4);
497 FillChar(tex
^, TxWidth
*TxHeight
*4, 0);
499 for cc
:= 0 to 255 do
503 for dy
:= 0 to mHeight
-1 do
505 if (mWidth
<= 8) then b
:= mFontBmp
[cc
*mHeight
+dy
] else b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+1];
506 //if prop then b := b shl (fontwdt[cc] shr 4);
507 tpp
:= tex
+((y
+dy
)*(TxWidth
*4))+x
*4;
510 if ((b
and $80) <> 0) then
512 tpp
^ := 255; Inc(tpp
);
513 tpp
^ := 255; Inc(tpp
);
514 tpp
^ := 255; Inc(tpp
);
515 tpp
^ := 255; Inc(tpp
);
524 b
:= (b
and $7f) shl 1;
528 b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+0];
531 if ((b
and $80) <> 0) then
533 tpp
^ := 255; Inc(tpp
);
534 tpp
^ := 255; Inc(tpp
);
535 tpp
^ := 255; Inc(tpp
);
536 tpp
^ := 255; Inc(tpp
);
545 b
:= (b
and $7f) shl 1;
551 glGenTextures(1, @mTexId
);
552 if (mTexId
= 0) then raise Exception
.Create('can''t create FlexUI font texture');
554 glBindTexture(GL_TEXTURE_2D
, mTexId
);
555 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
556 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
557 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
558 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
560 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, TxWidth
, TxHeight
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex
);
563 glBindTexture(GL_TEXTURE_2D
, 0);
568 procedure TGxBmpFont
.oglDestroyTexture ();
570 if (mTexId
<> 0) then
572 glDeleteTextures(1, @mTexId
);
578 function TGxBmpFont
.charWidth (const ch
: AnsiChar): Integer;
580 result
:= (mFontWdt
[Byte(ch
)] and $0f);
584 function TGxBmpFont
.textWidth (const s
: AnsiString): Integer;
588 if (Length(s
) > 0) then
591 for ch
in s
do result
+= (mFontWdt
[Byte(ch
)] and $0f)+1;
600 procedure TGxBmpFont
.initDrawText ();
602 glEnable(GL_ALPHA_TEST
);
603 glAlphaFunc(GL_NOTEQUAL
, 0.0);
604 glEnable(GL_TEXTURE_2D
);
605 glBindTexture(GL_TEXTURE_2D
, mTexId
);
609 procedure TGxBmpFont
.doneDrawText ();
611 glDisable(GL_ALPHA_TEST
);
612 glDisable(GL_TEXTURE_2D
);
613 glBindTexture(GL_TEXTURE_2D
, 0);
617 function TGxBmpFont
.drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer;
621 tx
:= (Integer(ch
) mod 16)*16;
622 ty
:= (Integer(ch
) div 16)*16;
624 glTexCoord2f((tx
+0)/256.0, (ty
+0)/256.0); glVertex2i(x
+0, y
+0); // top-left
625 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+0)/256.0); glVertex2i(x
+mWidth
, y
+0); // top-right
626 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+mWidth
, y
+mHeight
); // bottom-right
627 glTexCoord2f((tx
+0)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+0, y
+mHeight
); // bottom-left
629 result
:= (mFontWdt
[Byte(ch
)] and $0f);
633 function TGxBmpFont
.drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer;
636 result
:= drawCharInterim(x
, y
, ch
);
641 function TGxBmpFont
.drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer;
646 if (Length(s
) = 0) then begin result
:= 0; exit
; end;
651 wdt
:= drawCharInterim(x
, y
, ch
)+1;
659 // ////////////////////////////////////////////////////////////////////////// //
661 fontList
: array of TGxBmpFont
= nil;
662 defaultFontName
: AnsiString = 'win14';
665 function strEquCI (const s0
, s1
: AnsiString): Boolean;
670 result
:= (Length(s0
) = Length(s1
));
673 for f
:= 1 to Length(s0
) do
676 if (c0
>= 'a') and (c0
<= 'z') then Dec(c0
, 32); // poor man's `toupper()`
678 if (c1
>= 'a') and (c1
<= 'z') then Dec(c1
, 32); // poor man's `toupper()`
679 if (c0
<> c1
) then begin result
:= false; exit
; end;
685 function getFontByName (const aname
: AnsiString): TGxBmpFont
;
690 if (Length(fontList
) = 0) then raise Exception
.Create('font subsystem not initialized');
691 if (Length(aname
) = 0) or (strEquCI(aname
, 'default')) then fname
:= defaultFontName
else fname
:= aname
;
692 for f
:= 0 to High(fontList
) do
694 result
:= fontList
[f
];
695 if (result
= nil) then continue
;
696 if (strEquCI(result
.name
, fname
)) then exit
;
698 if (fontList
[0] = nil) then raise Exception
.Create('font subsystem not properly initialized');
699 result
:= fontList
[0];
704 procedure deleteFonts ();
708 for f := 0 to High(fontList) do freeAndNil(fontList[f]);
714 procedure fuiGfxLoadFont (const fontname
: AnsiString; const fontFile
: AnsiString; proportional
: Boolean=false);
718 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font '''+fontFile
+'''');
719 st
:= fuiOpenFile(fontFile
);
720 if (st
= nil) then raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
722 fuiGfxLoadFont(fontname
, st
, proportional
);
723 except on e
: Exception
do
725 writeln('FlexUI font loadin error: ', e
.message);
727 raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
736 procedure fuiGfxLoadFont (const fontname
: AnsiString; st
: TStream
; proportional
: Boolean=false);
738 fnt
: TGxBmpFont
= nil;
741 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font');
742 fnt
:= TGxBmpFont
.Create(fontname
, st
, proportional
);
744 for f
:= 0 to High(fontList
) do
746 if (strEquCI(fontList
[f
].name
, fontname
)) then
748 if (fontList
[f
].mTexId
<> 0) then raise Exception
.Create('FlexUI: cannot reload generated font named '''+fontname
+'''');
749 FreeAndNil(fontList
[f
]);
754 SetLength(fontList
, Length(fontList
)+1);
755 fontList
[High(fontList
)] := fnt
;
763 procedure oglInitFonts ();
767 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglCreateTexture();
771 procedure oglDeinitFonts ();
775 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglDestroyTexture();
779 // ////////////////////////////////////////////////////////////////////////// //
780 procedure oglSetup2DState ();
783 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
784 glDisable(GL_LINE_SMOOTH
);
785 glDisable(GL_POLYGON_SMOOTH
);
786 glDisable(GL_POINT_SMOOTH
);
787 glDisable(GL_DEPTH_TEST
);
788 glDisable(GL_TEXTURE_2D
);
789 glDisable(GL_LIGHTING
);
790 glDisable(GL_DITHER
);
791 glDisable(GL_STENCIL_TEST
);
792 glDisable(GL_SCISSOR_TEST
);
793 glDisable(GL_CULL_FACE
);
794 glDisable(GL_ALPHA_TEST
);
796 glClearColor(0, 0, 0, 0);
797 glColor4f(1, 1, 1, 1);
801 procedure oglSetup2D (winWidth
, winHeight
: Integer; upsideDown
: Boolean=false);
803 glViewport(0, 0, winWidth
, winHeight
);
807 glMatrixMode(GL_TEXTURE
);
810 glMatrixMode(GL_COLOR
);
813 glMatrixMode(GL_PROJECTION
);
817 glOrtho(0, winWidth
, 0, winHeight
, -1, 1); // set origin to bottom left
821 glOrtho(0, winWidth
, winHeight
, 0, -1, 1); // set origin to top left
824 glMatrixMode(GL_MODELVIEW
);
829 // ////////////////////////////////////////////////////////////////////////// //
830 {$INCLUDE fui_gfx_gl_cursor.inc}
832 procedure oglDrawCursor (); begin oglDrawCursorAt(fuiMouseX
, fuiMouseY
); end;
835 // ////////////////////////////////////////////////////////////////////////// //
836 constructor TGxContext
.Create ();
839 mColor
:= TGxRGBA
.Create(255, 255, 255);
840 mFont
:= getFontByName('default');
843 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
844 mClipOfs
:= TGxOfs
.Create(0, 0);
848 destructor TGxContext
.Destroy ();
850 if (mActive
) then gxSetContext(nil);
855 function TGxContext
.getFont (): AnsiString;
857 result
:= mFont
.name
;
860 procedure TGxContext
.setFont (const aname
: AnsiString);
862 mFont
:= getFontByName(aname
);
866 procedure TGxContext
.onActivate ();
868 setupGLColor(mColor
);
872 procedure TGxContext
.onDeactivate ();
877 procedure TGxContext
.setColor (const clr
: TGxRGBA
);
880 if (mActive
) then setupGLColor(mColor
);
884 procedure TGxContext
.realizeClip ();
886 sx
, sy
, sw
, sh
: Integer;
888 if (not mActive
) then exit
; // just in case
889 if (mClipRect
.w
<= 0) or (mClipRect
.h
<= 0) then
891 glEnable(GL_SCISSOR_TEST
);
892 glScissor(0, 0, 0, 0);
898 sx
:= trunc(mClipRect
.x
*mScale
);
899 sy
:= trunc(mClipRect
.y
*mScale
);
900 sw
:= trunc(mClipRect
.w
*mScale
);
901 sh
:= trunc(mClipRect
.h
*mScale
);
910 if (not intersectRect(sx
, sy
, sw
, sh
, 0, 0, fuiScrWdt
, fuiScrHgt
)) then
912 glEnable(GL_SCISSOR_TEST
);
913 glScissor(0, 0, 0, 0);
915 else if (sx
= 0) and (sy
= 0) and (sw
= fuiScrWdt
) and (sh
= fuiScrHgt
) then
917 glDisable(GL_SCISSOR_TEST
);
921 glEnable(GL_SCISSOR_TEST
);
922 sy
:= fuiScrHgt
-(sy
+sh
);
923 glScissor(sx
, sy
, sw
, sh
);
929 procedure TGxContext
.resetClip ();
931 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
932 if (mActive
) then realizeClip();
936 procedure TGxContext
.setClipOfs (const aofs
: TGxOfs
);
942 procedure TGxContext
.setClipRect (const aclip
: TGxRect
);
945 if (mActive
) then realizeClip();
949 function TGxContext
.setOffset (constref aofs
: TGxOfs
): TGxOfs
;
956 function TGxContext
.setClip (constref aclip
: TGxRect
): TGxRect
;
960 if (mActive
) then realizeClip();
964 function TGxContext
.combineClip (constref aclip
: TGxRect
): TGxRect
;
967 mClipRect
.intersect(aclip
);
968 if (mActive
) then realizeClip();
972 procedure TGxContext
.line (x1
, y1
, x2
, y2
: Integer);
974 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
976 if (not mScaled
) then
980 glVertex2f(x1
+0.375, y1
+0.375);
981 glVertex2f(x2
+0.375, y2
+0.375);
984 if (x1
<> x2
) or (y1
<> y2
) then
988 glVertex2f(x2
+0.375, y2
+0.375);
1000 glVertex2i(x2
+1, y2
+1);
1006 procedure TGxContext
.hline (x
, y
, len
: Integer);
1008 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1009 if (len
< 1) then exit
;
1010 if (not mScaled
) then
1014 glVertex2f(x
+0.375, y
+0.375);
1015 glVertex2f(x
+len
+0.375, y
+0.375);
1018 else if (mScale
> 1.0) then
1022 glVertex2i(x
+len
, y
);
1023 glVertex2i(x
+len
, y
+1);
1031 while (len
> 0) do begin glVertex2i(x
, y
); Inc(x
); Dec(len
); end;
1037 procedure TGxContext
.vline (x
, y
, len
: Integer);
1039 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1040 if (len
< 1) then exit
;
1041 if (not mScaled
) then
1045 glVertex2f(x
+0.375, y
+0.375);
1046 glVertex2f(x
+0.375, y
+len
+0.375);
1049 else if (mScale
> 1.0) then
1053 glVertex2i(x
, y
+len
);
1054 glVertex2i(x
+1, y
+len
);
1062 while (len
> 0) do begin glVertex2i(x
, y
); Inc(y
); Dec(len
); end;
1068 procedure TGxContext
.rect (x
, y
, w
, h
: Integer);
1070 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1071 if (w
< 0) or (h
< 0) then exit
;
1072 if (w
= 1) and (h
= 1) then
1076 if mScaled
then glVertex2i(x
, y
) else glVertex2f(x
+0.375, y
+0.375);
1081 if (not mScaled
) then
1085 glVertex2i(x
, y
); glVertex2i(x
+w
, y
); // top
1086 glVertex2i(x
, y
+h
-1); glVertex2i(x
+w
, y
+h
-1); // bottom
1087 glVertex2f(x
+0.375, y
+1); glVertex2f(x
+0.375, y
+h
-1); // left
1088 glVertex2f(x
+w
-1+0.375, y
+1); glVertex2f(x
+w
-1+0.375, y
+h
-1); // right
1096 vline(x
+w
-1, y
+1, h
-2);
1102 procedure TGxContext
.fillRect (x
, y
, w
, h
: Integer);
1104 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1105 if (w
< 0) or (h
< 0) then exit
;
1109 glVertex2f(x
+w
, y
+h
);
1115 procedure TGxContext
.darkenRect (x
, y
, w
, h
: Integer; a
: Integer);
1117 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (a
>= 255) then exit
;
1118 if (w
< 0) or (h
< 0) then exit
;
1119 if (a
< 0) then a
:= 0;
1121 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1122 glColor4f(0.0, 0.0, 0.0, a
/255.0);
1126 glVertex2i(x
+w
, y
+h
);
1129 setupGLColor(mColor
);
1133 function TGxContext
.charWidth (const ch
: AnsiChar): Integer;
1135 result
:= mFont
.charWidth(ch
);
1138 function TGxContext
.charHeight (const ch
: AnsiChar): Integer;
1140 result
:= mFont
.height
;
1144 function TGxContext
.textWidth (const s
: AnsiString): Integer;
1146 result
:= mFont
.textWidth(s
);
1149 function TGxContext
.textHeight (const s
: AnsiString): Integer;
1151 result
:= mFont
.height
;
1155 function TGxContext
.drawChar (x
, y
: Integer; const ch
: AnsiChar): Integer; // returns char width
1157 result
:= mFont
.charWidth(ch
);
1158 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1159 TGxBmpFont(mFont
).drawCharInternal(x
, y
, ch
);
1162 function TGxContext
.drawText (x
, y
: Integer; const s
: AnsiString): Integer; // returns text width
1164 result
:= mFont
.textWidth(s
);
1165 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) or (Length(s
) = 0) then exit
;
1166 TGxBmpFont(mFont
).drawTextInternal(x
, y
, s
);
1170 function TGxContext
.iconMarkWidth (ic
: TMarkIcon
): Integer;
1172 {$IFDEF FUI_TEXT_ICONS}
1174 TMarkIcon
.Checkbox
: result
:= textWidth('[x]');
1175 TMarkIcon
.Radiobox
: result
:= textWidth('(*)');
1176 else result
:= textWidth('[x]');
1183 function TGxContext
.iconMarkHeight (ic
: TMarkIcon
): Integer;
1185 {$IFDEF FUI_TEXT_ICONS}
1187 TMarkIcon
.Checkbox
: result
:= textHeight('[x]');
1188 TMarkIcon
.Radiobox
: result
:= textHeight('(*)');
1189 else result
:= textHeight('[x]');
1196 procedure TGxContext
.drawIconMark (ic
: TMarkIcon
; x
, y
: Integer; marked
: Boolean);
1198 {$IFDEF FUI_TEXT_ICONS}
1204 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1205 {$IFDEF FUI_TEXT_ICONS}
1207 TMarkIcon
.Checkbox
: xstr
:= '[x]';
1208 TMarkIcon
.Radiobox
: xstr
:= '(*)';
1213 drawText(x
, y
, xstr
);
1217 drawChar(x
, y
, xstr
[1]);
1218 drawChar(x
+textWidth(xstr
)-charWidth(xstr
[3]), y
, xstr
[3]);
1221 if (ic
= TMarkIcon
.Checkbox
) then
1233 vline(x
+10, y
+1, 5);
1239 if (not marked
) then exit
;
1245 vline(x
+3+f
, y
+1+f
, 1);
1246 vline(x
+7-f
, y
+1+f
, 1);
1262 function TGxContext
.iconWinWidth (ic
: TWinIcon
): Integer;
1264 {$IFDEF FUI_TEXT_ICONS}
1266 TWinIcon
.Close
: result
:= nmax(textWidth('[x]'), textWidth('[#]'));
1267 else result
:= nmax(textWidth('[x]'), textWidth('[#]'));
1274 function TGxContext
.iconWinHeight (ic
: TWinIcon
): Integer;
1276 {$IFDEF FUI_TEXT_ICONS}
1278 TWinIcon
.Close
: result
:= nmax(textHeight('[x]'), textHeight('[#]'));
1279 else result
:= nmax(textHeight('[x]'), textHeight('[#]'));
1286 procedure TGxContext
.drawIconWin (ic
: TWinIcon
; x
, y
: Integer; pressed
: Boolean);
1288 {$IFDEF FUI_TEXT_ICONS}
1295 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1296 {$IFDEF FUI_TEXT_ICONS}
1298 TWinIcon
.Close
: if (pressed
) then xstr
:= '[#]' else xstr
:= '[x]';
1301 wdt
:= nmax(textWidth('[x]'), textWidth('[#]'));
1302 drawChar(x
, y
, xstr
[1]);
1303 drawChar(x
+wdt
-charWidth(xstr
[3]), y
, xstr
[3]);
1304 drawChar(x
+((wdt
-charWidth(xstr
[2])) div 2), y
, xstr
[2]);
1306 if pressed
then rect(x
, y
, 9, 8);
1309 vline(x
+1+f
, y
+f
, 1);
1310 vline(x
+1+6-f
, y
+f
, 1);
1316 procedure TGxContext
.glSetScale (ascale
: Single);
1318 if (ascale
< 0.01) then ascale
:= 0.01;
1320 glScalef(ascale
, ascale
, 1.0);
1322 mScaled
:= (ascale
<> 1.0);
1325 procedure TGxContext
.glSetTrans (ax
, ay
: Single);
1328 glScalef(mScale
, mScale
, 1.0);
1329 glTranslatef(ax
, ay
, 0);
1333 procedure TGxContext
.glSetScaleTrans (ascale
, ax
, ay
: Single);
1336 glTranslatef(ax
, ay
, 0);
1340 // vertical scroll bar
1341 procedure TGxContext
.drawVSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
1345 if (wdt
< 1) or (hgt
< 1) then exit
;
1346 filled
:= sbarFilled(hgt
, cur
, min
, max
);
1348 fillRect(x
, y
, wdt
, filled
);
1350 fillRect(x
, y
+filled
, wdt
, hgt
-filled
);
1354 // horizontal scrollbar
1355 procedure TGxContext
.drawHSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
1359 if (wdt
< 1) or (hgt
< 1) then exit
;
1360 filled
:= sbarFilled(wdt
, cur
, min
, max
);
1362 fillRect(x
, y
, filled
, hgt
);
1364 fillRect(x
+filled
, y
, wdt
-filled
, hgt
);
1368 class function TGxContext
.sbarFilled (wh
: Integer; cur
, min
, max
: Integer): Integer;
1370 if (wh
< 1) then result
:= 0
1371 else if (min
> max
) then result
:= 0
1372 else if (min
= max
) then result
:= wh
1375 if (cur
< min
) then cur
:= min
else if (cur
> max
) then cur
:= max
;
1376 result
:= wh
*(cur
-min
) div (max
-min
);
1381 class function TGxContext
.sbarPos (cxy
: Integer; xy
, wh
: Integer; min
, max
: Integer): Integer;
1383 if (wh
< 1) then begin result
:= 0; exit
; end;
1384 if (min
> max
) then begin result
:= 0; exit
; end;
1385 if (min
= max
) then begin result
:= max
; exit
; end;
1386 if (cxy
< xy
) then begin result
:= min
; exit
; end;
1387 if (cxy
>= xy
+wh
) then begin result
:= max
; exit
; end;
1388 result
:= min
+((max
-min
)*(cxy
-xy
) div wh
);
1389 assert((result
>= min
) and (result
<= max
));
1395 // ////////////////////////////////////////////////////////////////////////// //
1397 procedure oglRestoreMode (doClear: Boolean);
1399 oglSetup2D(fuiScrWdt, fuiScrHgt);
1400 glScissor(0, 0, fuiScrWdt, fuiScrHgt);
1402 glBindTexture(GL_TEXTURE_2D, 0);
1403 glDisable(GL_BLEND);
1404 glDisable(GL_TEXTURE_2D);
1405 glDisable(GL_STENCIL_TEST);
1406 glDisable(GL_SCISSOR_TEST);
1407 glDisable(GL_LIGHTING);
1408 glDisable(GL_DEPTH_TEST);
1409 glDisable(GL_CULL_FACE);
1410 glDisable(GL_LINE_SMOOTH);
1411 glDisable(GL_POINT_SMOOTH);
1414 glColor4f(1, 1, 1, 1);
1418 glClearColor(0, 0, 0, 0);
1419 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_ACCUM_BUFFER_BIT or GL_STENCIL_BUFFER_BIT);
1423 glMatrixMode(GL_MODELVIEW);
1425 //glScalef(4, 4, 1);
1430 //procedure onWinFocus (); begin uiFocus(); end;
1431 //procedure onWinBlur (); begin fuiResetKMState(true); uiBlur(); end;
1433 //procedure onPreRender (); begin oglRestoreMode(gGfxDoClear); end;
1434 procedure onPostRender (); begin oglDrawCursor(); end;
1436 procedure onInit ();
1438 //oglSetup2D(fuiScrWdt, fuiScrHgt);
1439 createCursorTexture();
1443 procedure onDeinit ();
1445 fuiResetKMState(false);
1446 if (curtexid
<> 0) then glDeleteTextures(1, @curtexid
);
1456 // ////////////////////////////////////////////////////////////////////////// //
1458 savedGLState
:= TSavedGLState
.Create(false);
1460 //winFocusCB := onWinFocus;
1461 //winBlurCB := onWinBlur;
1462 //prerenderFrameCB := onPreRender;
1463 postrenderFrameCB
:= onPostRender
;
1464 oglInitCB
:= onInit
;
1465 oglDeinitCB
:= onDeinit
;