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