8 |
8 |
9 #import "GeneralSettingsViewController.h" |
9 #import "GeneralSettingsViewController.h" |
10 #import "SDL_uikitappdelegate.h" |
10 #import "SDL_uikitappdelegate.h" |
11 |
11 |
12 @implementation GeneralSettingsViewController |
12 @implementation GeneralSettingsViewController |
13 @synthesize dataDict, username, password, musicSwitch, soundSwitch, altDamageSwitch; |
13 @synthesize settingsDictionary, textFieldBeingEdited, musicSwitch, soundSwitch, altDamageSwitch; |
14 |
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 |
15 |
25 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
16 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
26 return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
17 return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
27 } |
18 } |
28 |
19 |
29 #pragma mark - |
20 #pragma mark - |
|
21 #pragma mark textfield methods |
|
22 // return to previous table |
|
23 -(void) cancel:(id) sender { |
|
24 if (textFieldBeingEdited != nil) |
|
25 [self.textFieldBeingEdited resignFirstResponder]; |
|
26 } |
|
27 |
|
28 // set the new value |
|
29 -(BOOL) save:(id) sender { |
|
30 if (textFieldBeingEdited != nil) { |
|
31 if (textFieldBeingEdited.tag == 0) { |
|
32 [self.settingsDictionary setObject:textFieldBeingEdited.text forKey:@"username"]; |
|
33 } else { |
|
34 [self.settingsDictionary setObject:textFieldBeingEdited.text forKey:@"password"]; |
|
35 } |
|
36 |
|
37 isWriteNeeded = YES; |
|
38 [self.textFieldBeingEdited resignFirstResponder]; |
|
39 return YES; |
|
40 } |
|
41 return NO; |
|
42 } |
|
43 |
|
44 // the textfield is being modified, update the navigation controller |
|
45 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
|
46 self.textFieldBeingEdited = aTextField; |
|
47 UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from the settings table") |
|
48 style:UIBarButtonItemStylePlain |
|
49 target:self |
|
50 action:@selector(cancel:)]; |
|
51 self.navigationItem.leftBarButtonItem = cancelButton; |
|
52 [cancelButton release]; |
|
53 |
|
54 UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"from the settings table") |
|
55 style:UIBarButtonItemStyleDone |
|
56 target:self |
|
57 action:@selector(save:)]; |
|
58 self.navigationItem.rightBarButtonItem = saveButton; |
|
59 [saveButton release]; |
|
60 } |
|
61 |
|
62 // we save every time a textfield is edited, so we don't risk to update only the hogs or only the temname |
|
63 -(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField { |
|
64 return [self save:nil]; |
|
65 } |
|
66 |
|
67 // the textfield has been modified, check for empty strings and restore original navigation bar |
|
68 -(void) textFieldDidEndEditing:(UITextField *)aTextField{ |
|
69 self.textFieldBeingEdited = nil; |
|
70 self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem; |
|
71 self.navigationItem.leftBarButtonItem = nil; |
|
72 } |
|
73 |
|
74 // limit the size of the field to 64 characters like in original frontend |
|
75 -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
|
76 int limit = 64; |
|
77 return !([textField.text length] > limit && [string length] > range.length); |
|
78 } |
|
79 |
|
80 |
|
81 #pragma mark - |
30 #pragma mark View Lifecycle |
82 #pragma mark View Lifecycle |
31 -(void) viewDidLoad { |
83 -(void) viewDidLoad { |
|
84 [super viewDidLoad]; |
32 self.musicSwitch = [[UISwitch alloc] init]; |
85 self.musicSwitch = [[UISwitch alloc] init]; |
33 self.soundSwitch = [[UISwitch alloc] init]; |
86 self.soundSwitch = [[UISwitch alloc] init]; |
34 self.altDamageSwitch = [[UISwitch alloc] init]; |
87 self.altDamageSwitch = [[UISwitch alloc] init]; |
35 [self.soundSwitch addTarget:self action:@selector(sameValueSwitch) forControlEvents:UIControlEventValueChanged]; |
88 [self.soundSwitch addTarget:self action:@selector(alsoTurnOffMusic:) forControlEvents:UIControlEventValueChanged]; |
36 [self.musicSwitch addTarget:self action:@selector(checkValueSwitch) forControlEvents:UIControlEventValueChanged]; |
89 [self.musicSwitch addTarget:self action:@selector(dontTurnOnMusic:) forControlEvents:UIControlEventValueChanged]; |
37 |
90 [self.altDamageSwitch addTarget:self action:@selector(justUpdateDictionary:) forControlEvents:UIControlEventValueChanged]; |
|
91 |
38 NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"]; |
92 NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"]; |
39 NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:filePath]; |
93 NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; |
40 self.dataDict = dictionary; |
94 self.settingsDictionary = dictionary; |
41 [dictionary release]; |
95 [dictionary release]; |
42 [super viewDidLoad]; |
96 } |
43 } |
97 |
44 |
98 |
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 |
99 |
55 -(void) viewWillAppear:(BOOL)animated { |
100 -(void) viewWillAppear:(BOOL)animated { |
56 [self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
101 [self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
57 |
102 isWriteNeeded = NO; |
58 username = [NSString stringWithString:[dataDict objectForKey:@"username"]]; |
103 |
59 password = [NSString stringWithString:[dataDict objectForKey:@"password"]]; |
104 musicSwitch.on = [[settingsDictionary objectForKey:@"music"] boolValue]; |
60 |
105 soundSwitch.on = [[settingsDictionary objectForKey:@"sound"] boolValue]; |
61 if (1 == [[dataDict objectForKey:@"music"] intValue]) { |
106 altDamageSwitch.on = [[settingsDictionary objectForKey:@"alternate"] boolValue]; |
62 musicSwitch.on = YES; |
107 |
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]; |
108 [super viewWillAppear:animated]; |
78 } |
109 } |
79 |
110 |
80 -(void) viewWillDisappear:(BOOL)animated { |
111 -(void) viewWillDisappear:(BOOL)animated { |
81 [super viewWillDisappear:animated]; |
112 [super viewWillDisappear:animated]; |
82 |
113 |
83 NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init]; |
114 if (isWriteNeeded) { |
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"); |
115 NSLog(@"writing preferences to file"); |
96 [saveDict writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES]; |
116 [self.settingsDictionary writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES]; |
97 // this will also relase the previous dictionary |
117 isWriteNeeded = NO; |
98 self.dataDict = saveDict; |
|
99 } |
118 } |
100 [saveDict release]; |
119 } |
101 } |
120 |
102 |
121 #pragma mark - |
103 #pragma mark - |
122 // if the sound system is off, turn off also the background music |
104 // set music off when sound is turned off |
123 -(void) alsoTurnOffMusic:(id) sender { |
105 -(void) sameValueSwitch { |
124 [self.settingsDictionary setObject:[NSNumber numberWithBool:soundSwitch.on] forKey:@"sound"]; |
106 if (YES == self.musicSwitch.on) { |
125 if (YES == self.musicSwitch.on) { |
107 [musicSwitch setOn:NO animated:YES]; |
126 [musicSwitch setOn:NO animated:YES]; |
108 } |
127 [self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"]; |
109 } |
128 } |
110 |
129 isWriteNeeded = YES; |
111 // don't enable music when sound is off |
130 } |
112 -(void) checkValueSwitch { |
131 |
|
132 // if the sound system is off, don't enable background music |
|
133 -(void) dontTurnOnMusic:(id) sender { |
113 if (NO == self.soundSwitch.on) { |
134 if (NO == self.soundSwitch.on) { |
114 [musicSwitch setOn:!musicSwitch.on animated:YES]; |
135 [musicSwitch setOn:NO animated:YES]; |
115 } |
136 } else { |
116 } |
137 [self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"]; |
117 |
138 isWriteNeeded = YES; |
118 /* |
139 } |
119 // makes the keyboard go away when background is tapped |
140 } |
120 -(IBAction) backgroundTap: (id)sender { |
141 |
121 // [username resignFirstResponder]; |
142 -(void) justUpdateDictionary:(id) sender { |
122 // [password resignFirstResponder]; |
143 UISwitch *theSwitch = (UISwitch *)sender; |
123 } |
144 [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"]; |
124 |
145 isWriteNeeded = YES; |
125 // makes the keyboard go away when "Done" is tapped |
146 } |
126 -(IBAction) textFieldDoneEditing: (id)sender { |
|
127 [sender resignFirstResponder]; |
|
128 } |
|
129 */ |
|
130 |
147 |
131 /* |
148 /* |
132 #pragma mark - |
149 #pragma mark - |
133 #pragma mark UIActionSheet Methods |
150 #pragma mark UIActionSheet Methods |
134 -(IBAction) deleteData: (id)sender { |
151 -(IBAction) deleteData: (id)sender { |
185 break; |
202 break; |
186 case kOtherFields: |
203 case kOtherFields: |
187 return 1; |
204 return 1; |
188 break; |
205 break; |
189 default: |
206 default: |
190 NSLog(@"Warning: unset case value for numberOfRowsInSection!"); |
|
191 break; |
207 break; |
192 } |
208 } |
193 return 0; |
209 return 0; |
194 } |
210 } |
195 |
211 |
196 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
212 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
197 static NSString *cellIdentifier1 = @"systemSettingsCell1"; |
213 static NSString *cellIdentifier = @"systemSettingsCell"; |
198 static NSString *cellIdentifier2 = @"systemSettingsCell2"; |
214 NSInteger row = [indexPath row]; |
199 |
215 NSInteger section = [indexPath section]; |
200 UITableViewCell *cell = nil; |
216 |
201 |
217 UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier]; |
202 switch ([indexPath section]) { |
218 if (nil == cell) { |
203 case kNetworkFields: |
219 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; |
204 cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier1]; |
220 if (section == kNetworkFields) { |
205 if (nil == cell) { |
221 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(-15, 10, 100, 25)]; |
206 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 |
222 label.textAlignment = UITextAlignmentRight; |
207 reuseIdentifier:cellIdentifier1] autorelease]; |
223 label.backgroundColor = [UIColor clearColor]; |
208 } |
224 label.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2]; |
209 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
225 if (row == 0) |
|
226 label.text = NSLocalizedString(@"Nickname","from the settings table"); |
|
227 else |
|
228 label.text = NSLocalizedString(@"Password","from the settings table"); |
|
229 [cell.contentView addSubview:label]; |
|
230 [label release]; |
210 |
231 |
211 switch ([indexPath row]) { |
232 UITextField *aTextField = [[UITextField alloc] initWithFrame: |
|
233 CGRectMake(110, 12, (cell.frame.size.width + cell.frame.size.width/3) - 90, 25)]; |
|
234 aTextField.clearsOnBeginEditing = NO; |
|
235 aTextField.returnKeyType = UIReturnKeyDone; |
|
236 aTextField.adjustsFontSizeToFitWidth = YES; |
|
237 aTextField.delegate = self; |
|
238 aTextField.tag = row; |
|
239 aTextField.clearButtonMode = UITextFieldViewModeWhileEditing; |
|
240 [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
|
241 [cell.contentView addSubview:aTextField]; |
|
242 [aTextField release]; |
|
243 } |
|
244 } |
|
245 |
|
246 cell.accessoryType = UITableViewCellAccessoryNone; |
|
247 cell.selectionStyle = UITableViewCellSelectionStyleNone; |
|
248 cell.imageView.image = nil; |
|
249 |
|
250 UITextField *aTextField; |
|
251 switch (section) { |
|
252 case kNetworkFields: |
|
253 for (UIView *oneView in cell.contentView.subviews) |
|
254 if ([oneView isMemberOfClass:[UITextField class]]) |
|
255 aTextField = (UITextField *)oneView; |
|
256 |
|
257 switch (row) { |
212 case 0: |
258 case 0: |
213 cell.textLabel.text = NSLocalizedString(@"Nickname", @""); |
259 aTextField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@""); |
214 if ([username isEqualToString:@""]) { |
260 aTextField.text = [self.settingsDictionary objectForKey:@"username"]; |
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; |
261 break; |
222 case 1: |
262 case 1: |
223 cell.textLabel.text = NSLocalizedString(@"Password", @""); |
263 aTextField.placeholder = NSLocalizedString(@"Insert your password",@""); |
224 if ([password isEqualToString:@""]) { |
264 aTextField.text = [self.settingsDictionary objectForKey:@"password"]; |
225 cell.detailTextLabel.text = @"insert password..."; |
265 aTextField.secureTextEntry = YES; |
226 cell.detailTextLabel.font = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]]; |
|
227 cell.detailTextLabel.textColor = [UIColor grayColor]; |
|
228 } else { |
|
229 cell.detailTextLabel.text = @"••••••••"; |
|
230 } |
|
231 break; |
266 break; |
232 default: |
267 default: |
233 NSLog(@"Warning: unset case value in kNetworkFields section!"); |
|
234 break; |
268 break; |
235 } |
269 } |
236 break; |
270 break; |
237 |
271 |
238 case kAudioFields: |
272 case kAudioFields: |
239 cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier2]; |
273 switch (row) { |
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: |
274 case 0: |
248 cell.textLabel.text = NSLocalizedString(@"Sound", @""); |
275 cell.textLabel.text = NSLocalizedString(@"Sound", @""); |
249 cell.accessoryView = soundSwitch; |
276 cell.accessoryView = soundSwitch; |
250 break; |
277 break; |
251 case 1: |
278 case 1: |
252 cell.textLabel.text = NSLocalizedString(@"Music", @""); |
279 cell.textLabel.text = NSLocalizedString(@"Music", @""); |
253 cell.accessoryView = musicSwitch; |
280 cell.accessoryView = musicSwitch; |
254 break; |
281 break; |
255 default: |
282 default: |
256 NSLog(@"Warning: unset case value in kAudioFields section!"); |
|
257 break; |
283 break; |
258 } |
284 } |
259 // this makes the row not selectable |
|
260 break; |
285 break; |
261 |
286 |
262 case kOtherFields: |
287 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; |
288 cell.selectionStyle = UITableViewCellSelectionStyleNone; |
269 cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @""); |
289 cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @""); |
270 cell.accessoryView = altDamageSwitch; |
290 cell.accessoryView = altDamageSwitch; |
271 break; |
291 break; |
272 default: |
292 default: |