-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathse_undo.pas
492 lines (437 loc) · 11.8 KB
/
se_undo.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//------------------------------------------------------------------------------
//
// SpeedEd: GLSpeed map Editor utilities
// Copyright (C) 2021-2022 by Jim Valavanis
//
// This program 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 program 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.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
// Undo/Redo manager
//
//------------------------------------------------------------------------------
// E-Mail: jimmyvalavanis@yahoo.gr
// Site : https://sourceforge.net/projects/speed-game/
//------------------------------------------------------------------------------
unit se_undo;
interface
uses
Windows, SysUtils, Classes, se_binary, zLibPas;
type
TStreamType = (sstMemory, sstFile);
PStreamStackNode = ^TStreamStackNode;
TStreamStackNode = record
data: TStream;
PathToStream: string;
next,
pred: PStreamStackNode;
end;
PBaseStack = ^TBaseStack;
TBaseStack = class(TObject)
private
Compressor: TBinaryData;
fStreamType: TStreamType;
protected
top: PStreamStackNode;
function GetCompressionLevel: TCompressionLevel; virtual;
procedure SetCompressionLevel(Value: TCompressionLevel); virtual;
function GetNewTmpPath: string; virtual;
public
constructor Create; virtual;
destructor Destroy; override;
function Empty: boolean;
procedure Push(s: TMemoryStream); virtual;
procedure Pop(var s: TMemoryStream); overload; virtual;
function Pop: TMemoryStream; overload; virtual;
procedure Clear; virtual;
property CompressionLevel: TCompressionLevel read GetCompressionLevel write SetCompressionLevel;
property StreamType: TStreamType read fStreamType write fStreamType default sstMemory;
end;
TLimitedSizeStack = class(TBaseStack)
protected
bottom: PStreamStackNode;
numItems: word;
fUndoLimit: word;
function GetUndoLimit: word; virtual;
procedure SetUndoLimit(Value: word); virtual;
public
constructor Create; override;
procedure Push(s: TMemoryStream); override;
procedure Pop(var s: TMemoryStream); overload; override;
procedure Clear; override;
property UndoLimit: word read GetUndoLimit write SetUndoLimit default 100;
end;
PRedoStack = ^TRedoStack;
TRedoStack = TBaseStack;
PUndoStack = ^TUndoStack;
TUndoStack = TLimitedSizeStack;
TOnStreamOperationEvent = procedure (const s: TStream) of object;
TUndoRedoManager = class(TObject)
private
UndoStack: TUndoStack;
RedoStack: TRedoStack;
FOnSaveToStream,
FOnLoadFromStream: TOnStreamOperationEvent;
protected
function GetCompressionLevel: TCompressionLevel; virtual;
procedure SetCompressionLevel(Value: TCompressionLevel); virtual;
procedure SaveDataToStream(s: TStream); virtual;
procedure LoadDataFromStream(s: TStream); virtual;
function GetUndoLimit: word; virtual;
procedure SetUndoLimit(Value: word); virtual;
function GetStreamType: TStreamType; virtual;
procedure SetStreamType(Value: TStreamType); virtual;
public
constructor Create; virtual;
destructor Destroy; override;
procedure Clear; virtual;
procedure Undo; virtual;
procedure Redo; virtual;
function CanUndo: boolean; virtual;
function CanRedo: boolean; virtual;
procedure SaveUndo; virtual;
published
property CompressionLevel: TCompressionLevel read GetCOmpressionLevel write SetCompressionLevel;
property OnLoadFromStream: TOnStreamOperationEvent read FOnLoadFromStream write FOnLoadFromStream;
property OnSaveToStream: TOnStreamOperationEvent read FOnSaveToStream write FOnSaveToStream;
property UndoLimit: word read GetUndoLimit write SetUndoLimit default 10;
property StreamType: TStreamType read GetStreamType write SetStreamType default sstMemory;
end;
procedure ClearMemoryStream(m: TMemoryStream);
procedure ClearFileStream(f: TFileStream; const Path: string);
implementation
resourceString
rsUndo = 'UNDO';
rsInternalError = 'Multiple Undo-Redo Manager: Internal Error!';
procedure ClearMemoryStream(m: TMemoryStream);
begin
if m <> nil then
begin
m.Size := 0;
m.Free;
end;
end;
procedure ClearFileStream(f: TFileStream; const Path: string);
begin
if f <> nil then
f.Free;
if FileExists(Path) then
if Path <> '' then
DeleteFile(Path);
end;
procedure ClearStreamStackNode(node: PStreamStackNode);
begin
if node <> nil then
begin
if node.data is TFileStream then
ClearFileStream(node.data as TFileStream, node.PathToStream)
else if node.data is TMemoryStream then
ClearMemoryStream(node.data as TMemoryStream);
if FileExists(node.PathToStream) then
if node.PathToStream <> '' then
DeleteFile(node.PathToStream);
end;
end;
{*** TBaseStack ***}
constructor TBaseStack.Create;
begin
Inherited;
top := nil;
Compressor := TBinaryData.Create;
CompressionLevel := clNone;
fStreamType := sstMemory;
end;
function TBaseStack.Empty;
begin
Empty := (top=nil)
end;
function TBaseStack.GetNewTmpPath: string;
var
FPath,
s: array[0..1023] of char;
begin
result := '';
FillChar(s, SizeOf(s), Chr(0));
FillChar(FPath, SizeOf(FPath), Chr(0));
if GetTempPath(1023, FPath) <> 0 then
if GetTempFileName(FPath, PChar(rsUndo), 0, s) <> 0 then
result := s;
end;
procedure TBaseStack.Push(s: TMemoryStream);
var
temp: PStreamStackNode;
begin
new(temp);
if fStreamType = sstFile then
begin
temp^.PathToStream := GetNewTmpPath;
if temp^.PathToStream <> '' then
temp^.data := TFileStream.Create(temp^.PathToStream, fmCreate or fmOpenReadWrite)
else
fStreamType := sstMemory
end;
if fStreamType = sstMemory then
begin
temp^.data := TMemoryStream.Create;
temp^.PathToStream := '';
end;
s.Seek(0, soFromBeginning);
Compressor.LoadFromStream(s, false);
Compressor.SaveToStream(temp^.data, true);
// If we use files to store undo operation we release the file handle,
// we keep information to PathToStream
if fStreamType = sstFile then
begin
temp^.data.Free;
temp^.data := nil;
end;
Compressor.Clear;
temp^.next := nil;
temp^.pred := top;
if top <> nil then top^.next := temp;
top := temp;
end;
procedure TBaseStack.Pop(var s: TMemoryStream);
var
temp: PStreamStackNode;
begin
if not Empty then
begin
if s = nil then s := TMemoryStream.Create;
// If s = nil we have a file, so we create the file handle from PathToStream
if top^.data = nil then
begin
if top^.PathToStream <> '' then
top^.data := TFileStream.Create(top^.PathToStream, fmOpenRead or fmShareDenyWrite)
else
raise Exception.Create(rsInternalError);
end;
top^.data.Seek(0, soFromBeginning);
Compressor.LoadFromStream(top^.data, true);
s.Seek(0, soFromBeginning);
Compressor.SaveToStream(s, false);
s.Seek(0, soFromBeginning);
Compressor.Clear;
ClearStreamStackNode(top);
temp := top;
top := top^.pred;
dispose(temp);
if top <> nil then top^.next := nil;
end;
end;
function TBaseStack.Pop: TMemoryStream;
begin
result := TMemoryStream.Create;
Pop(result);
end;
procedure TBaseStack.Clear;
var
temp: PStreamStackNode;
begin
while not Empty do
begin
ClearStreamStackNode(top);
temp := top;
top := top^.pred;
dispose(temp);
if top <> nil then top^.next := nil;
end;
end;
destructor TBaseStack.Destroy;
begin
Clear;
Compressor.Free;
Inherited;
end;
function TBaseStack.GetCompressionLevel: TCompressionLevel;
begin
result := Compressor.CompressionLevel;
end;
procedure TBaseStack.SetCompressionLevel(Value: TCompressionLevel);
begin
Compressor.CompressionLevel := Value;
end;
{*** TLimitedSizeStack ***}
constructor TLimitedSizeStack.Create;
begin
Inherited;
bottom := nil;
numItems := 0;
fUndoLimit := 1000;
end;
function TLimitedSizeStack.GetUndoLimit: word;
begin
result := fUndoLimit;
end;
procedure TLimitedSizeStack.SetUndoLimit(Value: word);
begin
if Value <> fUndoLimit then
begin
fUndoLimit := Value;
end;
end;
procedure TLimitedSizeStack.Push(s: TMemoryStream);
var
wasEmpty: boolean;
temp: PStreamStackNode;
begin
// If we reach the maximum number of undo's we reduce the stack size.
// If fUndoLimit = 0 there is no limit to the number of undo operations.
if (numItems >= fUndoLimit) and (fUndoLimit <> 0) then
begin
ClearStreamStackNode(bottom);
temp := bottom;
bottom := bottom^.next;
if bottom <> nil then bottom^.pred := nil;
dispose(temp);
end
else
inc(numItems);
wasEmpty := Empty;
Inherited;
if wasEmpty then bottom := top;
end;
procedure TLimitedSizeStack.Pop(var s: TMemoryStream);
begin
if not Empty then
begin
dec(numItems);
Inherited;
end;
end;
procedure TLimitedSizeStack.Clear;
begin
Inherited;
numItems := 0;
end;
{*** TUndoRedoManager ***}
constructor TUndoRedoManager.Create;
begin
Inherited;
UndoStack := TUndoStack.Create;
RedoStack := TRedoStack.Create;
CompressionLevel := clNone;
StreamType := sstFile;
end;
destructor TUndoRedoManager.Destroy;
begin
Clear;
UndoStack.Free; UndoStack := nil;
RedoStack.Free; RedoSTack := nil;
Inherited;
end;
function TUndoRedoManager.GetUndoLimit: word;
begin
if Assigned(UndoStack) then
result := UndoStack.UndoLimit
else
result := 0;
end;
procedure TUndoRedoManager.SetUndoLimit(Value: word);
begin
if Assigned(UndoStack) then
UndoStack.UndoLimit := Value
end;
function TUndoRedoManager.GetCompressionLevel: TCompressionLevel;
begin
result := UndoStack.CompressionLevel;
end;
procedure TUndoRedoManager.SetCompressionLevel(Value: TCompressionLevel);
begin
UndoStack.CompressionLevel := Value;
RedoStack.CompressionLevel := Value;
end;
procedure TUndoRedoManager.Clear;
begin
UndoStack.Clear;
RedoStack.Clear;
end;
procedure TUndoRedoManager.Undo;
var
mRedo, mUndo: TMemoryStream;
begin
if CanUndo then
begin
mUndo := TMemoryStream.Create;
mRedo := TMemoryStream.Create;
try
SaveDataToStream(mRedo);
RedoStack.Push(mRedo);
UndoStack.Pop(mUndo);
LoadDataFromStream(mUndo);
finally
ClearMemoryStream(mUndo);
ClearMemoryStream(mRedo);
end;
end;
end;
procedure TUndoRedoManager.Redo;
var
mRedo, mUndo: TMemoryStream;
begin
if CanRedo then
begin
mUndo := TMemoryStream.Create;
mRedo := TMemoryStream.Create;
try
SaveDataToStream(mUndo);
UndoStack.Push(mUndo);
RedoStack.Pop(mRedo);
LoadDataFromStream(mRedo);
finally
ClearMemoryStream(mUndo);
ClearMemoryStream(mRedo);
end;
end;
end;
function TUndoRedoManager.GetStreamType: TStreamType;
begin
result := UndoStack.StreamType;
end;
procedure TUndoRedoManager.SetStreamType(Value: TStreamType);
begin
UndoStack.StreamType := Value;
RedoStack.StreamType := Value;
end;
function TUndoRedoManager.CanUndo: boolean;
begin
result := not UndoStack.Empty
end;
function TUndoRedoManager.CanRedo: boolean;
begin
result := not RedoStack.Empty
end;
procedure TUndoRedoManager.SaveUndo;
var
m: TMemoryStream;
begin
m := TMemoryStream.Create;
try
SaveDataToStream(m);
UndoStack.Push(m);
finally
ClearMemoryStream(m);
end;
RedoStack.Clear;
end;
procedure TUndoRedoManager.SaveDataToStream(s: TStream);
begin
if Assigned(FOnSaveToStream) then FOnSaveToStream(s);
end;
procedure TUndoRedoManager.LoadDataFromStream(s: TStream);
begin
if Assigned(FOnLoadFromStream) then FOnLoadFromStream(s);
end;
end.