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 GLfloat
;
196 glGetFloatv(GL_MODELVIEW_MATRIX
, @mt
[0]);
197 result
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
201 // ////////////////////////////////////////////////////////////////////////// //
202 //TODO: OpenGL framebuffers and shaders state
204 TSavedGLState
= record
207 gltextbinding
: GLint
;
209 //oldfbr, oldfbw: GLint;
210 glvport
: packed array [0..3] of GLint
;
214 constructor Create (dosave
: Boolean);
216 procedure restore ();
219 constructor TSavedGLState
.Create (dosave
: Boolean);
221 FillChar(self
, sizeof(self
), 0);
222 if (dosave
) then save();
225 procedure TSavedGLState
.save ();
227 if (saved
) then raise Exception
.Create('cannot save into already saved OpenGL state');
228 glGetIntegerv(GL_MATRIX_MODE
, @glmatmode
);
229 glGetIntegerv(GL_TEXTURE_BINDING_2D
, @gltextbinding
);
230 glGetIntegerv(GL_VIEWPORT
, @glvport
[0]);
231 //glGetIntegerv(GL_CURRENT_PROGRAM, &oldprg);
232 //glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &oldfbr);
233 //glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfbw);
234 glMatrixMode(GL_PROJECTION
); glPushMatrix();
235 glMatrixMode(GL_MODELVIEW
); glPushMatrix();
236 glMatrixMode(GL_TEXTURE
); glPushMatrix();
237 glMatrixMode(GL_COLOR
); glPushMatrix();
238 glPushAttrib({GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_CURRENT_BIT}GL_ALL_ATTRIB_BITS
); // let's play safe
242 procedure TSavedGLState
.restore ();
244 if (not saved
) then raise Exception
.Create('cannot restore unsaved OpenGL state');
245 glPopAttrib({GL_ENABLE_BIT});
246 glMatrixMode(GL_PROJECTION
); glPopMatrix();
247 glMatrixMode(GL_MODELVIEW
); glPopMatrix();
248 glMatrixMode(GL_TEXTURE
); glPopMatrix();
249 glMatrixMode(GL_COLOR
); glPopMatrix();
250 glMatrixMode(glmatmode
);
251 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, oldfbr);
252 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, oldfbw);
253 glBindTexture(GL_TEXTURE_2D
, gltextbinding
);
254 //if (glHasFunc!"glUseProgram") glUseProgram(oldprg);
255 glViewport(glvport
[0], glvport
[1], glvport
[2], glvport
[3]);
261 curCtx
: TGxContext
= nil;
262 savedGLState
: TSavedGLState
;
265 // ////////////////////////////////////////////////////////////////////////// //
266 // set active context; `ctx` can be `nil`
267 procedure gxSetContextInternal (ctx
: TGxContext
; ascale
: Single; domatrix
: Boolean);
269 mt
: packed array [0..15] of GLfloat
;
271 if (savedGLState
.saved
) then savedGLState
.restore();
273 if (curCtx
<> nil) then
275 curCtx
.onDeactivate();
276 curCtx
.mActive
:= false;
286 oglSetup2D(fuiScrWdt
, fuiScrHgt
);
287 glScalef(ascale
, ascale
, 1.0);
288 ctx
.mScaled
:= (ascale
<> 1.0);
289 ctx
.mScale
:= ascale
;
293 // assume uniform scale
294 glGetFloatv(GL_MODELVIEW_MATRIX
, @mt
[0]);
295 ctx
.mScaled
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
304 procedure gxSetContext (ctx
: TGxContext
; ascale
: Single=1.0); begin gxSetContextInternal(ctx
, ascale
, true); end;
305 procedure gxSetContextNoMatrix (ctx
: TGxContext
); begin gxSetContextInternal(ctx
, 1, false); end;
308 // ////////////////////////////////////////////////////////////////////////// //
310 TScissorSave
= record
313 scxywh
: packed array[0..3] of GLint
;
318 procedure save (enableScissoring
: Boolean);
319 procedure restore ();
321 // set new scissor rect, bounded by the saved scissor rect
322 procedure combineRect (x
, y
, w
, h
: Integer);
326 procedure TScissorSave
.save (enableScissoring
: Boolean);
328 wassc
:= (glIsEnabled(GL_SCISSOR_TEST
) <> 0);
329 if wassc
then glGetIntegerv(GL_SCISSOR_BOX
, @scxywh
[0]) else glGetIntegerv(GL_VIEWPORT
, @scxywh
[0]);
330 //conwritefln('(%d,%d)-(%d,%d)', [scxywh[0], scxywh[1], scxywh[2], scxywh[3]]);
331 if enableScissoring
and (not wassc
) then glEnable(GL_SCISSOR_TEST
);
334 procedure TScissorSave
.restore ();
336 glScissor(scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]);
337 if wassc
then glEnable(GL_SCISSOR_TEST
) else glDisable(GL_SCISSOR_TEST
);
340 procedure TScissorSave
.combineRect (x
, y
, w
, h
: Integer);
341 //var ox, oy, ow, oh: Integer;
343 if (w
< 1) or (h
< 1) then begin glScissor(0, 0, 0, 0); exit
; end;
344 y
:= fuiScrHgt
-(y
+h
);
345 //ox := x; oy := y; ow := w; oh := h;
346 if not intersectRect(x
, y
, w
, h
, scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]) then
348 //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, ')');
349 //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, '>');
350 glScissor(0, 0, 0, 0);
354 glScissor(x
, y
, w
, h
);
359 // ////////////////////////////////////////////////////////////////////////// //
361 TGxBmpFont
= class(TGxFont
)
363 mTexId
: GLuint
; // OpenGL texture id
364 mWidth
: Integer; // <=0: proportional
367 mFreeFontWdt
: Boolean;
368 mFreeFontBmp
: Boolean;
371 procedure oglCreateTexture ();
372 procedure oglDestroyTexture ();
374 procedure initDrawText ();
375 procedure doneDrawText ();
376 function drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
377 function drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
378 function drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer; // return width (not including last empty pixel)
381 constructor Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
382 destructor Destroy (); override;
384 function charWidth (const ch
: AnsiChar): Integer; override;
385 function textWidth (const s
: AnsiString): Integer; override;
389 constructor TGxBmpFont
.Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
391 sign
: packed array [0..7] of AnsiChar;
392 enc
: packed array [0..16] of AnsiChar;
394 wdt
, hgt
, elen
: Integer;
399 mFreeFontBmp
:= true;
400 mFreeFontWdt
:= true;
404 st
.ReadBuffer(sign
[0], 8);
405 if (sign
<> 'FUIFONT0') then raise Exception
.Create('FlexUI: invalid font file signature');
406 // encoding length and width
408 wdt
:= (b
and $0f)+1; // 16 is not supported
409 if (wdt
= 16) then raise Exception
.Create('FlexUI: 16-wdt fonts aren''t supported yet');
410 elen
:= ((b
shr 4) and $0f);
411 if (elen
= 0) then raise Exception
.CreateFmt('FlexUI: invalid font encoding length: %d', [elen
]);
415 if (hgt
< 2) then raise Exception
.CreateFmt('FlexUI: invalid font height: %d', [hgt
]);
417 st
.ReadBuffer(enc
[0], elen
);
418 // check for 'cp1251' here (it can also be 'koi8')
419 if (wdt
<= 8) then fntbwdt
:= 1 else fntbwdt
:= 2;
420 // shift and width table (hi nibble: left shift for proportional print; lo nibble: shifted character width for proportional print)
421 GetMem(mFontWdt
, 256);
422 st
.ReadBuffer(mFontWdt
^, 256);
424 GetMem(mFontBmp
, (hgt
*fntbwdt
)*256);
425 st
.ReadBuffer(mFontBmp
^, (hgt
*fntbwdt
)*256);
428 mBaseLine
:= hgt
-1; //FIXME
429 if (proportional
) then
432 for ch
:= 0 to 255 do
434 for dy
:= 0 to hgt
-1 do
436 if (fntbwdt
= 1) then
438 mFontBmp
[ch
*hgt
+dy
] := mFontBmp
[ch
*hgt
+dy
] shl (mFontWdt
[ch
] shr 4);
442 wrd
:= mFontBmp
[ch
*(hgt
*2)+(dy
*2)]+256*mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1];
443 wrd
:= wrd
shl (mFontWdt
[ch
] shr 4);
444 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+0] := (wrd
and $ff);
445 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1] := ((wrd
shr 16) and $ff);
452 FillChar(mFontWdt
^, 256, wdt
);
457 destructor TGxBmpFont
.Destroy ();
459 if (mFreeFontBmp
) and (mFontBmp
<> nil) then FreeMem(mFontBmp
);
460 if (mFreeFontWdt
) and (mFontWdt
<> nil) then FreeMem(mFontWdt
);
467 mFreeFontWdt
:= false;
468 mFreeFontBmp
:= false;
474 procedure TGxBmpFont
.oglCreateTexture ();
482 x
, y
, dx
, dy
: Integer;
484 GetMem(tex
, TxWidth
*TxHeight
*4);
485 FillChar(tex
^, TxWidth
*TxHeight
*4, 0);
487 for cc
:= 0 to 255 do
491 for dy
:= 0 to mHeight
-1 do
493 if (mWidth
<= 8) then b
:= mFontBmp
[cc
*mHeight
+dy
] else b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+1];
494 //if prop then b := b shl (fontwdt[cc] shr 4);
495 tpp
:= tex
+((y
+dy
)*(TxWidth
*4))+x
*4;
498 if ((b
and $80) <> 0) then
500 tpp
^ := 255; Inc(tpp
);
501 tpp
^ := 255; Inc(tpp
);
502 tpp
^ := 255; Inc(tpp
);
503 tpp
^ := 255; Inc(tpp
);
512 b
:= (b
and $7f) shl 1;
516 b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+0];
519 if ((b
and $80) <> 0) then
521 tpp
^ := 255; Inc(tpp
);
522 tpp
^ := 255; Inc(tpp
);
523 tpp
^ := 255; Inc(tpp
);
524 tpp
^ := 255; Inc(tpp
);
533 b
:= (b
and $7f) shl 1;
539 glGenTextures(1, @mTexId
);
540 if (mTexId
= 0) then raise Exception
.Create('can''t create FlexUI font texture');
542 glBindTexture(GL_TEXTURE_2D
, mTexId
);
543 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
544 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
545 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
546 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
548 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, TxWidth
, TxHeight
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex
);
551 glBindTexture(GL_TEXTURE_2D
, 0);
556 procedure TGxBmpFont
.oglDestroyTexture ();
558 if (mTexId
<> 0) then
560 glDeleteTextures(1, @mTexId
);
566 function TGxBmpFont
.charWidth (const ch
: AnsiChar): Integer;
568 result
:= (mFontWdt
[Byte(ch
)] and $0f);
572 function TGxBmpFont
.textWidth (const s
: AnsiString): Integer;
576 if (Length(s
) > 0) then
579 for ch
in s
do result
+= (mFontWdt
[Byte(ch
)] and $0f)+1;
588 procedure TGxBmpFont
.initDrawText ();
590 glEnable(GL_ALPHA_TEST
);
591 glAlphaFunc(GL_NOTEQUAL
, 0.0);
592 glEnable(GL_TEXTURE_2D
);
593 glBindTexture(GL_TEXTURE_2D
, mTexId
);
597 procedure TGxBmpFont
.doneDrawText ();
599 glDisable(GL_ALPHA_TEST
);
600 glDisable(GL_TEXTURE_2D
);
601 glBindTexture(GL_TEXTURE_2D
, 0);
605 function TGxBmpFont
.drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer;
609 tx
:= (Integer(ch
) mod 16)*16;
610 ty
:= (Integer(ch
) div 16)*16;
612 glTexCoord2f((tx
+0)/256.0, (ty
+0)/256.0); glVertex2i(x
+0, y
+0); // top-left
613 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+0)/256.0); glVertex2i(x
+mWidth
, y
+0); // top-right
614 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+mWidth
, y
+mHeight
); // bottom-right
615 glTexCoord2f((tx
+0)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+0, y
+mHeight
); // bottom-left
617 result
:= (mFontWdt
[Byte(ch
)] and $0f);
621 function TGxBmpFont
.drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer;
624 result
:= drawCharInterim(x
, y
, ch
);
629 function TGxBmpFont
.drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer;
634 if (Length(s
) = 0) then begin result
:= 0; exit
; end;
639 wdt
:= drawCharInterim(x
, y
, ch
)+1;
647 // ////////////////////////////////////////////////////////////////////////// //
649 fontList
: array of TGxBmpFont
= nil;
650 defaultFontName
: AnsiString = 'win14';
653 function strEquCI (const s0
, s1
: AnsiString): Boolean;
658 result
:= (Length(s0
) = Length(s1
));
661 for f
:= 1 to Length(s0
) do
664 if (c0
>= 'a') and (c0
<= 'z') then Dec(c0
, 32); // poor man's `toupper()`
666 if (c1
>= 'a') and (c1
<= 'z') then Dec(c1
, 32); // poor man's `toupper()`
667 if (c0
<> c1
) then begin result
:= false; exit
; end;
673 function getFontByName (const aname
: AnsiString): TGxBmpFont
;
678 if (Length(fontList
) = 0) then raise Exception
.Create('font subsystem not initialized');
679 if (Length(aname
) = 0) or (strEquCI(aname
, 'default')) then fname
:= defaultFontName
else fname
:= aname
;
680 for f
:= 0 to High(fontList
) do
682 result
:= fontList
[f
];
683 if (result
= nil) then continue
;
684 if (strEquCI(result
.name
, fname
)) then exit
;
686 if (fontList
[0] = nil) then raise Exception
.Create('font subsystem not properly initialized');
687 result
:= fontList
[0];
692 procedure deleteFonts ();
696 for f := 0 to High(fontList) do freeAndNil(fontList[f]);
702 procedure fuiGfxLoadFont (const fontname
: AnsiString; const fontFile
: AnsiString; proportional
: Boolean=false);
706 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font '''+fontFile
+'''');
707 st
:= fuiOpenFile(fontFile
);
708 if (st
= nil) then raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
710 fuiGfxLoadFont(fontname
, st
, proportional
);
711 except on e
: Exception
do
713 writeln('FlexUI font loadin error: ', e
.message);
715 raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
724 procedure fuiGfxLoadFont (const fontname
: AnsiString; st
: TStream
; proportional
: Boolean=false);
726 fnt
: TGxBmpFont
= nil;
729 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font');
730 fnt
:= TGxBmpFont
.Create(fontname
, st
, proportional
);
732 for f
:= 0 to High(fontList
) do
734 if (strEquCI(fontList
[f
].name
, fontname
)) then
736 if (fontList
[f
].mTexId
<> 0) then raise Exception
.Create('FlexUI: cannot reload generated font named '''+fontname
+'''');
737 FreeAndNil(fontList
[f
]);
742 SetLength(fontList
, Length(fontList
)+1);
743 fontList
[High(fontList
)] := fnt
;
751 procedure oglInitFonts ();
755 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglCreateTexture();
759 procedure oglDeinitFonts ();
763 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglDestroyTexture();
767 // ////////////////////////////////////////////////////////////////////////// //
768 procedure oglSetup2DState ();
771 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
772 glDisable(GL_LINE_SMOOTH
);
773 glDisable(GL_POLYGON_SMOOTH
);
774 glDisable(GL_POINT_SMOOTH
);
775 glDisable(GL_DEPTH_TEST
);
776 glDisable(GL_TEXTURE_2D
);
777 glDisable(GL_LIGHTING
);
778 glDisable(GL_DITHER
);
779 glDisable(GL_STENCIL_TEST
);
780 glDisable(GL_SCISSOR_TEST
);
781 glDisable(GL_CULL_FACE
);
782 glDisable(GL_ALPHA_TEST
);
784 glClearColor(0, 0, 0, 0);
785 glColor4f(1, 1, 1, 1);
789 procedure oglSetup2D (winWidth
, winHeight
: Integer; upsideDown
: Boolean=false);
791 glViewport(0, 0, winWidth
, winHeight
);
795 glMatrixMode(GL_TEXTURE
);
798 glMatrixMode(GL_COLOR
);
801 glMatrixMode(GL_PROJECTION
);
805 glOrtho(0, winWidth
, 0, winHeight
, -1, 1); // set origin to bottom left
809 glOrtho(0, winWidth
, winHeight
, 0, -1, 1); // set origin to top left
812 glMatrixMode(GL_MODELVIEW
);
817 // ////////////////////////////////////////////////////////////////////////// //
818 {$INCLUDE fui_gfx_gl_cursor.inc}
820 procedure oglDrawCursor (); begin oglDrawCursorAt(fuiMouseX
, fuiMouseY
); end;
823 // ////////////////////////////////////////////////////////////////////////// //
824 constructor TGxContext
.Create ();
827 mColor
:= TGxRGBA
.Create(255, 255, 255);
828 mFont
:= getFontByName('default');
831 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
832 mClipOfs
:= TGxOfs
.Create(0, 0);
836 destructor TGxContext
.Destroy ();
838 if (mActive
) then gxSetContext(nil);
843 function TGxContext
.getFont (): AnsiString;
845 result
:= mFont
.name
;
848 procedure TGxContext
.setFont (const aname
: AnsiString);
850 mFont
:= getFontByName(aname
);
854 procedure TGxContext
.onActivate ();
856 setupGLColor(mColor
);
860 procedure TGxContext
.onDeactivate ();
865 procedure TGxContext
.setColor (const clr
: TGxRGBA
);
868 if (mActive
) then setupGLColor(mColor
);
872 procedure TGxContext
.realizeClip ();
874 sx
, sy
, sw
, sh
: Integer;
876 if (not mActive
) then exit
; // just in case
877 if (mClipRect
.w
<= 0) or (mClipRect
.h
<= 0) then
879 glEnable(GL_SCISSOR_TEST
);
880 glScissor(0, 0, 0, 0);
886 sx
:= trunc(mClipRect
.x
*mScale
);
887 sy
:= trunc(mClipRect
.y
*mScale
);
888 sw
:= trunc(mClipRect
.w
*mScale
);
889 sh
:= trunc(mClipRect
.h
*mScale
);
898 if (not intersectRect(sx
, sy
, sw
, sh
, 0, 0, fuiScrWdt
, fuiScrHgt
)) then
900 glEnable(GL_SCISSOR_TEST
);
901 glScissor(0, 0, 0, 0);
903 else if (sx
= 0) and (sy
= 0) and (sw
= fuiScrWdt
) and (sh
= fuiScrHgt
) then
905 glDisable(GL_SCISSOR_TEST
);
909 glEnable(GL_SCISSOR_TEST
);
910 sy
:= fuiScrHgt
-(sy
+sh
);
911 glScissor(sx
, sy
, sw
, sh
);
917 procedure TGxContext
.resetClip ();
919 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
920 if (mActive
) then realizeClip();
924 procedure TGxContext
.setClipOfs (const aofs
: TGxOfs
);
930 procedure TGxContext
.setClipRect (const aclip
: TGxRect
);
933 if (mActive
) then realizeClip();
937 function TGxContext
.setOffset (constref aofs
: TGxOfs
): TGxOfs
;
944 function TGxContext
.setClip (constref aclip
: TGxRect
): TGxRect
;
948 if (mActive
) then realizeClip();
952 function TGxContext
.combineClip (constref aclip
: TGxRect
): TGxRect
;
955 mClipRect
.intersect(aclip
);
956 if (mActive
) then realizeClip();
960 procedure TGxContext
.line (x1
, y1
, x2
, y2
: Integer);
962 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
964 if (not mScaled
) then
968 glVertex2f(x1
+0.375, y1
+0.375);
969 glVertex2f(x2
+0.375, y2
+0.375);
972 if (x1
<> x2
) or (y1
<> y2
) then
976 glVertex2f(x2
+0.375, y2
+0.375);
988 glVertex2i(x2
+1, y2
+1);
994 procedure TGxContext
.hline (x
, y
, len
: Integer);
996 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
997 if (len
< 1) then exit
;
998 if (not mScaled
) then
1002 glVertex2f(x
+0.375, y
+0.375);
1003 glVertex2f(x
+len
+0.375, y
+0.375);
1006 else if (mScale
> 1.0) then
1010 glVertex2i(x
+len
, y
);
1011 glVertex2i(x
+len
, y
+1);
1019 while (len
> 0) do begin glVertex2i(x
, y
); Inc(x
); Dec(len
); end;
1025 procedure TGxContext
.vline (x
, y
, len
: Integer);
1027 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1028 if (len
< 1) then exit
;
1029 if (not mScaled
) then
1033 glVertex2f(x
+0.375, y
+0.375);
1034 glVertex2f(x
+0.375, y
+len
+0.375);
1037 else if (mScale
> 1.0) then
1041 glVertex2i(x
, y
+len
);
1042 glVertex2i(x
+1, y
+len
);
1050 while (len
> 0) do begin glVertex2i(x
, y
); Inc(y
); Dec(len
); end;
1056 procedure TGxContext
.rect (x
, y
, w
, h
: Integer);
1058 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1059 if (w
< 0) or (h
< 0) then exit
;
1060 if (w
= 1) and (h
= 1) then
1064 if mScaled
then glVertex2i(x
, y
) else glVertex2f(x
+0.375, y
+0.375);
1069 if (not mScaled
) then
1073 glVertex2i(x
, y
); glVertex2i(x
+w
, y
); // top
1074 glVertex2i(x
, y
+h
-1); glVertex2i(x
+w
, y
+h
-1); // bottom
1075 glVertex2f(x
+0.375, y
+1); glVertex2f(x
+0.375, y
+h
-1); // left
1076 glVertex2f(x
+w
-1+0.375, y
+1); glVertex2f(x
+w
-1+0.375, y
+h
-1); // right
1084 vline(x
+w
-1, y
+1, h
-2);
1090 procedure TGxContext
.fillRect (x
, y
, w
, h
: Integer);
1092 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1093 if (w
< 0) or (h
< 0) then exit
;
1097 glVertex2f(x
+w
, y
+h
);
1103 procedure TGxContext
.darkenRect (x
, y
, w
, h
: Integer; a
: Integer);
1105 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (a
>= 255) then exit
;
1106 if (w
< 0) or (h
< 0) then exit
;
1107 if (a
< 0) then a
:= 0;
1109 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1110 glColor4f(0.0, 0.0, 0.0, a
/255.0);
1114 glVertex2i(x
+w
, y
+h
);
1117 setupGLColor(mColor
);
1121 function TGxContext
.charWidth (const ch
: AnsiChar): Integer;
1123 result
:= mFont
.charWidth(ch
);
1126 function TGxContext
.charHeight (const ch
: AnsiChar): Integer;
1128 result
:= mFont
.height
;
1132 function TGxContext
.textWidth (const s
: AnsiString): Integer;
1134 result
:= mFont
.textWidth(s
);
1137 function TGxContext
.textHeight (const s
: AnsiString): Integer;
1139 result
:= mFont
.height
;
1143 function TGxContext
.drawChar (x
, y
: Integer; const ch
: AnsiChar): Integer; // returns char width
1145 result
:= mFont
.charWidth(ch
);
1146 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1147 TGxBmpFont(mFont
).drawCharInternal(x
, y
, ch
);
1150 function TGxContext
.drawText (x
, y
: Integer; const s
: AnsiString): Integer; // returns text width
1152 result
:= mFont
.textWidth(s
);
1153 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) or (Length(s
) = 0) then exit
;
1154 TGxBmpFont(mFont
).drawTextInternal(x
, y
, s
);
1158 function TGxContext
.iconMarkWidth (ic
: TMarkIcon
): Integer;
1160 {$IFDEF FUI_TEXT_ICONS}
1162 TMarkIcon
.Checkbox
: result
:= textWidth('[x]');
1163 TMarkIcon
.Radiobox
: result
:= textWidth('(*)');
1164 else result
:= textWidth('[x]');
1171 function TGxContext
.iconMarkHeight (ic
: TMarkIcon
): Integer;
1173 {$IFDEF FUI_TEXT_ICONS}
1175 TMarkIcon
.Checkbox
: result
:= textHeight('[x]');
1176 TMarkIcon
.Radiobox
: result
:= textHeight('(*)');
1177 else result
:= textHeight('[x]');
1184 procedure TGxContext
.drawIconMark (ic
: TMarkIcon
; x
, y
: Integer; marked
: Boolean);
1186 {$IFDEF FUI_TEXT_ICONS}
1192 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1193 {$IFDEF FUI_TEXT_ICONS}
1195 TMarkIcon
.Checkbox
: xstr
:= '[x]';
1196 TMarkIcon
.Radiobox
: xstr
:= '(*)';
1201 drawText(x
, y
, xstr
);
1205 drawChar(x
, y
, xstr
[1]);
1206 drawChar(x
+textWidth(xstr
)-charWidth(xstr
[3]), y
, xstr
[3]);
1209 if (ic
= TMarkIcon
.Checkbox
) then
1221 vline(x
+10, y
+1, 5);
1227 if (not marked
) then exit
;
1233 vline(x
+3+f
, y
+1+f
, 1);
1234 vline(x
+7-f
, y
+1+f
, 1);
1250 function TGxContext
.iconWinWidth (ic
: TWinIcon
): Integer;
1252 {$IFDEF FUI_TEXT_ICONS}
1254 TWinIcon
.Close
: result
:= nmax(textWidth('[x]'), textWidth('[#]'));
1255 else result
:= nmax(textWidth('[x]'), textWidth('[#]'));
1262 function TGxContext
.iconWinHeight (ic
: TWinIcon
): Integer;
1264 {$IFDEF FUI_TEXT_ICONS}
1266 TWinIcon
.Close
: result
:= nmax(textHeight('[x]'), textHeight('[#]'));
1267 else result
:= nmax(textHeight('[x]'), textHeight('[#]'));
1274 procedure TGxContext
.drawIconWin (ic
: TWinIcon
; x
, y
: Integer; pressed
: Boolean);
1276 {$IFDEF FUI_TEXT_ICONS}
1283 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1284 {$IFDEF FUI_TEXT_ICONS}
1286 TWinIcon
.Close
: if (pressed
) then xstr
:= '[#]' else xstr
:= '[x]';
1289 wdt
:= nmax(textWidth('[x]'), textWidth('[#]'));
1290 drawChar(x
, y
, xstr
[1]);
1291 drawChar(x
+wdt
-charWidth(xstr
[3]), y
, xstr
[3]);
1292 drawChar(x
+((wdt
-charWidth(xstr
[2])) div 2), y
, xstr
[2]);
1294 if pressed
then rect(x
, y
, 9, 8);
1297 vline(x
+1+f
, y
+f
, 1);
1298 vline(x
+1+6-f
, y
+f
, 1);
1304 procedure TGxContext
.glSetScale (ascale
: Single);
1306 if (ascale
< 0.01) then ascale
:= 0.01;
1308 glScalef(ascale
, ascale
, 1.0);
1310 mScaled
:= (ascale
<> 1.0);
1313 procedure TGxContext
.glSetTrans (ax
, ay
: Single);
1316 glScalef(mScale
, mScale
, 1.0);
1317 glTranslatef(ax
, ay
, 0);
1321 procedure TGxContext
.glSetScaleTrans (ascale
, ax
, ay
: Single);
1324 glTranslatef(ax
, ay
, 0);
1328 // vertical scroll bar
1329 procedure TGxContext
.drawVSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
1333 if (wdt
< 1) or (hgt
< 1) then exit
;
1334 filled
:= sbarFilled(hgt
, cur
, min
, max
);
1336 fillRect(x
, y
, wdt
, filled
);
1338 fillRect(x
, y
+filled
, wdt
, hgt
-filled
);
1342 // horizontal scrollbar
1343 procedure TGxContext
.drawHSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
1347 if (wdt
< 1) or (hgt
< 1) then exit
;
1348 filled
:= sbarFilled(wdt
, cur
, min
, max
);
1350 fillRect(x
, y
, filled
, hgt
);
1352 fillRect(x
+filled
, y
, wdt
-filled
, hgt
);
1356 class function TGxContext
.sbarFilled (wh
: Integer; cur
, min
, max
: Integer): Integer;
1358 if (wh
< 1) then result
:= 0
1359 else if (min
> max
) then result
:= 0
1360 else if (min
= max
) then result
:= wh
1363 if (cur
< min
) then cur
:= min
else if (cur
> max
) then cur
:= max
;
1364 result
:= wh
*(cur
-min
) div (max
-min
);
1369 class function TGxContext
.sbarPos (cxy
: Integer; xy
, wh
: Integer; min
, max
: Integer): Integer;
1371 if (wh
< 1) then begin result
:= 0; exit
; end;
1372 if (min
> max
) then begin result
:= 0; exit
; end;
1373 if (min
= max
) then begin result
:= max
; exit
; end;
1374 if (cxy
< xy
) then begin result
:= min
; exit
; end;
1375 if (cxy
>= xy
+wh
) then begin result
:= max
; exit
; end;
1376 result
:= min
+((max
-min
)*(cxy
-xy
) div wh
);
1377 assert((result
>= min
) and (result
<= max
));
1383 // ////////////////////////////////////////////////////////////////////////// //
1385 procedure oglRestoreMode (doClear: Boolean);
1387 oglSetup2D(fuiScrWdt, fuiScrHgt);
1388 glScissor(0, 0, fuiScrWdt, fuiScrHgt);
1390 glBindTexture(GL_TEXTURE_2D, 0);
1391 glDisable(GL_BLEND);
1392 glDisable(GL_TEXTURE_2D);
1393 glDisable(GL_STENCIL_TEST);
1394 glDisable(GL_SCISSOR_TEST);
1395 glDisable(GL_LIGHTING);
1396 glDisable(GL_DEPTH_TEST);
1397 glDisable(GL_CULL_FACE);
1398 glDisable(GL_LINE_SMOOTH);
1399 glDisable(GL_POINT_SMOOTH);
1402 glColor4f(1, 1, 1, 1);
1406 glClearColor(0, 0, 0, 0);
1407 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_ACCUM_BUFFER_BIT or GL_STENCIL_BUFFER_BIT);
1411 glMatrixMode(GL_MODELVIEW);
1413 //glScalef(4, 4, 1);
1418 //procedure onWinFocus (); begin uiFocus(); end;
1419 //procedure onWinBlur (); begin fuiResetKMState(true); uiBlur(); end;
1421 //procedure onPreRender (); begin oglRestoreMode(gGfxDoClear); end;
1422 procedure onPostRender (); begin oglDrawCursor(); end;
1424 procedure onInit ();
1426 //oglSetup2D(fuiScrWdt, fuiScrHgt);
1427 createCursorTexture();
1431 procedure onDeinit ();
1433 fuiResetKMState(false);
1434 if (curtexid
<> 0) then glDeleteTextures(1, @curtexid
);
1444 // ////////////////////////////////////////////////////////////////////////// //
1446 savedGLState
:= TSavedGLState
.Create(false);
1448 //winFocusCB := onWinFocus;
1449 //winBlurCB := onWinBlur;
1450 //prerenderFrameCB := onPreRender;
1451 postrenderFrameCB
:= onPostRender
;
1452 oglInitCB
:= onInit
;
1453 oglDeinitCB
:= onDeinit
;