1 // |
|
2 // SettingsViewController.m |
|
3 // hwengine |
|
4 // |
|
5 // Created by Vittorio on 08/01/10. |
|
6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 // |
|
8 |
|
9 #import "SettingsViewController.h" |
|
10 #import "SDL_uikitappdelegate.h" |
|
11 |
|
12 @implementation SettingsViewController |
|
13 |
|
14 @synthesize username, password, musicSwitch, soundsSwitch, altDamageSwitch, |
|
15 settingsTable, buttonContainer, parentView; |
|
16 |
|
17 |
|
18 -(void) loadView { |
|
19 self.musicSwitch = [[UISwitch alloc] init]; |
|
20 self.soundsSwitch = [[UISwitch alloc] init]; |
|
21 self.altDamageSwitch = [[UISwitch alloc] init]; |
|
22 [self.soundsSwitch addTarget:self action:@selector(sameValueSwitch) forControlEvents:UIControlEventValueChanged]; |
|
23 [self.musicSwitch addTarget:self action:@selector(checkValueSwitch) forControlEvents:UIControlEventValueChanged]; |
|
24 |
|
25 [super loadView]; |
|
26 } |
|
27 |
|
28 -(void) viewDidLoad { |
|
29 NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"]; |
|
30 |
|
31 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { |
|
32 NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; |
|
33 username.text = [data objectForKey:@"username"]; |
|
34 password.text = [data objectForKey:@"password"]; |
|
35 if (1 == [[data objectForKey:@"music"] intValue]) { |
|
36 musicSwitch.on = YES; |
|
37 } else { |
|
38 musicSwitch.on = NO; |
|
39 } |
|
40 if (1 == [[data objectForKey:@"sounds"] intValue]) { |
|
41 soundsSwitch.on = YES; |
|
42 } else { |
|
43 soundsSwitch.on = NO; |
|
44 } |
|
45 if (1 == [[data objectForKey:@"alternate"] intValue]) { |
|
46 altDamageSwitch.on = YES; |
|
47 } else { |
|
48 altDamageSwitch.on = NO; |
|
49 } |
|
50 [data release]; |
|
51 } else { |
|
52 [NSException raise:@"File NOT found" format:@"The file settings.plist was not found at %@", filePath]; |
|
53 } |
|
54 |
|
55 |
|
56 username.textColor = [UIColor grayColor]; |
|
57 password.textColor = [UIColor grayColor]; |
|
58 settingsTable.backgroundColor = [UIColor clearColor]; |
|
59 settingsTable.allowsSelection = NO; |
|
60 buttonContainer.backgroundColor = [UIColor clearColor]; |
|
61 settingsTable.tableFooterView = buttonContainer; |
|
62 |
|
63 [super viewDidLoad]; |
|
64 } |
|
65 |
|
66 -(void) viewDidUnload { |
|
67 self.username = nil; |
|
68 self.password = nil; |
|
69 self.musicSwitch = nil; |
|
70 self.soundsSwitch = nil; |
|
71 self.altDamageSwitch = nil; |
|
72 self.settingsTable = nil; |
|
73 self.buttonContainer = nil; |
|
74 [super viewDidUnload]; |
|
75 } |
|
76 |
|
77 -(void) dealloc { |
|
78 [username release]; |
|
79 [password release]; |
|
80 [musicSwitch release]; |
|
81 [soundsSwitch release]; |
|
82 [altDamageSwitch release]; |
|
83 [settingsTable release]; |
|
84 [buttonContainer release]; |
|
85 [super dealloc]; |
|
86 } |
|
87 |
|
88 // Override to allow orientations other than the default portrait orientation. |
|
89 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
90 // Return YES for supported orientations |
|
91 return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
|
92 } |
|
93 |
|
94 // makes the keyboard go away when background is tapped |
|
95 -(IBAction) backgroundTap: (id)sender { |
|
96 [username resignFirstResponder]; |
|
97 [password resignFirstResponder]; |
|
98 } |
|
99 |
|
100 // makes the keyboard go away when "Done" is tapped |
|
101 -(IBAction) textFieldDoneEditing: (id)sender { |
|
102 [sender resignFirstResponder]; |
|
103 } |
|
104 |
|
105 // set music off when sound is turned off |
|
106 -(void) sameValueSwitch { |
|
107 if (YES == self.musicSwitch.on) { |
|
108 [musicSwitch setOn:NO animated:YES]; |
|
109 } |
|
110 } |
|
111 |
|
112 // don't enable music when sound is off |
|
113 -(void) checkValueSwitch { |
|
114 if (NO == self.soundsSwitch.on) { |
|
115 [musicSwitch setOn:!musicSwitch.on animated:YES]; |
|
116 } |
|
117 } |
|
118 |
|
119 #pragma mark - |
|
120 #pragma mark Return to mainView |
|
121 -(void) flushData { |
|
122 NSLog(@"writing preferences to file"); |
|
123 |
|
124 NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init]; |
|
125 NSString *tmpMus = (musicSwitch.on) ? @"1" : @"0"; |
|
126 NSString *tmpEff = (soundsSwitch.on) ? @"1" : @"0"; |
|
127 NSString *tmpAlt = (altDamageSwitch.on) ? @"1" : @"0"; |
|
128 |
|
129 [saveDict setObject:username.text forKey:@"username"]; |
|
130 [saveDict setObject:password.text forKey:@"password"]; |
|
131 [saveDict setObject:tmpMus forKey:@"music"]; |
|
132 [saveDict setObject:tmpEff forKey:@"sounds"]; |
|
133 [saveDict setObject:tmpAlt forKey:@"alternate"]; |
|
134 |
|
135 [saveDict writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES]; |
|
136 [saveDict release]; |
|
137 } |
|
138 |
|
139 -(void) returnMainView { |
|
140 [self flushData]; |
|
141 //[self dismissModalViewControllerAnimated:YES] |
|
142 |
|
143 [UIView beginAnimations:@"Get Back" context:NULL]; |
|
144 [UIView setAnimationDuration:1]; |
|
145 |
|
146 self.view.frame = CGRectMake(0, -257, 480, 278); |
|
147 self.parentView.frame = CGRectMake(0, 21, 480, 278); |
|
148 [UIView commitAnimations]; |
|
149 |
|
150 [self.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1]; |
|
151 self.parentView = nil; |
|
152 } |
|
153 |
|
154 #pragma mark - |
|
155 #pragma mark UIActionSheet Methods |
|
156 -(IBAction) deleteData: (id)sender { |
|
157 /* temporary commented out |
|
158 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Are you reeeeeally sure?", @"") |
|
159 delegate:self |
|
160 cancelButtonTitle:NSLocalizedString(@"Well, maybe not...", @"") |
|
161 destructiveButtonTitle:NSLocalizedString(@"As sure as I can be!", @"") |
|
162 otherButtonTitles:nil]; |
|
163 [actionSheet showInView:self.view]; |
|
164 [actionSheet release]; |
|
165 */ |
|
166 [self returnMainView]; |
|
167 } |
|
168 |
|
169 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex { |
|
170 if ([actionSheet cancelButtonIndex] != buttonIndex) { |
|
171 // get the documents dirctory |
|
172 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
|
173 NSString *documentsDirectory = [paths objectAtIndex:0]; |
|
174 |
|
175 // get the content of the directory |
|
176 NSFileManager *fm = [NSFileManager defaultManager]; |
|
177 NSArray *dirContent = [fm directoryContentsAtPath:documentsDirectory]; |
|
178 NSError *error; |
|
179 |
|
180 // delete data |
|
181 for (NSString *fileName in dirContent) { |
|
182 [fm removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:fileName] error:&error]; |
|
183 } |
|
184 |
|
185 // force resetting |
|
186 UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Hit Home Button to Exit", @"") |
|
187 message:NSLocalizedString(@"\nEverything is gone!\nNow you need to restart the game...", @"") |
|
188 delegate:self |
|
189 cancelButtonTitle:nil |
|
190 otherButtonTitles:nil]; |
|
191 [anAlert show]; |
|
192 [anAlert release]; |
|
193 } |
|
194 } |
|
195 |
|
196 #pragma mark - |
|
197 #pragma mark TableView Methods |
|
198 #define kNetworkFields 0 |
|
199 #define kAudioFields 1 |
|
200 #define kOtherFields 2 |
|
201 |
|
202 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
203 return 3; |
|
204 } |
|
205 |
|
206 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
207 switch (section) { |
|
208 case kNetworkFields: |
|
209 return 2; |
|
210 break; |
|
211 case kAudioFields: |
|
212 return 2; |
|
213 break; |
|
214 case kOtherFields: |
|
215 return 1; |
|
216 break; |
|
217 default: |
|
218 NSLog(@"Warning: unset case value for numberOfRowsInSection!"); |
|
219 break; |
|
220 } |
|
221 return 0; |
|
222 } |
|
223 |
|
224 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
225 static NSString *cellIdentifier = @"systemSettingsCell"; |
|
226 |
|
227 UITableViewCell *cell; |
|
228 if ( !(kAudioFields == [indexPath section] && 2 == [indexPath row]) ){ |
|
229 cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier]; |
|
230 if (nil == cell) { |
|
231 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
|
232 reuseIdentifier:cellIdentifier] autorelease]; |
|
233 } |
|
234 } |
|
235 |
|
236 switch ([indexPath section]) { |
|
237 case kNetworkFields: |
|
238 switch ([indexPath row]) { |
|
239 case 0: |
|
240 cell.textLabel.text = NSLocalizedString(@"Nickname", @""); |
|
241 cell.accessoryView = username; |
|
242 break; |
|
243 case 1: |
|
244 cell.textLabel.text = NSLocalizedString(@"Password", @""); |
|
245 cell.accessoryView = password; |
|
246 break; |
|
247 default: |
|
248 NSLog(@"Warning: unset case value in kNetworkFields section!"); |
|
249 break; |
|
250 } |
|
251 break; |
|
252 case kAudioFields: |
|
253 switch ([indexPath row]) { |
|
254 case 0: |
|
255 cell.accessoryView = soundsSwitch; |
|
256 cell.textLabel.text = NSLocalizedString(@"Sound", @""); |
|
257 break; |
|
258 case 1: |
|
259 cell.accessoryView = musicSwitch; |
|
260 cell.textLabel.text = NSLocalizedString(@"Music", @""); |
|
261 break; |
|
262 default: |
|
263 NSLog(@"Warning: unset case value in kAudioFields section!"); |
|
264 break; |
|
265 } |
|
266 break; |
|
267 case kOtherFields: |
|
268 cell.accessoryView = altDamageSwitch; |
|
269 cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @""); |
|
270 break; |
|
271 default: |
|
272 break; |
|
273 } |
|
274 |
|
275 return cell; |
|
276 } |
|
277 |
|
278 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { |
|
279 UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)] autorelease]; |
|
280 UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)] autorelease]; |
|
281 headerLabel.textColor = [UIColor lightGrayColor]; |
|
282 headerLabel.shadowColor = [UIColor blackColor]; |
|
283 headerLabel.shadowOffset = CGSizeMake(0, 1); |
|
284 headerLabel.font = [UIFont boldSystemFontOfSize:20]; |
|
285 headerLabel.backgroundColor = [UIColor clearColor]; |
|
286 |
|
287 switch (section) { |
|
288 case kNetworkFields: |
|
289 headerLabel.text = NSLocalizedString(@"Network Configuration", @""); |
|
290 break; |
|
291 case kAudioFields: |
|
292 headerLabel.text = NSLocalizedString(@"Audio Preferences", @""); |
|
293 break; |
|
294 case kOtherFields: |
|
295 headerLabel.text = NSLocalizedString(@"Other Settings", @""); |
|
296 break; |
|
297 default: |
|
298 NSLog(@"Warning: unset case value in titleForHeaderInSection!"); |
|
299 headerLabel.text = @"!"; |
|
300 break; |
|
301 } |
|
302 |
|
303 [containerView addSubview:headerLabel]; |
|
304 return containerView; |
|
305 } |
|
306 |
|
307 /* |
|
308 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
309 if (kAudioFields == [indexPath section] && 2 == [indexPath row]) |
|
310 return volumeCell.frame.size.height; |
|
311 else |
|
312 return table.rowHeight; |
|
313 } |
|
314 */ |
|
315 |
|
316 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { |
|
317 return 57.0; |
|
318 } |
|
319 |
|
320 @end |
|