-
Notifications
You must be signed in to change notification settings - Fork 17
/
PresetManager.m
257 lines (209 loc) · 5.86 KB
/
PresetManager.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
#import "PresetManager.h"
@implementation PresetManager
- (id)init
{
if (self = [super init])
{
NSUserDefaults *defaults = [ScreenSaverDefaults defaultsForModuleWithName:@"Flurry"];
NSData *presetsData = [defaults objectForKey:@"presets"];
nibLoaded = NO;
presets = [[NSMutableArray alloc] init];
if ([[defaults objectForKey:@"version"] isEqual:VERSION_STRING] && presetsData)
{
NSArray *unarchivedPresets;
unarchivedPresets = [NSUnarchiver unarchiveObjectWithData:presetsData];
[presets addObjectsFromArray:unarchivedPresets];
}
else
{
presetsData = [NSData data];
[defaults setObject:presetsData forKey:@"presets"];
[defaults setInteger:0 forKey:@"viewPresetIndex"];
[defaults synchronize];
}
if ([presets count] == 0)
{
currentPreset = [FlurryPreset classicFlurryPreset];
[presets addObject:currentPreset];
[presets addObject:[FlurryPreset rgbFlurryPreset]];
[presets addObject:[FlurryPreset waterFlurryPreset]];
[presets addObject:[FlurryPreset fireFlurryPreset]];
[presets addObject:[FlurryPreset psychedelicFlurryPreset]];
}
else
{
int index = [defaults integerForKey:@"viewPresetIndex"];
if (index >=0 && index < [presets count])
currentPreset = [presets objectAtIndex:index];
else
currentPreset = [presets objectAtIndex:0]; // will never have 0 items - so this is OK
}
viewPreset = currentPreset;
}
return self;
}
- (void)selectRandomPresetToView {
NSUserDefaults *defaults = [ScreenSaverDefaults defaultsForModuleWithName:@"Flurry"];
int index = (rand() >> 3 ^ rand() >> 6) % [presets count];
viewPreset = [presets objectAtIndex:index];
[defaults setInteger:index forKey:@"currentPresetIndex"];
[defaults synchronize];
}
- (void)setViewPresetToCurrent {
viewPreset = currentPreset;
[self refreshMenu];
}
- (FlurryPreset *)viewPreset {
return viewPreset;
}
- (FlurryPreset *)currentPreset {
return currentPreset;
}
- (BOOL)handleEvent:(NSEvent *)theEvent
{
int i;
NSString *keyCodes = [theEvent characters];
unichar keyCode;
if (keyCodes && [keyCodes length])
{
keyCode = [keyCodes characterAtIndex:0];
for (i=0;i<[presets count];i++)
{
FlurryPreset *preset = [presets objectAtIndex:i];
NSString *sc = [preset shortcut];
if (sc && keyCode == [[preset shortcut] characterAtIndex:0])
{
if (viewPreset != preset)
{
viewPreset = preset;
[target selectedPresetDidChange];
}
return YES;
}
}
}
return NO;
}
- (void)loadNibIfNeeded
{
if (!nibLoaded)
[NSBundle loadNibNamed:@"EditPreset.nib" owner:self];
[presetTable setDataSource:self];
[presetTable setDelegate:self];
nibLoaded = YES;
}
- (void)setTarget:(NSObject *)newTarget
{
target = newTarget;
}
- (IBAction)changePreset:(id)sender
{
viewPreset = currentPreset = [[popupButton selectedItem] representedObject];
[self refreshMenu];
[target selectedPresetDidChange];
}
- (void)refreshMenu
{
int i;
NSMenuItem *item;
NSMenu *menu = [[NSMenu alloc] init];
for (i=0;i<[presets count];i++)
{
FlurryPreset *preset = [presets objectAtIndex:i];
item = [menu insertItemWithTitle:[preset name] action:@selector(changePreset:)
keyEquivalent:@"" atIndex:i];
//NSLog(@"adding %@ %i", [preset name], i+1);
[item setTarget:self];
[item setRepresentedObject:preset];
}
[menu insertItem:[NSMenuItem separatorItem] atIndex:i++];
item = [menu insertItemWithTitle:@"Edit..."
action:@selector(editPresets:)
keyEquivalent:@"" atIndex:i++];
[item setTarget:self];
[popupButton setMenu:menu];
[popupButton selectItemAtIndex:[presets indexOfObject:currentPreset]];
//[target selectedPresetDidChange];
}
- (NSPopUpButton *)popupButton
{
[self loadNibIfNeeded];
[self refreshMenu];
return popupButton;
}
- (void)writeDefaults
{
NSUserDefaults *defaults = [ScreenSaverDefaults defaultsForModuleWithName:@"Flurry"];
[defaults setObject:VERSION_STRING forKey:@"version"];
[defaults setInteger:[presets indexOfObject:viewPreset] forKey:@"viewPresetIndex"];
[defaults setObject:[NSArchiver archivedDataWithRootObject:presets] forKey:@"presets"];
[defaults synchronize];
}
- (IBAction)deleteCurrentPreset:(id)sender
{
if ([presetTable numberOfSelectedRows] < [presets count])
{
int i;
i = [presets count];
// do it backwards (so we don't modify indexes of object we are about to delete)
while (i--)
{
if ([presetTable isRowSelected:i])
{
[presets removeObjectAtIndex:i];
}
}
[presetTable reloadData];
[self refreshMenu];
}
else
NSBeep();
}
- (IBAction)doneEditing:(id)sender
{
[self refreshMenu];
[editWindow orderOut:sender];
[target selectedPresetDidChange];
}
- (IBAction)editPresets:(id)sender
{
[popupButton selectItemAtIndex:[presets indexOfObject:currentPreset]];
[presetTable reloadData];
[editWindow makeKeyAndOrderFront:sender];
}
- (IBAction)newPreset:(id)sender
{
int i;
BOOL added = NO;
NSMutableArray *newPresets = [[NSMutableArray alloc] init];
for (i=0;i<[presets count];i++)
{
if ([presetTable isRowSelected:i])
{
added = YES;
[newPresets addObject:[[presets objectAtIndex:i] copy]];
}
}
if (added)
[presets addObjectsFromArray:newPresets];
else
[presets addObject:[FlurryPreset classicFlurryPreset]];
[newPresets release];
[presetTable reloadData];
[self refreshMenu];
}
- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
return [presets count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
NSParameterAssert(row >= 0 && row < [presets count]);
return [[presets objectAtIndex:row] valueForKey:[tableColumn identifier]];
}
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
NSParameterAssert(row >= 0 && row < [presets count]);
[[presets objectAtIndex:row] takeValue:object forKey:[tableColumn identifier]];
}
@end