-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnap.pas
564 lines (498 loc) · 16 KB
/
Snap.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
{+------------------------------------------------------------------------+
| AutoREALM. Copyright (c) 2000, Andrew J. Gryc. |
| |
| 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. |
| |
| For a copy of the GNU General Public License, write to the Free |
| Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 02111-1307, USA. |
+------------------------------------------------------------------------+}
unit Snap;
interface
uses Graphics,Primitives, Geometry;
//const DesignGridScaling = 0.01;
type DirectionalSnapType=(dst90,dst45);
// JD 8-11-02
Var
ApplyScreenSnapsDX1 : Coord;
ApplyScreenSnapsDY1 : Coord;
ApplyScreenSnapsDX2 : Coord;
ApplyScreenSnapsDY2 : Coord;
ApplyScreenSnapsEX1 : Coord;
ApplyScreenSnapsEY1 : Coord;
ApplyScreenSnapsEX2 : Coord;
ApplyScreenSnapsEY2 : Coord;
LastSnapKind : Coord;
LastSnapAngle : Coord;
function ApplySnaps(activateLED:boolean; var cx,cy:Coord; ForceType: Integer = -1; allowselected:boolean=true):boolean;
procedure ApplyScreenSnaps(activateLED:boolean; var cx,cy:integer; ForceType: Integer = -1; allowselected:boolean=true); Overload;
procedure ApplyScreenSnapsFloat(activateLED:boolean; var cx,cy:Coord; ForceType: Integer = -1; allowselected:boolean=true);
procedure ApplyDirectionalSnap(cx1,cy1:Coord; var cx2,cy2:Coord; kind:DirectionalSnapType=dst45);
procedure ApplyProportionalSnap(aspect:double; cx1,cy1:Coord; var cx2,cy2:Coord);
procedure DrawSnapGrid(force:boolean);
procedure SnapLEDsOff;
Procedure AdjustForSelBorder(CX,CY: Integer; Var DX1,DY1,DX2,DY2: Coord); // JD 8-11-02
Procedure ClearAdjust;
implementation
uses Main, MapObject, Math, MapSettings,SelectionTool,GraphGrid;
// JD 8-11-02
Procedure AdjustForSelBorder(CX,CY: Integer; Var DX1,DY1,DX2,DY2: Coord);
Var Sel: TSelectionToolHandler;
Begin
Sel := MainForm.GetSelectionToolHandler;
DX1 := Sel.Extent.Left + SelBorder - CX;
DX2 := Sel.Extent.Right - SelBorder - CX - 1;
DY1 := Sel.Extent.Top + SelBorder - CY;
DY2 := Sel.Extent.Bottom - SelBorder - CY - 1;
End; // AdjustForSelBorder
Procedure ClearAdjust;
Begin
ApplyScreenSnapsDX1 := 0;
ApplyScreenSnapsDY1 := 0;
ApplyScreenSnapsDX2 := 0;
ApplyScreenSnapsDY2 := 0;
End; // ClearAdjust
Procedure ClearScreenSnaps;
Begin
ApplyScreenSnapsEX1 := 0;
ApplyScreenSnapsEY1 := 0;
ApplyScreenSnapsEX2 := 0;
ApplyScreenSnapsEY2 := 0;
End; // ClearScreenSnaps
procedure ApplyScreenSnapsFloat(activateLED:boolean; var cx,cy:Coord; ForceType: Integer; allowselected:boolean);
var x,y:Coord;
begin
if MapSettingsDialog.SnapToGrid.Checked Or
MapSettingsDialog.SnapToPoint.Checked Or
MapSettingsDialog.cbSnapTo.Checked Then
begin
Map.CurrentView.ScreenToCoord(cx,cy,x,y);
if ApplySnaps(activateLED,x,y,ForceType,allowselected)
Then Map.CurrentView.CoordToScreen(x,y,cx,cy)
Else ClearScreenSnaps;
end
Else ClearScreenSnaps;
end;
procedure ApplyScreenSnaps(activateLED:boolean; var cx,cy:integer; ForceType: Integer; allowselected:boolean);
Var X,Y: Coord;
begin
if MapSettingsDialog.SnapToGrid.Checked Or
MapSettingsDialog.SnapToPoint.Checked Or
MapSettingsDialog.cbSnapTo.Checked Then
begin
Map.CurrentView.ScreenToCoord(CX,CY,X,Y);
If ApplySnaps(ActivateLED,X,Y,ForceType,AllowSelected)
Then Map.CurrentView.CoordToScreen(X,Y,CX,CY)
Else ClearScreenSnaps;
end
Else ClearScreenSnaps;
end;
Procedure SnapToGrid(var CX,CY: Coord);
Procedure SnapToSquareGrid;
Var V: Coord;
Begin
V := Map.CurrentView.Grid.CurrentGridSize / MapSettingsDialog.DesignGridUnits.Value;
If V <> 0 Then
begin
If CX < 0
Then CX := Int(CX / V) * V
Else CX := Int(CX / V + 0.5) * V;
If CY < 0
Then CY := Int(CY / V) * v
Else CY := Int(CY / V + 0.5) * V;
End;
End; // SnapToSquareGrid
Procedure SnapToTriangleGridVertical;
Var
V,V2,V3 : Coord;
I,J : Integer;
Begin
V := Map.CurrentView.Grid.CurrentGridSize / MapSettingsDialog.DesignGridUnits.Value;
If V <> 0 Then
Begin
V2 := V / 2;
V3 := V * Sqrt(3) / 2;
If CX < 0
Then I := Trunc(CX / V3)
Else I := Trunc(CX / V3 + 0.5);
If CY < 0
Then J := Trunc(CY / V)
Else J := Trunc(CY / V + 0.5);
CX := I * V3;
CY := J * V;
If (I And 1) = 1 Then CY := CY + V2;
End;
End; // SnapToTriangleGridVertical
Procedure SnapToTriangleGridHorizontal;
Var
V,V2,V3 : Coord;
I,J : Integer;
Begin
V := Map.CurrentView.Grid.CurrentGridSize / MapSettingsDialog.DesignGridUnits.Value;
If V <> 0 Then
Begin
V2 := V / 2;
V3 := V * Sqrt(3) / 2;
If CX < 0
Then I := Trunc(CX / V)
Else I := Trunc(CX / V + 0.5);
If CY < 0
Then J := Trunc(CY / V3)
Else J := Trunc(CY / V3 + 0.5);
CX := I * V;
CY := J * V3;
If (J And 1) = 1 Then CX := CX + V2;
End;
End; // SnapToTriangleGridHorizontal
Procedure SnapToDiamondGrid;
Var
V,V2,V22 : Coord;
I,J : Integer;
Begin
V := Map.CurrentView.Grid.CurrentGridSize / MapSettingsDialog.DesignGridUnits.Value;
If V <> 0 Then
Begin
V2 := V * Sqrt(2);
V22 := V2 / 2;
If CX < 0
Then I := Trunc(CX / V22)
Else I := Trunc(CX / V22 + 0.5);
If CY < 0
Then J := Trunc(CY / V2)
Else J := Trunc(CY / V2 + 0.5);
CX := I * V22;
CY := J * V2;
If (I And 1) = 1 Then CY := CY + V22;
End;
End; // SnapToDiamondGrid
Procedure SnapToHalfDiamondGrid;
Var
V,V2,V22,V24 : Coord;
I,J : Integer;
Begin
V := Map.CurrentView.Grid.CurrentGridSize / MapSettingsDialog.DesignGridUnits.Value;
If V <> 0 Then
Begin
V2 := V * Sqrt(2);
V22 := V2 / 2;
V24 := V2 / 4;
If CX < 0
Then I := Trunc(CX / V22)
Else I := Trunc(CX / V22 + 0.5);
If CY < 0
Then J := Trunc(CY / V22)
Else J := Trunc(CY / V22 + 0.5);
CX := I * V22;
CY := J * V22;
If (I And 1) = 1 Then CY := CY + V24;
End;
End; // SnapToHalfDiamondGrid
Begin
Case Map.CurrentView.Grid.GridType Of
gtHex: SnapToTriangleGridVertical;
gtTriangle,gtRotatedHex: SnapToTriangleGridHorizontal;
gtDiamond: SnapToDiamondGrid;
gtHalfDiamond: SnapToHalfDiamondGrid;
Else
SnapToSquareGrid;
End; // Case
End; // SnapToGrid
procedure ApplyProportionalSnap(aspect:double; cx1,cy1:Coord; var cx2,cy2:Coord);
var sign:double;
begin
if abs(cx1-cx2)>abs(cy1-cy2) then begin
if (cy2<cy1) then sign:=-1 else sign:=1;
cy2:=cy1+sign*abs(cx2-cx1)/aspect;
end
else begin
if (cx2<cx1) then sign:=-1 else sign:=1;
cx2:=cx1+sign*abs(cy2-cy1)*aspect;
end;
end;
procedure ApplyDirectionalSnap(cx1,cy1:Coord; var cx2,cy2:Coord;
kind:DirectionalSnapType);
const smallestangle=pi/4; // 45 degrees
var angle,dist:double;
begin
case kind of
dst90: begin
if abs(cx1-cx2)>abs(cy1-cy2) then
cy2:=cy1
else
cx2:=cx1;
end;
dst45: begin
dist:=distance(cx1,cy1,cx2,cy2);
angle:=ArcTan2(cy2-cy1,cx2-cx1);
angle:=round(angle/smallestangle)*smallestangle;
cx2:=cx1 + dist*cos(angle);
cy2:=cy1 + dist*sin(angle);
end;
end;
end;
procedure SnapLEDsOff;
begin
MainForm.aGridSnap.ImageIndex := 48;
MainForm.aGravitySnap.ImageIndex := 49;
MainForm.aGravitySnapAlong.ImageIndex := 64; // JD 8-13-02
end;
// Modified by JD 8-13-02
function ApplySnaps(activateLED:boolean; var cx,cy:Coord; ForceType: Integer; allowselected:boolean):boolean;
var p:CoordPoint;
d:double;
x,y:integer;
kind:integer;
begin
Result := false;
kind := 0;
if MapSettingsDialog.SnapToGrid.Checked then begin
SnapToGrid(cx,cy);
Result:=true;
kind:=1;
ApplyScreenSnapsEX1 := ApplyScreenSnapsDX1;
ApplyScreenSnapsEY1 := ApplyScreenSnapsDY1;
ApplyScreenSnapsEX2 := ApplyScreenSnapsDX2;
ApplyScreenSnapsEY2 := ApplyScreenSnapsDY2;
end;
if ((ForceType < 0) or (ForceType = 2)) And MapSettingsDialog.SnapToPoint.Checked then begin
if Map.FindClosestPoint(cx,cy,p,allowselected) then begin
d:=distance(cx,cy,p.x,p.y);
Map.CurrentView.DeltaCoordToScreen(d,d,x,y);
if (x<10) and (y<10) then begin
cx:=p.x;
cy:=p.y;
Result:=true;
kind:=2;
ApplyScreenSnapsEX1 := ApplyScreenSnapsDX1;
ApplyScreenSnapsEY1 := ApplyScreenSnapsDY1;
ApplyScreenSnapsEX2 := ApplyScreenSnapsDX2;
ApplyScreenSnapsEY2 := ApplyScreenSnapsDY2;
end;
end;
end;
if ((ForceType < 0) or (ForceType = 3)) And MapSettingsDialog.cbSnapTo.Checked then
begin
if Map.FindClosestPointOn(cx,cy,p,LastSnapAngle,allowselected) then
begin
d := distance(cx,cy,p.x,p.y);
Map.CurrentView.DeltaCoordToScreen(d,d,x,y);
if (x < 10) and (y < 10) then
begin
cx := p.x;
cy := p.y;
Result := true;
kind := 3;
ApplyScreenSnapsEX1 := ApplyScreenSnapsDX1;
ApplyScreenSnapsEY1 := ApplyScreenSnapsDY1;
ApplyScreenSnapsEX2 := ApplyScreenSnapsDX2;
ApplyScreenSnapsEY2 := ApplyScreenSnapsDY2;
end;
end;
end;
if activateLED then begin
case kind of
0: SnapLEDsOff;
1: begin
MainForm.aGridSnap.ImageIndex := 52;
MainForm.aGravitySnap.ImageIndex := 49;
MainForm.aGravitySnapAlong.ImageIndex := 64;
end;
2: begin
MainForm.aGridSnap.ImageIndex := 48;
MainForm.aGravitySnap.ImageIndex := 53;
MainForm.aGravitySnapAlong.ImageIndex := 64;
end;
3: begin
MainForm.aGridSnap.ImageIndex := 48;
MainForm.aGravitySnap.ImageIndex := 49;
MainForm.aGravitySnapAlong.ImageIndex := 65;
end;
end;
end;
LastSnapKind := Kind; // JD 8-13-02
end;
// Modified by JD 10-10-02
Procedure DrawSnapGrid(Force: Boolean);
Var CR: CoordRect;
Procedure SquareGrid;
Var
v : Coord;
bx,by : Coord;
ex,ey : Coord;
cx,cy : Coord;
x,y : Integer;
Begin
V := Map.CurrentView.Grid.CurrentGridSize / MapSettingsDialog.DesignGridUnits.Value;
bx := int(cr.Left / v) * v;
by := int(cr.Top / v) * v;
ex := int(cr.Right / v) * v;
ey := int(cr.Bottom / v) * v;
Map.CurrentView.DeltaCoordToScreen(v,v,x,y);
if (x < 2) or (y < 2) then exit;
cx := bx;
while cx < ex do
begin
cy := by;
while cy < ey do
begin
Map.CurrentView.CoordToScreen(cx,cy,x,y);
Map.CurrentView.Canvas.Pixels[x,y] := Map.CurrentView.Canvas.Pixels[x,y] xor clWhite;
cy := cy + v;
end; // While
cx := cx + v;
end; // While
End; // SquareGrid
Procedure TriangleGridVertical;
Var
v,V2,V32,V3 : Coord;
bx,by : Coord;
ex,ey : Coord;
cx,cy : Coord;
x,y : Integer;
Begin
V := Map.CurrentView.Grid.CurrentGridSize / MapSettingsDialog.DesignGridUnits.Value;
V2 := V / 2;
V3 := V * Sqrt(3);
V32 := V3 / 2;
bx := int(cr.Left / v3) * v3;
by := int(cr.Top / v) * v;
ex := int(cr.Right / v3) * v3;
ey := int(cr.Bottom / v) * v;
Map.CurrentView.DeltaCoordToScreen(v,v,x,y);
if (x < 2) or (y < 2) then exit;
cx := bx;
while cx < ex do
begin
cy := by;
while cy < ey do
begin
Map.CurrentView.CoordToScreen(cx,cy,x,y);
Map.CurrentView.Canvas.Pixels[x,y] := Map.CurrentView.Canvas.Pixels[x,y] xor clWhite;
Map.CurrentView.CoordToScreen(cx + V32,cy + V2,x,y);
Map.CurrentView.Canvas.Pixels[x,y] := Map.CurrentView.Canvas.Pixels[x,y] xor clWhite;
cy := cy + v;
end; // While
cx := cx + v * Sqrt(3);
end; // While
End; // TriangleGridVertical
Procedure TriangleGridHorizontal;
Var
v,V2,V32,V3 : Coord;
bx,by : Coord;
ex,ey : Coord;
cx,cy : Coord;
x,y : Integer;
Begin
V := Map.CurrentView.Grid.CurrentGridSize / MapSettingsDialog.DesignGridUnits.Value;
V2 := V / 2;
V3 := V * Sqrt(3);
V32 := V3 / 2;
bx := int(cr.Left / v) * v;
by := int(cr.Top / v3) * v3;
ex := int(cr.Right / v) * v;
ey := int(cr.Bottom / v3) * v3;
Map.CurrentView.DeltaCoordToScreen(v,v,x,y);
if (x < 2) or (y < 2) then exit;
cx := bx;
while cx < ex do
begin
cy := by;
while cy < ey do
begin
Map.CurrentView.CoordToScreen(cx,cy,x,y);
Map.CurrentView.Canvas.Pixels[x,y] := Map.CurrentView.Canvas.Pixels[x,y] xor clWhite;
Map.CurrentView.CoordToScreen(cx + V2,cy + V32,x,y);
Map.CurrentView.Canvas.Pixels[x,y] := Map.CurrentView.Canvas.Pixels[x,y] xor clWhite;
cy := cy + V * Sqrt(3);
end; // While
cx := cx + v;
end; // While
End; // TriangleGridHorizontal
Procedure DiamondGrid;
Var
v,V2,V22 : Coord;
bx,by : Coord;
ex,ey : Coord;
cx,cy : Coord;
x,y : Integer;
Begin
V := Map.CurrentView.Grid.CurrentGridSize / MapSettingsDialog.DesignGridUnits.Value;
V2 := V * Sqrt(2);
V22 := V2 / 2;
bx := int(cr.Left / v2) * v2;
by := int(cr.Top / v2) * v2;
ex := int(cr.Right / v2) * v2;
ey := int(cr.Bottom / v2) * v2;
Map.CurrentView.DeltaCoordToScreen(v,v,x,y);
if (x < 2) or (y < 2) then exit;
cx := bx;
while cx < ex do
begin
cy := by;
while cy < ey do
begin
Map.CurrentView.CoordToScreen(cx,cy,x,y);
Map.CurrentView.Canvas.Pixels[x,y] := Map.CurrentView.Canvas.Pixels[x,y] xor clWhite;
Map.CurrentView.CoordToScreen(cx + v22,cy + v22,x,y);
Map.CurrentView.Canvas.Pixels[x,y] := Map.CurrentView.Canvas.Pixels[x,y] xor clWhite;
cy := cy + v2;
end; // While
cx := cx + v2;
end; // While
End; // DiamondGrid
Procedure HalfDiamondGrid;
Var
v,V2,V22,V24 : Coord;
bx,by : Coord;
ex,ey : Coord;
cx,cy : Coord;
x,y : Integer;
Begin
V := Map.CurrentView.Grid.CurrentGridSize / MapSettingsDialog.DesignGridUnits.Value;
V2 := V * Sqrt(2);
V22 := V2 / 2;
V24 := V2 / 4;
bx := int(cr.Left / v2) * v2;
by := int(cr.Top / v22) * v22;
ex := int(cr.Right / v2) * v2;
ey := int(cr.Bottom / v22) * v22;
Map.CurrentView.DeltaCoordToScreen(v,v,x,y);
if (x < 2) or (y < 2) then exit;
cx := bx;
while cx < ex do
begin
cy := by;
while cy < ey do
begin
Map.CurrentView.CoordToScreen(cx,cy,x,y);
Map.CurrentView.Canvas.Pixels[x,y] := Map.CurrentView.Canvas.Pixels[x,y] xor clWhite;
Map.CurrentView.CoordToScreen(cx + v22,cy + v24,x,y);
Map.CurrentView.Canvas.Pixels[x,y] := Map.CurrentView.Canvas.Pixels[x,y] xor clWhite;
cy := cy + v22;
end; // While
cx := cx + v2;
end; // While
End; // HalfDiamondGrid
Begin
If Force Or MapSettingsDialog.DisplayGrid.Checked Then
Begin
Map.CurrentView.GetCoordinateRect(cr);
Case Map.CurrentView.Grid.GridType Of
gtHex: TriangleGridVertical;
gtTriangle,gtRotatedHex: TriangleGridHorizontal;
gtDiamond: DiamondGrid;
gtHalfDiamond: HalfDiamondGrid;
Else
SquareGrid;
End; // Case
End;
End; // DrawSnapGrid
end.