This repository was archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathundo_map.pas
256 lines (205 loc) · 6.08 KB
/
undo_map.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
{ This file is a part of Map editor for VCMI project
Copyright (C) 2013-2017 Alexander Shishkin [email protected]
This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit undo_map;
{$I compilersetup.inc}
interface
uses
Classes, SysUtils, Math, undo_base, map, map_rect, fgl;
type
{ TMapUndoItem }
TMapUndoItem = class(TAbstractUndoItem)
protected
FMap: TVCMIMap;
public
constructor Create(AMap: TVCMIMap); virtual;
//by default invalidates all tiles
function GetChangedRegion(ALevelIndex: integer): TMapRect; virtual;
end;
TItemStack = specialize TFPGObjectList<TAbstractUndoItem>;
TOnRegionInvalidated = procedure (ALevel: Integer; ARegion: TMapRect) of object;
TOnActionPreformed = procedure (AItem: TAbstractUndoItem) of object;
{ TMapUndoManager }
TMapUndoManager = class(TAbstractUndoManager)
strict private
FItemStack: TItemStack;
FCheckPoint: Integer;
FCurrentPosition: Integer;
FMap: TVCMIMap;
FOnActionPerformed: TOnActionPreformed;
FOnRegionInvalidated: TOnRegionInvalidated;
procedure SetMap(AValue: TVCMIMap);
procedure DoRegionInvalidated(ALevel: Integer; ARegion: TMapRect);
procedure CheckInvalidatedRegions(AItem: TAbstractUndoItem);
procedure DoActionPerformed(AItem: TAbstractUndoItem);
public
constructor Create;
destructor Destroy; override;
function CanRedo: boolean; override;
function CanUndo: boolean; override;
procedure Undo; override;
procedure Redo; override;
function ExecuteItem(AItem: TAbstractUndoItem): boolean; override;
function PeekCurrent: TAbstractUndoItem; override;
function PeekNext: TAbstractUndoItem; override;
procedure MarkCheckPoint;
function IsCheckPoint: Boolean;
procedure Clear; override;
property Map: TVCMIMap read FMap write SetMap;
property OnRegionInvalidated: TOnRegionInvalidated read FOnRegionInvalidated write FOnRegionInvalidated;
property OnActionPerformed: TOnActionPreformed read FOnActionPerformed write FOnActionPerformed;
end;
implementation
{ TMapUndoItem }
constructor TMapUndoItem.Create(AMap: TVCMIMap);
begin
FMap := AMap;
end;
function TMapUndoItem.GetChangedRegion(ALevelIndex: integer): TMapRect;
begin
Result := FMap.MapLevels[ALevelIndex].GetDimentions;
end;
{ TMapUndoManger }
function TMapUndoManager.CanRedo: boolean;
begin
Result := (FCurrentPosition >=-1) and (FCurrentPosition < FItemStack.Count - 1);
end;
function TMapUndoManager.CanUndo: boolean;
begin
Result := (FCurrentPosition >=0) and (FCurrentPosition < FItemStack.Count);
end;
procedure TMapUndoManager.Clear;
begin
FItemStack.Clear;
FCurrentPosition := -1;
FCheckPoint := -2;
end;
procedure TMapUndoManager.SetMap(AValue: TVCMIMap);
begin
if FMap=AValue then Exit;
FMap:=AValue;
end;
procedure TMapUndoManager.DoRegionInvalidated(ALevel: Integer; ARegion: TMapRect);
begin
if Assigned(FOnRegionInvalidated) then
begin
FOnRegionInvalidated(ALevel, ARegion);
end;
end;
procedure TMapUndoManager.CheckInvalidatedRegions(AItem: TAbstractUndoItem);
var
level_index: Integer;
invalid_region: TMapRect;
begin
if not Assigned(FMap) then
Exit;
if not (AItem is TMapUndoItem) then
Exit;
for level_index := 0 to FMap.MapLevels.Count - 1 do
begin
invalid_region := TMapUndoItem(AItem).GetChangedRegion(level_index);
if not invalid_region.IsEmpty then
DoRegionInvalidated(level_index, invalid_region);
end;
end;
procedure TMapUndoManager.DoActionPerformed(AItem: TAbstractUndoItem);
begin
if Assigned(FOnActionPerformed) then
begin
FOnActionPerformed(AItem);
end;
end;
constructor TMapUndoManager.Create;
begin
FItemStack := TItemStack.Create(True);
FCurrentPosition := -1;
FCheckPoint:= -2;
end;
destructor TMapUndoManager.Destroy;
begin
FItemStack.Free;
inherited Destroy;
end;
function TMapUndoManager.ExecuteItem(AItem: TAbstractUndoItem): boolean;
var
i: Integer;
begin
SetItemState(AItem,TUndoItemState.Idle);
Result := AItem.Execute;
if not Result then
begin
AItem.Free;
Exit;
end;
CheckInvalidatedRegions(AItem);
SetItemState(AItem,TUndoItemState.ReDone);
for i := FItemStack.Count - 1 downto Max(FCurrentPosition+1, 0) do
begin
FItemStack.Delete(i);
end;
FCurrentPosition := FItemStack.Add(AItem);
FMap.IsDirty:= not IsCheckPoint;
DoActionPerformed(AItem);
end;
function TMapUndoManager.PeekCurrent: TAbstractUndoItem;
begin
if CanUndo then
begin
Result := FItemStack[FCurrentPosition];
end
else begin
Result := nil;
end;
end;
function TMapUndoManager.PeekNext: TAbstractUndoItem;
begin
if CanRedo then
begin
Result := FItemStack[FCurrentPosition+1];
end
else begin
Result := nil;
end;
end;
procedure TMapUndoManager.MarkCheckPoint;
begin
FCheckPoint := FCurrentPosition;
end;
function TMapUndoManager.IsCheckPoint: Boolean;
begin
Result := FCheckPoint = FCurrentPosition;
end;
procedure TMapUndoManager.Redo;
var
item: TAbstractUndoItem;
begin
item := PeekNext;
item.Redo;
CheckInvalidatedRegions(item);
SetItemState(item,TUndoItemState.ReDone);
Inc(FCurrentPosition);
FMap.IsDirty:= not IsCheckPoint;
DoActionPerformed(Item);
end;
procedure TMapUndoManager.Undo;
var
item: TAbstractUndoItem;
begin
item := PeekCurrent;
item.Undo;
CheckInvalidatedRegions(item);
SetItemState(item,TUndoItemState.UnDone);
Dec(FCurrentPosition);
FMap.IsDirty:= not IsCheckPoint;
DoActionPerformed(Item);
end;
end.