-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameBgDrawEditorFMX.pas
More file actions
316 lines (278 loc) · 7.86 KB
/
FrameBgDrawEditorFMX.pas
File metadata and controls
316 lines (278 loc) · 7.86 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
unit FrameBgDrawEditorFMX;
{*******************************************************************************
背景绘制配置编辑器 Frame (FMX)
- 支持背景设置(图片/颜色/渐变)
- 支持元素列表管理(文字、图片、字幕等)
*******************************************************************************}
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
System.JSON,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
FMX.Edit, FMX.ListBox, FMX.Layouts, FMX.Controls.Presentation, FMX.EditBox,
FMX.SpinBox, FMX.Colors, FMX.Memo, FMX.ScrollBox,
UtilsTypesFMX, ConfigFrameBaseFMX;
type
TFrameBgDrawEditorFMX = class(TConfigFrameBaseFMX)
layMain: TLayout;
grpBackground: TGroupBox;
layBgType: TLayout;
lblBgType: TLabel;
cboBgType: TComboBox;
layBgColor: TLayout;
lblBgColor: TLabel;
cboBgColor: TColorComboBox;
layBgImage: TLayout;
lblBgImage: TLabel;
edtBgImage: TEdit;
btnBrowseBg: TButton;
grpElements: TGroupBox;
layElementList: TLayout;
lstElements: TListBox;
layElementButtons: TLayout;
btnAddElement: TButton;
btnEditElement: TButton;
btnRemoveElement: TButton;
btnMoveUp: TButton;
btnMoveDown: TButton;
procedure cboBgTypeChange(Sender: TObject);
procedure btnBrowseBgClick(Sender: TObject);
procedure btnAddElementClick(Sender: TObject);
procedure btnEditElementClick(Sender: TObject);
procedure btnRemoveElementClick(Sender: TObject);
procedure btnMoveUpClick(Sender: TObject);
procedure btnMoveDownClick(Sender: TObject);
procedure edtChange(Sender: TObject);
private
FElements: TJSONArray;
procedure UpdateBgControls;
procedure RefreshElementList;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure LoadFromJSON(const AJSON: TJSONObject); override;
function SaveToJSON: TJSONObject; override;
end;
implementation
{$R *.fmx}
{ TFrameBgDrawEditorFMX }
constructor TFrameBgDrawEditorFMX.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FElements := TJSONArray.Create;
// 初始化背景类型
cboBgType.Items.Clear;
cboBgType.Items.Add('图片');
cboBgType.Items.Add('纯色');
cboBgType.Items.Add('渐变');
cboBgType.ItemIndex := 0;
UpdateBgControls;
end;
destructor TFrameBgDrawEditorFMX.Destroy;
begin
FElements.Free;
inherited Destroy;
end;
procedure TFrameBgDrawEditorFMX.LoadFromJSON(const AJSON: TJSONObject);
var
BgValue: TJSONValue;
BgStr: string;
ElementsArr: TJSONArray;
begin
if AJSON = nil then Exit;
BeginUpdate;
try
// 背景
BgValue := AJSON.GetValue('background');
if BgValue <> nil then
begin
BgStr := BgValue.Value;
if BgStr.StartsWith('#') then
begin
// 颜色
cboBgType.ItemIndex := 1;
cboBgColor.Color := StringToAlphaColor(BgStr);
end
else if BgStr.Contains('gradient') then
begin
// 渐变
cboBgType.ItemIndex := 2;
end
else
begin
// 图片路径
cboBgType.ItemIndex := 0;
edtBgImage.Text := BgStr;
end;
end;
UpdateBgControls;
// 元素列表
FElements.Free;
if AJSON.TryGetValue<TJSONArray>('elements', ElementsArr) then
FElements := TJSONArray(ElementsArr.Clone)
else
FElements := TJSONArray.Create;
RefreshElementList;
finally
EndUpdate;
end;
end;
function TFrameBgDrawEditorFMX.SaveToJSON: TJSONObject;
var
BgStr: string;
begin
Result := TJSONObject.Create;
Result.AddPair('_type', 'etBgDraw');
// 背景
case cboBgType.ItemIndex of
0: // 图片
BgStr := edtBgImage.Text;
1: // 纯色
BgStr := AlphaColorToString(cboBgColor.Color);
2: // 渐变
BgStr := 'gradient:linear';
else
BgStr := '';
end;
Result.AddPair('background', BgStr);
// 元素列表
Result.AddPair('elements', TJSONArray(FElements.Clone));
end;
procedure TFrameBgDrawEditorFMX.UpdateBgControls;
begin
// 根据背景类型显示/隐藏控件
layBgImage.Visible := (cboBgType.ItemIndex = 0);
layBgColor.Visible := (cboBgType.ItemIndex >= 1);
end;
procedure TFrameBgDrawEditorFMX.RefreshElementList;
var
I: Integer;
Elem: TJSONObject;
ElemType, ElemText: string;
begin
lstElements.Clear;
for I := 0 to FElements.Count - 1 do
begin
if FElements.Items[I] is TJSONObject then
begin
Elem := TJSONObject(FElements.Items[I]);
ElemType := GetJSONString(Elem, 'type', 'unknown');
ElemText := GetJSONString(Elem, 'text', GetJSONString(Elem, 'src', ''));
lstElements.Items.Add(Format('[%s] %s', [ElemType, ElemText]));
end;
end;
end;
procedure TFrameBgDrawEditorFMX.cboBgTypeChange(Sender: TObject);
begin
UpdateBgControls;
DoModified;
end;
procedure TFrameBgDrawEditorFMX.btnBrowseBgClick(Sender: TObject);
var
Dlg: TOpenDialog;
begin
Dlg := TOpenDialog.Create(nil);
try
Dlg.Filter := '图片文件 (*.png;*.jpg;*.jpeg;*.bmp)|*.png;*.jpg;*.jpeg;*.bmp|所有文件 (*.*)|*.*';
if edtBgImage.Text <> '' then
Dlg.FileName := edtBgImage.Text;
if Dlg.Execute then
begin
edtBgImage.Text := Dlg.FileName;
DoModified;
end;
finally
Dlg.Free;
end;
end;
procedure TFrameBgDrawEditorFMX.btnAddElementClick(Sender: TObject);
var
ElemType: string;
NewElem: TJSONObject;
begin
// 简单实现: 添加文字元素
ElemType := InputBox('添加元素', '请输入元素类型 (text/image/caption):', 'text');
if ElemType = '' then Exit;
NewElem := TJSONObject.Create;
NewElem.AddPair('type', ElemType);
if ElemType = 'text' then
NewElem.AddPair('text', '新文字')
else if ElemType = 'image' then
NewElem.AddPair('src', '')
else if ElemType = 'caption' then
NewElem.AddPair('text', '字幕文字');
NewElem.AddPair('x', TJSONNumber.Create(0));
NewElem.AddPair('y', TJSONNumber.Create(0));
FElements.AddElement(NewElem);
RefreshElementList;
DoModified;
end;
procedure TFrameBgDrawEditorFMX.btnEditElementClick(Sender: TObject);
var
Idx: Integer;
Elem: TJSONObject;
CurrentText, NewText: string;
begin
Idx := lstElements.ItemIndex;
if (Idx < 0) or (Idx >= FElements.Count) then
begin
ShowMessage('请先选择一个元素');
Exit;
end;
if FElements.Items[Idx] is TJSONObject then
begin
Elem := TJSONObject(FElements.Items[Idx]);
CurrentText := GetJSONString(Elem, 'text', GetJSONString(Elem, 'src', ''));
NewText := InputBox('编辑元素', '请输入新内容:', CurrentText);
if NewText <> CurrentText then
begin
if Elem.GetValue('text') <> nil then
SetJSONString(Elem, 'text', NewText)
else if Elem.GetValue('src') <> nil then
SetJSONString(Elem, 'src', NewText);
RefreshElementList;
DoModified;
end;
end;
end;
procedure TFrameBgDrawEditorFMX.btnRemoveElementClick(Sender: TObject);
var
Idx: Integer;
begin
Idx := lstElements.ItemIndex;
if (Idx < 0) or (Idx >= FElements.Count) then
begin
ShowMessage('请先选择一个元素');
Exit;
end;
if MessageDlg('确定要删除此元素吗?', TMsgDlgType.mtConfirmation,
[TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0) = mrYes then
begin
FElements.Remove(Idx);
RefreshElementList;
DoModified;
end;
end;
procedure TFrameBgDrawEditorFMX.btnMoveUpClick(Sender: TObject);
var
Idx: Integer;
Temp: TJSONValue;
begin
Idx := lstElements.ItemIndex;
if Idx <= 0 then Exit;
// 交换元素
Temp := FElements.Items[Idx];
FElements.Remove(Idx);
// 注意: TJSONArray 没有 Insert 方法,需要重建
// 简化处理:暂不支持移动
ShowMessage('移动功能待完善');
end;
procedure TFrameBgDrawEditorFMX.btnMoveDownClick(Sender: TObject);
begin
ShowMessage('移动功能待完善');
end;
procedure TFrameBgDrawEditorFMX.edtChange(Sender: TObject);
begin
DoModified;
end;
end.