-
Notifications
You must be signed in to change notification settings - Fork 2
/
LevelView+Events.m
387 lines (346 loc) · 14.4 KB
/
LevelView+Events.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
//
// LevelView+Events.m
// LevelEditor
//
// Created by Michael Markowski on 14.12.09.
// Copyright (c) 2010 Artifacts - Fine Software Development
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "LevelView+Events.h"
#import "Model.h"
#import "AFGameEditor.h"
#import "Layer.h"
#import "NSColor+Hex.h"
#import "NSImage+Additions.h"
@implementation LevelView (Events)
- (IBAction)bgColorChanged:(id)sender {
[self setNeedsDisplay:YES];
}
- (IBAction)gridSizeSliderValueChanged:(id)sender
{
self.gridSize = [gridSizeSlider objectValue];
[gridSizeLabel setStringValue:[gridSize stringValue]];
[self setNeedsDisplay:YES];
}
- (IBAction)spriteAplhaValueChanged:(id)sender
{
[self setNeedsDisplay:YES];
}
- (IBAction)layerAplhaValueChanged:(id)sender
{
[self setNeedsDisplay:YES];
}
- (IBAction)layerTintValueChanged:(id)sender
{
[self setNeedsDisplay:YES];
}
- (IBAction)spriteImpactValueChanged:(id)sender
{
[self setNeedsDisplay:YES];
}
- (IBAction)toggleShowGrid:(id)sender {
[self setNeedsDisplay:YES];
}
- (void)rightMouseDown:(NSEvent *)theEvent {
[self mouseDown:theEvent];
}
- (void)keyDown:(NSEvent *)theEvent {
AFGameEditor *doc = (AFGameEditor*)[[NSDocumentController sharedDocumentController] currentDocument];
if ([[doc.selectedSprites arrangedObjects] count]==0) return;
unichar firstChar = [[theEvent characters] characterAtIndex: 0];
if ( ( firstChar == NSDeleteFunctionKey || firstChar == NSDeleteCharFunctionKey || firstChar == NSDeleteCharacter)) {
[self deleteSprite:nil];
}
if ([theEvent modifierFlags] & NSNumericPadKeyMask) { // arrow keys have this mask
int nudgeAmount = 1;
if ([theEvent modifierFlags] & NSShiftKeyMask) nudgeAmount = [gridSize floatValue];
NSString *theArrow = [theEvent charactersIgnoringModifiers];
unichar keyChar = 0;
if ( [theArrow length] == 0 )
return; // reject dead keys
if ( [theArrow length] == 1 ) {
keyChar = [theArrow characterAtIndex:0];
if ( keyChar == NSLeftArrowFunctionKey ) {
for (Sprite *s in [doc.selectedSprites arrangedObjects]) {
float x = [s.x floatValue];
x = fmax(-s.size.width-1, x-nudgeAmount);
s.x = [NSNumber numberWithFloat:x];
}
[self setNeedsDisplay:YES];
return;
}
if ( keyChar == NSRightArrowFunctionKey ) {
for (Sprite *s in [doc.selectedSprites arrangedObjects]) {
s.x = [NSNumber numberWithFloat:[s.x floatValue] + nudgeAmount];
}
[self setNeedsDisplay:YES];
return;
}
if ( keyChar == NSUpArrowFunctionKey ) {
for (Sprite *s in [doc.selectedSprites arrangedObjects]) {
s.y = [NSNumber numberWithFloat:[s.y floatValue] + nudgeAmount];
}
[self setNeedsDisplay:YES];
return;
}
if ( keyChar == NSDownArrowFunctionKey ) {
for (Sprite *s in [doc.selectedSprites arrangedObjects]) {
float y = [s.y floatValue];
y = fmax(-s.size.height-1, y-nudgeAmount);
s.y = [NSNumber numberWithFloat:y];
}
[self setNeedsDisplay:YES];
return;
}
[super keyDown:theEvent];
}
}
[super keyDown:theEvent];
}
- (void)mouseDown:(NSEvent *)theEvent {
spriteInteractionPending = YES;
AFGameEditor *doc = (AFGameEditor*)[[NSDocumentController sharedDocumentController] currentDocument];
BOOL keepOn = YES;
BOOL isInside = YES;
BOOL drawNewSelection = NO;
NSPoint mouseOffsets[[[doc.selectedSprites arrangedObjects] count]];
NSPoint mouseDownLocation = [self convertPoint:[theEvent locationInWindow] fromView:nil];
// NSMutableArray *sprites = [selectedLayer valueForKey:@"sprites"];
NSManagedObjectContext *moc = [doc managedObjectContext];
NSPoint mouseToSelectionRectOffset = NSMakePoint(mouseDownLocation.x - selectionRect.origin.x,
mouseDownLocation.y - selectionRect.origin.y);
NSSize grid = NSMakeSize([gridSize floatValue], [gridSize floatValue]);
Sprite *selectedSprite;
[[self window] makeFirstResponder:self];
switch (doc.editMode) {
case kEditModeMoveAndDrag:
selectedSprite = [self spriteAtPosition:mouseDownLocation];
if (selectedSprite && (selectionRect.size.width==0 && selectionRect.size.height==0)) {
if (!([theEvent modifierFlags] & NSCommandKeyMask) && [[doc.selectedSprites arrangedObjects] count]<2) {
[doc.selectedSprites removeObjects:[doc.selectedSprites arrangedObjects]];
}
[doc.selectedSprites addObject:selectedSprite];
}
break;
case kEditModeSelect:
if (!NSPointInRect(mouseDownLocation, selectionRect)) {
selectionRect.origin = mouseDownLocation;
selectionRect.size = NSMakeSize(0, 0);
[doc.selectedSprites removeObjects:[doc.selectedSprites arrangedObjects]];
drawNewSelection = YES;
}
break;
case kEditModeDraw:
{
NSPoint point = mouseDownLocation;
selectedSprite = [self spriteAtPosition:point];
if (!selectedSprite && doc.selectedTexture) {
selectedSprite = [NSEntityDescription insertNewObjectForEntityForName:@"Sprite" inManagedObjectContext:moc];
point.x = (int)(point.x - ((int)point.x) % (int)grid.width);
point.y = (int)(point.y - ((int)point.y) % (int)grid.height);
selectedSprite.location = point;
selectedSprite.key = doc.selectedTexture.key;
selectedSprite.layer = selectedLayer;
NSArrayController *controller = doc.textureAtlasArrayController;
id atlas = [[controller selection] valueForKey:@"self"];
selectedSprite.textureAtlas = atlas;
[[[doc managedObjectContext] undoManager] beginUndoGrouping];
[[[doc managedObjectContext] undoManager] setActionName:@"Undo Draw Sprite"];
[doc.selectedSprites removeObjects:[doc.selectedSprites arrangedObjects]];
[doc.selectedSprites addObject:selectedSprite];
[[[doc managedObjectContext] undoManager] endUndoGrouping];
} else {
[doc.selectedSprites removeObjects:[doc.selectedSprites arrangedObjects]];
[doc.selectedSprites addObject:selectedSprite];
}
break;
}
}
// save distance from mouse pointer to selected sprites
int count=0;
for (Sprite *s in [doc.selectedSprites arrangedObjects]) {
NSPoint mouseOffset = NSMakePoint(mouseDownLocation.x - s.location.x,
mouseDownLocation.y - s.location.y);
mouseOffsets[count] = mouseOffset;
count++;
}
while (keepOn) {
if ([selectedLayer.visibleInEditor boolValue]==NO) {
NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"No visible layer selected", @"No visible layer selected")
defaultButton:NSLocalizedString(@"Ok", @"Ok Button")
alternateButton:nil
otherButton:nil
informativeTextWithFormat:NSLocalizedString(@"The layer you want to draw on is currently not visible in the editor. \
Please select another layer or turn on visibility.",
@"Layer not visible for drawing alert.")];
[alert beginSheetModalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
keepOn = NO;
}
theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask | NSRightMouseUpMask | NSLeftMouseDraggedMask];
isInside = [self mouse:mouseDownLocation inRect:[self bounds]];
NSPoint currentMouseLocation = [self convertPoint:[theEvent locationInWindow] fromView:nil];
switch ([theEvent type]) {
case NSLeftMouseDragged:
if (!isInside) break;
switch (doc.editMode) {
// SELECT MODE
case kEditModeSelect:
if (drawNewSelection==YES) {
// enlarge selection rectangle
CGFloat width, height;
width = currentMouseLocation.x-selectionRect.origin.x;
height = currentMouseLocation.y-selectionRect.origin.y;
selectionRect.size = NSMakeSize(width, height);
[doc.selectedSprites removeObjects:[doc.selectedSprites arrangedObjects]];
[doc.selectedSprites addObjects:[self spritesInRect:selectionRect]];
// [self scrollPoint:currentMouseLocation];
[self autoscroll:theEvent];
} else {
// move selection rectangle
NSPoint newRectLocation = currentMouseLocation;
//newRectLocation.x = (int)(newRectLocation.x - ((int)newRectLocation.x) % (int)grid.width);
//newRectLocation.y = (int)(newRectLocation.y - ((int)newRectLocation.y) % (int)grid.height);
selectionRect.origin = newRectLocation;
selectionRect.origin.x -= mouseToSelectionRectOffset.x;
selectionRect.origin.y -= mouseToSelectionRectOffset.y;
}
break;
// DRAW MODE
case kEditModeDraw:
[self updateSprites:[doc.selectedSprites arrangedObjects] position:currentMouseLocation mouseOffsets:mouseOffsets];
break;
// MOVE & DRAG MODE
case kEditModeMoveAndDrag:
[self updateSprites:[doc.selectedSprites arrangedObjects] position:currentMouseLocation mouseOffsets:mouseOffsets];
break;
}
break;
case NSLeftMouseUp: {
keepOn = NO;
break;
}
case NSRightMouseUp:
if ([[doc.selectedSprites arrangedObjects] count]==0) {
keepOn = NO;
break;
}
NSMenu *theMenu = [[[NSMenu alloc] initWithTitle:@"Edit Sprite"] autorelease];
[theMenu insertItemWithTitle:@"Delete" action:@selector(deleteSprite:) keyEquivalent:@"" atIndex:0];
[theMenu insertItem:[NSMenuItem separatorItem] atIndex:1];
[theMenu insertItemWithTitle:@"Send forward" action:@selector(sendSpriteForward:) keyEquivalent:@"" atIndex:2];
[theMenu insertItemWithTitle:@"Send backward" action:@selector(sendSpriteBackward:) keyEquivalent:@"" atIndex:3];
[theMenu insertItem:[NSMenuItem separatorItem] atIndex:4];
[theMenu insertItemWithTitle:@"Reset zIndex" action:@selector(resetSpriteZIndex:) keyEquivalent:@"" atIndex:5];
[theMenu insertItem:[NSMenuItem separatorItem] atIndex:6];
[theMenu insertItemWithTitle:@"Send to front" action:@selector(bringSpriteToFront:) keyEquivalent:@"" atIndex:7];
[theMenu insertItemWithTitle:@"Send to back" action:@selector(sendSpriteToBack:) keyEquivalent:@"" atIndex:8];
[NSMenu popUpContextMenu:theMenu withEvent:theEvent forView:self];
keepOn = NO;
break;
default:
/* Ignore any other kind of event. */
break;
}
[self setNeedsDisplay:YES];
};
[NSCursor pop];
spriteInteractionPending = NO;
return;
}
- (NSInteger)highestZIndex {
NSArray *sprites = [self spritesSortedByZIndex:NO];
NSNumber *maxZIndex = [[sprites objectAtIndex:0] valueForKey:@"zIndex"];
NSLog(@"highest zIndex: %@", maxZIndex);
return [maxZIndex integerValue];
}
- (NSArray*)spritesSortedByZIndex:(BOOL)ascending {
AFGameEditor *doc = (AFGameEditor*)[[NSDocumentController sharedDocumentController] currentDocument];
if ([[doc.spriteArrayController arrangedObjects] count]==0) return 0;
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"zIndex" ascending:ascending] autorelease];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[doc.spriteArrayController setSortDescriptors:sortDescriptors];
NSArray *result = [doc.spriteArrayController arrangedObjects];
[doc.spriteArrayController setSortDescriptors:nil];
[sortDescriptors release];
return result;
}
- (NSInteger)lowestZIndex {
NSArray *sprites = [self spritesSortedByZIndex:YES];
NSNumber *minZIndex = [[sprites objectAtIndex:0] valueForKey:@"zIndex"];
NSLog(@"lowest zIndex: %@", minZIndex);
return [minZIndex integerValue];
}
- (void)sendSpriteForward:(id)sender {
AFGameEditor *doc = (AFGameEditor*)[[NSDocumentController sharedDocumentController] currentDocument];
for (Sprite *sprite in [doc.selectedSprites arrangedObjects]) {
NSInteger currentZIndex = [[sprite valueForKey:@"zIndex"] integerValue];
currentZIndex++;
[sprite setValue:[NSNumber numberWithInteger:currentZIndex] forKey:@"zIndex"];
NSLog(@"sprite: %@", [sprite valueForKey:@"zIndex"]);
}
[self setNeedsDisplay:YES];
}
- (void)sendSpriteBackward:(id)sender {
AFGameEditor *doc = (AFGameEditor*)[[NSDocumentController sharedDocumentController] currentDocument];
for (Sprite *sprite in [doc.selectedSprites arrangedObjects]) {
NSInteger currentZIndex = [[sprite valueForKey:@"zIndex"] integerValue];
currentZIndex--;
[sprite setValue:[NSNumber numberWithInteger:currentZIndex] forKey:@"zIndex"];
}
[self setNeedsDisplay:YES];
}
- (void)bringSpriteToFront:(id)sender {
NSInteger z = [self highestZIndex]; z++;
[self setSelectedSpritesZIndex:z];
}
- (void)sendSpriteToBack:(id)sender {
NSInteger z = [self lowestZIndex]; z--;
[self setSelectedSpritesZIndex:z];
}
- (void)resetSpriteZIndex:(id)sender {
[self setSelectedSpritesZIndex:0];
}
- (void)setSelectedSpritesZIndex:(int)zIndex {
AFGameEditor *doc = (AFGameEditor*)[[NSDocumentController sharedDocumentController] currentDocument];
for (Sprite *sprite in [doc.selectedSprites arrangedObjects]) {
[sprite setValue:[NSNumber numberWithInteger:zIndex] forKey:@"zIndex"];
}
[self setNeedsDisplay:YES];
}
- (void)updateSprites:(NSArray*)sprites position:(NSPoint)position mouseOffsets:(CGPoint[])mouseOffsets {
int count=0;
NSPoint newLocation;
AFGameEditor *doc = (AFGameEditor*)[[NSDocumentController sharedDocumentController] currentDocument];
if ([doc.game.showGridInEditor boolValue]==YES) {
NSSize grid = NSMakeSize([gridSize floatValue], [gridSize floatValue]);
CGFloat xSnappedToGrid = ((int)position.x/(int)grid.width) * (int)grid.width;
CGFloat ySnappedToGrid = ((int)position.y/(int)grid.height) * (int)grid.height;
position.x = xSnappedToGrid;
position.y = ySnappedToGrid;
}
for (Sprite *s in sprites) {
newLocation = position;
newLocation.x -= mouseOffsets[count].x;
newLocation.y -= mouseOffsets[count].y;
s.location = newLocation;
count++;
}
}
- (void)alertDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
[[alert window] orderOut:nil];
}
@end