-
Notifications
You must be signed in to change notification settings - Fork 1
/
MGOLAppController.m
executable file
·288 lines (251 loc) · 11.5 KB
/
MGOLAppController.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
//
// MGOLAppController.m
// MyGameOfLife
//
// Created by Eneko Alonso on 12/23/07.
// Copyright 2007 Eneko Alonso. All rights reserved.
//
#import "MGOLAppController.h"
#import "MGOLPreferencesController.h"
#import "MGOLStructureEditorController.h"
#import "MGOLGlobals.h"
@implementation MGOLAppController
- (id)init {
[super init];
NSLog(@"MGOLAppController init");
if (self) {
[self registerDefaults];
// Register as an observer for LoadDefaults messages
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(loadDefaults:) name:MGOLLoadDefaultsNotification object:nil];
}
preferencesPanel = nil;
structureEditor = nil;
return self;
}
-(void) dealloc
{
NSLog(@"MGOLAppController dealloc");
// Unregister from notifications
[[NSNotificationCenter defaultCenter] removeObserver:self];
[preferencesPanel release];
[structureEditor release];
[super dealloc];
}
- (void)registerDefaults
{
NSLog(@"MGOLAppController registerDefaults");
// Default values dictionary
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
// Initialize default colors
NSColor *bgColor = [NSColor colorWithCalibratedRed: 25.0/255.0 green: 25.0/255.0 blue: 25.0/255.0 alpha:1.0];
NSColor *cellColor = [NSColor colorWithCalibratedRed:204.0/255.0 green:255.0/255.0 blue:102.0/255.0 alpha:1.0];
NSColor *cellBorderColor = [NSColor blackColor];
NSData *bgColorAsData = [NSKeyedArchiver archivedDataWithRootObject:bgColor];
NSData *cellColorAsData = [NSKeyedArchiver archivedDataWithRootObject:cellColor];
NSData *cellBorderColorAsData = [NSKeyedArchiver archivedDataWithRootObject:cellBorderColor];
[defaultValues setObject:bgColorAsData forKey:MGOLBackgroundColorKey];
[defaultValues setObject:cellColorAsData forKey:MGOLCellColorKey];
[defaultValues setObject:cellBorderColorAsData forKey:MGOLCellBorderColorKey];
// World variables
[defaultValues setObject:[NSNumber numberWithInt:4] forKey:MGOLPixelsPerCellKey];
[defaultValues setObject:[NSNumber numberWithBool:YES] forKey:MGOLDrawCellBorderKey];
[defaultValues setObject:[NSNumber numberWithInt:200] forKey:MGOLWorldWidthKey];
[defaultValues setObject:[NSNumber numberWithInt:100] forKey:MGOLWorldHeightKey];
// Default cell behavior
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellBorn0"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellBorn1"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellBorn2"];
[defaultValues setObject:[NSNumber numberWithBool:YES] forKey:@"MGOLCellBorn3"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellBorn4"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellBorn5"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellBorn6"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellBorn7"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellBorn8"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellStayAlive0"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellStayAlive1"];
[defaultValues setObject:[NSNumber numberWithBool:YES] forKey:@"MGOLCellStayAlive2"];
[defaultValues setObject:[NSNumber numberWithBool:YES] forKey:@"MGOLCellStayAlive3"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellStayAlive4"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellStayAlive5"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellStayAlive6"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellStayAlive7"];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:@"MGOLCellStayAlive8"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
}
- (void)loadDefaults:(NSNotification *)notification
{
NSLog(@"MGOLAppController loadDefaults");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[self updateZoomSlider:[defaults integerForKey:MGOLPixelsPerCellKey]];
[cellProcessor setWorldSize:NSMakeSize([defaults integerForKey:MGOLWorldWidthKey],
[defaults integerForKey:MGOLWorldHeightKey])];
[cellProcessor setCellBehavior:self
cellBorn0:[defaults boolForKey:@"MGOLCellBorn0"]
cellBorn1:[defaults boolForKey:@"MGOLCellBorn1"]
cellBorn2:[defaults boolForKey:@"MGOLCellBorn2"]
cellBorn3:[defaults boolForKey:@"MGOLCellBorn3"]
cellBorn4:[defaults boolForKey:@"MGOLCellBorn4"]
cellBorn5:[defaults boolForKey:@"MGOLCellBorn5"]
cellBorn6:[defaults boolForKey:@"MGOLCellBorn6"]
cellBorn7:[defaults boolForKey:@"MGOLCellBorn7"]
cellBorn8:[defaults boolForKey:@"MGOLCellBorn8"]
cellStaysAlive0:[defaults boolForKey:@"MGOLCellStayAlive0"]
cellStaysAlive1:[defaults boolForKey:@"MGOLCellStayAlive1"]
cellStaysAlive2:[defaults boolForKey:@"MGOLCellStayAlive2"]
cellStaysAlive3:[defaults boolForKey:@"MGOLCellStayAlive3"]
cellStaysAlive4:[defaults boolForKey:@"MGOLCellStayAlive4"]
cellStaysAlive5:[defaults boolForKey:@"MGOLCellStayAlive5"]
cellStaysAlive6:[defaults boolForKey:@"MGOLCellStayAlive6"]
cellStaysAlive7:[defaults boolForKey:@"MGOLCellStayAlive7"]
cellStaysAlive8:[defaults boolForKey:@"MGOLCellStayAlive8"]];
}
- (void)windowDidLoad
{
NSLog(@"MGOLAppController windowDidLoad");
}
-(void)awakeFromNib
{
NSLog(@"MGOLAppController awakeFromNib");
timerFPS = [NSTimer scheduledTimerWithTimeInterval:1/10.0 // 10 times per second
target:self
selector:@selector(updateFPS:)
userInfo:nil
repeats:YES];
// Post notification for loading defaults
NSLog(@"MGOLAppController posting notification...");
[[NSNotificationCenter defaultCenter] postNotificationName:MGOLLoadDefaultsNotification object:self];
}
- (IBAction)start:(id)sender {
NSLog(@"MGOLAppController start");
if (timerProcessor == nil)
{
timerProcessor = [NSTimer scheduledTimerWithTimeInterval:0.001//1.0/25.0 // 25 times per second
target:self
selector:@selector(stepAnimation:)
userInfo:nil
repeats:YES];
[progress startAnimation:self];
}
}
- (IBAction)step:(id)sender {
NSLog(@"MGOLAppController step");
[self stepAnimation:nil];
}
- (IBAction)stop:(id)sender {
NSLog(@"MGOLAppController pause");
if (timerProcessor != nil) {
[timerProcessor invalidate];
//[timer release]; This blows up if called multiple times!
timerProcessor = nil;
[progress stopAnimation:self];
}
}
- (void)stepAnimation:(NSTimer *)timer
{
[cellProcessor processCells];
[myView setNeedsDisplay:YES];
}
- (IBAction)clearCells:(id)sender {
NSLog(@"MGOLAppController stop/clear");
[self stop:nil]; // Stop timer
[cellProcessor clearCells];
[myView setNeedsDisplay:YES];
}
- (IBAction)fillRandom:(id)sender {
NSLog(@"MGOLAppController Fill Random");
[cellProcessor fillRandom];
[myView setNeedsDisplay:YES];
}
- (void)windowDidResize:(NSNotification *)notification
{
NSLog(@"MGOLAppController Window did resize");
}
- (void)updateFPS:(NSTimer *)timer
{
// NSLog(@"MGOLAppController updateFPS");
[lblFramesPerSecond setStringValue:[NSString stringWithFormat:@"Cycles/s: %0.0f", (1/[timerProcessor timeInterval]) ]];
[lblPopulation setStringValue:[NSString stringWithFormat:@"Population: %d", [cellProcessor population]]];
[lblCycles setStringValue:[NSString stringWithFormat:@"Cycles: %d", [cellProcessor cycleCounter]]];
NSSize worldSize = [cellProcessor worldSize];
[lblWorldSize setStringValue:[NSString stringWithFormat:@"World: %0.0fx%0.0f", worldSize.width, worldSize.height]];
// [lblPopulation setIntValue:cellCounter];
// [lblCycles setIntValue:cycleCounter];
}
- (IBAction)showPreferences:(id)sender
{
NSLog(@"MGOLAppController showPreferences");
// Is already created?
if (!preferencesPanel) {
preferencesPanel = [MGOLPreferencesController new];
}
[preferencesPanel showWindow:self];
}
- (IBAction)showStructureEditor:(id)sender
{
NSLog(@"MGOLAppController showStructureEditor");
// Is already created?
if (!structureEditor) {
structureEditor = [MGOLStructureEditorController new];
}
[structureEditor showWindow:self];
}
- (IBAction)setCellSize:(id)sender
{
NSLog(@"MGOLAppController setCellSize");
[self updateZoomSlider:[sender intValue]];
// Save user defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:[sender intValue] forKey:@"pixelsPerCell"];
// Post notification for loading defaults
[[NSNotificationCenter defaultCenter] postNotificationName:MGOLLoadDefaultsNotification object:self];
}
- (void)updateZoomSlider:(unsigned int)pixelsPerCell
{
NSLog(@"MGOLAppController updateZoomSlider");
[slPixelsPerCell setIntValue:pixelsPerCell];
[lblZoom setStringValue:[NSString stringWithFormat:@"x%d", pixelsPerCell]];
}
- (IBAction)copyCells:(id)sender
{
NSLog(@"MGOLAppController copyCells");
if (NSEqualSizes([myView selection].size, NSMakeSize(0, 0)))
{
NSLog(@"MGOLAppController copyCells - No selection!");
return;
}
[self writeStructure:[cellProcessor structureFromRect:[myView selection]]
ToPasteboard:[NSPasteboard generalPasteboard]];
}
- (IBAction)pasteCells:(id)sender
{
NSLog(@"MGOLAppController pasteCells");
if (![self readStructureFromPasteboard:[NSPasteboard generalPasteboard]])
{
NSBeep();
return;
}
[myView setNeedsDisplay:YES];
}
- (void)writeStructure:(MGOLStructure *)structure ToPasteboard:(NSPasteboard *)pb
{
NSLog(@"MGOLAppController writeStructureToPasteboard - structure:\n%@", structure);
[pb declareTypes:[NSArray arrayWithObject:MGOLStructureDataType] owner:self];
NSData *structureAsData = [NSKeyedArchiver archivedDataWithRootObject:structure];
[pb setData:structureAsData forType:MGOLStructureDataType];
}
- (BOOL)readStructureFromPasteboard:(NSPasteboard *)pb
{
NSLog(@"MGOLAppController readStructureFromPasteboard");
NSString *type = [pb availableTypeFromArray:[NSArray arrayWithObject:MGOLStructureDataType]];
if (type)
{
NSData *structureAsData = [pb dataForType:MGOLStructureDataType];
MGOLStructure *structure = [NSKeyedUnarchiver unarchiveObjectWithData:structureAsData];
NSLog(@"MGOLAppController readStructureFromPasteboard - structure:\n%@", structure);
[cellProcessor placeStructure:structure At:[myView selection].origin];
return YES;
}
return NO;
}
@end