forked from alisdair/freecell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameView.m
258 lines (219 loc) · 9 KB
/
GameView.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
//
// GameView.m
// Freecell
//
// Created by Alisdair McDiarmid on Thu Jul 03 2003.
// Copyright (c) 2003 Alisdair McDiarmid. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// ALISDAIR MCDIARMID BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#import "GameView.h"
#import "Game.h"
#import "Card.h"
#import "CardView.h"
#import "Table.h"
@implementation GameView
- (id) initWithFrame: (NSRect) frame
{
self = [super initWithFrame:frame];
if (self)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Default colour is a nice shade of green
NSColor *colour = [NSColor colorWithCalibratedRed: 0.2 green: 0.4
blue: 0.1 alpha: 1.0];
NSData *data = [NSArchiver archivedDataWithRootObject: colour];
// Set the default
[defaults registerDefaults:
[NSDictionary dictionaryWithObjectsAndKeys:
data, @"backgroundColour", nil]];
// Then try to read the preference
data = [defaults dataForKey: @"backgroundColour"];
if (data)
colour = [NSUnarchiver unarchiveObjectWithData: data];
[defaults synchronize];
// And set the coloour
[self setBackgroundColour: colour];
}
return self;
}
- (void) dealloc
{
[game release];
[cardView release];
[super dealloc];
}
- (void) drawRect: (NSRect) rect
{
int i;
NSRect frame = [self frame];
[backgroundColour set];
[NSBezierPath fillRect: frame];
if (game == nil || cardView == nil)
return;
for (i = 0; i < NUMBER_OF_FREE_CELLS; i++)
{
TableLocation *location = [TableLocation locationWithType: FREE_CELL number: i];
NSPoint origin = NSMakePoint(edgeMargin + (cardWidth + margin) * i,
frame.size.height - edgeMargin - cardHeight);
Card *card = [table firstCardAtLocation: location];
BOOL selected = [game isTableLocationSelected: location];
NSImage *image = [cardView imageForCard: card selected: selected];
NSCompositingOperation operation;
if (card == nil && !selected)
operation = NSCompositePlusLighter;
else
operation = NSCompositeSourceOver;
[image compositeToPoint: origin
operation: operation
fraction: card == nil? 0.5 : 1.0 ];
}
for (i = 0; i < NUMBER_OF_STACKS; i++)
{
TableLocation *location = [TableLocation locationWithType: STACK number: i];
NSPoint origin = NSMakePoint(edgeMargin + (cardWidth + margin) * (i + 4),
frame.size.height - edgeMargin - cardHeight);
Card *card = [table firstCardAtLocation: location];
BOOL selected = [game isTableLocationSelected: location];
NSImage *image = [cardView imageForCard: card selected: selected];
NSCompositingOperation operation;
if (card == nil && !selected)
operation = NSCompositePlusLighter;
else
operation = NSCompositeSourceOver;
[image compositeToPoint: origin
operation: operation
fraction: card == nil? 0.25 : 1.0 ];
}
for (i = 0; i < NUMBER_OF_COLUMNS; i++)
{
TableLocation *location = [TableLocation locationWithType: COLUMN number: i];
NSArray *column = [table arrayForLocation: location];
NSEnumerator *enumerator = [column objectEnumerator];
Card *card;
int row;
NSUInteger count = [column count];
unsigned maxHeight = 19 * smallOverlap;
unsigned short o;
o = (count * overlap > maxHeight)? maxHeight/count: overlap;
for (row = 0; card = [enumerator nextObject]; row++)
{
NSPoint origin = NSMakePoint(edgeMargin + (cardWidth + margin) * i,
frame.size.height - edgeMargin
- (cardHeight + margin) * 2 - o * row);
NSImage *image = [cardView imageForCard: card selected: [game isCardSelected: card]];
[image compositeToPoint: origin operation: NSCompositeSourceOver];
}
// If the column is empty and selected, draw the selected image
if (row == 0 && [game isTableLocationSelected: [TableLocation locationWithType: COLUMN number: i]])
{
NSPoint origin = NSMakePoint(edgeMargin + (cardWidth + margin) * i,
frame.size.height - edgeMargin
- (cardHeight + margin) * 2);
NSImage *image = [cardView imageForCard: nil selected: YES];
[image compositeToPoint: origin operation: NSCompositePlusDarker fraction: 0.5];
}
}
}
- (void) mouseDown: (NSEvent *) event
{
TableLocation *location = nil;
NSPoint pos = [event locationInWindow];
NSRect frame = [self frame];
if (pos.x > edgeMargin && pos.x < frame.size.width - edgeMargin
&& pos.y < frame.size.height - edgeMargin)
{
if (pos.y >= frame.size.height - cardHeight - margin)
{
if (pos.x < edgeMargin + (cardWidth + margin) * NUMBER_OF_FREE_CELLS)
location = [TableLocation locationWithType: FREE_CELL
number: (pos.x - edgeMargin)/(cardWidth + margin)];
else
location = [TableLocation locationWithType: STACK
number: (pos.x - edgeMargin)/(cardWidth + margin) - 4];
}
else if (pos.y <= frame.size.height - cardHeight - margin * 2)
location = [TableLocation locationWithType: COLUMN
number: (pos.x - edgeMargin)/(cardWidth + margin)];
}
if (location)
{
// Take any even number of clicks as a double-click. This allows a
// quad-click to be understood as two double-clicks in quick succession,
// which makes perfect UI sense in this case.
if (([event clickCount] % 2) == 0)
[game performSelectorInBackground:@selector(doubleClickedTableLocation:) withObject:location];
//[game doubleClickedTableLocation: location];
else
[game performSelectorInBackground:@selector(clickedTableLocation:) withObject:location];
//[game clickedTableLocation: location];
}
}
- (void) setNeedsDisplaySafely {
[self performSelectorOnMainThread:@selector(setNeedsDisplaySafely2) withObject:nil waitUntilDone:NO];
}
- (void) setNeedsDisplaySafely2 {
[self setNeedsDisplay:YES];
}
// Mutators
//
- (void) setGame: (Game *) newGame
{
[game release];
game = [newGame retain];
table = [game table];
//[self setNeedsDisplay: YES];
[self setNeedsDisplaySafely];
}
- (void) setController: (GameController *) newController;
{
controller = newController;
[self setCardView: [CardView cardView]];
}
- (void) setCardView: (CardView *) newCardView
{
NSSize size;
[newCardView retain];
[cardView release];
cardView = newCardView;
// Calculate frame size
margin = 8;
edgeMargin = 2 * margin;
overlap = [cardView overlap];
smallOverlap = [cardView smallOverlap];
cardWidth = [cardView size].width;
cardHeight = [cardView size].height;
size = NSMakeSize(edgeMargin + (cardWidth + margin) * 8 - margin + edgeMargin,
edgeMargin + (cardHeight + margin) * 2 + smallOverlap * 18 + edgeMargin);
[controller setWindowSize: NSMakeSize(size.width, size.height + 22)];
//[self setNeedsDisplay: YES];
[self setNeedsDisplaySafely];
}
- (void) setBackgroundColour: (NSColor *) colour
{
[colour retain];
[backgroundColour release];
backgroundColour = colour;
//[self setNeedsDisplay: YES];
[self setNeedsDisplaySafely];
}
@end