This repository has been 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 pathmap_actions.pas
503 lines (404 loc) · 10.9 KB
/
map_actions.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
492
493
494
495
496
497
498
499
500
501
502
503
{ 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 map_actions;
{$I compilersetup.inc}
interface
uses
Classes, SysUtils, Math, Map, gset, undo_base, editor_types, map_rect, undo_map,
editor_gl;
type
TBrushMode = (none, fixed, area);
{ TGLessCoord }
generic TGLessCoord <T> = class
public
class function c(a,b: T): boolean;
end;
TCompareCoord = specialize TGLessCoord<TMapCoord>;
TCoordSet = specialize TSet<TMapCoord,TCompareCoord>;
type
{ TMultiTileMapAction }
TMultiTileMapAction = class(TMapUndoItem)
private
FLevel: Integer;
protected
//by default invalidates all tiles
function GetChangedRegion(): TMapRect; virtual;
public
constructor Create(AMap: TVCMIMap); override;
procedure AddTile(X,Y: integer); virtual; abstract;
property Level: Integer read FLevel;
procedure LoadTiles(ASource: TCoordSet);
function GetChangedRegion(ALevelIndex: integer): TMapRect; override; final;
end;
TMultiTileMapActionClass = class of TMultiTileMapAction;
{ TMapBrush }
TMapBrush = class abstract (TComponent)
strict private
FDragging: Boolean;
FMode: TBrushMode;
FSelection: TCoordSet;
FSize: Integer;
FStartCoord: TMapCoord;
FEndCooord: TMapCoord;
procedure SetSize(AValue: Integer);
procedure AddSquare(AMap: TVCMIMap; AX, AY: integer);
procedure RenderSelectionRect(State: TLocalState; const StartCoord: TMapCoord; const EndCooord: TMapCoord);
protected
const
RECT_COLOR: TRBGAColor = (r:50; g:50; b:50; a:255);
protected
property Selection: TCoordSet read FSelection;
property Dragging: Boolean read FDragging;
procedure ClearSelection; virtual;
procedure CheckAddOneTile(AMap: TVCMIMap; const Coord: TMapCoord); virtual;
procedure CheckAddTiles(AMap: TVCMIMap; point: TMapCoord);
procedure RenderCursor(State: TLocalState; X, Y: integer);
procedure RenderSelectionAllTiles(State: TLocalState);
procedure SetMode(AMode: TBrushMode);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Clear; virtual;
procedure Execute(AManager: TAbstractUndoManager; AMap: TVCMIMap); virtual; abstract;
procedure TileClicked(AMap: TVCMIMap; X, Y: integer);virtual;
procedure TileMouseDown(AMap: TVCMIMap; X, Y: integer);virtual;
procedure TileMouseUp(AMap: TVCMIMap; X, Y: integer);virtual;
procedure TileMouseMove(AMap: TVCMIMap; X, Y: integer);virtual;
procedure RenderCursor(State: TLocalState; AMap: TVCMIMap; X, Y: integer); virtual;
procedure RenderSelection(State: TLocalState); virtual;
procedure MouseLeave(AMap: TVCMIMap); virtual;
property Mode: TBrushMode read FMode;
property Size: Integer read FSize write SetSize;
end;
{ TIdleMapBrush }
TIdleMapBrush = class(TMapBrush)
public
constructor Create(AOwner: TComponent); override;
procedure Execute(AManager: TAbstractUndoManager; AMap: TVCMIMap); override;
end;
implementation
uses editor_consts;
{ TMultiTileMapAction }
function TMultiTileMapAction.GetChangedRegion: TMapRect;
begin
Result := inherited GetChangedRegion(Level);
end;
constructor TMultiTileMapAction.Create(AMap: TVCMIMap);
begin
inherited Create(AMap);
FLevel := AMap.CurrentLevelIndex;
end;
procedure TMultiTileMapAction.LoadTiles(ASource: TCoordSet);
var
it: TCoordSet.TIterator;
begin
it := ASource.Min;
if Assigned(it) then
begin
repeat
AddTile(it.Data.X, it.Data.Y);
until not it.next;
FreeAndNil(it);
end;
end;
function TMultiTileMapAction.GetChangedRegion(ALevelIndex: integer): TMapRect;
begin
if ALevelIndex = Level then
begin
Result := GetChangedRegion();
end
else
begin
Result.Create();
end;
end;
{ TGLessCoord }
class function TGLessCoord.c(a, b: T): boolean;
begin
Result := (a.X < b.X) or ((a.X=b.X) and (a.y<b.Y));
end;
{ TIdleMapBrush }
constructor TIdleMapBrush.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Size := 0;
SetMode(TBrushMode.none);
end;
procedure TIdleMapBrush.Execute(AManager: TAbstractUndoManager; AMap: TVCMIMap);
begin
//do nothing
end;
{ TMapBrush }
procedure TMapBrush.SetSize(AValue: Integer);
begin
FSize:=AValue;
Clear;
end;
procedure TMapBrush.AddSquare(AMap: TVCMIMap; AX, AY: integer);
procedure CheckAddOneTileNested(const Coord: TMapCoord; var Stop: Boolean);
begin
CheckAddOneTile(Amap, Coord);
end;
var
r: TMapRect;
begin
r.Create();
r.FTopLeft.Reset(AX,AY);
r.FHeight := Size;
r.FWidth := Size;
r.Iterate(@CheckAddOneTileNested);
end;
procedure TMapBrush.CheckAddTiles(AMap: TVCMIMap; point: TMapCoord);
begin
case Mode of
TBrushMode.area:
begin
end;
TBrushMode.fixed:
begin
AddSquare(AMap, point.X, point.Y);
end;
end;
end;
procedure TMapBrush.ClearSelection;
begin
FSelection.Free;
FSelection := TCoordSet.Create;
end;
procedure TMapBrush.CheckAddOneTile(AMap: TVCMIMap; const Coord: TMapCoord);
begin
Selection.Insert(Coord);
end;
procedure TMapBrush.RenderCursor(State: TLocalState; X, Y: integer);
var
dim: Integer;
cx,cy: Integer;
begin
cx := X * TILE_SIZE;
cy := Y * TILE_SIZE;
State.StartDrawingRects;
State.SetFragmentColor(RECT_COLOR);
dim := TILE_SIZE * Size;
State.RenderRect(cx,cy,dim,dim);
State.StopDrawing;
end;
procedure TMapBrush.RenderSelectionAllTiles(State: TLocalState);
var
it: TCoordSet.TIterator;
dim,cx,cy: Integer;
begin
if Dragging then
begin
it := Selection.Min;
if Assigned(it) then
begin
State.StartDrawingRects;
State.SetFragmentColor(RECT_COLOR);
dim := TILE_SIZE;
repeat
cx := it.Data.X * TILE_SIZE;
cy := it.Data.Y * TILE_SIZE;
State.RenderRect(cx,cy,dim,dim);
until not it.next ;
FreeAndNil(it);
State.StopDrawing;
end;
end;
end;
procedure TMapBrush.RenderSelectionRect(State: TLocalState; const StartCoord: TMapCoord; const EndCooord: TMapCoord);
var
cx,cy: Integer;
r:TMapRect;
begin
if Dragging then
begin
State.StartDrawingRects;
r.SetFromCorners(StartCoord,EndCooord);
cx := r.FTopLeft.X * TILE_SIZE;
cy := r.FTopLeft.Y * TILE_SIZE;
State.SetFragmentColor(RECT_COLOR);
State.RenderRect(cx,cy,r.FWidth * TILE_SIZE ,r.FHeight * TILE_SIZE);
State.StopDrawing;
end;
end;
procedure TMapBrush.SetMode(AMode: TBrushMode);
begin
FMode := AMode;
Clear;
end;
constructor TMapBrush.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FSelection := TCoordSet.Create;
end;
destructor TMapBrush.Destroy;
begin
FSelection.Free;
inherited Destroy;
end;
procedure TMapBrush.Clear;
begin
FDragging := false;
ClearSelection;
end;
procedure TMapBrush.TileClicked(AMap: TVCMIMap; X, Y: integer);
begin
end;
procedure TMapBrush.TileMouseDown(AMap: TVCMIMap; X, Y: integer);
begin
FDragging:=True;
FStartCoord.Reset(X,Y);
FEndCooord.Reset(X,Y);
end;
procedure TMapBrush.TileMouseUp(AMap: TVCMIMap; X, Y: integer);
procedure ProcessTile(const Coord: TMapCoord; var Stop: Boolean);
begin
CheckAddOneTile(AMap, Coord);
end;
var
r:TMapRect;
begin
if FDragging then
begin
FDragging := False;
case Mode of
TBrushMode.area:
begin
ClearSelection;
r.SetFromCorners(FStartCoord,FEndCooord);
r.Iterate(@ProcessTile);
end;
TBrushMode.fixed:
begin
end;
end;
end;
end;
procedure TMapBrush.TileMouseMove(AMap: TVCMIMap; X, Y: integer);
procedure CheckAddOneTileNested(const Coord: TMapCoord; var Stop: Boolean);
begin
CheckAddOneTile(Amap, Coord);
end;
var
NewPoint: TMapCoord = (x:0; y:0);
d, c: TMapCoord;
SX, SY, E: integer;
r: TMapRect;
begin
if not (AMap.IsOnMap(AMap.CurrentLevelIndex,X,Y)) then
exit;
if Dragging then
begin
if Mode = TBrushMode.fixed then
begin
NewPoint.Reset(X,Y);
if FEndCooord <> NewPoint then
begin
//draw line
//(modified)Bresenham's algorimth
//implementation based on BGRADrawLineAliased from http://sourceforge.net/projects/lazpaint (LGPL)
D := NewPoint-FEndCooord;
if D.X < 0 then
begin
SX := -1;
D.X := -D.X;
end
else
begin
SX := 1;
end;
if D.Y < 0 then
begin
SY := -1;
D.Y := -D.Y;
end
else
begin
SY := 1;
end;
D.X := D.X shl 1;
D.Y := D.Y shl 1;
c := FEndCooord;
if D.X > D.Y then
begin
E := D.Y - D.X shr 1;
while c.X <> NewPoint.X do
begin
CheckAddTiles(AMap, c);
if E >= 0 then
begin
Inc(c.Y, SY);
CheckAddTiles(AMap, c);
Dec(E, D.X);
end;
Inc(c.X, SX);
Inc(E, D.Y);
end;
end
else
begin
E := D.X - D.Y shr 1;
while c.Y <> NewPoint.Y do
begin
CheckAddTiles(AMap, c) ;
if E >= 0 then
begin
Inc(c.X, SX);
CheckAddTiles(AMap, c);
Dec(E, D.Y);
end;
Inc(c.Y, SY);
Inc(E, D.X);
end;
end;
end;
FEndCooord.Reset(x,y);
CheckAddTiles(AMap, FEndCooord);
AddSquare(AMap, x, y);
end
else if Mode = TBrushMode.area then
begin
ClearSelection;
FEndCooord.Reset(x,y);
r.SetFromCorners(FStartCoord, FEndCooord);
r.Iterate(@CheckAddOneTileNested);
end;
end;
end;
procedure TMapBrush.RenderCursor(State: TLocalState; AMap: TVCMIMap; X, Y: integer);
begin
if (AMap.IsOnMap(AMap.CurrentLevelIndex,X,Y)) then
begin
//TODO: check cursor partially outside map
if Mode = TBrushMode.fixed then
RenderCursor(State, X, Y);
end;
end;
procedure TMapBrush.RenderSelection(State: TLocalState);
begin
case Mode of
TBrushMode.area:
begin
RenderSelectionRect(State, FStartCoord, FEndCooord);
end;
TBrushMode.fixed:
begin
RenderSelectionAllTiles(State);
end;
end;
end;
procedure TMapBrush.MouseLeave(AMap: TVCMIMap);
begin
Clear;
end;
end.