-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerConfigs.pas.bak
More file actions
550 lines (459 loc) · 17.1 KB
/
ControllerConfigs.pas.bak
File metadata and controls
550 lines (459 loc) · 17.1 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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
unit ControllerConfigs;
interface
uses
System.SysUtils, System.Classes, Vcl.Forms, Vcl.Controls, Vcl.ExtCtrls,
UtilsTypes, System.JSON, JSONHelpers;
type
TControllerConfigsClass = class
private
class var FInstance: TControllerConfigsClass;
// 瑜版挸澧犻柅大夎厬閻ㄥ嫬顦查弶鍌氱潣閹呰閸?
FCurrentPropertyType: TComplexPropertyType;
// 瑜版挸澧犵紓标帆閻ㄥ嚙SON鐎电钖?
FCurrentJson: TJSONObject;
// 瑜版挸澧犵紓标帆閻ㄥ嫬鐫橀幀褍鎮曠粔?
FCurrentPropertyName: string;
// 閻栬泛顔愰崳銊╂桨閺夊尅绱濋悽銊ょ艾閺€鍓х枂编傛牞绶崳鈥祌ame
FParentPanel: TPanel;
// 瑜版挸澧犵紓标帆閸b€祌ame
FCurrentEditorFrame: TFrame;
// 闁氨鏁ょ仦鐐粹偓褏绱潏鎴濈暚閹存劕娲栫拫?
FOnEditComplete: TNotifyEvent;
// 閸掓稑编撻崥鍕閸ㄥ绱潏鎴濇珤閻ㄥ嫭鏌熷▔?
function CreateEditorFrame(PropertyType: TComplexPropertyType): TFrame;
// 閸旂姾流嘕SON閺佺増宓侀崚所椽鏉堟垵娅?
procedure LoadJsonToEditor(Editor: TFrame; Json: TJSONObject);
// 流犲海绱潏鎴濇珤娣囨繂鐡ㄩ弫鐗堝祦閸掔檻SON
procedure SaveEditorToJson(Editor: TFrame; var Json: TJSONObject);
constructor Create;
public
// 閸楁洑绶ョ拋坐楁6閺傝纭?
class function GetInstance: TControllerConfigsClass;
// 閺嶈宓乀ag閺勫墽銇氭径宥嗘絽鐏炵偞鈧囧帳编冾喚鏅棃?
procedure ShowConfigByTag(Tag: Integer);
// 鐠佸墽鐤嗛悥璺侯啇閸c劑娼伴弶?
procedure SetParentPanel(Panel: TPanel);
// 编傛牞绶径宥嗘絽鐏炵偞鈧?
procedure EditComplexProperty(PropertyType: TComplexPropertyType;
PropertyName: string; Json: TJSONObject; OnComplete: TNotifyEvent);
// 閸掓稑编撻弬所畱婢跺秵娼呯仦鐐粹偓?
function CreateComplexProperty(PropertyType: TComplexPropertyType;
PropertyName: string; OnComplete: TNotifyEvent): TJSONObject;
// 娣囨繂鐡ㄨぐ鎾冲编傛牞绶惃鍕潣閹?
procedure SaveCurrentProperty;
// 閸欐牗称疯ぐ鎾冲编傛牞绶?
procedure CancelCurrentEdit;
// 闁插﹥文佺挧鍕爱
destructor Destroy; override;
// 鐏炵偞鈧?
function GetCurrentPropertyType: TComplexPropertyType;
function GetCurrentJson: TJSONObject;
function GetCurrentPropertyName: string;
property CurrentPropertyType: TComplexPropertyType read GetCurrentPropertyType;
property CurrentJson: TJSONObject read GetCurrentJson;
property CurrentPropertyName: string read GetCurrentPropertyName;
end;
// 閸忋劌鐪拋坐楁6閻?
function GetControllerConfigs: TControllerConfigsClass;
implementation
uses
Vcl.Dialogs, System.Generics.Collections, ConfigFrameBase,
FrameBgDrawEditor, FrameVideoClipEditor, FrameVideoEditor, FrameDateTimeRangeEditor,
FrameKeyValueDictEditor, FrameUrlConfigEditor, FrameNetConfigEditor, {FrameGeoLocationEditor,}
FrameEncryptEditor;
var
// 娣囨繂鐡ㄩ幍鈧張大婂灡对よ櫣娈慒rame锁涘瞼鏁ゆ禍度℃倵编侇參器撮弨?
FrameList: TList<TFrame>;
function GetControllerConfigs: TControllerConfigsClass;
begin
Result := TControllerConfigsClass.GetInstance;
end;
{ TControllerConfigsClass }
constructor TControllerConfigsClass.Create;
begin
inherited;
FCurrentJson := nil;
FCurrentEditorFrame := nil;
end;
destructor TControllerConfigsClass.Destroy;
begin
// 濞撳懐鎮婄挧鍕爱
if Assigned(FCurrentJson) then
FCurrentJson.Free;
if Assigned(FCurrentEditorFrame) then
FCurrentEditorFrame.Free;
inherited;
end;
function TControllerConfigsClass.GetCurrentPropertyType: TComplexPropertyType;
begin
Result := FCurrentPropertyType;
end;
function TControllerConfigsClass.GetCurrentJson: TJSONObject;
begin
Result := FCurrentJson;
end;
function TControllerConfigsClass.GetCurrentPropertyName: string;
begin
Result := FCurrentPropertyName;
end;
class function TControllerConfigsClass.GetInstance: TControllerConfigsClass;
begin
if not Assigned(FInstance) then
FInstance := TControllerConfigsClass.Create;
Result := FInstance;
end;
procedure TControllerConfigsClass.SetParentPanel(Panel: TPanel);
begin
FParentPanel := Panel;
end;
procedure TControllerConfigsClass.ShowConfigByTag(Tag: Integer);
var
CPT: TComplexPropertyType;
PropertyName: string;
Json: TJSONObject;
begin
// 鑾峰彇大嶆潅灞炴€х被鍨?
CPT := TComplexPropertyType(Tag);
// 鑾峰彇灞炴€у悕称帮紙鐢ㄦ埛辑撳叆锛?
PropertyName := '';
if not InputQuery('New property', 'Enter property name', PropertyName) then
Exit;
if PropertyName = '' then
Exit;
// 创涘缓文扮殑大嶆潅灞炴€?
Json := CreateComplexProperty(CPT, PropertyName, nil);
// 是剧ず鎴愬姛娑堟伅
if Assigned(Json) then
ShowMessage('Property ' + PropertyName + ' added successfully');
end;
function TControllerConfigsClass.CreateEditorFrame(
PropertyType: TComplexPropertyType): TFrame;
begin
Result := nil;
// Create editor frame based on property type
case PropertyType of
// Priority complex properties
cptBgDraw, cptTextOnBg, cptImageOnBg, cptCaptionOnBg:
begin
Result := TFrameBgDrawEditor.Create(nil);
end;
cptVideoClip:
begin
Result := TFrameVideoClipEditor.Create(nil);
end;
cptVideo:
begin
Result := TFrameVideoEditor.Create(nil);
end;
// New complex property editors
cptDateTimeRange:
begin
Result := TFrameDateTimeRangeEditor.Create(nil);
end;
cptKeyValueDict:
begin
Result := TFrameKeyValueDictEditor.Create(nil);
end;
cptUrlConfig:
begin
Result := TFrameUrlConfigEditor.Create(nil);
end;
cptNetConfig:
begin
Result := TFrameNetConfigEditor.Create(nil);
end;
cptGeoLocation:
begin
Result := TFrameGeoLocationEditor.Create(nil);
end;
cptEncrypt:
begin
Result := TFrameEncryptEditor.Create(nil);
end;
// Other complex properties
else
begin
ShowMessage('Editor for property type ' + IntToStr(Ord(PropertyType)) + ' not implemented yet');
end;
end;
// If Frame was created, add it to the list
if Assigned(Result) and not Assigned(FrameList) then
FrameList := TList<TFrame>.Create;
if Assigned(Result) and Assigned(FrameList) then
FrameList.Add(Result);
end;
procedure TControllerConfigsClass.EditComplexProperty(PropertyType: TComplexPropertyType;
PropertyName: string; Json: TJSONObject; OnComplete: TNotifyEvent);
begin
// 娣囨繂鐡ㄨぐ鎾冲閻樿埖鈧?
FCurrentPropertyType := PropertyType;
FCurrentPropertyName := PropertyName;
// 闁插﹥文佹稊瀣閻ㄥ嚙SON鐎电钖?
if Assigned(FCurrentJson) then
FCurrentJson.Free;
// 婢跺秴鍩桱SON鐎电钖?
if Assigned(Json) then
FCurrentJson := TJSONObject(Json.Clone)
else
FCurrentJson := TJSONObject.Create;
// 闁插﹥文佹稊瀣閻ㄥ嫮绱潏鎴濇珤
if Assigned(FCurrentEditorFrame) then
begin
FCurrentEditorFrame.Free;
FCurrentEditorFrame := nil;
end;
// 閸掓稑编撻弬所畱编傛牞绶崳?
FCurrentEditorFrame := CreateEditorFrame(PropertyType);
// 鐠佸墽鐤嗛崶鐐剁殶
FOnEditComplete := OnComplete;
if Assigned(FCurrentEditorFrame) and Assigned(FParentPanel) then
begin
// 鐠佸墽鐤嗙紓标帆閸b墥arent
FCurrentEditorFrame.Parent := FParentPanel;
FCurrentEditorFrame.Align := alClient;
// 閸旂姾流囬弫鐗堝祦閸掓壆绱潏鎴濇珤
LoadJsonToEditor(FCurrentEditorFrame, FCurrentJson);
// 閺勫墽銇氱紓标帆閸?
FCurrentEditorFrame.Visible := True;
end;
end;
function TControllerConfigsClass.CreateComplexProperty(PropertyType: TComplexPropertyType;
PropertyName: string; OnComplete: TNotifyEvent): TJSONObject;
var
Json: TJSONObject;
begin
// 閸掓稑编撻弬所畱JSON鐎电钖?
Json := TJSONObject.Create;
// 閺嶈宓佺仦鐐粹偓褏琚崹瀣綖閸忓懘绮拋銈呪偓?
case PropertyType of
// 流兼ê鍘涚€圭偟骞囬惃鍕槻閺夊倸鐫橀幀?
cptBgDraw:
begin
Json.AddPair('_type', 'etBgDraw');
Json.AddPair('_id', 'etBgDraw.' + PropertyName);
Json.AddPair('background', '');
Json.AddPair('elements', TJSONArray.Create);
end;
cptTextOnBg:
begin
Json.AddPair('_type', 'etTextOnBg');
Json.AddPair('_id', 'etTextOnBg.' + PropertyName);
Json.AddPair('text', '閺傚洦婀伴崘鍛啇');
Json.AddPair('x', '0');
Json.AddPair('y', '0');
Json.AddPair('font', TJSONObject.Create);
end;
cptImageOnBg:
begin
Json.AddPair('_type', 'etImageOnBg');
Json.AddPair('_id', 'etImageOnBg.' + PropertyName);
Json.AddPair('image', '');
Json.AddPair('x', '0');
Json.AddPair('y', '0');
Json.AddPair('scale', '1.0');
end;
cptCaptionOnBg:
begin
Json.AddPair('_type', 'etCaptionOnBg');
Json.AddPair('_id', 'etCaptionOnBg.' + PropertyName);
Json.AddPair('text', '鐎涙绠烽弬图ㄦ拱');
Json.AddPair('x', '0');
Json.AddPair('y', '0');
Json.AddPair('font', TJSONObject.Create);
Json.AddPair('duration', '5.0');
end;
cptVideoClip:
begin
Json.AddPair('_type', 'etVideoClip');
Json.AddPair('_id', 'etVideoClip.' + PropertyName);
Json.AddPair('background', '');
Json.AddPair('duration', '10.0');
Json.AddPair('fps', '30');
Json.AddPair('audio', '');
Json.AddPair('captions', TJSONArray.Create);
end;
cptVideo:
begin
Json.AddPair('_type', 'etVideo');
Json.AddPair('_id', 'etVideo.' + PropertyName);
Json.AddPair('cover', '');
Json.AddPair('ending', '');
Json.AddPair('bg_directory', '');
Json.AddPair('audio_directory', '');
Json.AddPair('subtitle_file', '');
Json.AddPair('media_settings', TJSONObject.Create);
Json.AddPair('clips', TJSONArray.Create);
end;
// 閺傛澘顤冮惃鍕槻閺夊倸鐫橀幀?
cptDateTimeRange:
begin
Json.AddPair('_type', 'etDateTimeRange');
Json.AddPair('_id', 'etDateTimeRange.' + PropertyName);
Json.AddPair('name', PropertyName);
Json.AddPair('description', '');
Json.AddPair('start_date', FormatDateTime('yyyy-mm-dd"T"hh:nn:ss', Now));
Json.AddPair('end_date', FormatDateTime('yyyy-mm-dd"T"hh:nn:ss', Now + 1));
Json.AddPair('use_time', TJSONBool.Create(True));
Json.AddPair('duration_seconds', TJSONNumber.Create(86400)); // 姒涙顓婢?
Json.AddPair('repeat', TJSONBool.Create(False));
Json.AddPair('repeat_type', TJSONNumber.Create(0)); // 濮e繐銇?
Json.AddPair('repeat_interval', TJSONNumber.Create(1));
Json.AddPair('enable_notification', TJSONBool.Create(False));
Json.AddPair('notify_before', TJSONNumber.Create(15));
Json.AddPair('notify_unit', '分钟');
end;
cptKeyValueDict:
begin
Json.AddPair('_type', 'etKeyValueDict');
Json.AddPair('_id', 'etKeyValueDict.' + PropertyName);
Json.AddPair('name', PropertyName);
Json.AddPair('description', '');
Json.AddPair('enable_type_check', TJSONBool.Create(False));
Json.AddPair('default_type', '字符串');
Json.AddPair('items', TJSONArray.Create);
end;
cptUrlConfig:
begin
Json.AddPair('_type', 'etUrlConfig');
Json.AddPair('_id', 'etUrlConfig.' + PropertyName);
Json.AddPair('name', PropertyName);
Json.AddPair('description', '');
Json.AddPair('base_url', '');
Json.AddPair('protocol', 'https');
Json.AddPair('use_ssl', TJSONBool.Create(True));
Json.AddPair('port', '443');
Json.AddPair('timeout', '30000');
Json.AddPair('enable_proxy', TJSONBool.Create(False));
Json.AddPair('headers', TJSONArray.Create);
Json.AddPair('parameters', TJSONArray.Create);
end;
cptNetConfig:
begin
Json.AddPair('_type', 'etNetConfig');
Json.AddPair('_id', 'etNetConfig.' + PropertyName);
Json.AddPair('name', PropertyName);
Json.AddPair('description', '');
Json.AddPair('dhcp', TJSONBool.Create(True));
Json.AddPair('ip_address', '');
Json.AddPair('subnet_mask', '');
Json.AddPair('gateway', '');
Json.AddPair('mac_address', '');
Json.AddPair('ipv6', TJSONBool.Create(False));
Json.AddPair('network_name', '');
Json.AddPair('network_type', TJSONNumber.Create(0));
Json.AddPair('mtu', '1500');
Json.AddPair('auto_connect', TJSONBool.Create(True));
Json.AddPair('enable_proxy', TJSONBool.Create(False));
Json.AddPair('custom_dns', TJSONBool.Create(False));
end;
cptGeoLocation:
begin
Json.AddPair('_type', 'etGeoLocation');
Json.AddPair('_id', 'etGeoLocation.' + PropertyName);
Json.AddPair('name', PropertyName);
Json.AddPair('description', '');
Json.AddPair('latitude', '39.9042'); // 姒涙顓婚崠妞惧惈閸ф劖鐖?
Json.AddPair('longitude', '116.4074');
Json.AddPair('altitude', '0');
Json.AddPair('accuracy', '10');
Json.AddPair('location_name', '');
Json.AddPair('location_type', '其他');
Json.AddPair('is_favorite', TJSONBool.Create(False));
Json.AddPair('formatted_address', '');
Json.AddPair('show_on_map', TJSONBool.Create(True));
Json.AddPair('map_provider', '閻ф儳瀹抽崷鏉挎禈');
Json.AddPair('default_zoom', TJSONNumber.Create(12));
end;
cptEncrypt:
begin
Json.AddPair('_type', 'etEncrypt');
Json.AddPair('_id', 'etEncrypt.' + PropertyName);
Json.AddPair('name', PropertyName);
Json.AddPair('description', '');
Json.AddPair('encryption_type', '对称加密');
Json.AddPair('algorithm', 'AES');
Json.AddPair('key_size', '256 位');
Json.AddPair('mode', 'CBC');
Json.AddPair('padding', 'PKCS7');
Json.AddPair('use_iv', TJSONBool.Create(True));
Json.AddPair('default_iv', TJSONBool.Create(True));
Json.AddPair('key_format', 'Base64');
Json.AddPair('password', '');
Json.AddPair('salt', '');
Json.AddPair('iterations', TJSONNumber.Create(10000));
Json.AddPair('key_derivation', 'PBKDF2');
end;
// 閸忔湹绮径宥嗘絽鐏炵偞鈧?
else
begin
Json.AddPair('_type', 'etObject');
Json.AddPair('_id', 'etObject.' + PropertyName);
Json.AddPair('name', PropertyName);
end;
end;
// 编傛牞绶仦鐐粹偓?
EditComplexProperty(PropertyType, PropertyName, Json, OnComplete);
Result := Json;
end;
procedure TControllerConfigsClass.LoadJsonToEditor(Editor: TFrame; Json: TJSONObject);
begin
// 瀹告煡注﹂幋鎰畱编傛牞绶崳?
if Editor is TBaseConfigFrame then
begin
TBaseConfigFrame(Editor).SetJSONObject(Json);
TBaseConfigFrame(Editor).LoadFromJSON;
end
else
begin
ShowMessage('未知的编辑器类型,无法加载JSON数据');
end;
end;
procedure TControllerConfigsClass.SaveEditorToJson(Editor: TFrame; var Json: TJSONObject);
begin
// 瀹告煡注﹂幋鎰畱编傛牞绶崳?
if Editor is TBaseConfigFrame then
begin
// 閸忓牏'娣囨患SON鐎电钖勭€涙ê婀?
if not Assigned(Json) then
Json := TJSONObject.Create;
// 鐏忓棛绱潏鎴濇珤閺佺増宓佹穱婵嗙摠閸掔檻SON
TBaseConfigFrame(Editor).SaveToJSON;
end
else
begin
ShowMessage('未知的编辑器类型,无法保存JSON数据');
end;
end;
procedure TControllerConfigsClass.SaveCurrentProperty;
begin
// 娣囨繂鐡ㄨぐ鎾冲鐏炵偞鈧?
if Assigned(FCurrentEditorFrame) and Assigned(FCurrentJson) then
begin
SaveEditorToJson(FCurrentEditorFrame, FCurrentJson);
// 鐠嬪啰鏁ら崶鐐剁殶
if Assigned(FOnEditComplete) then
FOnEditComplete(Self);
end;
end;
procedure TControllerConfigsClass.CancelCurrentEdit;
begin
// 閸欐牗称疯ぐ鎾冲编傛牞绶?
if Assigned(FCurrentEditorFrame) then
begin
FCurrentEditorFrame.Visible := False;
// 婵″倹鐏夐棁鈧憰渚婄礉閸欘垯京掗崷銊ㄧ箹闁插矂器撮弨鎹愮カ濠?
end;
end;
initialization
finalization
// 濞撳懐鎮婇崡鏇氱伐鐎圭偘绶?
if Assigned(TControllerConfigsClass.FInstance) then
TControllerConfigsClass.FInstance.Free;
// 濞撳懐鎮婇幍鈧張大婂灡对よ櫣娈慒rame
if Assigned(FrameList) then
begin
for var Frame in FrameList do
Frame.Free;
FrameList.Free;
end;
end.