author | koda |
Sat, 10 Apr 2010 01:09:29 +0000 | |
changeset 3330 | 987ec27b6042 |
parent 3329 | d8e41ee0b3ae |
child 3332 | 3c90a923f156 |
permissions | -rw-r--r-- |
3305 | 1 |
// |
2 |
// SingleTeamViewController.m |
|
3 |
// HedgewarsMobile |
|
4 |
// |
|
5 |
// Created by Vittorio on 02/04/10. |
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "SingleTeamViewController.h" |
|
10 |
#import "HogHatViewController.h" |
|
3325 | 11 |
#import "FlagsViewController.h" |
12 |
#import "FortsViewController.h" |
|
13 |
#import "CommodityFunctions.h" |
|
3305 | 14 |
|
3330 | 15 |
#define TEAMNAME_TAG 1234 |
16 |
||
3305 | 17 |
@implementation SingleTeamViewController |
3330 | 18 |
@synthesize teamDictionary, hatArray, secondaryItems, secondaryControllers, textFieldBeingEdited, teamName; |
3325 | 19 |
|
3305 | 20 |
|
3325 | 21 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
22 |
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
|
23 |
} |
|
3305 | 24 |
|
3329 | 25 |
|
26 |
#pragma mark - |
|
27 |
#pragma mark textfield methods |
|
28 |
// return to previous table |
|
29 |
-(void) cancel:(id) sender { |
|
30 |
if (textFieldBeingEdited != nil) |
|
31 |
[self.textFieldBeingEdited resignFirstResponder]; |
|
32 |
} |
|
33 |
||
34 |
// set the new value |
|
3330 | 35 |
-(BOOL) save:(id) sender { |
3329 | 36 |
if (textFieldBeingEdited != nil) { |
3330 | 37 |
if (TEAMNAME_TAG == selectedHog) { |
38 |
NSLog(@"%@", textFieldBeingEdited.text); |
|
39 |
[self.teamDictionary setObject:textFieldBeingEdited.text forKey:@"teamname"]; |
|
40 |
} else { |
|
41 |
//replace the old value with the new one |
|
42 |
NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
|
43 |
NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
|
44 |
[newHog setObject:textFieldBeingEdited.text forKey:@"hogname"]; |
|
45 |
[[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
|
46 |
[newHog release]; |
|
47 |
} |
|
3329 | 48 |
|
49 |
isWriteNeeded = YES; |
|
50 |
[self.textFieldBeingEdited resignFirstResponder]; |
|
3330 | 51 |
return YES; |
3329 | 52 |
} |
3330 | 53 |
return NO; |
3329 | 54 |
} |
55 |
||
56 |
// the textfield is being modified, update the navigation controller |
|
3330 | 57 |
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
3329 | 58 |
self.textFieldBeingEdited = aTextField; |
59 |
selectedHog = aTextField.tag; |
|
60 |
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from the hog name table") |
|
61 |
style:UIBarButtonItemStylePlain |
|
62 |
target:self |
|
63 |
action:@selector(cancel:)]; |
|
64 |
self.navigationItem.leftBarButtonItem = cancelButton; |
|
65 |
[cancelButton release]; |
|
66 |
||
67 |
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"from the hog name table") |
|
68 |
style:UIBarButtonItemStyleDone |
|
69 |
target:self |
|
70 |
action:@selector(save:)]; |
|
71 |
self.navigationItem.rightBarButtonItem = saveButton; |
|
72 |
[saveButton release]; |
|
73 |
} |
|
74 |
||
3330 | 75 |
// we save every time a textfield is edited, so we don't risk to update only the hogs or only the temname |
76 |
-(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField { |
|
77 |
return [self save:nil]; |
|
78 |
} |
|
79 |
||
3329 | 80 |
// the textfield has been modified, check for empty strings and restore original navigation bar |
81 |
-(void) textFieldDidEndEditing:(UITextField *)aTextField{ |
|
82 |
if ([textFieldBeingEdited.text length] == 0) |
|
83 |
textFieldBeingEdited.text = [NSString stringWithFormat:@"hedgehog %d",textFieldBeingEdited.tag]; |
|
84 |
||
3330 | 85 |
//self.textFieldBeingEdited = nil; |
3329 | 86 |
self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem; |
87 |
self.navigationItem.leftBarButtonItem = nil; |
|
88 |
} |
|
89 |
||
90 |
// limit the size of the field to 64 characters like in original frontend |
|
91 |
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
|
92 |
int limit = 64; |
|
93 |
return !([textField.text length] > limit && [string length] > range.length); |
|
94 |
} |
|
95 |
||
96 |
||
3305 | 97 |
#pragma mark - |
98 |
#pragma mark View lifecycle |
|
99 |
- (void)viewDidLoad { |
|
100 |
[super viewDidLoad]; |
|
101 |
||
3329 | 102 |
// labels for the entries |
3305 | 103 |
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: |
104 |
NSLocalizedString(@"Grave",@""), |
|
105 |
NSLocalizedString(@"Voice",@""), |
|
106 |
NSLocalizedString(@"Fort",@""), |
|
107 |
NSLocalizedString(@"Flag",@""), |
|
108 |
NSLocalizedString(@"Level",@""),nil]; |
|
109 |
self.secondaryItems = array; |
|
110 |
[array release]; |
|
3312 | 111 |
|
3325 | 112 |
// insert controllers here |
113 |
NSMutableArray *controllersArray = [[NSMutableArray alloc] initWithCapacity:[secondaryItems count]]; |
|
114 |
||
115 |
FlagsViewController *flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
116 |
[controllersArray addObject:flagsViewController]; |
|
117 |
[flagsViewController release]; |
|
118 |
||
119 |
FortsViewController *fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
120 |
[controllersArray addObject:fortsViewController]; |
|
121 |
[fortsViewController release]; |
|
122 |
||
123 |
self.secondaryControllers = controllersArray; |
|
124 |
[controllersArray release]; |
|
3315 | 125 |
|
126 |
// listen if any childController modifies the plist and write it if needed |
|
127 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil]; |
|
128 |
isWriteNeeded = NO; |
|
129 |
} |
|
130 |
||
131 |
- (void)viewWillAppear:(BOOL)animated { |
|
132 |
[super viewWillAppear:animated]; |
|
3323 | 133 |
|
3329 | 134 |
// load data about the team and write if there has been a change |
3330 | 135 |
if (isWriteNeeded) |
136 |
[self writeFile]; |
|
137 |
||
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
138 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
139 |
NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
140 |
self.teamDictionary = teamDict; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
141 |
[teamDict release]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
142 |
[teamFile release]; |
3330 | 143 |
|
144 |
self.teamName = self.title; |
|
145 |
||
3312 | 146 |
// load the images of the hat for aach hog |
3330 | 147 |
NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
148 |
UIImage *normalHogImage = [[UIImage alloc] initWithContentsOfFile:normalHogFile]; |
|
149 |
[normalHogFile release]; |
|
150 |
CGRect hogSpriteArea = CGRectMake(96, 0, 32, 32); |
|
151 |
CGImageRef cgImg = CGImageCreateWithImageInRect([normalHogImage CGImage], hogSpriteArea); |
|
152 |
[normalHogImage release]; |
|
153 |
UIImage *normalHogSprite = [[UIImage alloc] initWithCGImage:cgImg]; |
|
154 |
CGImageRelease(cgImg); |
|
155 |
||
3315 | 156 |
NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
157 |
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]]; |
|
158 |
for (NSDictionary *hog in hogArray) { |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
159 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png",HATS_DIRECTORY(),[hog objectForKey:@"hat"]]; |
3312 | 160 |
|
161 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile]; |
|
3316 | 162 |
[hatFile release]; |
3312 | 163 |
CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32); |
164 |
CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea); |
|
165 |
[image release]; |
|
166 |
||
167 |
UIImage *hatSprite = [[UIImage alloc] initWithCGImage:cgImgage]; |
|
168 |
CGImageRelease(cgImgage); |
|
3330 | 169 |
|
170 |
[array addObject:mergeTwoImages(normalHogSprite, hatSprite)]; |
|
3312 | 171 |
[hatSprite release]; |
172 |
} |
|
3330 | 173 |
[normalHogSprite release]; |
3315 | 174 |
self.hatArray = array; |
3312 | 175 |
[array release]; |
3315 | 176 |
|
177 |
[self.tableView reloadData]; |
|
3305 | 178 |
} |
179 |
||
3329 | 180 |
// write on file if there has been a change |
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
181 |
-(void) viewWillDisappear:(BOOL)animated { |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
182 |
[super viewWillDisappear:animated]; |
3330 | 183 |
if (isWriteNeeded) |
184 |
[self writeFile]; |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
185 |
} |
3329 | 186 |
|
3325 | 187 |
// needed by other classes to warn about a user change |
3315 | 188 |
-(void) setWriteNeeded { |
189 |
isWriteNeeded = YES; |
|
190 |
} |
|
191 |
||
3330 | 192 |
-(void) writeFile { |
193 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title]; |
|
194 |
||
195 |
NSString *newTeamName = [self.teamDictionary objectForKey:@"teamname"]; |
|
196 |
if (![newTeamName isEqualToString:self.teamName]) { |
|
197 |
//delete old |
|
198 |
[[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL]; |
|
199 |
[teamFile release]; |
|
200 |
self.title = newTeamName; |
|
201 |
self.teamName = newTeamName; |
|
202 |
teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),newTeamName]; |
|
203 |
} |
|
204 |
||
205 |
[self.teamDictionary writeToFile:teamFile atomically:YES]; |
|
206 |
NSLog(@"writing: %@",teamDictionary); |
|
207 |
isWriteNeeded = NO; |
|
208 |
[teamFile release]; |
|
209 |
} |
|
210 |
||
3305 | 211 |
#pragma mark - |
212 |
#pragma mark Table view data source |
|
213 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
214 |
return 3; |
|
215 |
} |
|
216 |
||
217 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
3312 | 218 |
NSInteger rows = 0; |
3305 | 219 |
switch (section) { |
3325 | 220 |
case 0: // team name |
3305 | 221 |
rows = 1; |
222 |
break; |
|
3325 | 223 |
case 1: // team members |
224 |
rows = MAX_HOGS; |
|
3305 | 225 |
break; |
3325 | 226 |
case 2: // team details |
227 |
rows = [self.secondaryItems count]; |
|
3305 | 228 |
break; |
229 |
default: |
|
230 |
break; |
|
231 |
} |
|
232 |
return rows; |
|
233 |
} |
|
234 |
||
235 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
236 |
static NSString *CellIdentifier = @"Cell"; |
|
237 |
||
238 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
239 |
if (cell == nil) { |
|
240 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
|
241 |
reuseIdentifier:CellIdentifier] autorelease]; |
|
3330 | 242 |
if ([indexPath section] != 2) { |
3329 | 243 |
// create a uitextfield for each row, expand it to take the maximum size |
3330 | 244 |
UITextField *aTextField; |
245 |
if ([indexPath section] == 1) { |
|
246 |
aTextField = [[UITextField alloc] |
|
247 |
initWithFrame:CGRectMake(42, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)]; |
|
248 |
} else { |
|
249 |
aTextField = [[UITextField alloc] |
|
250 |
initWithFrame:CGRectMake(5, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)]; |
|
251 |
} |
|
252 |
||
3329 | 253 |
aTextField.clearsOnBeginEditing = NO; |
254 |
aTextField.returnKeyType = UIReturnKeyDone; |
|
255 |
aTextField.adjustsFontSizeToFitWidth = YES; |
|
256 |
aTextField.delegate = self; |
|
257 |
aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2]; |
|
258 |
aTextField.clearButtonMode = UITextFieldViewModeWhileEditing; |
|
259 |
[aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
|
260 |
[cell.contentView addSubview:aTextField]; |
|
261 |
[aTextField release]; |
|
262 |
} |
|
3305 | 263 |
} |
3325 | 264 |
|
3315 | 265 |
NSArray *hogArray; |
3305 | 266 |
NSInteger row = [indexPath row]; |
267 |
switch ([indexPath section]) { |
|
268 |
case 0: |
|
3315 | 269 |
cell.imageView.image = nil; |
3330 | 270 |
cell.accessoryType = UITableViewCellAccessoryNone; |
271 |
for (UIView *oneView in cell.contentView.subviews) { |
|
272 |
if ([oneView isMemberOfClass:[UITextField class]]) { |
|
273 |
// we find the uitextfied and we'll use its tag to understand which one is being edited |
|
274 |
UITextField *textFieldFound = (UITextField *)oneView; |
|
275 |
textFieldFound.text = [self.teamDictionary objectForKey:@"teamname"]; |
|
276 |
textFieldFound.tag = TEAMNAME_TAG; |
|
277 |
} |
|
278 |
} |
|
3305 | 279 |
break; |
280 |
case 1: |
|
3315 | 281 |
hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
3329 | 282 |
|
3315 | 283 |
cell.imageView.image = [self.hatArray objectAtIndex:row]; |
3329 | 284 |
|
285 |
for (UIView *oneView in cell.contentView.subviews) { |
|
286 |
if ([oneView isMemberOfClass:[UITextField class]]) { |
|
287 |
// we find the uitextfied and we'll use its tag to understand which one is being edited |
|
288 |
UITextField *textFieldFound = (UITextField *)oneView; |
|
289 |
textFieldFound.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"]; |
|
290 |
textFieldFound.tag = row; |
|
291 |
} |
|
292 |
} |
|
293 |
||
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
294 |
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; |
3305 | 295 |
break; |
296 |
case 2: |
|
297 |
cell.textLabel.text = [self.secondaryItems objectAtIndex:row]; |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
298 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3325 | 299 |
switch (row) { |
300 |
case 3: // flags |
|
301 |
cell.imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
|
302 |
FLAGS_DIRECTORY(),[teamDictionary objectForKey:@"flag"]]]; |
|
303 |
break; |
|
304 |
default: |
|
305 |
cell.imageView.image = nil; |
|
306 |
break; |
|
307 |
} |
|
3305 | 308 |
break; |
309 |
default: |
|
310 |
break; |
|
311 |
} |
|
312 |
||
313 |
return cell; |
|
314 |
} |
|
315 |
||
316 |
||
317 |
#pragma mark - |
|
318 |
#pragma mark Table view delegate |
|
3329 | 319 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
3325 | 320 |
NSInteger row = [indexPath row]; |
321 |
UITableViewController *nextController; |
|
3330 | 322 |
UITableViewCell *cell; |
323 |
switch ([indexPath section]) { |
|
324 |
case 2: |
|
325 |
//TODO: this part should be rewrittend with lazy loading instead of an array of controllers |
|
326 |
nextController = [secondaryControllers objectAtIndex:row%2 ]; //TODO: fix the objectAtIndex |
|
327 |
nextController.title = [secondaryItems objectAtIndex:row]; |
|
328 |
[nextController setTeamDictionary:teamDictionary]; |
|
329 |
[self.navigationController pushViewController:nextController animated:YES]; |
|
330 |
break; |
|
331 |
default: |
|
332 |
cell = [aTableView cellForRowAtIndexPath:indexPath]; |
|
333 |
for (UIView *oneView in cell.contentView.subviews) { |
|
334 |
if ([oneView isMemberOfClass:[UITextField class]]) { |
|
335 |
textFieldBeingEdited = (UITextField *)oneView; |
|
336 |
textFieldBeingEdited.tag = row; |
|
337 |
[textFieldBeingEdited becomeFirstResponder]; |
|
338 |
} |
|
3329 | 339 |
} |
3330 | 340 |
[aTableView deselectRowAtIndexPath:indexPath animated:NO]; |
341 |
break; |
|
3325 | 342 |
} |
3305 | 343 |
} |
344 |
||
3329 | 345 |
// action to perform when you want to change a hog hat |
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
346 |
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
347 |
if (nil == hogChildController) { |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
348 |
hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
349 |
} |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
350 |
|
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
351 |
// cache the dictionary file of the team, so that other controllers can modify it |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
352 |
hogChildController.teamDictionary = self.teamDictionary; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
353 |
hogChildController.selectedHog = [indexPath row]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
354 |
|
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
355 |
[self.navigationController pushViewController:hogChildController animated:YES]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
356 |
} |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
357 |
|
3305 | 358 |
|
359 |
#pragma mark - |
|
360 |
#pragma mark Memory management |
|
361 |
-(void) didReceiveMemoryWarning { |
|
362 |
// Releases the view if it doesn't have a superview. |
|
363 |
[super didReceiveMemoryWarning]; |
|
364 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
365 |
} |
|
366 |
||
367 |
-(void) viewDidUnload { |
|
3329 | 368 |
self.teamDictionary = nil; |
369 |
self.textFieldBeingEdited = nil; |
|
3330 | 370 |
self.teamName = nil; |
3329 | 371 |
self.hatArray = nil; |
3325 | 372 |
self.secondaryItems = nil; |
3329 | 373 |
self.secondaryControllers = nil; |
374 |
hogChildController = nil; |
|
3315 | 375 |
[super viewDidUnload]; |
3305 | 376 |
} |
377 |
||
378 |
-(void) dealloc { |
|
3329 | 379 |
[teamDictionary release]; |
380 |
[textFieldBeingEdited release]; |
|
3330 | 381 |
[teamName release]; |
3329 | 382 |
[hatArray release]; |
3305 | 383 |
[secondaryItems release]; |
3329 | 384 |
[secondaryControllers release]; |
385 |
[hogChildController release]; |
|
3305 | 386 |
[super dealloc]; |
387 |
} |
|
388 |
||
389 |
||
390 |
@end |
|
391 |