jens@0
|
1 |
/* This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
|
jens@0
|
2 |
http://developer.apple.com/samplecode/GeekGameBoard/
|
jens@0
|
3 |
Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
|
jens@0
|
4 |
|
jens@0
|
5 |
Redistribution and use in source and binary forms, with or without modification, are permitted
|
jens@0
|
6 |
provided that the following conditions are met:
|
jens@0
|
7 |
|
jens@0
|
8 |
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
jens@0
|
9 |
and the following disclaimer.
|
jens@0
|
10 |
* Redistributions in binary form must reproduce the above copyright notice, this list of
|
jens@0
|
11 |
conditions and the following disclaimer in the documentation and/or other materials provided
|
jens@0
|
12 |
with the distribution.
|
jens@0
|
13 |
|
jens@0
|
14 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
jens@0
|
15 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
jens@0
|
16 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
|
jens@0
|
17 |
BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
jens@0
|
18 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
jens@0
|
19 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
jens@0
|
20 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
jens@0
|
21 |
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
jens@0
|
22 |
*/
|
jens@0
|
23 |
#import "BoardView.h"
|
jens@0
|
24 |
#import "Bit.h"
|
jens@0
|
25 |
#import "BitHolder.h"
|
jens@0
|
26 |
#import "Game.h"
|
jens@0
|
27 |
#import "QuartzUtils.h"
|
jens@0
|
28 |
#import "GGBUtils.h"
|
jens@0
|
29 |
|
jens@0
|
30 |
|
jens@0
|
31 |
@implementation BoardView
|
jens@0
|
32 |
|
jens@0
|
33 |
|
jens@0
|
34 |
@synthesize game=_game, gameboard=_gameboard;
|
jens@0
|
35 |
|
jens@0
|
36 |
|
jens@0
|
37 |
- (void) dealloc
|
jens@0
|
38 |
{
|
jens@0
|
39 |
[_game release];
|
jens@0
|
40 |
[super dealloc];
|
jens@0
|
41 |
}
|
jens@0
|
42 |
|
jens@0
|
43 |
|
jens@0
|
44 |
- (void) startGameNamed: (NSString*)gameClassName
|
jens@0
|
45 |
{
|
jens@0
|
46 |
if( _gameboard ) {
|
jens@0
|
47 |
[_gameboard removeFromSuperlayer];
|
jens@0
|
48 |
_gameboard = nil;
|
jens@0
|
49 |
}
|
jens@0
|
50 |
_gameboard = [[CALayer alloc] init];
|
jens@0
|
51 |
_gameboard.frame = [self gameBoardFrame];
|
jens@0
|
52 |
_gameboard.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
|
jens@0
|
53 |
[self.layer addSublayer: _gameboard];
|
jens@0
|
54 |
[_gameboard release];
|
jens@0
|
55 |
|
jens@0
|
56 |
Class gameClass = NSClassFromString(gameClassName);
|
jens@0
|
57 |
setObj(&_game, [[gameClass alloc] initWithBoard: _gameboard]);
|
jens@0
|
58 |
}
|
jens@0
|
59 |
|
jens@0
|
60 |
|
jens@0
|
61 |
- (CGRect) gameBoardFrame
|
jens@0
|
62 |
{
|
jens@0
|
63 |
return self.layer.bounds;
|
jens@0
|
64 |
}
|
jens@0
|
65 |
|
jens@0
|
66 |
|
jens@0
|
67 |
- (void)resetCursorRects
|
jens@0
|
68 |
{
|
jens@0
|
69 |
[super resetCursorRects];
|
jens@0
|
70 |
[self addCursorRect: self.bounds cursor: [NSCursor openHandCursor]];
|
jens@0
|
71 |
}
|
jens@0
|
72 |
|
jens@0
|
73 |
|
jens@0
|
74 |
- (IBAction) enterFullScreen: (id)sender
|
jens@0
|
75 |
{
|
jens@0
|
76 |
if( self.isInFullScreenMode ) {
|
jens@0
|
77 |
[self exitFullScreenModeWithOptions: nil];
|
jens@0
|
78 |
} else {
|
jens@0
|
79 |
[self enterFullScreenMode: self.window.screen
|
jens@0
|
80 |
withOptions: nil];
|
jens@0
|
81 |
}
|
jens@0
|
82 |
}
|
jens@0
|
83 |
|
jens@0
|
84 |
|
jens@0
|
85 |
#pragma mark -
|
jens@0
|
86 |
#pragma mark KEY EVENTS:
|
jens@0
|
87 |
|
jens@0
|
88 |
|
jens@0
|
89 |
- (void) keyDown: (NSEvent*)ev
|
jens@0
|
90 |
{
|
jens@0
|
91 |
if( self.isInFullScreenMode ) {
|
jens@0
|
92 |
if( [ev.charactersIgnoringModifiers hasPrefix: @"\033"] ) // Esc key
|
jens@0
|
93 |
[self enterFullScreen: self];
|
jens@0
|
94 |
}
|
jens@0
|
95 |
}
|
jens@0
|
96 |
|
jens@0
|
97 |
|
jens@0
|
98 |
#pragma mark -
|
jens@0
|
99 |
#pragma mark HIT-TESTING:
|
jens@0
|
100 |
|
jens@0
|
101 |
|
jens@0
|
102 |
// Hit-testing callbacks (to identify which layers caller is interested in):
|
jens@0
|
103 |
typedef BOOL (*LayerMatchCallback)(CALayer*);
|
jens@0
|
104 |
|
jens@0
|
105 |
static BOOL layerIsBit( CALayer* layer ) {return [layer isKindOfClass: [Bit class]];}
|
jens@0
|
106 |
static BOOL layerIsBitHolder( CALayer* layer ) {return [layer conformsToProtocol: @protocol(BitHolder)];}
|
jens@0
|
107 |
static BOOL layerIsDropTarget( CALayer* layer ) {return [layer respondsToSelector: @selector(draggingEntered:)];}
|
jens@0
|
108 |
|
jens@0
|
109 |
|
jens@0
|
110 |
/** Locates the layer at a given point in window coords.
|
jens@0
|
111 |
If the leaf layer doesn't pass the layer-match callback, the nearest ancestor that does is returned.
|
jens@0
|
112 |
If outOffset is provided, the point's position relative to the layer is stored into it. */
|
jens@0
|
113 |
- (CALayer*) hitTestPoint: (NSPoint)locationInWindow
|
jens@0
|
114 |
forLayerMatching: (LayerMatchCallback)match
|
jens@0
|
115 |
offset: (CGPoint*)outOffset
|
jens@0
|
116 |
{
|
jens@0
|
117 |
CGPoint where = NSPointToCGPoint([self convertPoint: locationInWindow fromView: nil]);
|
jens@0
|
118 |
where = [_gameboard convertPoint: where fromLayer: self.layer];
|
jens@0
|
119 |
CALayer *layer = [_gameboard hitTest: where];
|
jens@0
|
120 |
while( layer ) {
|
jens@0
|
121 |
if( match(layer) ) {
|
jens@0
|
122 |
CGPoint bitPos = [self.layer convertPoint: layer.position
|
jens@0
|
123 |
fromLayer: layer.superlayer];
|
jens@0
|
124 |
if( outOffset )
|
jens@0
|
125 |
*outOffset = CGPointMake( bitPos.x-where.x, bitPos.y-where.y);
|
jens@0
|
126 |
return layer;
|
jens@0
|
127 |
} else
|
jens@0
|
128 |
layer = layer.superlayer;
|
jens@0
|
129 |
}
|
jens@0
|
130 |
return nil;
|
jens@0
|
131 |
}
|
jens@0
|
132 |
|
jens@0
|
133 |
|
jens@0
|
134 |
#pragma mark -
|
jens@0
|
135 |
#pragma mark MOUSE CLICKS & DRAGS:
|
jens@0
|
136 |
|
jens@0
|
137 |
|
jens@0
|
138 |
- (void) mouseDown: (NSEvent*)ev
|
jens@0
|
139 |
{
|
jens@0
|
140 |
_dragStartPos = ev.locationInWindow;
|
jens@0
|
141 |
_dragBit = (Bit*) [self hitTestPoint: _dragStartPos
|
jens@0
|
142 |
forLayerMatching: layerIsBit
|
jens@0
|
143 |
offset: &_dragOffset];
|
jens@0
|
144 |
if( _dragBit ) {
|
jens@0
|
145 |
_dragMoved = NO;
|
jens@0
|
146 |
_dropTarget = nil;
|
jens@0
|
147 |
_oldHolder = _dragBit.holder;
|
jens@0
|
148 |
// Ask holder's and game's permission before dragging:
|
jens@0
|
149 |
if( _oldHolder )
|
jens@0
|
150 |
_dragBit = [_oldHolder canDragBit: _dragBit];
|
jens@0
|
151 |
if( _dragBit && ! [_game canBit: _dragBit moveFrom: _oldHolder] ) {
|
jens@0
|
152 |
[_oldHolder cancelDragBit: _dragBit];
|
jens@0
|
153 |
_dragBit = nil;
|
jens@0
|
154 |
}
|
jens@0
|
155 |
if( ! _dragBit ) {
|
jens@0
|
156 |
_oldHolder = nil;
|
jens@0
|
157 |
NSBeep();
|
jens@0
|
158 |
return;
|
jens@0
|
159 |
}
|
jens@0
|
160 |
// Start dragging:
|
jens@0
|
161 |
_oldSuperlayer = _dragBit.superlayer;
|
jens@0
|
162 |
_oldLayerIndex = [_oldSuperlayer.sublayers indexOfObjectIdenticalTo: _dragBit];
|
jens@0
|
163 |
_oldPos = _dragBit.position;
|
jens@0
|
164 |
ChangeSuperlayer(_dragBit, self.layer, self.layer.sublayers.count);
|
jens@0
|
165 |
_dragBit.pickedUp = YES;
|
jens@0
|
166 |
[[NSCursor closedHandCursor] push];
|
jens@0
|
167 |
} else
|
jens@0
|
168 |
NSBeep();
|
jens@0
|
169 |
}
|
jens@0
|
170 |
|
jens@0
|
171 |
- (void) mouseDragged: (NSEvent*)ev
|
jens@0
|
172 |
{
|
jens@0
|
173 |
if( _dragBit ) {
|
jens@0
|
174 |
// Get the mouse position, and see if we've moved 3 pixels since the mouseDown:
|
jens@0
|
175 |
NSPoint pos = ev.locationInWindow;
|
jens@0
|
176 |
if( fabs(pos.x-_dragStartPos.x)>=3 || fabs(pos.y-_dragStartPos.y)>=3 )
|
jens@0
|
177 |
_dragMoved = YES;
|
jens@0
|
178 |
|
jens@0
|
179 |
// Move the _dragBit (without animation -- it's unnecessary and slows down responsiveness):
|
jens@0
|
180 |
NSPoint where = [self convertPoint: pos fromView: nil];
|
jens@0
|
181 |
where.x += _dragOffset.x;
|
jens@0
|
182 |
where.y += _dragOffset.y;
|
jens@0
|
183 |
|
jens@0
|
184 |
CGPoint newPos = [_dragBit.superlayer convertPoint: NSPointToCGPoint(where)
|
jens@0
|
185 |
fromLayer: self.layer];
|
jens@0
|
186 |
|
jens@0
|
187 |
[CATransaction flush];
|
jens@0
|
188 |
[CATransaction begin];
|
jens@0
|
189 |
[CATransaction setValue:(id)kCFBooleanTrue
|
jens@0
|
190 |
forKey:kCATransactionDisableActions];
|
jens@0
|
191 |
_dragBit.position = newPos;
|
jens@0
|
192 |
[CATransaction commit];
|
jens@0
|
193 |
|
jens@0
|
194 |
// Find what it's over:
|
jens@0
|
195 |
id<BitHolder> target = (id<BitHolder>) [self hitTestPoint: where
|
jens@0
|
196 |
forLayerMatching: layerIsBitHolder
|
jens@0
|
197 |
offset: NULL];
|
jens@0
|
198 |
if( target == _oldHolder )
|
jens@0
|
199 |
target = nil;
|
jens@0
|
200 |
if( target != _dropTarget ) {
|
jens@0
|
201 |
[_dropTarget willNotDropBit: _dragBit];
|
jens@0
|
202 |
_dropTarget.highlighted = NO;
|
jens@0
|
203 |
_dropTarget = nil;
|
jens@0
|
204 |
}
|
jens@0
|
205 |
if( target ) {
|
jens@0
|
206 |
CGPoint targetPos = [(CALayer*)target convertPoint: _dragBit.position
|
jens@0
|
207 |
fromLayer: _dragBit.superlayer];
|
jens@0
|
208 |
if( [target canDropBit: _dragBit atPoint: targetPos]
|
jens@0
|
209 |
&& [_game canBit: _dragBit moveFrom: _oldHolder to: target] ) {
|
jens@0
|
210 |
_dropTarget = target;
|
jens@0
|
211 |
_dropTarget.highlighted = YES;
|
jens@0
|
212 |
}
|
jens@0
|
213 |
}
|
jens@0
|
214 |
}
|
jens@0
|
215 |
}
|
jens@0
|
216 |
|
jens@0
|
217 |
- (void) mouseUp: (NSEvent*)ev
|
jens@0
|
218 |
{
|
jens@0
|
219 |
if( _dragBit ) {
|
jens@0
|
220 |
if( _dragMoved ) {
|
jens@0
|
221 |
// Update the drag tracking to the final mouse position:
|
jens@0
|
222 |
[self mouseDragged: ev];
|
jens@0
|
223 |
_dropTarget.highlighted = NO;
|
jens@0
|
224 |
_dragBit.pickedUp = NO;
|
jens@0
|
225 |
|
jens@0
|
226 |
// Is the move legal?
|
jens@0
|
227 |
if( _dropTarget && [_dropTarget dropBit: _dragBit
|
jens@0
|
228 |
atPoint: [(CALayer*)_dropTarget convertPoint: _dragBit.position
|
jens@0
|
229 |
fromLayer: _dragBit.superlayer]] ) {
|
jens@0
|
230 |
// Yes, notify the interested parties:
|
jens@0
|
231 |
[_oldHolder draggedBit: _dragBit to: _dropTarget];
|
jens@0
|
232 |
[_game bit: _dragBit movedFrom: _oldHolder to: _dropTarget];
|
jens@0
|
233 |
} else {
|
jens@0
|
234 |
// Nope, cancel:
|
jens@0
|
235 |
[_dropTarget willNotDropBit: _dragBit];
|
jens@0
|
236 |
ChangeSuperlayer(_dragBit, _oldSuperlayer, _oldLayerIndex);
|
jens@0
|
237 |
_dragBit.position = _oldPos;
|
jens@0
|
238 |
[_oldHolder cancelDragBit: _dragBit];
|
jens@0
|
239 |
}
|
jens@0
|
240 |
} else {
|
jens@0
|
241 |
// Just a click, without a drag:
|
jens@0
|
242 |
_dropTarget.highlighted = NO;
|
jens@0
|
243 |
_dragBit.pickedUp = NO;
|
jens@0
|
244 |
ChangeSuperlayer(_dragBit, _oldSuperlayer, _oldLayerIndex);
|
jens@0
|
245 |
[_oldHolder cancelDragBit: _dragBit];
|
jens@0
|
246 |
if( ! [_game clickedBit: _dragBit] )
|
jens@0
|
247 |
NSBeep();
|
jens@0
|
248 |
}
|
jens@0
|
249 |
_dropTarget = nil;
|
jens@0
|
250 |
_dragBit = nil;
|
jens@0
|
251 |
[NSCursor pop];
|
jens@0
|
252 |
}
|
jens@0
|
253 |
}
|
jens@0
|
254 |
|
jens@0
|
255 |
|
jens@0
|
256 |
#pragma mark -
|
jens@0
|
257 |
#pragma mark INCOMING DRAGS:
|
jens@0
|
258 |
|
jens@0
|
259 |
|
jens@0
|
260 |
// subroutine to call the target
|
jens@0
|
261 |
static int tell( id target, SEL selector, id arg, int defaultValue )
|
jens@0
|
262 |
{
|
jens@0
|
263 |
if( target && [target respondsToSelector: selector] )
|
jens@0
|
264 |
return (ssize_t) [target performSelector: selector withObject: arg];
|
jens@0
|
265 |
else
|
jens@0
|
266 |
return defaultValue;
|
jens@0
|
267 |
}
|
jens@0
|
268 |
|
jens@0
|
269 |
|
jens@0
|
270 |
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
|
jens@0
|
271 |
{
|
jens@0
|
272 |
_viewDropTarget = [self hitTestPoint: [sender draggingLocation]
|
jens@0
|
273 |
forLayerMatching: layerIsDropTarget
|
jens@0
|
274 |
offset: NULL];
|
jens@0
|
275 |
_viewDropOp = _viewDropTarget ?[_viewDropTarget draggingEntered: sender] :NSDragOperationNone;
|
jens@0
|
276 |
return _viewDropOp;
|
jens@0
|
277 |
}
|
jens@0
|
278 |
|
jens@0
|
279 |
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
|
jens@0
|
280 |
{
|
jens@0
|
281 |
CALayer *target = [self hitTestPoint: [sender draggingLocation]
|
jens@0
|
282 |
forLayerMatching: layerIsDropTarget
|
jens@0
|
283 |
offset: NULL];
|
jens@0
|
284 |
if( target == _viewDropTarget ) {
|
jens@0
|
285 |
if( _viewDropTarget )
|
jens@0
|
286 |
_viewDropOp = tell(_viewDropTarget,@selector(draggingUpdated:),sender,_viewDropOp);
|
jens@0
|
287 |
} else {
|
jens@0
|
288 |
tell(_viewDropTarget,@selector(draggingExited:),sender,0);
|
jens@0
|
289 |
_viewDropTarget = target;
|
jens@0
|
290 |
if( _viewDropTarget )
|
jens@0
|
291 |
_viewDropOp = [_viewDropTarget draggingEntered: sender];
|
jens@0
|
292 |
else
|
jens@0
|
293 |
_viewDropOp = NSDragOperationNone;
|
jens@0
|
294 |
}
|
jens@0
|
295 |
return _viewDropOp;
|
jens@0
|
296 |
}
|
jens@0
|
297 |
|
jens@0
|
298 |
- (BOOL)wantsPeriodicDraggingUpdates
|
jens@0
|
299 |
{
|
jens@0
|
300 |
return (_viewDropTarget!=nil);
|
jens@0
|
301 |
}
|
jens@0
|
302 |
|
jens@0
|
303 |
- (void)draggingExited:(id <NSDraggingInfo>)sender
|
jens@0
|
304 |
{
|
jens@0
|
305 |
tell(_viewDropTarget,@selector(draggingExited:),sender,0);
|
jens@0
|
306 |
_viewDropTarget = nil;
|
jens@0
|
307 |
}
|
jens@0
|
308 |
|
jens@0
|
309 |
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
|
jens@0
|
310 |
{
|
jens@0
|
311 |
return tell(_viewDropTarget,@selector(prepareForDragOperation:),sender,YES);
|
jens@0
|
312 |
}
|
jens@0
|
313 |
|
jens@0
|
314 |
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
|
jens@0
|
315 |
{
|
jens@0
|
316 |
return [_viewDropTarget performDragOperation: sender];
|
jens@0
|
317 |
}
|
jens@0
|
318 |
|
jens@0
|
319 |
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
|
jens@0
|
320 |
{
|
jens@0
|
321 |
tell(_viewDropTarget,@selector(concludeDragOperation:),sender,0);
|
jens@0
|
322 |
}
|
jens@0
|
323 |
|
jens@0
|
324 |
- (void)draggingEnded:(id <NSDraggingInfo>)sender
|
jens@0
|
325 |
{
|
jens@0
|
326 |
tell(_viewDropTarget,@selector(draggingEnded:),sender,0);
|
jens@0
|
327 |
}
|
jens@0
|
328 |
|
jens@0
|
329 |
@end
|