-
Notifications
You must be signed in to change notification settings - Fork 19
/
Commands.pas
490 lines (427 loc) · 14 KB
/
Commands.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
{Copyright (c) 2008 Ville Krumlinde
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.}
unit Commands;
{$include zzdc_globalopt.inc}
interface
uses ZClasses,ZExpressions;
//General purpose commands for eventtree
type
TRepeat = class(TCommand)
protected
procedure DefineProperties(List: TZPropertyList); override;
public
Count : integer; //Set for a fixed nr of iterations
OnIteration : TZComponentList;
WhileExp : TZExpressionPropValue; //Set to use a expression to test against
Iteration : integer;
procedure Execute; override;
procedure Update; override;
end;
TCondition = class(TCommand)
protected
procedure DefineProperties(List: TZPropertyList); override;
public
Expression : TZExpressionPropValue;
OnTrue,OnFalse : TZComponentList;
procedure Execute; override;
procedure Update; override;
end;
TKeyPress = class(TCommand)
private
LastPressedAt : single;
protected
procedure DefineProperties(List: TZPropertyList); override;
public
Keys : TPropString;
RepeatDelay : single;
OnPressed : TZComponentList;
OnKeyUp : TZComponentList;
OnKeyDown : TZComponentList;
KeyIndex : integer;
CharCode : word;
procedure Execute; override;
end;
//Force a component to refresh itself by calling .Change
TRefreshContent = class(TCommand)
protected
procedure DefineProperties(List: TZPropertyList); override;
public
Component : TZComponent;
procedure Execute; override;
{$ifndef minimal}
function GetDisplayName: AnsiString; override;
{$endif}
end;
TZTimer = class(TCommand)
strict private
Time : single;
CurrentIteration : integer;
Stopped : boolean;
protected
procedure DefineProperties(List: TZPropertyList); override;
public
OnTimer : TZComponentList;
Interval : single;
RepeatCount : integer;
procedure Execute; override;
{$ifndef minimal}
procedure DesignerReset; override;
{$endif}
end;
TWebOpen = class(TCommand)
strict private
Buffer : pointer;
BufferSize : integer;
IsWaiting : boolean;
private
procedure AddToResultList;
protected
procedure DefineProperties(List: TZPropertyList); override;
public
Url : TPropString;
ResultString : TPropString;
OnResult : TZComponentList;
InBrowser : boolean;
Handle : pointer;
class procedure FlushResultList;
procedure Execute; override;
procedure StartReading;
destructor Destroy; override;
{$ifndef minimal}
function GetDisplayName: AnsiString; override;
{$endif}
end;
TCallComponent = class(TCommand)
protected
procedure DefineProperties(List: TZPropertyList); override;
public
Component : TCommand;
procedure Execute; override;
{$ifndef minimal}
function GetDisplayName: AnsiString; override;
{$endif}
end;
var
//Used by TWebOpen
NetMutex : pointer;
NetResultList : TZArrayList;
implementation
uses ZPlatform,ZApplication
{$ifndef minimal},ZLog{$endif};
{ TRepeat }
procedure TRepeat.DefineProperties(List: TZPropertyList);
begin
inherited;
List.AddProperty({$IFNDEF MINIMAL}'Count',{$ENDIF}(@Count), zptInteger);
List.AddProperty({$IFNDEF MINIMAL}'OnIteration',{$ENDIF}(@OnIteration), zptComponentList);
List.AddProperty({$IFNDEF MINIMAL}'WhileExp',{$ENDIF}(@WhileExp), zptExpression);
{$ifndef minimal}
List.GetLast.DefaultValue.ExpressionValue.Source:='//this.Iteration=current iteration nr. Return false to end loop.';
List.GetLast.ReturnType.Kind := zctFloat;
{$endif}
List.AddProperty({$IFNDEF MINIMAL}'Iteration',{$ENDIF}(@Iteration), zptInteger);
List.GetLast.NeverPersist:=True;
{$ifndef minimal}List.GetLast.IsReadOnly := True;{$endif}
end;
procedure TRepeat.Execute;
{$IFNDEF MINIMAL}
{$IFNDEF ZGEVIZ}
{$DEFINE GUARD_LIMIT}
{$ENDIF}
{$ENDIF}
var
I : integer;
{$ifdef GUARD_LIMIT}BailOutTime : single;{$endif}
begin
{$ifdef GUARD_LIMIT}BailOutTime:=Platform_GetTime + 3.0;{$endif}
Iteration := 0;
if WhileExp.Code.Count>0 then
begin
while True do
begin
if ZExpressions.RunCode(WhileExp.Code).SingleValue=0 then
Break;
OnIteration.ExecuteCommands;
Inc(Iteration);
{$ifdef GUARD_LIMIT}
if (Platform_GetTime>BailoutTime) and (Iteration>1) then
ZHalt('Repeat-loop timeout. Check your repeat-components for infinite loops.');
{$endif}
end;
end
else
for I := 0 to Count-1 do
begin
OnIteration.ExecuteCommands;
Inc(Iteration);
end;
end;
procedure TRepeat.Update;
begin
inherited;
OnIteration.Update;
end;
{ TCondition }
procedure TCondition.DefineProperties(List: TZPropertyList);
begin
inherited;
List.AddProperty({$IFNDEF MINIMAL}'Expression',{$ENDIF}(@Expression), zptExpression);
{$ifndef minimal}List.GetLast.ReturnType.Kind := zctFloat;{$endif}
List.AddProperty({$IFNDEF MINIMAL}'OnTrue',{$ENDIF}(@OnTrue), zptComponentList);
{$ifndef minimal}{List.GetLast.SetChildClasses([TCommand,TZExpression]);}{$endif}
List.AddProperty({$IFNDEF MINIMAL}'OnFalse',{$ENDIF}(@OnFalse), zptComponentList);
{$ifndef minimal}{List.GetLast.SetChildClasses([TCommand,TZExpression]);}{$endif}
end;
procedure TCondition.Execute;
begin
if ZExpressions.RunCode(Expression.Code).SingleValue<>0 then
OnTrue.ExecuteCommands
else
OnFalse.ExecuteCommands;
end;
procedure TCondition.Update;
begin
inherited;
OnTrue.Update;
OnFalse.Update;
end;
{ TKeyPress }
procedure TKeyPress.DefineProperties(List: TZPropertyList);
begin
inherited;
List.AddProperty({$IFNDEF MINIMAL}'Keys',{$ENDIF}(@Keys), zptString);
List.GetLast.IsManagedTarget := True;
List.AddProperty({$IFNDEF MINIMAL}'CharCode',{$ENDIF}(@CharCode), zptByte);
List.AddProperty({$IFNDEF MINIMAL}'RepeatDelay',{$ENDIF}(@RepeatDelay), zptFloat);
List.AddProperty({$IFNDEF MINIMAL}'OnPressed',{$ENDIF}(@OnPressed), zptComponentList);
List.AddProperty({$IFNDEF MINIMAL}'OnKeyUp',{$ENDIF}@OnKeyUp, zptComponentList);
List.AddProperty({$IFNDEF MINIMAL}'OnKeyDown',{$ENDIF}@OnKeyDown, zptComponentList);
List.AddProperty({$IFNDEF MINIMAL}'KeyIndex',{$ENDIF}(@KeyIndex), zptInteger);
List.GetLast.NeverPersist := True;
{$ifndef minimal}List.GetLast.IsReadOnly := True;{$endif}
end;
procedure TKeyPress.Execute;
var
P : PBytes;
Index : integer;
begin
if Keys^=#0 then
//Charcode is byte-sized but declared as word so it is zero-terminated
P := @CharCode
else
P := pointer(Keys);
if (P=nil) or
((ZApp.Time<LastPressedAt+RepeatDelay)
//Time gets reset in designer every time app is started, validate LastPressedAt
{$ifndef minimal}and (LastPressedAt<ZApp.Time){$endif}
)
then
Exit;
Index := 0;
while P^[Index]<>0 do
begin
Self.KeyIndex := Index;
if Platform_IsKeyPressed(AnsiChar(P^[Index])) then
begin
OnPressed.ExecuteCommands;
LastPressedAt := ZApp.Time;
end;
if Assigned(ZPlatform.KeyDownList) and (ZPlatform.KeyDownList.IndexOf(TObject(P^[Index]))>=0) then
OnKeyDown.ExecuteCommands;
if Assigned(ZPlatform.KeyUpList) and (ZPlatform.KeyUpList.IndexOf(TObject(P^[Index]))>=0) then
OnKeyUp.ExecuteCommands;
Inc(Index);
end;
end;
{ TRefreshContent }
procedure TRefreshContent.DefineProperties(List: TZPropertyList);
begin
inherited;
List.AddProperty({$IFNDEF MINIMAL}'Component',{$ENDIF}(@Component), zptComponentRef);
end;
procedure TRefreshContent.Execute;
begin
{$ifndef minimal}
if not Assigned(Component) then Exit;
{$endif}
Component.Change;
Component.Update;
end;
{$ifndef minimal}
function TRefreshContent.GetDisplayName: AnsiString;
begin
Result := inherited GetDisplayName;
if Assigned(Component) then
Result := Result + ' ' + Component.Name;
end;
{$endif}
{ TZTimer }
procedure TZTimer.DefineProperties(List: TZPropertyList);
begin
inherited;
List.AddProperty({$IFNDEF MINIMAL}'OnTimer',{$ENDIF}(@OnTimer), zptComponentList);
{$ifndef minimal}{List.GetLast.SetChildClasses([TCommand,TZExpression]);}{$endif}
List.AddProperty({$IFNDEF MINIMAL}'Interval',{$ENDIF}(@Interval), zptFloat);
List.AddProperty({$IFNDEF MINIMAL}'RepeatCount',{$ENDIF}(@RepeatCount), zptInteger);
List.GetLast.DefaultValue.IntegerValue := -1;
List.AddProperty({$IFNDEF MINIMAL}'CurrentRelativeTime',{$ENDIF}@Time, zptFloat);
List.GetLast.NeverPersist := True;
end;
{$ifndef minimal}
procedure TZTimer.DesignerReset;
begin
inherited;
CurrentIteration := 0;
Time := 0;
Stopped := False;
end;
{$endif}
procedure TZTimer.Execute;
begin
if (Interval<>0) and (not Stopped) then
begin
Time := Time + ZApp.DeltaTime;
if Time>=Interval then
begin
Time := Time - Interval;
OnTimer.ExecuteCommands;
if RepeatCount<>-1 then
begin
Inc(CurrentIteration);
Stopped := CurrentIteration>RepeatCount;
end;
end;
end;
end;
{ TWebOpen }
procedure TWebOpen.DefineProperties(List: TZPropertyList);
begin
inherited;
List.AddProperty({$IFNDEF MINIMAL}'Url',{$ENDIF}(@Url), zptString);
List.GetLast.IsManagedTarget := True;
List.AddProperty({$IFNDEF MINIMAL}'ResultString',{$ENDIF}(@ResultString), zptString);
List.GetLast.NeverPersist := True;
List.GetLast.IsManagedTarget := True;
List.AddProperty({$IFNDEF MINIMAL}'InBrowser',{$ENDIF}(@InBrowser), zptBoolean);
List.AddProperty({$IFNDEF MINIMAL}'OnResult',{$ENDIF}(@OnResult), zptComponentList);
end;
destructor TWebOpen.Destroy;
begin
FreeMem(Buffer);
inherited;
end;
procedure TWebOpen.Execute;
var
Buf : array[0..1024-1] of byte;
begin
if not Self.IsWaiting then
begin
if not Self.InBrowser then
IsWaiting := True;
ZStrCopy(@Buf,PAnsiChar(Self.Url));
Platform_NetOpen(PAnsiChar(@Buf),Self.InBrowser,Self);
end;
end;
class procedure TWebOpen.FlushResultList;
var
I : integer;
begin
Platform_EnterMutex(NetMutex);
for I := 0 to NetResultList.Count - 1 do
begin
TWebOpen(NetResultList[I]).OnResult.ExecuteCommands;
end;
NetResultList.Clear;
Platform_LeaveMutex(NetMutex);
end;
{$ifndef minimal}
function TWebOpen.GetDisplayName: AnsiString;
begin
Result := inherited GetDisplayName;
Result := Result + ' ' + Url;
end;
{$endif}
procedure TWebOpen.StartReading;
var
BytesRead : integer;
begin
if not Self.IsWaiting then
Exit;
Self.IsWaiting := False;
BufferSize := 512 * 1024;
ReAllocMem(Self.Buffer,BufferSize);
BytesRead := Platform_NetRead(Self.Handle,Self.Buffer,Self.BufferSize);
if BytesRead>0 then
begin
Self.ResultString := ManagedHeap_Alloc(BytesRead+1);
Move(Self.Buffer^,Self.ResultString^,BytesRead);
PBytes(Self.ResultString)^[BytesRead+1] := 0; //Zero terminate
end;
AddToResultList;
end;
procedure TWebOpen.AddToResultList;
begin
Platform_EnterMutex(NetMutex);
NetResultList.Add(Self);
Platform_LeaveMutex(NetMutex);
end;
{ TCallComponent }
procedure TCallComponent.DefineProperties(List: TZPropertyList);
begin
inherited;
List.AddProperty({$IFNDEF MINIMAL}'Component',{$ENDIF}(@Component), zptComponentRef);
{$ifndef minimal}List.GetLast.SetChildClasses([TCommand]);{$endif}
end;
procedure TCallComponent.Execute;
begin
if Component<>nil then
Component.Execute;
end;
{$ifndef minimal}
function TCallComponent.GetDisplayName: AnsiString;
begin
Result := inherited GetDisplayName;
if Component<>nil then
Result := Result + ' ' + Component.Name;
end;
{$endif}
initialization
ZClasses.Register(TRepeat,RepeatClassId);
{$ifndef minimal}ComponentManager.LastAdded.ImageIndex:=7;{$endif}
ZClasses.Register(TCondition,ConditionClassId);
{$ifndef minimal}ComponentManager.LastAdded.ImageIndex:=27;{$endif}
ZClasses.Register(TKeyPress,KeyPressClassId);
{$ifndef minimal}ComponentManager.LastAdded.NeedParentList := 'OnUpdate';{$endif}
{$ifndef minimal}ComponentManager.LastAdded.ImageIndex:=34;{$endif}
ZClasses.Register(TRefreshContent,RefreshContentClassId);
{$ifndef minimal}ComponentManager.LastAdded.ImageIndex:=39;{$endif}
ZClasses.Register(TZTimer,ZTimerClassId);
{$ifndef minimal}ComponentManager.LastAdded.ZClassName := 'Timer';{$endif}
{$ifndef minimal}ComponentManager.LastAdded.NeedParentList := 'OnUpdate';{$endif}
{$ifndef minimal}ComponentManager.LastAdded.ImageIndex:=5;{$endif}
ZClasses.Register(TWebOpen,WebOpenClassId);
{$ifndef minimal}ComponentManager.LastAdded.ImageIndex:=37;{$endif}
ZClasses.Register(TCallComponent,CallComponentClassId);
{$ifndef minimal}ComponentManager.LastAdded.ImageIndex:=28;{$endif}
NetResultList := TZArrayList.CreateReferenced;
NetMutex := Platform_CreateMutex;
{$ifndef minimal}
finalization
NetResultList.Free;
Platform_FreeMutex( NetMutex );
{$endif}
end.