-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGSXBoloMapView.m
1032 lines (832 loc) · 28.8 KB
/
GSXBoloMapView.m
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// GSXBoloMapView.m
// XBolo Map Editor
//
// Created by Robert Chrzanowski on 12/29/09.
// Copyright 2009 Robert Chrzanowski. All rights reserved.
//
#import "GSXBoloMapView.h"
#import "GSXBoloMap.h"
#import "GSToolsController.h"
#import "GSPaletteController.h"
#import "GSTileRect.h"
static NSImage *sprites = nil;
static CGFloat phase = 0.0f;
static NSMutableArray *boloMapViews = nil;
@interface GSXBoloMapView (Private)
+ (void)phaseIncrement:(id)obj;
- (GSPoint)convertScreenToWorld:(NSPoint)point;
- (void)fillTool;
- (void)filledEllipseTool;
- (void)filledRectangleTool;
- (void)ellipseTool;
- (void)rectangleTool;
- (void)mineTool;
- (void)pillTool;
- (void)baseTool;
- (void)deleteTool;
- (GSRect)getSelection;
- (NSInteger)pillAtPoint:(GSPoint)point;
- (NSInteger)baseAtPoint:(GSPoint)point;
- (NSInteger)startAtPoint:(GSPoint)point;
- (void)setUnderSelection:(GSTileRect *)newUnderSelection;
- (void)setNeedsDisplayInSelectionRect;
@end
@implementation GSXBoloMapView
+ (void)initialize {
if (self == [GSXBoloMapView class]) {
NSAssert((sprites = [[NSImage imageNamed:@"Sprites"] retain]) != nil, @"Failed to Open Sprites File");
boloMapViews = [[NSMutableArray alloc] init];
[[NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(phaseIncrement:) userInfo:nil repeats:YES] retain];
}
}
- (void)awakeFromNib {
[self center:self];
}
- (id)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
boloMap = nil;
firstMouseEvent = GSMakePoint(-1, -1);
lastMouseEvent = GSMakePoint(-1, -1);
startTool = FALSE;
start.x = 0;
start.y = 0;
start.dir = 0;
underSelection = nil;
move = FALSE;
[boloMapViews addObject:self];
}
return self;
}
- (void)release {
if ([self retainCount] == 2) {
[boloMapViews removeObject:self];
}
[super release];
}
+ (void)phaseIncrement:(id)obj {
phase += 1.0f;
[boloMapViews makeObjectsPerformSelector:@selector(setNeedsDisplayInSelectionRect)];
}
- (void)setNeedsDisplayInSelectionRect {
if (underSelection) {
GSRect rect = [underSelection rect];
[self setNeedsDisplayInRect:NSMakeRect(GSMinX(rect) * TILE_WIDTH, (WIDTH - (GSMinY(rect) + GSHeight(rect))) * TILE_WIDTH, GSWidth(rect) * TILE_WIDTH, 1.0f)];
[self setNeedsDisplayInRect:NSMakeRect(GSMinX(rect) * TILE_WIDTH, ((WIDTH - GSMinY(rect)) * TILE_WIDTH) - 1.0f, GSWidth(rect) * TILE_WIDTH, 1.0f)];
[self setNeedsDisplayInRect:NSMakeRect(GSMinX(rect) * TILE_WIDTH, ((WIDTH - (GSMinY(rect) + GSHeight(rect))) * TILE_WIDTH) + 1.0f, 1.0f, (GSHeight(rect) * TILE_WIDTH) - 2.0f)];
[self setNeedsDisplayInRect:NSMakeRect(((GSMinX(rect) + GSWidth(rect)) * TILE_WIDTH) - 1.0f, ((WIDTH - (GSMinY(rect) + GSHeight(rect))) * TILE_WIDTH) + 1.0f, 1.0f, (GSHeight(rect) * TILE_WIDTH) - 2.0f)];
}
}
- (void)drawRect:(NSRect)rect {
[boloMap drawRect:rect];
// draw tank shadow
if (startTool) {
int sprite = PTKB00IMAGE + start.dir;
NSRect srcRect = NSMakeRect((sprite % 16) * TILE_WIDTH, (sprite / 16) * TILE_WIDTH, TILE_WIDTH, TILE_WIDTH);
NSRect dstRect = NSMakeRect(start.x * TILE_WIDTH, (WIDTH - start.y - 1) * TILE_WIDTH, TILE_WIDTH, TILE_WIDTH);
[sprites drawInRect:dstRect fromRect:srcRect operation:NSCompositeSourceOver fraction:0.5];
}
// draw selection ring
if (underSelection) {
GSRect rect;
NSBezierPath *b;
NSInteger count = 2;
CGFloat pattern[2] = { 5.0f, 5.0f };
rect = [underSelection rect];
b = [NSBezierPath bezierPathWithRect:NSMakeRect((GSMinX(rect) * TILE_WIDTH) + 0.5f, ((WIDTH - (GSMinY(rect) + GSHeight(rect))) * TILE_WIDTH) + 0.5f, (GSWidth(rect) * TILE_WIDTH) - 1.0f, (GSHeight(rect) * TILE_WIDTH) - 1.0f)];
[b setLineDash:pattern count:count phase:(CGFloat)phase];
[[NSColor selectedControlColor] set];
[b stroke];
}
}
- (void)fillTool {
int palette;
palette = [GSPaletteController palette];
if ([boloMap tileAtPoint:firstMouseEvent] != palette) {
GSTileRect *tileRect;
if (underSelection) {
tileRect = [boloMap tilesInRect:[underSelection rect]];
}
else {
tileRect = [boloMap tilesRectFloodAtPoint:firstMouseEvent];
}
[tileRect floodFillWithTile:palette atPoint:firstMouseEvent];
if (underSelection) {
tileRect = [GSTileRect tileRectWithTileRect:tileRect inRect:[underSelection rect]];
}
[boloMap setTileRect:tileRect];
[boloMap setAppropriateTilesForObjectsInRect:[tileRect rect]];
[[boloMap undoManager] setActionName:@"Fill"];
}
}
- (void)filledEllipseTool {
GSTileRect *tileRect;
tileRect = [boloMap tilesInRect:[self getSelection]];
[tileRect drawFilledEllipse:[GSPaletteController palette]];
if (underSelection) {
tileRect = [GSTileRect tileRectWithTileRect:tileRect inRect:[underSelection rect]];
}
[boloMap setTileRect:tileRect];
[boloMap setAppropriateTilesForObjectsInRect:[tileRect rect]];
[[boloMap undoManager] setActionName:@"Draw Ellipse"];
}
- (void)filledRectangleTool {
GSTileRect *tileRect = [GSTileRect tileRectWithTile:[GSPaletteController palette] inRect:[self getSelection]];
if (underSelection) {
tileRect = [GSTileRect tileRectWithTileRect:tileRect inRect:[underSelection rect]];
}
[boloMap setTileRect:tileRect];
[boloMap setAppropriateTilesForObjectsInRect:[tileRect rect]];
[[boloMap undoManager] setActionName:@"Draw Rectangle"];
}
- (void)ellipseTool {
GSTileRect *tileRect = [boloMap tilesInRect:[self getSelection]];
[tileRect drawEllipse:[GSPaletteController palette]];
if (underSelection) {
tileRect = [GSTileRect tileRectWithTileRect:tileRect inRect:[underSelection rect]];
}
[boloMap setTileRect:tileRect];
[boloMap setAppropriateTilesForObjectsInRect:[tileRect rect]];
[[boloMap undoManager] setActionName:@"Draw Ellipse"];
}
- (void)rectangleTool {
GSTileRect *tileRect = [boloMap tilesInRect:[self getSelection]];
[tileRect drawRectangle:[GSPaletteController palette]];
if (underSelection) {
tileRect = [GSTileRect tileRectWithTileRect:tileRect inRect:[underSelection rect]];
}
[boloMap setTileRect:tileRect];
[boloMap setAppropriateTilesForObjectsInRect:[tileRect rect]];
[[boloMap undoManager] setActionName:@"Draw Rectangle"];
}
- (void)mineTool {
switch ([boloMap tileAtX:lastMouseEvent.x y:lastMouseEvent.y]) {
case kSwampTile:
[boloMap setTile:kMinedSwampTile at:lastMouseEvent];
[boloMap setAppropriateTilesForObjectsInRect:GSMakeRect(lastMouseEvent.x, lastMouseEvent.y, 1, 1)];
break;
case kCraterTile:
[boloMap setTile:kMinedCraterTile at:lastMouseEvent];
[boloMap setAppropriateTilesForObjectsInRect:GSMakeRect(lastMouseEvent.x, lastMouseEvent.y, 1, 1)];
break;
case kRoadTile:
[boloMap setTile:kMinedRoadTile at:lastMouseEvent];
[boloMap setAppropriateTilesForObjectsInRect:GSMakeRect(lastMouseEvent.x, lastMouseEvent.y, 1, 1)];
break;
case kForestTile:
[boloMap setTile:kMinedForestTile at:lastMouseEvent];
[boloMap setAppropriateTilesForObjectsInRect:GSMakeRect(lastMouseEvent.x, lastMouseEvent.y, 1, 1)];
break;
case kRubbleTile:
[boloMap setTile:kMinedRubbleTile at:lastMouseEvent];
[boloMap setAppropriateTilesForObjectsInRect:GSMakeRect(lastMouseEvent.x, lastMouseEvent.y, 1, 1)];
break;
case kGrassTile:
[boloMap setTile:kMinedGrassTile at:lastMouseEvent];
[boloMap setAppropriateTilesForObjectsInRect:GSMakeRect(lastMouseEvent.x, lastMouseEvent.y, 1, 1)];
break;
default:
break;
}
}
- (void)pillTool {
if ([boloMap pillCount] < MAX_PILLS &&
[self pillAtPoint:firstMouseEvent] == -1 &&
[self baseAtPoint:firstMouseEvent] == -1 &&
[self startAtPoint:firstMouseEvent] == -1) {
[boloMap setTile:appropriateTileForPill([boloMap tileAtX:firstMouseEvent.x y:firstMouseEvent.y]) at:GSMakePoint(firstMouseEvent.x, firstMouseEvent.y)];
[boloMap createPillAt:firstMouseEvent];
[[boloMap undoManager] setActionName:@"Add Pill"];
}
}
- (void)baseTool {
if ([boloMap baseCount] < MAX_BASES &&
[self pillAtPoint:firstMouseEvent] == -1 &&
[self baseAtPoint:firstMouseEvent] == -1 &&
[self startAtPoint:firstMouseEvent] == -1) {
[boloMap setTile:appropriateTileForBase([boloMap tileAtX:firstMouseEvent.x y:firstMouseEvent.y]) at:GSMakePoint(firstMouseEvent.x, firstMouseEvent.y)];
[boloMap createBaseAt:firstMouseEvent];
[[boloMap undoManager] setActionName:@"Add Base"];
}
}
- (void)deleteTool {
int i;
if ((i = [self pillAtPoint:firstMouseEvent]) != -1) {
[boloMap removePillAtIndex:i];
}
else if ((i = [self baseAtPoint:firstMouseEvent]) != -1) {
[boloMap removeBaseAtIndex:i];
}
else if ((i = [self startAtPoint:firstMouseEvent]) != -1) {
[boloMap removeStartAtIndex:i];
}
}
- (GSRect)getSelection {
if ([NSEvent modifierFlags] & NSShiftKeyMask) {
int x, y, width, height;
x = firstMouseEvent.x;
y = firstMouseEvent.y;
width = lastMouseEvent.x - firstMouseEvent.x;
height = lastMouseEvent.y - firstMouseEvent.y;
if (width > 0) {
width++;
}
else {
width--;
}
if (height > 0) {
height++;
}
else {
height--;
}
if (abs(width) > abs(height)) {
width = width > 0 ? abs(height) : -abs(height);
}
if (abs(width) < abs(height)) {
height = height > 0 ? abs(width) : -abs(width);
}
if (width < 0) {
x += width + 1;
width = -width;
}
if (height < 0) {
y += height + 1;
height = -height;
}
return GSMakeRect(x, y, width, height);
}
else {
return GSMakeRect(MIN(firstMouseEvent.x, lastMouseEvent.x),
MIN(firstMouseEvent.y, lastMouseEvent.y),
MAX(firstMouseEvent.x, lastMouseEvent.x) - MIN(firstMouseEvent.x, lastMouseEvent.x) + 1,
MAX(firstMouseEvent.y, lastMouseEvent.y) - MIN(firstMouseEvent.y, lastMouseEvent.y) + 1);
}
}
- (void)setUnderSelection:(GSTileRect *)newUnderSelection {
[[boloMap undoManager] registerUndoWithTarget:self selector:@selector(setUnderSelection:) object:underSelection];
[self setNeedsDisplayInSelectionRect];
[underSelection release];
underSelection = [newUnderSelection retain];
[self setNeedsDisplayInSelectionRect];
}
- (NSInteger)pillAtPoint:(GSPoint)point {
int i;
for (i = 0; i < [boloMap pillCount]; i++) {
if (GSEqualPoints(point, GSMakePoint([boloMap pillAtIndex:i].x, [boloMap pillAtIndex:i].y))) {
return i;
}
}
return -1;
}
- (NSInteger)baseAtPoint:(GSPoint)point {
int i;
for (i = 0; i < [boloMap baseCount]; i++) {
if (GSEqualPoints(point, GSMakePoint([boloMap baseAtIndex:i].x, [boloMap baseAtIndex:i].y))) {
return i;
}
}
return -1;
}
- (NSInteger)startAtPoint:(GSPoint)point {
int i;
for (i = 0; i < [boloMap startCount]; i++) {
if (GSEqualPoints(point, GSMakePoint([boloMap startAtIndex:i].x, [boloMap startAtIndex:i].y))) {
return i;
}
}
return -1;
}
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)mouseDown:(NSEvent *)event {
GSPoint mouseEvent = [self convertScreenToWorld:[event locationInWindow]];
if (GSPointInRect(kWorldRect, mouseEvent)) {
firstMouseEvent = mouseEvent;
lastMouseEvent = mouseEvent;
switch ([GSToolsController tool]) {
case kPencilTool:
{
NSUndoManager *undoManager = [boloMap undoManager];
[undoManager setGroupsByEvent:NO];
[undoManager beginUndoGrouping];
if (GSPointInRect(kSeaRect, mouseEvent) && (!underSelection || GSPointInRect([underSelection rect], mouseEvent))) {
[boloMap setTile:[GSPaletteController palette] at:lastMouseEvent];
[boloMap setAppropriateTilesForObjectsInRect:GSMakeRect(lastMouseEvent.x, lastMouseEvent.y, 1, 1)];
}
}
break;
case kFillTool:
if (GSPointInRect(kSeaRect, mouseEvent) && (!underSelection || GSPointInRect([underSelection rect], mouseEvent))) {
[self fillTool];
}
break;
case kSelectTool:
if (underSelection && GSPointInRect([underSelection rect], firstMouseEvent)) {
move = TRUE;
[[boloMap undoManager] setActionName:@"Move"];
}
else {
[self setUnderSelection:nil];
[[boloMap undoManager] setActionName:@"Clear Selection"];
}
break;
case kFilledEllipseTool:
if (GSPointInRect(kSeaRect, mouseEvent)) {
[self filledEllipseTool];
}
break;
case kFilledRectangleTool:
if (GSPointInRect(kSeaRect, mouseEvent)) {
[self filledRectangleTool];
}
break;
case kEllipseTool:
if (GSPointInRect(kSeaRect, mouseEvent)) {
[self ellipseTool];
}
break;
case kRectangleTool:
if (GSPointInRect(kSeaRect, mouseEvent)) {
[self rectangleTool];
}
break;
case kMineTool:
{
NSUndoManager *undoManager = [boloMap undoManager];
[undoManager setGroupsByEvent:NO];
[undoManager beginUndoGrouping];
if (GSPointInRect(kSeaRect, mouseEvent) && (!underSelection || GSPointInRect([underSelection rect], mouseEvent))) {
[self mineTool];
}
}
break;
case kPillTool:
if (GSPointInRect(kSeaRect, mouseEvent)) {
[self pillTool];
}
break;
case kBaseTool:
if (GSPointInRect(kSeaRect, mouseEvent)) {
[self baseTool];
}
break;
case kStartTool:
if (GSPointInRect(kSeaRect, mouseEvent) &&
[boloMap startCount] < MAX_STARTS &&
[self startAtPoint:firstMouseEvent] == -1 &&
[self pillAtPoint:firstMouseEvent] == -1 &&
[self baseAtPoint:firstMouseEvent] == -1) {
NSUndoManager *undoManager = [boloMap undoManager];
[undoManager setGroupsByEvent:NO];
[undoManager beginUndoGrouping];
// modify terrain to be valid for start placement
if ([boloMap tileAtX:firstMouseEvent.x y:firstMouseEvent.y] != kSeaTile) {
[boloMap setTile:kSeaTile at:firstMouseEvent];
}
startTool = TRUE;
start.x = firstMouseEvent.x;
start.y = firstMouseEvent.y;
start.dir = 0;
[self setNeedsDisplayInRect:GSRect2NSRect(GSMakeRect(firstMouseEvent.x, firstMouseEvent.y, 1, 1))];
}
break;
case kDeleteTool:
if (GSPointInRect(kSeaRect, mouseEvent)) {
[self deleteTool];
}
break;
default:
break;
}
}
}
- (void)mouseDragged:(NSEvent *)event {
// only if first click was accepted
if (!GSEqualPoints(firstMouseEvent, GSMakePoint(-1, -1))) {
GSPoint mouseEvent = [self convertScreenToWorld:[event locationInWindow]];
switch ([GSToolsController tool]) {
case kStartTool:
if (startTool) {
NSPoint view, startp, vect;
float dirf;
int dir;
view = [self convertPoint:[event locationInWindow] fromView:nil];
startp.x = start.x * TILE_WIDTH + (TILE_WIDTH*0.5f);
startp.y = (WIDTH - start.y - 1) * TILE_WIDTH + (TILE_WIDTH*0.5f);
vect.x = view.x - startp.x;
vect.y = view.y - startp.y;
if (vect.x != 0.0f || vect.y != 0.0f) {
dirf = atan2f(vect.y, vect.x);
if (dirf < 0.0f) {
dirf += k2Pif;
}
dir = ((int)roundf(dirf / (k2Pif / 16))) % 16;
if (start.dir != dir) {
start.dir = dir;
[self setNeedsDisplayInRect:GSRect2NSRect(GSMakeRect(lastMouseEvent.x, lastMouseEvent.y, 1, 1))];
}
}
}
break;
default:
// only process dragged invents inside mined border
if (GSPointInRect(kSeaRect, mouseEvent) && !GSEqualPoints(lastMouseEvent, mouseEvent)) {
lastMouseEvent = mouseEvent;
switch ([GSToolsController tool]) {
case kPencilTool:
if (!underSelection || GSPointInRect([underSelection rect], mouseEvent)) {
[boloMap setTile:[GSPaletteController palette] at:lastMouseEvent];
[boloMap setAppropriateTilesForObjectsInRect:GSMakeRect(lastMouseEvent.x, lastMouseEvent.y, 1, 1)];
}
break;
case kFilledEllipseTool:
[[boloMap undoManager] undo];
[self filledEllipseTool];
break;
case kFilledRectangleTool:
[[boloMap undoManager] undo];
[self filledRectangleTool];
break;
case kEllipseTool:
[[boloMap undoManager] undo];
[self ellipseTool];
break;
case kRectangleTool:
[[boloMap undoManager] undo];
[self rectangleTool];
break;
case kMineTool:
if (!underSelection || GSPointInRect([underSelection rect], mouseEvent)) {
[self mineTool];
}
break;
case kSelectTool:
// undo last move from mouse drag
[[boloMap undoManager] undo];
if (move) {
int dX = lastMouseEvent.x - firstMouseEvent.x;
int dY = lastMouseEvent.y - firstMouseEvent.y;
// copy selected rect after clipping with edge
GSTileRect *over = [boloMap tilesInRect:GSOffsetRect(GSIntersectionRect(GSOffsetRect([underSelection rect], dX, dY), kSeaRect), -dX, -dY)];
// offset copy
[over offsetX:dX y:dY];
// write under copy
[boloMap setTileRect:underSelection];
// offset objects
[boloMap offsetObjectsInRect:[underSelection rect] dX:dX dY:dY];
// set new under selection
[self setUnderSelection:[boloMap tilesInRect:[over rect]]];
// write copy
[boloMap setTileRect:over];
// for objects that entered
[boloMap setAppropriateTilesForObjectsInRect:[over rect]];
// set undo name
[[boloMap undoManager] setActionName:@"Move"];
}
else {
[self setUnderSelection:[GSTileRect tileRectWithTile:kSeaTile inRect:[self getSelection]]];
[[boloMap undoManager] setActionName:@"Select"];
}
break;
default:
break;
}
}
break;
}
}
}
- (void)mouseUp:(NSEvent *)event {
if (!GSEqualPoints(firstMouseEvent, GSMakePoint(-1, -1))) {
switch ([GSToolsController tool]) {
case kPencilTool:
{
NSUndoManager *undoManager = [boloMap undoManager];
NSString *actionName;
switch ([GSPaletteController palette]) {
case kWallTile:
actionName = @"Draw Wall";
break;
case kRiverTile:
actionName = @"Draw River";
break;
case kSwampTile:
actionName = @"Draw Swamp";
break;
case kCraterTile:
actionName = @"Draw Crater";
break;
case kRoadTile:
actionName = @"Draw Road";
break;
case kForestTile:
actionName = @"Draw Forest";
break;
case kRubbleTile:
actionName = @"Draw Rubble";
break;
case kGrassTile:
actionName = @"Draw Grass";
break;
case kDamagedWallTile:
actionName = @"Draw Damaged Wall";
break;
case kBoatTile:
actionName = @"Draw Boat";
break;
case kMinedSwampTile:
actionName = @"Draw Mined Swamp";
break;
case kMinedCraterTile:
actionName = @"Draw Mined Crater";
break;
case kMinedRoadTile:
actionName = @"Draw Mined Road";
break;
case kMinedForestTile:
actionName = @"Draw Mined Forest";
break;
case kMinedRubbleTile:
actionName = @"Draw Mined Rubble";
break;
case kMinedGrassTile:
actionName = @"Draw Mined Grass";
break;
case kSeaTile:
actionName = @"Draw Sea";
break;
default:
NSAssert(nil, @"");
break;
}
[undoManager setActionName:actionName];
[undoManager endUndoGrouping];
[undoManager setGroupsByEvent:YES];
}
break;
case kMineTool:
{
NSUndoManager *undoManager = [boloMap undoManager];
[undoManager setActionName:@"Draw Mines"];
[undoManager endUndoGrouping];
[undoManager setGroupsByEvent:YES];
}
break;
case kStartTool:
if (startTool) {
NSUndoManager *undoManager = [boloMap undoManager];
[boloMap insertStart:start atIndex:[boloMap startCount]];
[undoManager setActionName:@"Create Start"];
[undoManager endUndoGrouping];
[undoManager setGroupsByEvent:YES];
startTool = FALSE;
}
break;
case kSelectTool:
move = FALSE;
break;
default:
break;
}
firstMouseEvent = GSMakePoint(-1, -1);
lastMouseEvent = GSMakePoint(-1, -1);
}
}
- (void)flagsChanged:(NSEvent *)event {
// only if mouse is down
if (!GSEqualPoints(firstMouseEvent, GSMakePoint(-1, -1))) {
switch ([GSToolsController tool]) {
case kSelectTool:
if (underSelection) {
[[boloMap undoManager] undo];
[self setUnderSelection:[GSTileRect tileRectWithTile:kSeaTile inRect:[self getSelection]]];
[[boloMap undoManager] setActionName:@"Select"];
}
break;
case kFilledEllipseTool:
[[boloMap undoManager] undo];
[self filledEllipseTool];
break;
case kFilledRectangleTool:
[[boloMap undoManager] undo];
[self filledRectangleTool];
break;
case kEllipseTool:
[[boloMap undoManager] undo];
[self ellipseTool];
break;
case kRectangleTool:
[[boloMap undoManager] undo];
[self rectangleTool];
break;
default:
break;
}
}
}
- (IBAction)cut:(id)sender {
if (underSelection) {
NSPasteboard *pasteboard;
// prepare pasteboard for cut
pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
// copy selection to pasteboard
[pasteboard writeObjects:[NSArray arrayWithObject:[boloMap tilesInRect:[underSelection rect]]]];
// overwrite selection with kSeaTile
[boloMap setTileRect:[GSTileRect tileRectWithTile:kSeaTile inRect:[underSelection rect]]];
[boloMap setAppropriateTilesForObjectsInRect:[underSelection rect]];
[[boloMap undoManager] setActionName:@"Cut"];
}
}
- (IBAction)copy:(id)sender {
if (underSelection) {
NSPasteboard *pasteboard;
// prepare pasteboard for copy
pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
// copy selection to pasteboard
[pasteboard writeObjects:[NSArray arrayWithObject:[boloMap tilesInRect:[underSelection rect]]]];
}
}
- (IBAction)paste:(id)sender {
NSPasteboard *pasteboard;
NSArray *classArray;
NSDictionary *options;
pasteboard = [NSPasteboard generalPasteboard];
classArray = [NSArray arrayWithObject:[GSTileRect class]];
options = [NSDictionary dictionary];
if ([pasteboard canReadObjectForClasses:classArray options:options]) {
NSArray *objectsToPaste;
GSTileRect *tileRect;
objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
tileRect = [objectsToPaste objectAtIndex:0];
[self setUnderSelection:[boloMap tilesInRect:[tileRect rect]]];
[boloMap setTileRect:tileRect];
[boloMap setAppropriateTilesForObjectsInRect:[tileRect rect]];
[[boloMap undoManager] setActionName:@"Paste"];
}
}
- (IBAction)delete:(id)sender {
[boloMap setTileRect:[GSTileRect tileRectWithTile:kSeaTile inRect:underSelection == nil ? kSeaRect : [underSelection rect]]];
[boloMap deleteObjectsInRect:underSelection == nil ? kSeaRect : [underSelection rect]];
[[boloMap undoManager] setActionName:@"Delete"];
}
- (IBAction)selectAll:(id)sender {
[self setUnderSelection:[GSTileRect tileRectWithTile:kSeaTile inRect:[boloMap mapRect]]];
[[boloMap undoManager] setActionName:@"Select All"];
}
- (IBAction)clearSelection:(id)sender {
[self setUnderSelection:nil];
[[boloMap undoManager] setActionName:@"Clear Selection"];
}
- (IBAction)rotateLeft:(id)sender {
if (underSelection) {
GSTileRect *tileRect;
tileRect = [boloMap tilesInRect:[underSelection rect]];
[tileRect rotateLeft];
[boloMap rotateLeftObjectsInRect:[underSelection rect]];
[boloMap setTileRect:underSelection];
[self setUnderSelection:[boloMap tilesInRect:[tileRect rect]]];
[boloMap setTileRect:tileRect];
[boloMap setAppropriateTilesForObjectsInRect:[tileRect rect]];
}
else {
GSTileRect *tileRect;
[boloMap rotateLeftObjectsInRect:kSeaRect];
tileRect = [boloMap tilesInRect:kSeaRect];
[tileRect rotateLeft];
[boloMap setTileRect:tileRect];
[boloMap setAppropriateTilesForObjectsInRect:[tileRect rect]];
}
[[boloMap undoManager] setActionName:@"Rotate Left"];
}
- (IBAction)rotateRight:(id)sender {
if (underSelection) {
GSTileRect *tileRect;
tileRect = [boloMap tilesInRect:[underSelection rect]];
[tileRect rotateRight];
[boloMap rotateRightObjectsInRect:[underSelection rect]];
[boloMap setTileRect:underSelection];
[self setUnderSelection:[boloMap tilesInRect:[tileRect rect]]];
[boloMap setTileRect:tileRect];
[boloMap setAppropriateTilesForObjectsInRect:[tileRect rect]];
}
else {
GSTileRect *tileRect;
[boloMap rotateRightObjectsInRect:kSeaRect];
tileRect = [boloMap tilesInRect:kSeaRect];
[tileRect rotateRight];
[boloMap setTileRect:tileRect];
[boloMap setAppropriateTilesForObjectsInRect:[tileRect rect]];
}
[[boloMap undoManager] setActionName:@"Rotate Right"];
}
- (IBAction)flipHorizontal:(id)sender {
if (underSelection) {
GSTileRect *tileRect;
[boloMap flipHorinzontalObjectsInRect:[underSelection rect]];
tileRect = [boloMap tilesInRect:[underSelection rect]];
[tileRect flipHorizontal];
[boloMap setTileRect:tileRect];
}
else {
GSTileRect *tileRect;
[boloMap flipHorinzontalObjectsInRect:kSeaRect];
tileRect = [boloMap tilesInRect:kSeaRect];
[tileRect flipHorizontal];
[boloMap setTileRect:tileRect];
}
[[boloMap undoManager] setActionName:@"Flip Horizontal"];
}
- (IBAction)flipVertical:(id)sender {
if (underSelection) {
GSTileRect *tileRect;
[boloMap flipVerticalObjectsInRect:[underSelection rect]];
tileRect = [boloMap tilesInRect:[underSelection rect]];
[tileRect flipVertical];
[boloMap setTileRect:tileRect];
}
else {
GSTileRect *tileRect;
[boloMap flipVerticalObjectsInRect:kSeaRect];
tileRect = [boloMap tilesInRect:kSeaRect];
[tileRect flipVertical];
[boloMap setTileRect:tileRect];
}
[[boloMap undoManager] setActionName:@"Flip Vertical"];
}
- (IBAction)center:(id)sender {
NSRect rect = GSRect2NSRect([boloMap mapRect]);
NSSize size = [self visibleRect].size;
[self scrollRectToVisible:NSInsetRect(rect, (NSWidth(rect) - size.width) * 0.5f, (NSHeight(rect) - size.height) * 0.5f)];
}
- (BOOL)validateUserInterfaceItem:(id < NSValidatedUserInterfaceItem >)anItem {
if ([anItem action] == @selector(cut:)) {
return underSelection != nil;
}
else if ([anItem action] == @selector(copy:)) {
return underSelection != nil;
}
else if ([anItem action] == @selector(paste:)) {
NSPasteboard *pasteboard;
NSArray *classArray;
NSDictionary *options;
pasteboard = [NSPasteboard generalPasteboard];
classArray = [NSArray arrayWithObject:[GSTileRect class]];
options = [NSDictionary dictionary];
return [pasteboard canReadObjectForClasses:classArray options:options];
}
else if ([anItem action] == @selector(delete:)) {
return TRUE;
}
else if ([anItem action] == @selector(selectAll:)) {
return !underSelection || !GSEqualRects([underSelection rect], [boloMap mapRect]);
}
else if ([anItem action] == @selector(clearSelection:)) {
return underSelection != nil;
}
else if ([anItem action] == @selector(rotateLeft:)) {
return TRUE;
}
else if ([anItem action] == @selector(rotateRight:)) {
return TRUE;
}
else if ([anItem action] == @selector(flipHorizontal:)) {
return TRUE;
}
else if ([anItem action] == @selector(flipVertical:)) {
return TRUE;
}
else if ([anItem action] == @selector(center:)) {
return TRUE;
}
return NO;