-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathApus.Engine.MessageScene.pas
235 lines (201 loc) · 6.31 KB
/
Apus.Engine.MessageScene.pas
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
unit Apus.Engine.MessageScene;
interface
uses Apus.Common;
var
msgMainFont:cardinal; // regular font for text and buttons (0 - use inherited or default)
msgTitleFont:cardinal; // large font for message title (0 - use inherited or default)
procedure InitMessageScene;
// mes format: [Title]<CRLF>line1<CRLF>line2...lineN
// String separator: CRLF or '~'
procedure ShowMessage(mes:String8;OkEvent:String8='';x:integer=0;y:integer=0);
procedure Ask(mes,YesEvent,NoEvent:String8;x:integer=0;y:integer=0);
procedure Confirm(mes,OkEvent,CancelEvent:String8;x:integer=0;y:integer=0);
implementation
uses Types, Apus.CrossPlatform, SysUtils, Apus.EventMan, Apus.Structs,
Apus.Engine.API, Apus.Engine.UIRender, Apus.Engine.UITypes, Apus.Engine.UI, Apus.Engine.UIScene,
Apus.Engine.SceneEffects;
const
MODE_MSG = 1;
MODE_ASK = 2;
MODE_CONFIRM = 3;
type
TMessageScene=class(TUIScene)
wnd:TUIElement;
btnOk,btnYes,btnNo:TUIButton;
title:string;
lines:StringArr;
constructor Create;
procedure Initialize;
procedure UpdateUI(msgText:string;mode,x,y:integer);
procedure Render; override;
end;
TQueuedMessage=class
mType:integer;
msg,event1,event2:String8;
x,y:integer;
end;
var
scene:TMessageScene;
queue:TObjectQueue;
curMsg:TQueuedMessage;
procedure InitMessageScene;
begin
queue.Init(16);
scene:=TMessageScene.Create;
scene.Initialize;
end;
procedure QueueMsg(msg,e1,e2:String8;mType,x,y:integer);
var
obj:TQueuedMessage;
begin
obj:=TQueuedMessage.Create;
obj.mType:=mType;
obj.msg:=msg;
obj.event1:=e1;
obj.event2:=e2;
obj.x:=x;
obj.y:=y;
queue.Add(obj);
end;
// Ïðîâåðèòü íàëè÷èå â î÷åðåäè ñîîáùåíèÿ è åñëè îíî åñòü - ïîêàçàòü îêîøêî
procedure CheckQueue;
begin
if scene.IsActive then exit;
if curMsg<>nil then curMsg.Free;
curMsg:=TQueuedMessage(queue.Get);
if curMsg=nil then exit;
// Ïîäãîòîâèòü UI
with curMsg do
scene.UpdateUI(DecodeUTF8(msg),mType,x,y);
// Ïîêàçàòü
LogMessage('ShowMessage: '+curMsg.msg);
TShowWindowEffect.Create(scene,200,sweShow,2);
end;
procedure ShowMessage(mes:String8;OkEvent:String8='';x:integer=0;y:integer=0);
begin
QueueMsg(mes,okEvent,'',MODE_MSG,x,y);
CheckQueue;
end;
procedure Ask(mes,YesEvent,NoEvent:String8;x:integer=0;y:integer=0);
begin
QueueMsg(mes,YesEvent,NoEvent,MODE_ASK,x,y);
CheckQueue;
end;
procedure Confirm(mes,OkEvent,CancelEvent:String8;x:integer=0;y:integer=0);
begin
QueueMsg(mes,OkEvent,CancelEvent,MODE_CONFIRM,x,y);
CheckQueue;
end;
// UI\Message
// Scene\MessageScene
procedure EventHandler(event:TEventStr;tag:TTag);
var
close:boolean;
begin
if SameText('UI\Message\Next',event) then begin
CheckQueue;
exit;
end;
close:=false;
if (SameText(event,'Scene\MessageScene\KEYDOWN') and (tag and $FF=VK_RETURN)) or
(SameText(event,'UI\Message\OK\Click') or
SameText(event,'UI\Message\YES\Click')) then begin
if curMsg.event1<>'' then Signal(curMsg.event1);
close:=true;
end;
if (SameText(event,'Scene\MessageScene\KEYDOWN') and (tag and $FF=VK_ESCAPE)) or
SameText(event,'UI\Message\NO\Click') then begin
if curMsg.event2<>'' then Signal(curMsg.event2);
close:=true;
end;
if close then begin
TShowWindowEffect.Create(scene,150,sweHide,2);
DelayedSignal('UI\Message\Next',200);
end;
end;
{ TMessageScene }
constructor TMessageScene.Create;
begin
inherited Create('MessageScene',false);
zOrder:=100;
SetEventHandler('UI\Message',EventHandler,emQueued);
SetEventHandler('Scene\MessageScene',EventHandler,emMixed);
end;
procedure TMessageScene.Initialize;
begin
wnd:=TUIElement.Create(400,200,ui,'Message\Wnd');
wnd.SetPos(game.renderWidth/2,game.renderHeight/2,pivotCenter);
wnd.shape:=shapeFull;
wnd.manualDraw:=true;
wnd.styleInfo:='border:FFD0D8E0; radius:8; fill:C0C0C8D0';
wnd.font:=msgMainFont;
btnOk:=TUIButton.Create(90,35,'Message\OK','Ok',0,wnd);
btnOk.SetPos(200,165,pivotCenter).SetAnchors(0.5,1,0.5,1);
btnYes:=TUIButton.Create(90,35,'Message\YES','Yes',0,wnd);
btnYes.SetPos(200-70,165,pivotCenter).SetAnchors(0.5,1,0.5,1);
btnNo:=TUIButton.Create(90,35,'Message\NO','No',0,wnd);
btnNo.SetPos(200+70,165,pivotCenter).SetAnchors(0.5,1,0.5,1);
end;
// Update scene with new text and buttons
procedure TMessageScene.UpdateUI(msgText:string;mode,x,y:integer);
var
i,width,height,w,btnY:integer;
begin
msgText:=StringReplace(msgtext,'~',#13,[rfReplaceAll]);
msgText:=StringReplace(msgtext,#10,'',[rfReplaceAll]);
lines:=split(#13,msgText,#0);
if (length(lines)>0) and lines[0].StartsWith('[') and lines[0].EndsWith(']') then begin
title:=lines[0];
title:=copy(title,2,length(title)-2);
RemoveString(lines,0);
end else
title:='';
width:=round(300*windowScale);
if title<>'' then width:=Max2(width,txt.WidthW(msgTitleFont,title));
for i:=0 to high(lines) do
width:=max2(width,txt.WidthW(msgMainFont,lines[i]));
inc(width,round(100*windowScale));
height:=round((120+30*length(lines)+40*byte(title<>''))*windowScale);
wnd.Resize(width,height);
wnd.font:=msgMainFont;
btnOk.visible:=(mode=MODE_MSG);
btnYes.visible:=(mode in [MODE_ASK,MODE_CONFIRM]);
btnNo.visible:=(mode in [MODE_ASK,MODE_CONFIRM]);
if mode=MODE_ASK then begin
btnYes.caption:='Yes';
btnNo.caption:='No';
end;
if mode=MODE_CONFIRM then begin
btnYes.caption:='Ok';
btnNo.caption:='Cancel';
end;
if (x>0) or (y>0) then begin
wnd.position.x:=x;
wnd.position.y:=y;
end else
wnd.Center;
end;
procedure TMessageScene.Render;
var
r:TRect;
i,x,y:integer;
begin
// Draw background
r:=wnd.GetPosOnScreen;
DrawManualUI(wnd);
// Draw content
x:=r.CenterPoint.X;
y:=r.Top+round(40*windowScale);
if title<>'' then begin
txt.WriteW(msgTitleFont,x,y,$FF402000,title,TTextAlignment.taCenter);
inc(y,round(40*windowScale));
end else
inc(y,round(8*windowScale));
for i:=0 to high(lines) do begin
txt.WriteW(msgMainFont,x,y,$FF202020,lines[i],TTextAlignment.taCenter);
inc(y,round(30*windowScale));
end;
// Buttons and child elements
inherited;
end;
end.