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}
24 {$INCLUDE ../nogl/noGLuses.inc}
28 fui_common
, fui_events
;
31 // ////////////////////////////////////////////////////////////////////////// //
40 function charWidth (const ch
: AnsiChar): Integer; virtual; abstract;
41 function textWidth (const s
: AnsiString): Integer; virtual; abstract;
44 property name
: AnsiString read mName
;
45 property height
: Integer read mHeight
;
46 property baseLine
: Integer read mBaseLine
;
66 // for active contexts
73 function getFont (): AnsiString;
74 procedure setFont (const aname
: AnsiString);
76 procedure onActivate ();
77 procedure onDeactivate ();
79 procedure setColor (const clr
: TGxRGBA
);
81 procedure realizeClip (); // setup scissoring
83 procedure setClipOfs (const aofs
: TGxOfs
);
84 procedure setClipRect (const aclip
: TGxRect
);
87 constructor Create ();
88 destructor Destroy (); override;
90 procedure line (x1
, y1
, x2
, y2
: Integer);
91 procedure hline (x
, y
, len
: Integer);
92 procedure vline (x
, y
, len
: Integer);
93 procedure rect (x
, y
, w
, h
: Integer);
94 procedure fillRect (x
, y
, w
, h
: Integer);
95 procedure darkenRect (x
, y
, w
, h
: Integer; a
: Integer);
97 function charWidth (const ch
: AnsiChar): Integer;
98 function charHeight (const ch
: AnsiChar): Integer;
99 function textWidth (const s
: AnsiString): Integer;
100 function textHeight (const s
: AnsiString): Integer;
101 function drawChar (x
, y
: Integer; const ch
: AnsiChar): Integer; // returns char width
102 function drawText (x
, y
: Integer; const s
: AnsiString): Integer; // returns text width
104 function iconMarkWidth (ic
: TMarkIcon
): Integer;
105 function iconMarkHeight (ic
: TMarkIcon
): Integer;
106 procedure drawIconMark (ic
: TMarkIcon
; x
, y
: Integer; marked
: Boolean);
108 function iconWinWidth (ic
: TWinIcon
): Integer;
109 function iconWinHeight (ic
: TWinIcon
): Integer;
110 procedure drawIconWin (ic
: TWinIcon
; x
, y
: Integer; pressed
: Boolean);
112 procedure resetClip ();
114 function setOffset (constref aofs
: TGxOfs
): TGxOfs
; // returns previous offset
115 function setClip (constref aclip
: TGxRect
): TGxRect
; // returns previous clip
117 function combineClip (constref aclip
: TGxRect
): TGxRect
; // returns previous clip
119 // vertical scrollbar
120 procedure drawVSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
121 // horizontal scrollbar
122 procedure drawHSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
124 class function sbarFilled (wh
: Integer; cur
, min
, max
: Integer): Integer;
125 class function sbarPos (cxy
: Integer; xy
, wh
: Integer; min
, max
: Integer): Integer;
128 procedure glSetScale (ascale
: Single);
129 procedure glSetTrans (ax
, ay
: Single);
130 procedure glSetScaleTrans (ascale
, ax
, ay
: Single);
133 property active
: Boolean read mActive
;
134 property color
: TGxRGBA read mColor write setColor
;
135 property font
: AnsiString read getFont write setFont
;
136 property offset
: TGxOfs read mClipOfs write setClipOfs
;
137 property clip
: TGxRect read mClipRect write setClipRect
; // clipping is unaffected by offset
141 // set active context; `ctx` can be `nil`
142 procedure gxSetContext (ctx
: TGxContext
; ascale
: Single=1.0);
143 procedure gxSetContextNoMatrix (ctx
: TGxContext
);
146 // setup 2D OpenGL mode; will be called automatically in `glInit()`
147 procedure oglSetup2D (winWidth
, winHeight
: Integer; upsideDown
: Boolean=false);
148 procedure oglSetup2DState (); // don't modify viewports and matrices
150 procedure oglDrawCursor ();
151 procedure oglDrawCursorAt (msX
, msY
: Integer);
154 procedure fuiGfxLoadFont (const fontname
: AnsiString; const fontFile
: AnsiString; proportional
: Boolean=false);
155 procedure fuiGfxLoadFont (const fontname
: AnsiString; st
: TStream
; proportional
: Boolean=false);
158 // ////////////////////////////////////////////////////////////////////////// //
160 gGfxDoClear
: Boolean = true;
170 // ////////////////////////////////////////////////////////////////////////// //
171 // returns `false` if the color is transparent
172 // returns `false` if the color is transparent
173 function setupGLColor (constref clr
: TGxRGBA
): Boolean;
175 if (clr
.a
< 255) then
178 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
184 glColor4ub(clr
.r
, clr
.g
, clr
.b
, clr
.a
);
185 result
:= (clr
.a
<> 0);
188 function isScaled (): Boolean;
190 mt
: packed array [0..15] of GLfloat
;
192 glGetFloatv(GL_MODELVIEW_MATRIX
, @mt
[0]);
193 result
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
197 // ////////////////////////////////////////////////////////////////////////// //
198 //TODO: OpenGL framebuffers and shaders state
200 TSavedGLState
= record
203 gltextbinding
: GLint
;
205 //oldfbr, oldfbw: GLint;
206 glvport
: packed array [0..3] of GLint
;
210 constructor Create (dosave
: Boolean);
212 procedure restore ();
215 constructor TSavedGLState
.Create (dosave
: Boolean);
217 FillChar(self
, sizeof(self
), 0);
218 if (dosave
) then save();
221 procedure TSavedGLState
.save ();
223 if (saved
) then raise Exception
.Create('cannot save into already saved OpenGL state');
224 glGetIntegerv(GL_MATRIX_MODE
, @glmatmode
);
225 glGetIntegerv(GL_TEXTURE_BINDING_2D
, @gltextbinding
);
226 glGetIntegerv(GL_VIEWPORT
, @glvport
[0]);
227 //glGetIntegerv(GL_CURRENT_PROGRAM, &oldprg);
228 //glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &oldfbr);
229 //glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfbw);
230 glMatrixMode(GL_PROJECTION
); glPushMatrix();
231 glMatrixMode(GL_MODELVIEW
); glPushMatrix();
232 glMatrixMode(GL_TEXTURE
); glPushMatrix();
233 glMatrixMode(GL_COLOR
); glPushMatrix();
234 glPushAttrib({GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_CURRENT_BIT}GL_ALL_ATTRIB_BITS
); // let's play safe
238 procedure TSavedGLState
.restore ();
240 if (not saved
) then raise Exception
.Create('cannot restore unsaved OpenGL state');
241 glPopAttrib({GL_ENABLE_BIT});
242 glMatrixMode(GL_PROJECTION
); glPopMatrix();
243 glMatrixMode(GL_MODELVIEW
); glPopMatrix();
244 glMatrixMode(GL_TEXTURE
); glPopMatrix();
245 glMatrixMode(GL_COLOR
); glPopMatrix();
246 glMatrixMode(glmatmode
);
247 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, oldfbr);
248 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, oldfbw);
249 glBindTexture(GL_TEXTURE_2D
, gltextbinding
);
250 //if (glHasFunc!"glUseProgram") glUseProgram(oldprg);
251 glViewport(glvport
[0], glvport
[1], glvport
[2], glvport
[3]);
257 curCtx
: TGxContext
= nil;
258 savedGLState
: TSavedGLState
;
261 // ////////////////////////////////////////////////////////////////////////// //
262 // set active context; `ctx` can be `nil`
263 procedure gxSetContextInternal (ctx
: TGxContext
; ascale
: Single; domatrix
: Boolean);
265 mt
: packed array [0..15] of GLfloat
;
267 if (savedGLState
.saved
) then savedGLState
.restore();
269 if (curCtx
<> nil) then
271 curCtx
.onDeactivate();
272 curCtx
.mActive
:= false;
282 oglSetup2D(fuiScrWdt
, fuiScrHgt
);
283 glScalef(ascale
, ascale
, 1.0);
284 ctx
.mScaled
:= (ascale
<> 1.0);
285 ctx
.mScale
:= ascale
;
289 // assume uniform scale
290 glGetFloatv(GL_MODELVIEW_MATRIX
, @mt
[0]);
291 ctx
.mScaled
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
300 procedure gxSetContext (ctx
: TGxContext
; ascale
: Single=1.0); begin gxSetContextInternal(ctx
, ascale
, true); end;
301 procedure gxSetContextNoMatrix (ctx
: TGxContext
); begin gxSetContextInternal(ctx
, 1, false); end;
304 // ////////////////////////////////////////////////////////////////////////// //
306 TScissorSave
= record
309 scxywh
: packed array[0..3] of GLint
;
314 procedure save (enableScissoring
: Boolean);
315 procedure restore ();
317 // set new scissor rect, bounded by the saved scissor rect
318 procedure combineRect (x
, y
, w
, h
: Integer);
322 procedure TScissorSave
.save (enableScissoring
: Boolean);
324 wassc
:= (glIsEnabled(GL_SCISSOR_TEST
) <> 0);
325 if wassc
then glGetIntegerv(GL_SCISSOR_BOX
, @scxywh
[0]) else glGetIntegerv(GL_VIEWPORT
, @scxywh
[0]);
326 //conwritefln('(%d,%d)-(%d,%d)', [scxywh[0], scxywh[1], scxywh[2], scxywh[3]]);
327 if enableScissoring
and (not wassc
) then glEnable(GL_SCISSOR_TEST
);
330 procedure TScissorSave
.restore ();
332 glScissor(scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]);
333 if wassc
then glEnable(GL_SCISSOR_TEST
) else glDisable(GL_SCISSOR_TEST
);
336 procedure TScissorSave
.combineRect (x
, y
, w
, h
: Integer);
337 //var ox, oy, ow, oh: Integer;
339 if (w
< 1) or (h
< 1) then begin glScissor(0, 0, 0, 0); exit
; end;
340 y
:= fuiScrHgt
-(y
+h
);
341 //ox := x; oy := y; ow := w; oh := h;
342 if not intersectRect(x
, y
, w
, h
, scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]) then
344 //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, ')');
345 //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, '>');
346 glScissor(0, 0, 0, 0);
350 glScissor(x
, y
, w
, h
);
355 // ////////////////////////////////////////////////////////////////////////// //
357 TGxBmpFont
= class(TGxFont
)
359 mTexId
: GLuint
; // OpenGL texture id
360 mWidth
: Integer; // <=0: proportional
363 mFreeFontWdt
: Boolean;
364 mFreeFontBmp
: Boolean;
367 procedure oglCreateTexture ();
368 procedure oglDestroyTexture ();
370 procedure initDrawText ();
371 procedure doneDrawText ();
372 function drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
373 function drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
374 function drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer; // return width (not including last empty pixel)
377 constructor Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
378 destructor Destroy (); override;
380 function charWidth (const ch
: AnsiChar): Integer; override;
381 function textWidth (const s
: AnsiString): Integer; override;
385 constructor TGxBmpFont
.Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
387 sign
: packed array [0..7] of AnsiChar;
388 enc
: packed array [0..16] of AnsiChar;
390 wdt
, hgt
, elen
: Integer;
395 mFreeFontBmp
:= true;
396 mFreeFontWdt
:= true;
400 st
.ReadBuffer(sign
[0], 8);
401 if (sign
<> 'FUIFONT0') then raise Exception
.Create('FlexUI: invalid font file signature');
402 // encoding length and width
404 wdt
:= (b
and $0f)+1; // 16 is not supported
405 if (wdt
= 16) then raise Exception
.Create('FlexUI: 16-wdt fonts aren''t supported yet');
406 elen
:= ((b
shr 4) and $0f);
407 if (elen
= 0) then raise Exception
.CreateFmt('FlexUI: invalid font encoding length: %d', [elen
]);
411 if (hgt
< 2) then raise Exception
.CreateFmt('FlexUI: invalid font height: %d', [hgt
]);
413 st
.ReadBuffer(enc
[0], elen
);
414 // check for 'cp1251' here (it can also be 'koi8')
415 if (wdt
<= 8) then fntbwdt
:= 1 else fntbwdt
:= 2;
416 // shift and width table (hi nibble: left shift for proportional print; lo nibble: shifted character width for proportional print)
417 GetMem(mFontWdt
, 256);
418 st
.ReadBuffer(mFontWdt
^, 256);
420 GetMem(mFontBmp
, (hgt
*fntbwdt
)*256);
421 st
.ReadBuffer(mFontBmp
^, (hgt
*fntbwdt
)*256);
424 mBaseLine
:= hgt
-1; //FIXME
425 if (proportional
) then
428 for ch
:= 0 to 255 do
430 for dy
:= 0 to hgt
-1 do
432 if (fntbwdt
= 1) then
434 mFontBmp
[ch
*hgt
+dy
] := mFontBmp
[ch
*hgt
+dy
] shl (mFontWdt
[ch
] shr 4);
438 wrd
:= mFontBmp
[ch
*(hgt
*2)+(dy
*2)]+256*mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1];
439 wrd
:= wrd
shl (mFontWdt
[ch
] shr 4);
440 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+0] := (wrd
and $ff);
441 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1] := ((wrd
shr 16) and $ff);
448 FillChar(mFontWdt
^, 256, wdt
);
453 destructor TGxBmpFont
.Destroy ();
455 if (mFreeFontBmp
) and (mFontBmp
<> nil) then FreeMem(mFontBmp
);
456 if (mFreeFontWdt
) and (mFontWdt
<> nil) then FreeMem(mFontWdt
);
463 mFreeFontWdt
:= false;
464 mFreeFontBmp
:= false;
470 procedure TGxBmpFont
.oglCreateTexture ();
478 x
, y
, dx
, dy
: Integer;
480 GetMem(tex
, TxWidth
*TxHeight
*4);
481 FillChar(tex
^, TxWidth
*TxHeight
*4, 0);
483 for cc
:= 0 to 255 do
487 for dy
:= 0 to mHeight
-1 do
489 if (mWidth
<= 8) then b
:= mFontBmp
[cc
*mHeight
+dy
] else b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+1];
490 //if prop then b := b shl (fontwdt[cc] shr 4);
491 tpp
:= tex
+((y
+dy
)*(TxWidth
*4))+x
*4;
494 if ((b
and $80) <> 0) then
496 tpp
^ := 255; Inc(tpp
);
497 tpp
^ := 255; Inc(tpp
);
498 tpp
^ := 255; Inc(tpp
);
499 tpp
^ := 255; Inc(tpp
);
508 b
:= (b
and $7f) shl 1;
512 b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+0];
515 if ((b
and $80) <> 0) then
517 tpp
^ := 255; Inc(tpp
);
518 tpp
^ := 255; Inc(tpp
);
519 tpp
^ := 255; Inc(tpp
);
520 tpp
^ := 255; Inc(tpp
);
529 b
:= (b
and $7f) shl 1;
535 glGenTextures(1, @mTexId
);
536 if (mTexId
= 0) then raise Exception
.Create('can''t create FlexUI font texture');
538 glBindTexture(GL_TEXTURE_2D
, mTexId
);
539 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
540 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
541 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
542 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
544 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, TxWidth
, TxHeight
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex
);
547 glBindTexture(GL_TEXTURE_2D
, 0);
552 procedure TGxBmpFont
.oglDestroyTexture ();
554 if (mTexId
<> 0) then
556 glDeleteTextures(1, @mTexId
);
562 function TGxBmpFont
.charWidth (const ch
: AnsiChar): Integer;
564 result
:= (mFontWdt
[Byte(ch
)] and $0f);
568 function TGxBmpFont
.textWidth (const s
: AnsiString): Integer;
572 if (Length(s
) > 0) then
575 for ch
in s
do result
+= (mFontWdt
[Byte(ch
)] and $0f)+1;
584 procedure TGxBmpFont
.initDrawText ();
586 glEnable(GL_ALPHA_TEST
);
587 glAlphaFunc(GL_NOTEQUAL
, 0.0);
588 glEnable(GL_TEXTURE_2D
);
589 glBindTexture(GL_TEXTURE_2D
, mTexId
);
593 procedure TGxBmpFont
.doneDrawText ();
595 glDisable(GL_ALPHA_TEST
);
596 glDisable(GL_TEXTURE_2D
);
597 glBindTexture(GL_TEXTURE_2D
, 0);
601 function TGxBmpFont
.drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer;
605 tx
:= (Integer(ch
) mod 16)*16;
606 ty
:= (Integer(ch
) div 16)*16;
608 glTexCoord2f((tx
+0)/256.0, (ty
+0)/256.0); glVertex2i(x
+0, y
+0); // top-left
609 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+0)/256.0); glVertex2i(x
+mWidth
, y
+0); // top-right
610 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+mWidth
, y
+mHeight
); // bottom-right
611 glTexCoord2f((tx
+0)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+0, y
+mHeight
); // bottom-left
613 result
:= (mFontWdt
[Byte(ch
)] and $0f);
617 function TGxBmpFont
.drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer;
620 result
:= drawCharInterim(x
, y
, ch
);
625 function TGxBmpFont
.drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer;
630 if (Length(s
) = 0) then begin result
:= 0; exit
; end;
635 wdt
:= drawCharInterim(x
, y
, ch
)+1;
643 // ////////////////////////////////////////////////////////////////////////// //
645 fontList
: array of TGxBmpFont
= nil;
646 defaultFontName
: AnsiString = 'win14';
649 function strEquCI (const s0
, s1
: AnsiString): Boolean;
654 result
:= (Length(s0
) = Length(s1
));
657 for f
:= 1 to Length(s0
) do
660 if (c0
>= 'a') and (c0
<= 'z') then Dec(c0
, 32); // poor man's `toupper()`
662 if (c1
>= 'a') and (c1
<= 'z') then Dec(c1
, 32); // poor man's `toupper()`
663 if (c0
<> c1
) then begin result
:= false; exit
; end;
669 function getFontByName (const aname
: AnsiString): TGxBmpFont
;
674 if (Length(fontList
) = 0) then raise Exception
.Create('font subsystem not initialized');
675 if (Length(aname
) = 0) or (strEquCI(aname
, 'default')) then fname
:= defaultFontName
else fname
:= aname
;
676 for f
:= 0 to High(fontList
) do
678 result
:= fontList
[f
];
679 if (result
= nil) then continue
;
680 if (strEquCI(result
.name
, fname
)) then exit
;
682 if (fontList
[0] = nil) then raise Exception
.Create('font subsystem not properly initialized');
683 result
:= fontList
[0];
688 procedure deleteFonts ();
692 for f := 0 to High(fontList) do freeAndNil(fontList[f]);
698 procedure fuiGfxLoadFont (const fontname
: AnsiString; const fontFile
: AnsiString; proportional
: Boolean=false);
702 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font '''+fontFile
+'''');
703 st
:= fuiOpenFile(fontFile
);
704 if (st
= nil) then raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
706 fuiGfxLoadFont(fontname
, st
, proportional
);
707 except on e
: Exception
do
709 writeln('FlexUI font loadin error: ', e
.message);
711 raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
720 procedure fuiGfxLoadFont (const fontname
: AnsiString; st
: TStream
; proportional
: Boolean=false);
722 fnt
: TGxBmpFont
= nil;
725 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font');
726 fnt
:= TGxBmpFont
.Create(fontname
, st
, proportional
);
728 for f
:= 0 to High(fontList
) do
730 if (strEquCI(fontList
[f
].name
, fontname
)) then
732 if (fontList
[f
].mTexId
<> 0) then raise Exception
.Create('FlexUI: cannot reload generated font named '''+fontname
+'''');
733 FreeAndNil(fontList
[f
]);
738 SetLength(fontList
, Length(fontList
)+1);
739 fontList
[High(fontList
)] := fnt
;
747 procedure oglInitFonts ();
751 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglCreateTexture();
755 procedure oglDeinitFonts ();
759 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglDestroyTexture();
763 // ////////////////////////////////////////////////////////////////////////// //
764 procedure oglSetup2DState ();
767 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
768 glDisable(GL_LINE_SMOOTH
);
769 glDisable(GL_POLYGON_SMOOTH
);
770 glDisable(GL_POINT_SMOOTH
);
771 glDisable(GL_DEPTH_TEST
);
772 glDisable(GL_TEXTURE_2D
);
773 glDisable(GL_LIGHTING
);
774 glDisable(GL_DITHER
);
775 glDisable(GL_STENCIL_TEST
);
776 glDisable(GL_SCISSOR_TEST
);
777 glDisable(GL_CULL_FACE
);
778 glDisable(GL_ALPHA_TEST
);
780 glClearColor(0, 0, 0, 0);
781 glColor4f(1, 1, 1, 1);
785 procedure oglSetup2D (winWidth
, winHeight
: Integer; upsideDown
: Boolean=false);
787 glViewport(0, 0, winWidth
, winHeight
);
791 glMatrixMode(GL_TEXTURE
);
794 glMatrixMode(GL_COLOR
);
797 glMatrixMode(GL_PROJECTION
);
801 glOrtho(0, winWidth
, 0, winHeight
, -1, 1); // set origin to bottom left
805 glOrtho(0, winWidth
, winHeight
, 0, -1, 1); // set origin to top left
808 glMatrixMode(GL_MODELVIEW
);
813 // ////////////////////////////////////////////////////////////////////////// //
814 {$INCLUDE fui_gfx_gl_cursor.inc}
816 procedure oglDrawCursor (); begin oglDrawCursorAt(fuiMouseX
, fuiMouseY
); end;
819 // ////////////////////////////////////////////////////////////////////////// //
820 constructor TGxContext
.Create ();
823 mColor
:= TGxRGBA
.Create(255, 255, 255);
824 mFont
:= getFontByName('default');
827 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
828 mClipOfs
:= TGxOfs
.Create(0, 0);
832 destructor TGxContext
.Destroy ();
834 if (mActive
) then gxSetContext(nil);
839 function TGxContext
.getFont (): AnsiString;
841 result
:= mFont
.name
;
844 procedure TGxContext
.setFont (const aname
: AnsiString);
846 mFont
:= getFontByName(aname
);
850 procedure TGxContext
.onActivate ();
852 setupGLColor(mColor
);
856 procedure TGxContext
.onDeactivate ();
861 procedure TGxContext
.setColor (const clr
: TGxRGBA
);
864 if (mActive
) then setupGLColor(mColor
);
868 procedure TGxContext
.realizeClip ();
870 sx
, sy
, sw
, sh
: Integer;
872 if (not mActive
) then exit
; // just in case
873 if (mClipRect
.w
<= 0) or (mClipRect
.h
<= 0) then
875 glEnable(GL_SCISSOR_TEST
);
876 glScissor(0, 0, 0, 0);
882 sx
:= trunc(mClipRect
.x
*mScale
);
883 sy
:= trunc(mClipRect
.y
*mScale
);
884 sw
:= trunc(mClipRect
.w
*mScale
);
885 sh
:= trunc(mClipRect
.h
*mScale
);
894 if (not intersectRect(sx
, sy
, sw
, sh
, 0, 0, fuiScrWdt
, fuiScrHgt
)) then
896 glEnable(GL_SCISSOR_TEST
);
897 glScissor(0, 0, 0, 0);
899 else if (sx
= 0) and (sy
= 0) and (sw
= fuiScrWdt
) and (sh
= fuiScrHgt
) then
901 glDisable(GL_SCISSOR_TEST
);
905 glEnable(GL_SCISSOR_TEST
);
906 sy
:= fuiScrHgt
-(sy
+sh
);
907 glScissor(sx
, sy
, sw
, sh
);
913 procedure TGxContext
.resetClip ();
915 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
916 if (mActive
) then realizeClip();
920 procedure TGxContext
.setClipOfs (const aofs
: TGxOfs
);
926 procedure TGxContext
.setClipRect (const aclip
: TGxRect
);
929 if (mActive
) then realizeClip();
933 function TGxContext
.setOffset (constref aofs
: TGxOfs
): TGxOfs
;
940 function TGxContext
.setClip (constref aclip
: TGxRect
): TGxRect
;
944 if (mActive
) then realizeClip();
948 function TGxContext
.combineClip (constref aclip
: TGxRect
): TGxRect
;
951 mClipRect
.intersect(aclip
);
952 if (mActive
) then realizeClip();
956 procedure TGxContext
.line (x1
, y1
, x2
, y2
: Integer);
958 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
960 if (not mScaled
) then
964 glVertex2f(x1
+0.375, y1
+0.375);
965 glVertex2f(x2
+0.375, y2
+0.375);
968 if (x1
<> x2
) or (y1
<> y2
) then
972 glVertex2f(x2
+0.375, y2
+0.375);
984 glVertex2i(x2
+1, y2
+1);
990 procedure TGxContext
.hline (x
, y
, len
: Integer);
992 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
993 if (len
< 1) then exit
;
994 if (not mScaled
) then
998 glVertex2f(x
+0.375, y
+0.375);
999 glVertex2f(x
+len
+0.375, y
+0.375);
1002 else if (mScale
> 1.0) then
1006 glVertex2i(x
+len
, y
);
1007 glVertex2i(x
+len
, y
+1);
1015 while (len
> 0) do begin glVertex2i(x
, y
); Inc(x
); Dec(len
); end;
1021 procedure TGxContext
.vline (x
, y
, len
: Integer);
1023 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1024 if (len
< 1) then exit
;
1025 if (not mScaled
) then
1029 glVertex2f(x
+0.375, y
+0.375);
1030 glVertex2f(x
+0.375, y
+len
+0.375);
1033 else if (mScale
> 1.0) then
1037 glVertex2i(x
, y
+len
);
1038 glVertex2i(x
+1, y
+len
);
1046 while (len
> 0) do begin glVertex2i(x
, y
); Inc(y
); Dec(len
); end;
1052 procedure TGxContext
.rect (x
, y
, w
, h
: Integer);
1054 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1055 if (w
< 0) or (h
< 0) then exit
;
1056 if (w
= 1) and (h
= 1) then
1060 if mScaled
then glVertex2i(x
, y
) else glVertex2f(x
+0.375, y
+0.375);
1065 if (not mScaled
) then
1069 glVertex2i(x
, y
); glVertex2i(x
+w
, y
); // top
1070 glVertex2i(x
, y
+h
-1); glVertex2i(x
+w
, y
+h
-1); // bottom
1071 glVertex2f(x
+0.375, y
+1); glVertex2f(x
+0.375, y
+h
-1); // left
1072 glVertex2f(x
+w
-1+0.375, y
+1); glVertex2f(x
+w
-1+0.375, y
+h
-1); // right
1080 vline(x
+w
-1, y
+1, h
-2);
1086 procedure TGxContext
.fillRect (x
, y
, w
, h
: Integer);
1088 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1089 if (w
< 0) or (h
< 0) then exit
;
1093 glVertex2f(x
+w
, y
+h
);
1099 procedure TGxContext
.darkenRect (x
, y
, w
, h
: Integer; a
: Integer);
1101 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (a
>= 255) then exit
;
1102 if (w
< 0) or (h
< 0) then exit
;
1103 if (a
< 0) then a
:= 0;
1105 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1106 glColor4f(0.0, 0.0, 0.0, a
/255.0);
1110 glVertex2i(x
+w
, y
+h
);
1113 setupGLColor(mColor
);
1117 function TGxContext
.charWidth (const ch
: AnsiChar): Integer;
1119 result
:= mFont
.charWidth(ch
);
1122 function TGxContext
.charHeight (const ch
: AnsiChar): Integer;
1124 result
:= mFont
.height
;
1128 function TGxContext
.textWidth (const s
: AnsiString): Integer;
1130 result
:= mFont
.textWidth(s
);
1133 function TGxContext
.textHeight (const s
: AnsiString): Integer;
1135 result
:= mFont
.height
;
1139 function TGxContext
.drawChar (x
, y
: Integer; const ch
: AnsiChar): Integer; // returns char width
1141 result
:= mFont
.charWidth(ch
);
1142 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1143 TGxBmpFont(mFont
).drawCharInternal(x
, y
, ch
);
1146 function TGxContext
.drawText (x
, y
: Integer; const s
: AnsiString): Integer; // returns text width
1148 result
:= mFont
.textWidth(s
);
1149 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) or (Length(s
) = 0) then exit
;
1150 TGxBmpFont(mFont
).drawTextInternal(x
, y
, s
);
1154 function TGxContext
.iconMarkWidth (ic
: TMarkIcon
): Integer;
1156 {$IFDEF FUI_TEXT_ICONS}
1158 TMarkIcon
.Checkbox
: result
:= textWidth('[x]');
1159 TMarkIcon
.Radiobox
: result
:= textWidth('(*)');
1160 else result
:= textWidth('[x]');
1167 function TGxContext
.iconMarkHeight (ic
: TMarkIcon
): Integer;
1169 {$IFDEF FUI_TEXT_ICONS}
1171 TMarkIcon
.Checkbox
: result
:= textHeight('[x]');
1172 TMarkIcon
.Radiobox
: result
:= textHeight('(*)');
1173 else result
:= textHeight('[x]');
1180 procedure TGxContext
.drawIconMark (ic
: TMarkIcon
; x
, y
: Integer; marked
: Boolean);
1182 {$IFDEF FUI_TEXT_ICONS}
1188 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1189 {$IFDEF FUI_TEXT_ICONS}
1191 TMarkIcon
.Checkbox
: xstr
:= '[x]';
1192 TMarkIcon
.Radiobox
: xstr
:= '(*)';
1197 drawText(x
, y
, xstr
);
1201 drawChar(x
, y
, xstr
[1]);
1202 drawChar(x
+textWidth(xstr
)-charWidth(xstr
[3]), y
, xstr
[3]);
1205 if (ic
= TMarkIcon
.Checkbox
) then
1217 vline(x
+10, y
+1, 5);
1223 if (not marked
) then exit
;
1229 vline(x
+3+f
, y
+1+f
, 1);
1230 vline(x
+7-f
, y
+1+f
, 1);
1246 function TGxContext
.iconWinWidth (ic
: TWinIcon
): Integer;
1248 {$IFDEF FUI_TEXT_ICONS}
1250 TWinIcon
.Close
: result
:= nmax(textWidth('[x]'), textWidth('[#]'));
1251 else result
:= nmax(textWidth('[x]'), textWidth('[#]'));
1258 function TGxContext
.iconWinHeight (ic
: TWinIcon
): Integer;
1260 {$IFDEF FUI_TEXT_ICONS}
1262 TWinIcon
.Close
: result
:= nmax(textHeight('[x]'), textHeight('[#]'));
1263 else result
:= nmax(textHeight('[x]'), textHeight('[#]'));
1270 procedure TGxContext
.drawIconWin (ic
: TWinIcon
; x
, y
: Integer; pressed
: Boolean);
1272 {$IFDEF FUI_TEXT_ICONS}
1279 if (not mActive
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1280 {$IFDEF FUI_TEXT_ICONS}
1282 TWinIcon
.Close
: if (pressed
) then xstr
:= '[#]' else xstr
:= '[x]';
1285 wdt
:= nmax(textWidth('[x]'), textWidth('[#]'));
1286 drawChar(x
, y
, xstr
[1]);
1287 drawChar(x
+wdt
-charWidth(xstr
[3]), y
, xstr
[3]);
1288 drawChar(x
+((wdt
-charWidth(xstr
[2])) div 2), y
, xstr
[2]);
1290 if pressed
then rect(x
, y
, 9, 8);
1293 vline(x
+1+f
, y
+f
, 1);
1294 vline(x
+1+6-f
, y
+f
, 1);
1300 procedure TGxContext
.glSetScale (ascale
: Single);
1302 if (ascale
< 0.01) then ascale
:= 0.01;
1304 glScalef(ascale
, ascale
, 1.0);
1306 mScaled
:= (ascale
<> 1.0);
1309 procedure TGxContext
.glSetTrans (ax
, ay
: Single);
1312 glScalef(mScale
, mScale
, 1.0);
1313 glTranslatef(ax
, ay
, 0);
1317 procedure TGxContext
.glSetScaleTrans (ascale
, ax
, ay
: Single);
1320 glTranslatef(ax
, ay
, 0);
1324 // vertical scroll bar
1325 procedure TGxContext
.drawVSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
1329 if (wdt
< 1) or (hgt
< 1) then exit
;
1330 filled
:= sbarFilled(hgt
, cur
, min
, max
);
1332 fillRect(x
, y
, wdt
, filled
);
1334 fillRect(x
, y
+filled
, wdt
, hgt
-filled
);
1338 // horizontal scrollbar
1339 procedure TGxContext
.drawHSBar (x
, y
, wdt
, hgt
: Integer; cur
, min
, max
: Integer; constref clrfull
, clrempty
: TGxRGBA
);
1343 if (wdt
< 1) or (hgt
< 1) then exit
;
1344 filled
:= sbarFilled(wdt
, cur
, min
, max
);
1346 fillRect(x
, y
, filled
, hgt
);
1348 fillRect(x
+filled
, y
, wdt
-filled
, hgt
);
1352 class function TGxContext
.sbarFilled (wh
: Integer; cur
, min
, max
: Integer): Integer;
1354 if (wh
< 1) then result
:= 0
1355 else if (min
> max
) then result
:= 0
1356 else if (min
= max
) then result
:= wh
1359 if (cur
< min
) then cur
:= min
else if (cur
> max
) then cur
:= max
;
1360 result
:= wh
*(cur
-min
) div (max
-min
);
1365 class function TGxContext
.sbarPos (cxy
: Integer; xy
, wh
: Integer; min
, max
: Integer): Integer;
1367 if (wh
< 1) then begin result
:= 0; exit
; end;
1368 if (min
> max
) then begin result
:= 0; exit
; end;
1369 if (min
= max
) then begin result
:= max
; exit
; end;
1370 if (cxy
< xy
) then begin result
:= min
; exit
; end;
1371 if (cxy
>= xy
+wh
) then begin result
:= max
; exit
; end;
1372 result
:= min
+((max
-min
)*(cxy
-xy
) div wh
);
1373 assert((result
>= min
) and (result
<= max
));
1379 // ////////////////////////////////////////////////////////////////////////// //
1381 procedure oglRestoreMode (doClear: Boolean);
1383 oglSetup2D(fuiScrWdt, fuiScrHgt);
1384 glScissor(0, 0, fuiScrWdt, fuiScrHgt);
1386 glBindTexture(GL_TEXTURE_2D, 0);
1387 glDisable(GL_BLEND);
1388 glDisable(GL_TEXTURE_2D);
1389 glDisable(GL_STENCIL_TEST);
1390 glDisable(GL_SCISSOR_TEST);
1391 glDisable(GL_LIGHTING);
1392 glDisable(GL_DEPTH_TEST);
1393 glDisable(GL_CULL_FACE);
1394 glDisable(GL_LINE_SMOOTH);
1395 glDisable(GL_POINT_SMOOTH);
1398 glColor4f(1, 1, 1, 1);
1402 glClearColor(0, 0, 0, 0);
1403 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_ACCUM_BUFFER_BIT or GL_STENCIL_BUFFER_BIT);
1407 glMatrixMode(GL_MODELVIEW);
1409 //glScalef(4, 4, 1);
1414 //procedure onWinFocus (); begin uiFocus(); end;
1415 //procedure onWinBlur (); begin fuiResetKMState(true); uiBlur(); end;
1417 //procedure onPreRender (); begin oglRestoreMode(gGfxDoClear); end;
1418 procedure onPostRender (); begin oglDrawCursor(); end;
1420 procedure onInit ();
1422 //oglSetup2D(fuiScrWdt, fuiScrHgt);
1423 createCursorTexture();
1427 procedure onDeinit ();
1429 fuiResetKMState(false);
1430 if (curtexid
<> 0) then glDeleteTextures(1, @curtexid
);
1440 // ////////////////////////////////////////////////////////////////////////// //
1442 savedGLState
:= TSavedGLState
.Create(false);
1444 //winFocusCB := onWinFocus;
1445 //winBlurCB := onWinBlur;
1446 //prerenderFrameCB := onPreRender;
1447 postrenderFrameCB
:= onPostRender
;
1448 oglInitCB
:= onInit
;
1449 oglDeinitCB
:= onDeinit
;