-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathApus.Engine.UIRender.pas
More file actions
213 lines (183 loc) · 6.66 KB
/
Apus.Engine.UIRender.pas
File metadata and controls
213 lines (183 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// -----------------------------------------------------
// User Interface rendering block
//
// Author: Ivan Polyacov (C) 2003-2014, Apus Software (ivan@apus-software.com)
// This file is licensed under the terms of BSD-3 license (see license.txt)
// This file is a part of the Apus Game Engine (http://apus-software.com/engine/)
// ------------------------------------------------------
unit Apus.Engine.UIRender;
interface
uses Apus.Core, Apus.Engine.API, Apus.Engine.UITypes, Apus.Engine.UI;
// Render an UI element and all its descendants (skip elements with manualDraw=true)
procedure DrawUI(item:TUIElement);
// Draw just this element (and it descedants with manualDraw=true)
procedure DrawManualUI(item:TUIElement;recursive:boolean=false);
// Render single UI element
procedure DrawUIElement(item:TUIElement;styleOverride:integer=-1);
procedure BackgroundRenderBegin;
procedure BackgroundRenderEnd;
procedure DrawGlobalShadow(color:cardinal);
// Set custom style drawer (style=1..50)
// 0..9 - reserved for engine styles
// 10..19 - for private game styles
// 20..50 - 3-rd party libraries
procedure RegisterUIStyle(style:byte;drawer:TUIDrawer;name:string8='');
// Get registered style drawer by ID (for backward-compat numeric style APIs)
function GetUIStyle(n:byte):TUIDrawer;
// Fill control rect with image (helper function)
procedure DrawControlWithImage(c:TUIElement;img:TTexture;centered:boolean=false);
var
// Глобальная переменная для отрисовщиков: может содержать время, прошедшее с
// предыдущей отрисовки (время кадра), но может содержать и 0
//frameTime:int64;
transpBgnd:boolean=false; // render target is (probably) transparent so blMove should be used to fill it
defaultHintFont:cardinal=0; // Шрифт, которым показываются хинты
implementation
uses Apus.Images, SysUtils, Types,
Apus.EventMan, Apus.Geom2D, Apus.Engine.DefaultStyle,
Apus.Log;
var
styleDrawers:array[0..50] of TUIDrawer;
procedure DrawGlobalShadow(color:cardinal);
begin
draw.FillRect(0,0,window.renderWidth,window.renderHeight,color);
end;
procedure BackgroundRenderBegin;
begin
if transpBgnd then gfx.target.BlendMode(blMove);
end;
procedure BackgroundRenderEnd;
begin
if transpBgnd then gfx.target.BlendMode(blAlpha);
end;
procedure DrawUITree(item:TUIElement;manualDraw:boolean;recursive:boolean);
var
i,j,n,cnt:integer;
tmp:pointer;
r:TRect;
list:array of TUIElement;
maskChange:boolean;
clipping:boolean;
begin
if not item.flags.visible then exit;
if item.layout<>nil then begin
item.layout.Layout(item);
item.Resize(-1,-1);
if item is TUIScrollable then
item.SetupScrollers;
end;
// Draw self first
if (item.size.x<=0) or (item.size.y<=0) then exit;
item.globalRect:=item.GetPosOnScreen;
{ /// TODO: alpha should be masked ONLY if semi-transparent element is drawn on an opaque background, not vice-versa.
/// Need to find a generic approach.
maskChange:=(item.parent<>nil) and (item.parent.transpmode<>tmTransparent);
maskChange:=false;
if maskChange then gfx.target.Mask(true,false);}
try
// Draw element
if item.flags.manualDraw=manualDraw then
DrawUIElement(item);
// Debug: Highlight with border when Ctrl+Alt+Win pressed
if (window.shiftState and $F=sscCtrl+sscWin+sscAlt) then
if (item=underMouse) or
((underMouse<>nil) and (item=underMouse.parent)) then
with item.globalRect do begin
if (item=underMouse) and (window.frameStartMs and $100=0) then
draw.FillRect(left,top,right-1,bottom-1,$1800FF00);
draw.Rect(left,top,right-1,bottom-1,$80FFFFFF xor ($FFFFFF*((CoreTime.Ticks shr 8) and 1)));
end;
except
on E:Exception do begin
Log.Force('Error drawing control '+item.name+' - '+ExceptionMsg(e));
CoreTime.Sleep(0);
end;
end;
if not recursive then exit;
// Now prepare list of child elements to draw
n:=length(item.children);
cnt:=0;
SetLength(list,n);
for i:=0 to n-1 do
if item.children[i].flags.visible then begin
list[cnt]:=item.children[i];
inc(cnt);
end;
// Process children elements
if cnt>0 then begin
// Затем отсортировать и нарисовать вложенные эл-ты
for i:=0 to cnt-2 do
for j:=cnt-1 downto i+1 do
if list[j].order<list[j-1].order then begin
tmp:=list[j];
list[j]:=list[j-1];
list[j-1]:=tmp;
end;
clipping:=not item.flags.dontClipChildren;
if clipping then begin
r:=item.GetClientPosOnScreen;
gfx.clip.Rect(r);
end;
for i:=0 to cnt-1 do begin
// если элемент не клипится и фон - не прозрачный - нарисовать без отсечения
if clipping and list[i].flags.noParentClip and not transpBgnd then begin
gfx.clip.Nothing;
DrawUITree(list[i],manualDraw,true);
gfx.clip.Restore;
end else
DrawUITree(list[i],manualDraw,true);
end;
if clipping then gfx.clip.Restore;
end;
{ // вернуть маску назад
if maskChange then gfx.target.UnMask;}
end;
procedure DrawUI(item:TUIElement);
begin
DrawUITree(item,false,true);
end;
procedure DrawManualUI(item:TUIElement;recursive:boolean=false);
begin
DrawUITree(item,true,recursive);
end;
procedure DrawUIElement(item:TUIElement;styleOverride:integer=-1);
var
drawerProc:TUIDrawer;
begin
if styleOverride=-1 then begin
if @item.drawer<>nil then drawerProc:=item.drawer
else drawerProc:=styleDrawers[0];
end else
drawerProc:=styleDrawers[styleOverride];
ASSERT(@drawerProc<>nil,'Style not registered: '+item.name);
try
drawerProc(item);
except
on e:Exception do raise EError.Create('Error drawing UI element: '+item.ObjInfo+' - '+ExceptionMsg(e));
end;
end;
function GetUIStyle(n:byte):TUIDrawer;
begin
if n<=high(styleDrawers) then result:=styleDrawers[n]
else result:=nil;
end;
procedure RegisterUIStyle(style:byte;drawer:TUIDrawer;name:string8='');
begin
ASSERT(style in [0..high(styleDrawers)]);
styleDrawers[style]:=drawer;
if name<>'' then Log.Msg(Format('UI style registered: %d - %s',[style,name]));
end;
procedure DrawControlWithImage(c:TUIElement;img:TTexture;centered:boolean=false);
var
r:TRect;
scale:single;
begin
r:=c.GetPosOnScreen;
if centered then begin
scale:=c.globalScale;
draw.RotScaled((r.left+r.right)/2,(r.top+r.bottom)/2,scale,scale,0,img);
end else begin
with r do draw.Scaled(left,top,right+1,bottom+1,img);
end;
end;
end.