-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuhistory.pas
159 lines (134 loc) · 3.58 KB
/
uhistory.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
unit UHistory;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, UFigures, UTransform, Math, UAppState;
type
TCanvasState = record
Figures: TFigureList;
CanvasScale: double;
Offset: TDoublePoint;
Saved: boolean;
end;
{ TCanvasHistory }
TCanvasHistory = class(TPersistent)
private
FStates: array of TCanvasState;
FCurrentStateIndex: integer;
public
constructor Create;
destructor Destroy; override;
procedure PushState(AState: TCanvasState);
procedure PushState(AFigures: TFigureList; AScale: double;
AOffset: TDoublePoint; ASaved: boolean);
procedure PushState;
procedure RefreshSaveStates;
procedure Undo;
procedure Redo;
function CanUndo: boolean;
function CanRedo: boolean;
procedure Clear;
function GetCurrentState: TCanvasState;
end;
function CanvasState(AFigures: TFigureList; AScale: double;
AOffset: TDoublePoint; ASaved: boolean): TCanvasState;
var
History: TCanvasHistory;
implementation
function CanvasState(AFigures: TFigureList; AScale: double;
AOffset: TDoublePoint; ASaved: boolean): TCanvasState;
begin
with Result do
begin
Figures := AFigures;
CanvasScale := AScale;
Offset := AOffset;
Saved := ASaved;
end;
end;
{ TCanvasHistory }
constructor TCanvasHistory.Create;
begin
FCurrentStateIndex := -1;
end;
destructor TCanvasHistory.Destroy;
var
i, j: integer;
begin
for i := Low(FStates) to High(FStates) do
for j := Low(FStates[i].Figures) to High(FStates[i].Figures) do
FreeAndNil(FStates[i].Figures[j]);
SetLength(FStates, 0);
inherited Destroy;
end;
procedure TCanvasHistory.PushState(AState: TCanvasState);
var
i, j: integer;
begin
inc(FCurrentStateIndex);
for i := FCurrentStateIndex + 1 to High(FStates) do
for j := Low(FStates[i].Figures) to High(FStates[i].Figures) do
FreeAndNil(FStates[i].Figures[j]);
SetLength(FStates, FCurrentStateIndex + 1);
FStates[High(FStates)] := AState;
end;
procedure TCanvasHistory.PushState(AFigures: TFigureList; AScale: double;
AOffset: TDoublePoint; ASaved: boolean);
begin
PushState(CanvasState(AFigures, AScale, AOffset, ASaved));
end;
procedure TCanvasHistory.PushState;
begin
PushState(CloneCanvasItems, Scale, CanvasOffset, not Modified);
end;
procedure TCanvasHistory.RefreshSaveStates;
var
i: integer;
begin
for i := Low(FStates) to High(FStates) do
FStates[i].Saved := false;
FStates[FCurrentStateIndex].Saved := not Modified;
end;
procedure TCanvasHistory.Undo;
begin
SelectAll;
DeleteSelected;
FCurrentStateIndex := Max(0, FCurrentStateIndex-1);
CanvasItems := CloneFigures(GetCurrentState.Figures);
Modified := not GetCurrentState.Saved;
end;
procedure TCanvasHistory.Redo;
begin
SelectAll;
DeleteSelected;
FCurrentStateIndex := Min(FCurrentStateIndex+1, High(FStates));
CanvasItems := CloneFigures(GetCurrentState.Figures);
Modified := not GetCurrentState.Saved;
end;
function TCanvasHistory.CanUndo: boolean;
begin
Result := FCurrentStateIndex > 0;
end;
function TCanvasHistory.CanRedo: boolean;
begin
Result := (FCurrentStateIndex > -1) and
(FCurrentStateIndex < High(FStates));
end;
procedure TCanvasHistory.Clear;
var
i, j: integer;
begin
for i := Low(FStates) to High(FStates) do
for j := Low(FStates[i].Figures) to High(FStates[i].Figures) do
FreeAndNil(FStates[i].Figures[j]);
SetLength(FStates, 0);
FCurrentStateIndex := -1;
PushState;
end;
function TCanvasHistory.GetCurrentState: TCanvasState;
begin
Result := FStates[FCurrentStateIndex];
end;
initialization
History := TCanvasHistory.Create;
end.