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