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