-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathufileworker.pas
312 lines (292 loc) · 9.06 KB
/
ufileworker.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
unit UFileWorker;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, UFigures, UToolParams, UTransform, typinfo, fpjson,
jsonparser, Graphics, math, UHistory;
const
FILE_HEADER = 'VECDRAW';
VERSION = 1;
NEW_FILE_NAME = 'New Image.vd';
var
CurrentFile: string;
function GetProps(AObject: TObject; out APropList: PPropList): integer;
function GetFiguresJSON(SelectedOnly: boolean = false): TJSONObject;
function GetFiguresFromJSON(Root: TJSONObject): TFigureList;
function GetFiguresBitmap(SelectedOnly: boolean = false): TBitmap;
function SaveFile(FileName: string): integer;
function LoadFile(FileName: string): integer;
implementation
function GetProps(AObject: TObject; out APropList: PPropList): integer;
var
PropCount: Integer;
begin
PropCount := GetPropList(AObject.ClassInfo, tkAny, Nil);
GetMem(APropList, PropCount * SizeOf(PPropInfo));
GetPropList(AObject.ClassInfo, tkAny, APropList);
Result := PropCount;
end;
function GetFiguresJSON(SelectedOnly: boolean): TJSONObject;
var
CurrentFigure: TFigure;
FigureJSON, CurrPropJSON: TJSONObject;
FigureProps, PropProps: PPropList;
i, j, PropCount, PropPropCount: integer;
FiguresJSON, VertexesJSON, CurrVertexJSON: TJSONArray;
PropInfo, PropPropInfo: TPropInfo;
ParamProp: TToolParam;
v: TDoublePoint;
begin
FiguresJSON := TJSONArray.Create;
for CurrentFigure in CanvasItems do
if CurrentFigure.Selected or (not SelectedOnly) then
begin
FigureJSON := TJSONObject.Create;
FigureJSON.Add('type', CurrentFigure.ClassName);
PropCount := GetProps(CurrentFigure, FigureProps);
for i := 0 to PropCount-1 do
begin
PropInfo := FigureProps^[i]^;
case PropInfo.PropType^.Kind of
tkInt64, tkInteger:
begin
FigureJSON.Add(PropInfo.Name,
GetInt64Prop(CurrentFigure, PropInfo.Name));
end;
tkFloat:
begin
FigureJSON.Add(PropInfo.Name,
GetFloatProp(CurrentFigure, PropInfo.Name));
end;
else
begin
ParamProp := GetObjectProp(CurrentFigure, PropInfo.Name) as TToolParam;
CurrPropJSON := TJSONObject.Create;
CurrPropJSON.Add('class', ParamProp.ClassName);
CurrPropJSON.Add('Name', ParamProp.Name);
PropPropCount := GetProps(ParamProp, PropProps);
for j := 0 to PropPropCount-1 do
begin
PropPropInfo := PropProps^[j]^;
case PropPropInfo.PropType^.Kind of
tkInt64, tkInteger:
begin
CurrPropJSON.Add(PropPropInfo.Name,
GetInt64Prop(ParamProp, PropPropInfo.Name));
end;
tkEnumeration:
begin
CurrPropJSON.Add(PropPropInfo.Name,
GetEnumProp(ParamProp, PropPropInfo.Name));
end;
tkString:
begin
CurrPropJSON.Add(PropPropInfo.Name,
GetStrProp(ParamProp, PropPropInfo.Name));
end;
end;
end;
FigureJSON.Add(PropInfo.Name, CurrPropJSON.Clone);
FreeAndNil(CurrPropJSON);
end;
end;
end;
VertexesJSON := TJSONArray.Create;
for v in CurrentFigure.Vertexes do
begin
CurrVertexJSON := TJSONArray.Create;
CurrVertexJSON.Add(v.x);
CurrVertexJSON.Add(v.y);
VertexesJSON.Add(TJSONData(CurrVertexJSON).Clone);
FreeAndNil(CurrVertexJSON);
end;
FigureJSON.Add('vertexes', TJSONData(VertexesJSON).Clone);
FreeAndNil(VertexesJSON);
FiguresJSON.Add(FigureJSON.Clone);
FreeAndNil(FigureJSON);
end;
Result := TJSONObject.Create;
Result.Add(FILE_HEADER, FiguresJSON.Clone);
Result.Add('version', VERSION);
FreeAndNil(FiguresJSON);
end;
function GetFiguresFromJSON(Root: TJSONObject): TFigureList;
var
CurrFigureJSON, PropJSON: TJSONObject;
FiguresJSON, FigureVertexesJSON: TJSONArray;
i, j, FileVersion: integer;
k, l: TJSONEnum;
FigureClassName, PropClassName: string;
FigureClass, PropClass: TPersistentClass;
CurrFigure: TFigure;
CurrProp: TToolParam;
FigureVertexes: TDPointList;
begin
SetLength(Result, 0);
FileVersion := Root.Get('version', -1);
if FileVersion = -1 then
FileVersion := 1; //assuming it's the first version
FiguresJSON := TJSONArray(Root.GetPath(FILE_HEADER));
for i := 0 to FiguresJSON.Count-1 do
begin
CurrFigureJSON := FiguresJSON.Objects[i];
FigureClassName := CurrFigureJSON.GetPath('type').AsString;
FigureClass := FindClass(FigureClassName);
CurrFigure := FigureClass.Create as TFigure;
FigureVertexesJSON := CurrFigureJSON.GetPath('vertexes') as TJSONArray;
for j := 0 to FigureVertexesJSON.Count-1 do
begin
SetLength(FigureVertexes, Length(FigureVertexes)+1);
FigureVertexes[High(FigureVertexes)] := DoublePoint(
FigureVertexesJSON.Arrays[j].Floats[0],
FigureVertexesJSON.Arrays[j].Floats[1]);
end;
CurrFigure.Vertexes := FigureVertexes;
SetLength(FigureVertexes, 0);
for k in CurrFigureJSON do
begin
if (k.Key = 'class') or (k.Key = 'vertexes') then
Continue;
case k.Value.JSONType of
jtNumber:
begin
SetFloatProp(CurrFigure, k.Key, k.Value.AsFloat);
end;
jtObject:
begin
PropJSON := k.Value as TJSONObject;
PropClassName := PropJSON.Get('class');
PropClass := FindClass(PropClassName);
CurrProp := PropClass.Create as TToolParam;
for l in PropJSON do
begin
case l.Value.JSONType of
jtString:
begin
if l.Key = 'Name' then
CurrProp.Name := l.Value.AsString
else
if l.Key = 'class' then
Continue
else
begin
SetEnumProp(CurrProp, l.Key, l.Value.AsString);
end;
end;
jtNumber:
begin
SetInt64Prop(CurrProp, l.Key, l.Value.AsInteger);
end;
end;
end;
SetObjectProp(CurrFigure, k.Key, CurrProp);
end;
end;
end;
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := CurrFigure;
end;
end;
function GetFiguresBitmap(SelectedOnly: boolean): TBitmap;
var
i: TFigure;
TL, BR: TDoublePoint;
CanvasTL, CanvasBR: TPoint;
FirstFigure: boolean;
FigureLineWidth: double;
begin
Result := TBitmap.Create;
FirstFigure := true;
TL := DoublePoint(0,0);
for i in CanvasItems do
if i.Selected or (not SelectedOnly) then
begin
if FirstFigure then
begin
FigureLineWidth := (i as TDrawableFigure).LineWidth.Value / 2;
TL.x := i.TopLeftBorder.x - FigureLineWidth;
TL.y := i.TopLeftBorder.y - FigureLineWidth;
BR.x := i.BottomRightBorder.x + FigureLineWidth;
BR.y := i.BottomRightBorder.y + FigureLineWidth;
FirstFigure := false;
end
else
begin
FigureLineWidth := (i as TDrawableFigure).LineWidth.Value / 2;
TL.x := min(TL.x, i.TopLeftBorder.x - FigureLineWidth);
TL.y := min(TL.y, i.TopLeftBorder.y - FigureLineWidth);
BR.x := max(BR.x, i.BottomRightBorder.x + FigureLineWidth);
BR.y := max(BR.y, i.BottomRightBorder.y + FigureLineWidth);
end;
end;
SetScale(1);
if SelectedOnly then
CanvasOffset := GetSelectionTopLeft
else
CanvasOffset := TL;
CanvasOffset.x := CanvasOffset.x - PADDING;
CanvasOffset.y := CanvasOffset.y - PADDING;
CanvasTL := WorldToCanvas(TL);
CanvasBR := WorldToCanvas(BR);
with Result do
begin
Width := CanvasBR.x - CanvasTL.x + 2*PADDING;
Height := CanvasBR.y - CanvasTL.y + 2*PADDING;
Canvas.Brush.Style := bsSolid;
Canvas.Brush.Color := clWhite;
Canvas.Pen.Style := psClear;
Canvas.Rectangle(-1, -1, Width+1, Height+1);
end;
for i in CanvasItems do
if i.Selected or (not SelectedOnly) then
i.Draw(Result.Canvas);
CanvasOffset := History.GetCurrentState.Offset;
SetScale(History.GetCurrentState.CanvasScale);
end;
function SaveFile(FileName: string): integer;
var
f: text;
Root: TJSONObject;
begin
Result := 0;
try
Root := GetFiguresJSON;
Root.CompressedJSON := true;
AssignFile(f, FileName);
Rewrite(f);
System.Write(f, Root.AsJSON);
FreeAndNil(Root);
System.Close(f);
CurrentFile := FileName;
except
on E: Exception do
begin
CurrentFile := NEW_FILE_NAME;
Result := 1;
end;
end;
end;
function LoadFile(FileName: string): integer;
var
f: TFileStream;
Root: TJSONObject;
begin
Result := 0;
f := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
Root := TJSONObject(GetJSON(f));
CanvasItems := GetFiguresFromJSON(Root);
FreeAndNil(f);
FreeAndNil(Root);
CurrentFile := FileName;
except
on E: Exception do
begin
CurrentFile := NEW_FILE_NAME;
Result := 1;
end;
end;
end;
initialization
CurrentFile := NEW_FILE_NAME;
end.