-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGobanGoke.m
321 lines (259 loc) · 8.69 KB
/
GobanGoke.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
/*$Id: GobanGoke.m,v 1.3 2001/09/17 09:47:37 phink Exp $*/
// This is Goban, a Go program for Mac OS X. Contact goban@sente.ch,
// or see http://www.sente.ch/software/goban for more information.
//
// Copyright (c) 1997-2001, Sen:te (Sente SA). All rights reserved.
//
// 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 - version 2.
//
// 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 in file COPYING
// for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111, USA.
#import "GobanGoke.h"
#import <SenFoundation/SenFoundation.h>
#define MIN_SIZE 20
#define MAX_SIZE 130
#define HALF_PIXEL 0.5
@interface GobanWhiteGoke : GobanGoke
@end
@interface GobanBlackGoke : GobanGoke
@end
@interface GoSymbol (GokePrivate)
- (id) goke;
@end
@implementation GoSymbol (Goke)
- (id) goke
{
return nil;
}
- (NSImage *) imageOfSize:(int) aSize;
{
return [self imageOfSize:aSize style:-1];
}
- (NSImage *) imageOfSize:(int) aSize style:(int) aStyle
{
int size = MIN (MAX_SIZE, MAX (aSize, MIN_SIZE));
return [[self goke] stoneOfSize:size style:aStyle];
}
@end
@implementation GoBlack (Goke)
- (id) goke
{
return [GobanBlackGoke sharedInstance];
}
@end
@implementation GoWhite (Goke)
- (id) goke
{
return [GobanWhiteGoke sharedInstance];
}
@end
@interface GoSymbol (Overlay)
- (NSImage *) prisonerImageWithSource:(NSImage *) anImage andColor:(NSColor *) aColor;
- (NSImage *) territoryImageOfSize:(int) aSize andColor:(NSColor *) aColor;
@end
@implementation GoSymbol (Overlay)
- (NSImage *) prisonerImageWithSource:(NSImage *) anImage andColor:(NSColor *) aColor
{
NSBezierPath *disc = [NSBezierPath bezierPath];
NSImage *image = [anImage copy];
float width = [image size].width;
float radius = width / 8;
float offset = width / 4;
NSRect square = NSMakeRect (offset + 0.5, offset + 0.5, 2 * offset - 1, 2 * offset - 1);
[image lockFocus];
[aColor set];
NSFrameRectWithWidth (square, 0.75);
[disc appendBezierPathWithOvalInRect:
NSMakeRect (
width / 2 - radius ,
width / 2 - radius ,
2.0 * radius,
2.0 * radius)];
[disc fill];
[image unlockFocus];
return [image autorelease];
}
- (NSImage *) territoryImageOfSize:(int) aSize andColor:(NSColor *) aColor
{
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize (aSize, aSize)];
NSBezierPath *disc = [NSBezierPath bezierPath];
float width = [image size].width;
float diameter = width / 4;
NSPoint origin = NSMakePoint (HALF_PIXEL + floor((width - diameter) / 2), 1.0 + HALF_PIXEL + floor((width - diameter) / 2));
[image lockFocus];
[aColor set];
[disc appendBezierPathWithOvalInRect:NSMakeRect (origin.x , origin.y , diameter, diameter)];
[disc fill];
[image unlockFocus];
return [image autorelease];
}
@end
@implementation GoWhitePrisoner (Image)
- (NSImage *) imageOfSize:(int) aSize style:(int) aStyle
{
return [self prisonerImageWithSource:[[[GoSymbol white] goke] stoneOfSize:aSize style:aStyle] andColor:[NSColor blackColor]];
}
@end
@implementation GoBlackPrisoner (Image)
- (NSImage *) imageOfSize:(int) aSize style:(int) aStyle
{
return [self prisonerImageWithSource:[[[GoSymbol black] goke] stoneOfSize:aSize style:aStyle] andColor:[NSColor whiteColor]];
}
@end
@implementation GoWhiteTerritory (Image)
- (NSImage *) imageOfSize:(int) aSize style:(int) aStyle
{
static NSMutableDictionary *goWhiteTerritoryCache = nil;
NSImage *image = nil;
if (goWhiteTerritoryCache == nil) {
goWhiteTerritoryCache = [[NSMutableDictionary alloc] init];
}
image = [goWhiteTerritoryCache objectForKey:[NSNumber numberWithInt:aSize]];
if (image == nil) {
image = [self territoryImageOfSize:aSize andColor:[NSColor whiteColor]];
[goWhiteTerritoryCache setObject:image forKey:[NSNumber numberWithInt:aSize]];
}
return image;
}
@end
@implementation GoBlackTerritory (Image)
- (NSImage *) imageOfSize:(int) aSize style:(int) aStyle
{
static NSMutableDictionary *goBlackTerritoryCache = nil;
NSImage *image = nil;
if (goBlackTerritoryCache == nil) {
goBlackTerritoryCache = [[NSMutableDictionary alloc] init];
}
image = [goBlackTerritoryCache objectForKey:[NSNumber numberWithInt:aSize]];
if (image == nil) {
image = [self territoryImageOfSize:aSize andColor:[NSColor blackColor]];
[goBlackTerritoryCache setObject:image forKey:[NSNumber numberWithInt:aSize]];
}
return image;
}
@end
@implementation GoNeutralTerritory (Image)
- (NSImage *) imageOfSize:(int) aSize style:(int) aStyle
{
static NSMutableDictionary *goNeutralTerritoryCache = nil;
NSImage *image = nil;
if (goNeutralTerritoryCache == nil) {
goNeutralTerritoryCache = [[NSMutableDictionary alloc] init];
}
image = [goNeutralTerritoryCache objectForKey:[NSNumber numberWithInt:aSize]];
if (image == nil) {
float width = aSize;
float offset = width / 4;
NSRect square = NSMakeRect (offset + 0.5 , offset + 0.5, 2 * offset - 1, 2 * offset - 1);
image = [[[NSImage alloc] initWithSize:NSMakeSize (aSize, aSize)] autorelease];
[image lockFocus];
[[NSColor blackColor] set];
NSFrameRectWithWidth (square, 0.75);
[image unlockFocus];
[goNeutralTerritoryCache setObject:image forKey:[NSNumber numberWithInt:aSize]];
}
return image;
}
@end
@implementation GobanGoke
- (id) init
{
[super init];
cache = [[NSMutableDictionary alloc] init];
return self;
}
- (void) dealloc
{
RELEASE (cache);
[super dealloc];
}
- (NSImage *) imageForKey:(id) aKey atIndex:(int) anIndex
{
return nil;
}
- (NSImage *) stoneOfSize:(int) aSize style:(int) aStyle
{
NSNumber *key = [NSNumber numberWithInt:aSize];
if (aSize <= 48) {
NSImage *image = [self imageForKey:key atIndex:aStyle];
return image;
}
else {
NSImage *image = [[[self imageForKey:[NSNumber numberWithInt:130] atIndex:aStyle] copy] autorelease];
[image setScalesWhenResized:YES];
[image setSize:NSMakeSize (aSize, aSize)];
return image;
}
}
+ (id) sharedInstance
{
return nil;
}
@end
@implementation GobanWhiteGoke
+ (id) sharedInstance
{
static id whiteGoke = nil;
if (whiteGoke == nil) {
whiteGoke = [[self alloc] init];
}
return whiteGoke;
}
- (NSImage *) imageForKey:(id) aKey atIndex:(int) anIndex
{
NSMutableArray *stoneArray = [cache objectForKey:aKey];
if (stoneArray == nil) {
int i;
stoneArray = [NSMutableArray arrayWithCapacity:STYLE_COUNT];
for (i = 0; i < STYLE_COUNT; i++) {
NSString *stoneName = [NSString stringWithFormat:@"WhiteStone%@-%d", aKey, i];
NSString *stonePath = [[NSBundle bundleForClass:[self class]] pathForResource:stoneName ofType:@"png" inDirectory:@"WhiteStones"];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:stonePath];
if (image != nil) {
[stoneArray addObject:image];
RELEASE (image);
}
}
if (!isNilOrEmpty (stoneArray)) {
[cache setObject:stoneArray forKey:aKey];
}
}
if (stoneArray != nil) {
return [stoneArray objectAtIndex:anIndex];
}
return nil;
}
@end
@implementation GobanBlackGoke
+ (id) sharedInstance
{
static id blackGoke = nil;
if (blackGoke == nil) {
blackGoke = [[self alloc] init];
}
return blackGoke;
}
- (NSImage *) imageForKey:(id) aKey atIndex:(int) anIndex
{
NSImage *image = [cache objectForKey:aKey];
if (image == nil) {
NSString *stoneName = [NSString stringWithFormat:@"BlackStone%@", aKey];
NSString *stonePath = [[NSBundle bundleForClass:[self class]] pathForResource:stoneName ofType:@"png" inDirectory:@"BlackStones"];
image = [[[NSImage alloc] initWithContentsOfFile:stonePath] autorelease];
if (image != nil) {
[cache setObject:image forKey:aKey];
}
}
return image;
}
@end