/
RectGrid.pas
415 lines (342 loc) · 9.59 KB
/
RectGrid.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
unit RectGrid;
{$MODE Delphi}
interface
uses SysUtils, Classes, LCLIntf, LCLType, LMessages, Contnrs, xml;
type
TSliceType = (stNone, stHorizontal, stVertical);
TRectGrid = class;
TRectGridList = class
private
FList: TObjectList;
function GetItem(Index: Integer): TRectGrid;
function GetCount: Integer;
protected
procedure Remove(rg: TRectGrid);
procedure Insert(Index: Integer; rg: TRectGrid);
procedure Add(rg: TRectGrid);
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function IndexOf(rg: TRectGrid): Integer;
property Items[Index: Integer]: TRectGrid read GetItem; default;
property Count: Integer read GetCount;
end;
TRectGrid = class
private
FRect: TRect;
FKids: TRectGridList;
FName: String;
FSliceType: TSliceType;
FParent: TRectGrid;
FSingleColor: DWORD;
FIsSingleColor: Boolean;
FIsBackground: Boolean;
FAttributes: TStringList;
function GetAttribute(const AttrName: String): String;
procedure SetAttribute(const AttrName: String; const Value: String);
function IsSliced: Boolean;
function NewKid(const rt: TRect): TRectGrid;
public
constructor Create(const rt: TRect);
destructor Destroy; override;
function RectFromPos(const Pt: TPoint): TRectGrid;
procedure RectsFromX(const X: Integer; list: TObjectList);
procedure RectsFromY(const Y: Integer; list: TObjectList);
procedure SliceHorizontal(const Y: Integer);
procedure SliceVertical(const X: Integer);
procedure GroupKids(FromIndex, ToIndex: Integer);
procedure LoadFromXML(me: TXMLNode);
procedure SaveToXML(parent: TXMLNode);
property Parent: TRectGrid read FParent;
property Rect: TRect read FRect;
property Kids: TRectGridList read FKids;
property Sliced: Boolean read IsSliced;
property SliceType: TSliceType read FSliceType;
property Name: String read FName write FName;
property IsSingleColor: Boolean read FIsSingleColor write FIsSingleColor;
property SingleColor: DWORD read FSingleColor write FSingleColor;
property IsBackground: Boolean read FIsBackground write FIsBackground;
property Attributes[const AttrName: String]: String
read GetAttribute write SetAttribute;
end;
implementation
uses FileUtils;
constructor TRectGridList.Create;
begin
FList := TObjectList.Create;
end;
destructor TRectGridList.Destroy;
begin
FList.Free;
end;
function TRectGridList.GetCount: Integer;
begin
Result := FList.Count;
end;
function TRectGridList.GetItem(Index: Integer): TRectGrid;
begin
Result := TRectGrid(FList.Items[Index]);
end;
function TRectGridList.IndexOf(rg: TRectGrid): Integer;
begin
Result := FList.IndexOf(rg);
end;
procedure TRectGridList.Insert(Index: Integer; rg: TRectGrid);
begin
FList.Insert(Index, rg);
end;
procedure TRectGridList.Add(rg: TRectGrid);
begin
FList.Add(rg);
end;
procedure TRectGridList.Remove(rg: TRectGrid);
begin
FList.Remove(rg);
end;
procedure TRectGridList.Clear;
begin
FList.Clear;
end;
constructor TRectGrid.Create(const rt: TRect);
begin
Self.FRect := rt;
FKids := TRectGridList.Create;
FSliceType := stNone;
FParent := nil;
FAttributes := TStringList.Create;
end;
destructor TRectGrid.Destroy;
begin
if FParent <> nil then
FParent.FKids.Remove(Self);
FKids.Free;
FAttributes.Free;
inherited Destroy;
end;
function TRectGrid.GetAttribute(const AttrName: String): String;
begin
Result := FAttributes.Values[AttrName];
end;
procedure TRectGrid.SetAttribute(const AttrName: String; const Value: String);
begin
FAttributes.Values[AttrName] := Value;
end;
function TRectGrid.IsSliced: Boolean;
begin
Result := FKids.Count >= 2;
end;
function TRectGrid.RectFromPos(const Pt: TPoint): TRectGrid;
var
i: Integer;
begin
Result := nil;
if PtInRect(FRect, pt) then
begin
if Sliced then
begin
for i := 0 to FKids.Count - 1 do
begin
Result := TRectGrid(FKids[i]).RectFromPos(pt);
if Result <> nil then
Break;
end;
end
else
begin
Result := Self;
end;
end;
end;
procedure TRectGrid.RectsFromX(const X: Integer; list: TObjectList);
var
i: Integer;
begin
if (FRect.Left <= X) and (X <= FRect.Right) then
if (not Sliced) then
list.Add(Self)
else
begin
for i := 0 to FKids.Count - 1 do
TRectGrid(FKids[i]).RectsFromX(X, list);
end;
end;
procedure TRectGrid.RectsFromY(const Y: Integer; list: TObjectList);
var
i: Integer;
begin
if (FRect.Top <= Y) and (Y <= FRect.Bottom) then
if (not Sliced) then
list.Add(Self)
else
begin
for i := 0 to FKids.Count - 1 do
TRectGrid(FKids[i]).RectsFromY(Y, list);
end;
end;
function TRectGrid.NewKid(const rt: TRect): TRectGrid;
begin
Result := TRectGrid.Create(rt);
Result.FParent := Self;
end;
procedure TRectGrid.SliceHorizontal(const Y: Integer);
var
parentIndex: Integer;
rtA, rtB: TRect;
begin
if (FRect.Top > Y) or (Y > FRect.Bottom) then
raise Exception.Create('Invalid Y coordinate');
if (Sliced) then
raise Exception.Create('Already sliced');
rtA := Classes.Rect(FRect.Left, FRect.Top, FRect.Right, Y);
rtB := Classes.Rect(FRect.Left, Y, FRect.Right, FRect.Bottom);
if (FParent <> nil) and (FParent.FSliceType = stHorizontal) then
begin
parentIndex := FParent.FKids.IndexOf(Self);
if parentIndex = -1 then
raise Exception.Create('Kid was not found in parent list');
FParent.FKids.Insert(parentIndex, FParent.NewKid(rtA));
FRect := rtB;
end
else
begin
FKids.Add(NewKid(rtA));
FKids.Add(NewKid(rtB));
FSliceType := stHorizontal;
end;
end;
procedure TRectGrid.SliceVertical(const X: Integer);
var
parentIndex: Integer;
rtA, rtB: TRect;
begin
if (FRect.Left > X) or (X > FRect.Right) then
raise Exception.Create('Invalid X coordinate');
if (Sliced) then
raise Exception.Create('Already sliced');
rtA := Classes.Rect(FRect.Left, FRect.Top, X, FRect.Bottom);
rtB := Classes.Rect(X, FRect.Top, FRect.Right, FRect.Bottom);
if (FParent <> nil) and (FParent.FSliceType = stVertical) then
begin
parentIndex := FParent.FKids.IndexOf(Self);
if parentIndex = -1 then
raise Exception.Create('Kid was not found in parent list');
FParent.FKids.Insert(parentIndex, FParent.NewKid(rtA));
FRect := rtB;
end
else
begin
FKids.Add(NewKid(rtA));
FKids.Add(NewKid(rtB));
FSliceType := stVertical;
end;
end;
procedure TRectGrid.LoadFromXML(me: TXMLNode);
const
strSlice: array [TSliceType] of String = ('stNone', 'stHorizontal', 'stVertical');
var
i: Integer;
rg: TRectGrid;
tmp: String;
enum: TXMLNodeEnum;
strName, strValue: String;
begin
Self.Name := me.Attributes['Name'];
tmp := me.Attributes['SliceType'];
if (tmp = 'stNone') then
FSliceType := stNone
else if (tmp = 'stHorizontal') then
FSliceType := stHorizontal
else if (tmp = 'stVertical') then
FSliceType := stVertical
else
raise Exception.Create('Invalid slice type ' + tmp + ' was specified');
FRect.Left := StrToInt(me.Attributes['Left']);
FRect.Top := StrToInt(me.Attributes['Top']);
FRect.Right := StrToInt(me.Attributes['Right']);
FRect.Bottom := StrToInt(me.Attributes['Bottom']);
FIsBackground := me.Attributes['IsBackground'] = 'true';
tmp := me.Attributes['SingleColor'];
if tmp <> '' then
begin
FSingleColor := StrToInt(tmp);
FIsSingleColor := True;
end
else
FIsSingleColor := False;
FKids.Clear;
enum := me.GetNodesByName('attr');
for i := 0 to enum.Count - 1 do
begin
strName := enum[i].Attributes['name'];
strValue := enum[i].Attributes['value'];
Self.Attributes[strName] := strValue;
end;
enum.Free;
enum := me.GetNodesByName('cell');
for i := 0 to enum.Count - 1 do
begin
rg := NewKid(Classes.Rect(0, 0, 0, 0));
rg.LoadFromXML(enum[i]);
FKids.Add(rg);
end;
enum.Free;
end;
procedure TRectGrid.SaveToXML(parent: TXMLNode);
const
strSlice: array [TSliceType] of String = ('stNone', 'stHorizontal', 'stVertical');
var
i: Integer;
me, attrNode: TXMLNode;
strName, strValue: String;
begin
me := parent.AddChild;
me.Name := 'cell';
me.Attributes['Name'] := FName;
me.Attributes['SliceType'] := strSlice[FSliceType];
me.Attributes['Left'] := IntToStr(FRect.Left);
me.Attributes['Top'] := IntToStr(FRect.Top);
me.Attributes['Right'] := IntToStr(FRect.Right);
me.Attributes['Bottom'] := IntToStr(FRect.Bottom);
if (IsSingleColor) then
me.Attributes['SingleColor'] := IntToStr(SingleColor);
if (IsBackground) then
me.Attributes['IsBackground'] := 'true';
for i := 0 to FAttributes.Count - 1 do
begin
strName := FAttributes.Names[i];
strValue := FAttributes.Values[strName];
if (strName <> '') and (strValue <> '') then
begin
attrNode := me.AddChild;
attrNode.Name := 'attr';
attrNode.Attributes['name'] := strName;
attrNode.Attributes['value'] := strValue;
end;
end;
for i := 0 to FKids.Count - 1 do
FKids[i].SaveToXML(me);
end;
procedure TRectGrid.GroupKids(FromIndex, ToIndex: Integer);
var
rt: TRect;
newParent, rg: TRectGrid;
i: Integer;
begin
rt.Left := FKids[FromIndex].FRect.Left;
rt.Top := FKids[FromIndex].FRect.Top;
rt.Right := FKids[ToIndex].FRect.Right;
rt.Bottom := FKids[ToIndex].FRect.Bottom;
newParent := TRectGrid.Create(rt);
newParent.FSliceType := Self.FSliceType;
newParent.FParent := Self;
for i := FromIndex to ToIndex do
begin
rg := FKids[FromIndex]; { because we extract FromIndex }
FKids.FList.Extract(rg);
newParent.FKids.Add(rg);
rg.FParent := newParent;
end;
FKids.Insert(FromIndex, newParent);
end;
end.