3547
|
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 "CommodityFunctions.h"
|
3660
|
11 |
#import "EditableCellView.h"
|
3547
|
12 |
|
|
13 |
@implementation GeneralSettingsViewController
|
|
14 |
@synthesize settingsDictionary, textFieldBeingEdited, musicSwitch, soundSwitch, altDamageSwitch;
|
|
15 |
|
|
16 |
|
|
17 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
|
|
18 |
return rotationManager(interfaceOrientation);
|
|
19 |
}
|
|
20 |
|
|
21 |
#pragma mark -
|
|
22 |
#pragma mark textfield methods
|
|
23 |
// return to previous table
|
|
24 |
-(void) cancel:(id) sender {
|
|
25 |
if (textFieldBeingEdited != nil)
|
|
26 |
[self.textFieldBeingEdited resignFirstResponder];
|
|
27 |
}
|
|
28 |
|
|
29 |
// set the new value
|
3660
|
30 |
-(void) save:(id) sender {
|
3547
|
31 |
if (textFieldBeingEdited != nil) {
|
3660
|
32 |
if (textFieldBeingEdited.tag == 0)
|
3547
|
33 |
[self.settingsDictionary setObject:textFieldBeingEdited.text forKey:@"username"];
|
3660
|
34 |
else
|
3547
|
35 |
[self.settingsDictionary setObject:textFieldBeingEdited.text forKey:@"password"];
|
|
36 |
|
|
37 |
[self.textFieldBeingEdited resignFirstResponder];
|
|
38 |
}
|
|
39 |
}
|
|
40 |
|
|
41 |
// the textfield is being modified, update the navigation controller
|
|
42 |
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{
|
|
43 |
self.textFieldBeingEdited = aTextField;
|
|
44 |
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from the settings table")
|
|
45 |
style:UIBarButtonItemStylePlain
|
|
46 |
target:self
|
|
47 |
action:@selector(cancel:)];
|
|
48 |
self.navigationItem.leftBarButtonItem = cancelButton;
|
|
49 |
[cancelButton release];
|
|
50 |
|
|
51 |
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"from the settings table")
|
|
52 |
style:UIBarButtonItemStyleDone
|
|
53 |
target:self
|
|
54 |
action:@selector(save:)];
|
|
55 |
self.navigationItem.rightBarButtonItem = saveButton;
|
|
56 |
[saveButton release];
|
|
57 |
}
|
|
58 |
|
|
59 |
// the textfield has been modified, check for empty strings and restore original navigation bar
|
|
60 |
-(void) textFieldDidEndEditing:(UITextField *)aTextField{
|
|
61 |
self.textFieldBeingEdited = nil;
|
|
62 |
self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem;
|
|
63 |
self.navigationItem.leftBarButtonItem = nil;
|
|
64 |
}
|
|
65 |
|
|
66 |
// limit the size of the field to 64 characters like in original frontend
|
|
67 |
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
|
3660
|
68 |
return !([textField.text length] > MAX_STRING_LENGTH && [string length] > range.length);
|
3547
|
69 |
}
|
|
70 |
|
|
71 |
#pragma mark -
|
|
72 |
#pragma mark View Lifecycle
|
|
73 |
-(void) viewDidLoad {
|
|
74 |
[super viewDidLoad];
|
|
75 |
self.musicSwitch = [[UISwitch alloc] init];
|
|
76 |
self.soundSwitch = [[UISwitch alloc] init];
|
|
77 |
self.altDamageSwitch = [[UISwitch alloc] init];
|
|
78 |
[self.soundSwitch addTarget:self action:@selector(alsoTurnOffMusic:) forControlEvents:UIControlEventValueChanged];
|
|
79 |
[self.musicSwitch addTarget:self action:@selector(dontTurnOnMusic:) forControlEvents:UIControlEventValueChanged];
|
|
80 |
[self.altDamageSwitch addTarget:self action:@selector(justUpdateDictionary:) forControlEvents:UIControlEventValueChanged];
|
|
81 |
|
|
82 |
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
|
|
83 |
self.settingsDictionary = dictionary;
|
|
84 |
[dictionary release];
|
|
85 |
}
|
|
86 |
|
|
87 |
-(void) viewWillAppear:(BOOL)animated {
|
|
88 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
|
|
89 |
|
|
90 |
musicSwitch.on = [[settingsDictionary objectForKey:@"music"] boolValue];
|
|
91 |
soundSwitch.on = [[settingsDictionary objectForKey:@"sound"] boolValue];
|
|
92 |
altDamageSwitch.on = [[settingsDictionary objectForKey:@"alternate"] boolValue];
|
|
93 |
|
|
94 |
[super viewWillAppear:animated];
|
|
95 |
}
|
|
96 |
|
|
97 |
-(void) viewWillDisappear:(BOOL)animated {
|
3660
|
98 |
[super viewWillDisappear:animated];
|
|
99 |
[self.settingsDictionary writeToFile:SETTINGS_FILE() atomically:YES];
|
3547
|
100 |
}
|
|
101 |
|
|
102 |
#pragma mark -
|
|
103 |
// if the sound system is off, turn off also the background music
|
|
104 |
-(void) alsoTurnOffMusic:(id) sender {
|
|
105 |
[self.settingsDictionary setObject:[NSNumber numberWithBool:soundSwitch.on] forKey:@"sound"];
|
|
106 |
if (YES == self.musicSwitch.on) {
|
|
107 |
[musicSwitch setOn:NO animated:YES];
|
|
108 |
[self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"];
|
|
109 |
}
|
|
110 |
}
|
|
111 |
|
|
112 |
// if the sound system is off, don't enable background music
|
|
113 |
-(void) dontTurnOnMusic:(id) sender {
|
3660
|
114 |
if (NO == self.soundSwitch.on)
|
3547
|
115 |
[musicSwitch setOn:NO animated:YES];
|
3660
|
116 |
else
|
3547
|
117 |
[self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"];
|
|
118 |
}
|
|
119 |
|
|
120 |
-(void) justUpdateDictionary:(id) sender {
|
|
121 |
UISwitch *theSwitch = (UISwitch *)sender;
|
|
122 |
[self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"];
|
|
123 |
}
|
|
124 |
|
|
125 |
#pragma mark -
|
|
126 |
#pragma mark TableView Methods
|
|
127 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
|
|
128 |
return 3;
|
|
129 |
}
|
|
130 |
|
|
131 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
132 |
switch (section) {
|
3660
|
133 |
case 0: // user and pass
|
3547
|
134 |
return 2;
|
|
135 |
break;
|
3660
|
136 |
case 1: // audio
|
3547
|
137 |
return 2;
|
|
138 |
break;
|
3660
|
139 |
case 2: // alternate damage
|
3547
|
140 |
return 1;
|
|
141 |
break;
|
|
142 |
default:
|
|
143 |
break;
|
|
144 |
}
|
|
145 |
return 0;
|
|
146 |
}
|
|
147 |
|
|
148 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
149 |
static NSString *cellIdentifier = @"systemSettingsCell";
|
|
150 |
NSInteger row = [indexPath row];
|
|
151 |
NSInteger section = [indexPath section];
|
|
152 |
|
3660
|
153 |
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier];
|
3547
|
154 |
UITextField *aTextField;
|
|
155 |
switch (section) {
|
3660
|
156 |
case 0:
|
|
157 |
if (nil == cell) {
|
|
158 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
|
|
159 |
if (section == 0) {
|
|
160 |
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(-9, 10, 100, [UIFont labelFontSize] + 4)];
|
|
161 |
label.textAlignment = UITextAlignmentRight;
|
|
162 |
label.backgroundColor = [UIColor clearColor];
|
|
163 |
label.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
|
|
164 |
if (row == 0)
|
|
165 |
label.text = NSLocalizedString(@"Nickname","from the settings table");
|
|
166 |
else
|
|
167 |
label.text = NSLocalizedString(@"Password","from the settings table");
|
|
168 |
[cell.contentView addSubview:label];
|
|
169 |
[label release];
|
|
170 |
|
|
171 |
UITextField *aTextField = [[UITextField alloc] initWithFrame:
|
|
172 |
CGRectMake(110, 10, (cell.frame.size.width + cell.frame.size.width/3) - 90, [UIFont labelFontSize] + 4)];
|
|
173 |
aTextField.clearsOnBeginEditing = NO;
|
|
174 |
aTextField.returnKeyType = UIReturnKeyDone;
|
|
175 |
aTextField.adjustsFontSizeToFitWidth = YES;
|
|
176 |
aTextField.delegate = self;
|
|
177 |
aTextField.tag = row;
|
|
178 |
aTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
|
|
179 |
[aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
|
|
180 |
[cell.contentView addSubview:aTextField];
|
|
181 |
[aTextField release];
|
|
182 |
}
|
|
183 |
}
|
3547
|
184 |
for (UIView *oneView in cell.contentView.subviews)
|
|
185 |
if ([oneView isMemberOfClass:[UITextField class]])
|
|
186 |
aTextField = (UITextField *)oneView;
|
|
187 |
|
|
188 |
switch (row) {
|
|
189 |
case 0:
|
|
190 |
aTextField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@"");
|
|
191 |
aTextField.text = [self.settingsDictionary objectForKey:@"username"];
|
3660
|
192 |
aTextField.secureTextEntry = NO;
|
3547
|
193 |
break;
|
|
194 |
case 1:
|
|
195 |
aTextField.placeholder = NSLocalizedString(@"Insert your password",@"");
|
|
196 |
aTextField.text = [self.settingsDictionary objectForKey:@"password"];
|
|
197 |
aTextField.secureTextEntry = YES;
|
|
198 |
break;
|
|
199 |
default:
|
|
200 |
break;
|
|
201 |
}
|
|
202 |
break;
|
|
203 |
|
3660
|
204 |
cell.accessoryView = nil;
|
|
205 |
case 1:
|
|
206 |
if (nil == cell)
|
|
207 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
|
|
208 |
|
3547
|
209 |
switch (row) {
|
|
210 |
case 0:
|
|
211 |
cell.textLabel.text = NSLocalizedString(@"Sound", @"");
|
|
212 |
cell.accessoryView = soundSwitch;
|
|
213 |
break;
|
|
214 |
case 1:
|
|
215 |
cell.textLabel.text = NSLocalizedString(@"Music", @"");
|
|
216 |
cell.accessoryView = musicSwitch;
|
|
217 |
break;
|
|
218 |
default:
|
|
219 |
break;
|
|
220 |
}
|
|
221 |
break;
|
|
222 |
|
3660
|
223 |
case 2:
|
|
224 |
if (nil == cell)
|
|
225 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
|
|
226 |
|
3547
|
227 |
cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @"");
|
|
228 |
cell.accessoryView = altDamageSwitch;
|
|
229 |
break;
|
|
230 |
default:
|
|
231 |
break;
|
|
232 |
}
|
3660
|
233 |
|
|
234 |
cell.accessoryType = UITableViewCellAccessoryNone;
|
|
235 |
cell.selectionStyle = UITableViewCellSelectionStyleNone;
|
|
236 |
cell.imageView.image = nil;
|
|
237 |
|
3547
|
238 |
return cell;
|
|
239 |
}
|
|
240 |
|
|
241 |
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
|
|
242 |
NSString *sectionTitle = nil;
|
|
243 |
switch (section) {
|
3660
|
244 |
case 0:
|
3547
|
245 |
sectionTitle = NSLocalizedString(@"Network Configuration", @"");
|
|
246 |
break;
|
3660
|
247 |
case 1:
|
3547
|
248 |
sectionTitle = NSLocalizedString(@"Audio Preferences", @"");
|
|
249 |
break;
|
3660
|
250 |
case 2:
|
3547
|
251 |
sectionTitle = NSLocalizedString(@"Other Settings", @"");
|
|
252 |
break;
|
|
253 |
default:
|
3660
|
254 |
DLog(@"Nope");
|
3547
|
255 |
break;
|
|
256 |
}
|
|
257 |
return sectionTitle;
|
|
258 |
}
|
|
259 |
|
|
260 |
/*
|
|
261 |
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
|
|
262 |
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)] autorelease];
|
|
263 |
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)] autorelease];
|
|
264 |
headerLabel.textColor = [UIColor lightGrayColor];
|
|
265 |
headerLabel.shadowColor = [UIColor blackColor];
|
|
266 |
headerLabel.shadowOffset = CGSizeMake(0, 1);
|
|
267 |
headerLabel.font = [UIFont boldSystemFontOfSize:20];
|
|
268 |
headerLabel.backgroundColor = [UIColor clearColor];
|
|
269 |
|
|
270 |
switch (section) {
|
|
271 |
case kNetworkFields:
|
|
272 |
headerLabel.text = NSLocalizedString(@"Network Configuration", @"");
|
|
273 |
break;
|
|
274 |
case kAudioFields:
|
|
275 |
headerLabel.text = NSLocalizedString(@"Audio Preferences", @"");
|
|
276 |
break;
|
|
277 |
case kOtherFields:
|
|
278 |
headerLabel.text = NSLocalizedString(@"Other Settings", @"");
|
|
279 |
break;
|
|
280 |
default:
|
|
281 |
NSLog(@"Warning: unset case value in titleForHeaderInSection!");
|
|
282 |
headerLabel.text = @"!";
|
|
283 |
break;
|
|
284 |
}
|
|
285 |
|
|
286 |
[containerView addSubview:headerLabel];
|
|
287 |
return containerView;
|
|
288 |
}
|
|
289 |
|
|
290 |
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
291 |
if (kAudioFields == [indexPath section] && 2 == [indexPath row])
|
|
292 |
return volumeCell.frame.size.height;
|
|
293 |
else
|
|
294 |
return table.rowHeight;
|
|
295 |
}
|
|
296 |
|
|
297 |
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
|
|
298 |
return 57.0;
|
|
299 |
}
|
|
300 |
*/
|
|
301 |
|
3574
|
302 |
/*
|
|
303 |
causes segfault if pressing twice cancel
|
3547
|
304 |
#pragma mark -
|
|
305 |
#pragma mark Table view delegate
|
|
306 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
307 |
UITableViewCell *cell;
|
3660
|
308 |
if (0 == [indexPath section]) {
|
3547
|
309 |
cell = [aTableView cellForRowAtIndexPath:indexPath];
|
|
310 |
for (UIView *oneView in cell.contentView.subviews) {
|
|
311 |
if ([oneView isMemberOfClass:[UITextField class]]) {
|
|
312 |
textFieldBeingEdited = (UITextField *)oneView;
|
|
313 |
[textFieldBeingEdited becomeFirstResponder];
|
|
314 |
}
|
|
315 |
}
|
|
316 |
[aTableView deselectRowAtIndexPath:indexPath animated:NO];
|
|
317 |
}
|
|
318 |
}
|
3574
|
319 |
*/
|
3547
|
320 |
|
|
321 |
#pragma mark -
|
|
322 |
#pragma mark Memory management
|
|
323 |
-(void) didReceiveMemoryWarning {
|
|
324 |
[super didReceiveMemoryWarning];
|
|
325 |
}
|
|
326 |
|
|
327 |
-(void) viewDidUnload {
|
|
328 |
self.settingsDictionary = nil;
|
|
329 |
self.textFieldBeingEdited = nil;
|
|
330 |
self.musicSwitch = nil;
|
|
331 |
self.soundSwitch = nil;
|
|
332 |
self.altDamageSwitch = nil;
|
|
333 |
[super viewDidUnload];
|
|
334 |
MSG_DIDUNLOAD();
|
|
335 |
}
|
|
336 |
|
|
337 |
-(void) dealloc {
|
|
338 |
[settingsDictionary release];
|
|
339 |
[textFieldBeingEdited release];
|
|
340 |
[musicSwitch release];
|
|
341 |
[soundSwitch release];
|
|
342 |
[altDamageSwitch release];
|
|
343 |
[super dealloc];
|
|
344 |
}
|
|
345 |
|
|
346 |
|
|
347 |
@end
|