author | koda |
Mon, 02 Aug 2010 00:55:24 +0200 | |
changeset 3703 | 12d17c6e8855 |
parent 3697 | d5b30d6373fc |
child 3783 | 8e9daf967406 |
permissions | -rw-r--r-- |
3547 | 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" |
|
11 |
#import "GravesViewController.h" |
|
12 |
#import "VoicesViewController.h" |
|
13 |
#import "FortsViewController.h" |
|
14 |
#import "FlagsViewController.h" |
|
15 |
#import "LevelViewController.h" |
|
16 |
#import "CommodityFunctions.h" |
|
17 |
#import "UIImageExtra.h" |
|
18 |
||
3660 | 19 |
#define TEAMNAME_TAG 78789 |
3547 | 20 |
|
21 |
@implementation SingleTeamViewController |
|
3660 | 22 |
@synthesize teamDictionary, normalHogSprite, secondaryItems, teamName; |
3547 | 23 |
|
24 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
25 |
return rotationManager(interfaceOrientation); |
|
26 |
} |
|
27 |
||
28 |
#pragma mark - |
|
3660 | 29 |
#pragma mark editableCellViewDelegate methods |
3547 | 30 |
// set the new value |
3660 | 31 |
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { |
32 |
if (TEAMNAME_TAG == tagValue) { |
|
33 |
// delete old file |
|
34 |
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.teamName] error:NULL]; |
|
35 |
// update filename |
|
36 |
self.teamName = textString; |
|
37 |
// save new file |
|
38 |
[self writeFile]; |
|
39 |
} else { |
|
40 |
// replace the old value with the new one |
|
41 |
NSMutableDictionary *hog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:tagValue]; |
|
42 |
[hog setObject:textString forKey:@"hogname"]; |
|
3547 | 43 |
isWriteNeeded = YES; |
44 |
} |
|
45 |
} |
|
46 |
||
47 |
#pragma mark - |
|
48 |
#pragma mark View lifecycle |
|
49 |
-(void) viewDidLoad { |
|
50 |
[super viewDidLoad]; |
|
3697 | 51 |
|
3547 | 52 |
// labels for the entries |
53 |
NSArray *array = [[NSArray alloc] initWithObjects: |
|
54 |
NSLocalizedString(@"Grave",@""), |
|
55 |
NSLocalizedString(@"Voice",@""), |
|
56 |
NSLocalizedString(@"Fort",@""), |
|
57 |
NSLocalizedString(@"Flag",@""), |
|
58 |
NSLocalizedString(@"Level",@""),nil]; |
|
59 |
self.secondaryItems = array; |
|
60 |
[array release]; |
|
61 |
||
62 |
// load the base hog image, drawing will occure in cellForRow... |
|
63 |
NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
|
64 |
UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)]; |
|
65 |
[normalHogFile release]; |
|
66 |
self.normalHogSprite = hogSprite; |
|
67 |
[hogSprite release]; |
|
3697 | 68 |
|
3547 | 69 |
// listen if any childController modifies the plist and write it if needed |
70 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil]; |
|
71 |
isWriteNeeded = NO; |
|
3697 | 72 |
|
3660 | 73 |
self.title = NSLocalizedString(@"Edit team settings",@""); |
3547 | 74 |
} |
75 |
||
76 |
-(void) viewWillAppear:(BOOL)animated { |
|
77 |
[super viewWillAppear:animated]; |
|
3697 | 78 |
|
3660 | 79 |
// load data about the team and write if there has been a change from other childControllers |
3697 | 80 |
if (isWriteNeeded) |
3547 | 81 |
[self writeFile]; |
3697 | 82 |
|
3660 | 83 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.teamName]; |
3547 | 84 |
NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile]; |
85 |
self.teamDictionary = teamDict; |
|
86 |
[teamDict release]; |
|
87 |
[teamFile release]; |
|
3697 | 88 |
|
3547 | 89 |
[self.tableView reloadData]; |
90 |
} |
|
91 |
||
92 |
// write on file if there has been a change |
|
93 |
-(void) viewWillDisappear:(BOOL)animated { |
|
94 |
[super viewWillDisappear:animated]; |
|
95 |
||
3697 | 96 |
if (isWriteNeeded) |
3660 | 97 |
[self writeFile]; |
3547 | 98 |
} |
99 |
||
100 |
#pragma mark - |
|
101 |
// needed by other classes to warn about a user change |
|
102 |
-(void) setWriteNeeded { |
|
103 |
isWriteNeeded = YES; |
|
104 |
} |
|
105 |
||
106 |
-(void) writeFile { |
|
3660 | 107 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.teamName]; |
108 |
[self.teamDictionary writeToFile:teamFile atomically:YES]; |
|
109 |
[teamFile release]; |
|
3547 | 110 |
|
3660 | 111 |
DLog(@"%@",teamDictionary); |
3547 | 112 |
isWriteNeeded = NO; |
113 |
} |
|
114 |
||
115 |
#pragma mark - |
|
116 |
#pragma mark Table view data source |
|
117 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
118 |
return 3; |
|
119 |
} |
|
120 |
||
121 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
122 |
NSInteger rows = 0; |
|
123 |
switch (section) { |
|
124 |
case 0: // team name |
|
125 |
rows = 1; |
|
126 |
break; |
|
127 |
case 1: // team members |
|
128 |
rows = MAX_HOGS; |
|
129 |
break; |
|
130 |
case 2: // team details |
|
131 |
rows = [self.secondaryItems count]; |
|
132 |
break; |
|
133 |
default: |
|
134 |
break; |
|
135 |
} |
|
136 |
return rows; |
|
137 |
} |
|
138 |
||
3660 | 139 |
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
140 |
NSString *sectionTitle = nil; |
|
141 |
switch (section) { |
|
142 |
case 0: |
|
143 |
sectionTitle = NSLocalizedString(@"Team Name", @""); |
|
144 |
break; |
|
145 |
case 1: |
|
146 |
sectionTitle = NSLocalizedString(@"Names and Hats", @""); |
|
147 |
break; |
|
148 |
case 2: |
|
149 |
sectionTitle = NSLocalizedString(@"Team Preferences", @""); |
|
150 |
break; |
|
151 |
default: |
|
152 |
DLog(@"Nope"); |
|
153 |
break; |
|
154 |
} |
|
155 |
return sectionTitle; |
|
156 |
} |
|
157 |
||
3547 | 158 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
159 |
static NSString *CellIdentifier0 = @"Cell0"; |
|
160 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
161 |
static NSString *CellIdentifier2 = @"Cell2"; |
|
3697 | 162 |
|
3547 | 163 |
NSArray *hogArray; |
164 |
UITableViewCell *cell = nil; |
|
3660 | 165 |
EditableCellView *editableCell = nil; |
3547 | 166 |
NSInteger row = [indexPath row]; |
167 |
UIImage *accessoryImage; |
|
3697 | 168 |
|
3547 | 169 |
switch ([indexPath section]) { |
170 |
case 0: |
|
3660 | 171 |
editableCell = (EditableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
172 |
if (editableCell == nil) { |
|
3697 | 173 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
3547 | 174 |
reuseIdentifier:CellIdentifier0] autorelease]; |
3660 | 175 |
editableCell.delegate = self; |
176 |
editableCell.tag = TEAMNAME_TAG; |
|
3547 | 177 |
} |
3697 | 178 |
|
3660 | 179 |
editableCell.imageView.image = nil; |
180 |
editableCell.accessoryType = UITableViewCellAccessoryNone; |
|
181 |
editableCell.textField.text = self.teamName; |
|
182 |
||
183 |
cell = editableCell; |
|
3547 | 184 |
break; |
185 |
case 1: |
|
3660 | 186 |
editableCell = (EditableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
187 |
if (editableCell == nil) { |
|
3697 | 188 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
3547 | 189 |
reuseIdentifier:CellIdentifier1] autorelease]; |
3660 | 190 |
editableCell.delegate = self; |
191 |
editableCell.tag = [indexPath row]; |
|
3547 | 192 |
} |
3697 | 193 |
|
3547 | 194 |
hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
3697 | 195 |
|
3660 | 196 |
// draw the hat on top of the hog |
3547 | 197 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png", HATS_DIRECTORY(), [[hogArray objectAtIndex:row] objectForKey:@"hat"]]; |
198 |
UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
|
199 |
[hatFile release]; |
|
3660 | 200 |
editableCell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)]; |
3547 | 201 |
[hatSprite release]; |
3697 | 202 |
|
3660 | 203 |
editableCell.textField.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"]; |
204 |
editableCell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; |
|
3697 | 205 |
|
3660 | 206 |
cell = editableCell; |
3547 | 207 |
break; |
208 |
case 2: |
|
209 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; |
|
210 |
if (cell == nil) { |
|
3697 | 211 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
3547 | 212 |
reuseIdentifier:CellIdentifier2] autorelease]; |
213 |
} |
|
3697 | 214 |
|
3547 | 215 |
cell.textLabel.text = [self.secondaryItems objectAtIndex:row]; |
216 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
|
217 |
switch (row) { |
|
218 |
case 0: // grave |
|
219 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
|
220 |
GRAVES_DIRECTORY(),[teamDictionary objectForKey:@"grave"]] |
|
221 |
andCutAt:CGRectMake(0,0,32,32)]; |
|
222 |
cell.imageView.image = accessoryImage; |
|
223 |
[accessoryImage release]; |
|
224 |
break; |
|
225 |
case 2: // fort |
|
226 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@L.png", |
|
227 |
FORTS_DIRECTORY(),[teamDictionary objectForKey:@"fort"]]]; |
|
228 |
cell.imageView.image = [accessoryImage scaleToSize:CGSizeMake(42, 42)]; |
|
229 |
[accessoryImage release]; |
|
230 |
break; |
|
3697 | 231 |
|
3547 | 232 |
case 3: // flags |
233 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
|
234 |
FLAGS_DIRECTORY(),[teamDictionary objectForKey:@"flag"]]]; |
|
235 |
cell.imageView.image = accessoryImage; |
|
236 |
[accessoryImage release]; |
|
237 |
break; |
|
238 |
case 4: // level |
|
239 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%d.png", |
|
240 |
BOTLEVELS_DIRECTORY(),[[[[teamDictionary objectForKey:@"hedgehogs"] |
|
241 |
objectAtIndex:0] objectForKey:@"level"] |
|
242 |
intValue]]]; |
|
243 |
cell.imageView.image = accessoryImage; |
|
244 |
[accessoryImage release]; |
|
245 |
break; |
|
246 |
default: |
|
247 |
cell.imageView.image = nil; |
|
248 |
break; |
|
249 |
} |
|
250 |
break; |
|
251 |
} |
|
3697 | 252 |
|
3547 | 253 |
return cell; |
254 |
} |
|
255 |
||
256 |
||
257 |
#pragma mark - |
|
258 |
#pragma mark Table view delegate |
|
259 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
260 |
NSInteger row = [indexPath row]; |
|
261 |
NSInteger section = [indexPath section]; |
|
262 |
UITableViewController *nextController = nil; |
|
3697 | 263 |
|
3547 | 264 |
if (2 == section) { |
265 |
switch (row) { |
|
266 |
case 0: // grave |
|
267 |
if (nil == gravesViewController) |
|
268 |
gravesViewController = [[GravesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3697 | 269 |
|
3547 | 270 |
nextController = gravesViewController; |
271 |
break; |
|
272 |
case 1: // voice |
|
273 |
if (nil == voicesViewController) |
|
274 |
voicesViewController = [[VoicesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3697 | 275 |
|
276 |
nextController = voicesViewController; |
|
3547 | 277 |
break; |
278 |
case 2: // fort |
|
279 |
if (nil == fortsViewController) |
|
280 |
fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3697 | 281 |
|
3547 | 282 |
nextController = fortsViewController; |
283 |
break; |
|
284 |
case 3: // flag |
|
3697 | 285 |
if (nil == flagsViewController) |
3547 | 286 |
flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
3697 | 287 |
|
3547 | 288 |
nextController = flagsViewController; |
289 |
break; |
|
290 |
case 4: // level |
|
291 |
if (nil == levelViewController) |
|
292 |
levelViewController = [[LevelViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3697 | 293 |
|
3547 | 294 |
nextController = levelViewController; |
295 |
break; |
|
296 |
} |
|
3697 | 297 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3660
diff
changeset
|
298 |
if ([nextController respondsToSelector:@selector(setTeamDictionary:)]) |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3660
diff
changeset
|
299 |
[nextController setTeamDictionary:teamDictionary]; |
3547 | 300 |
[self.navigationController pushViewController:nextController animated:YES]; |
301 |
} else { |
|
3660 | 302 |
EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
303 |
[cell replyKeyboard]; |
|
3547 | 304 |
[aTableView deselectRowAtIndexPath:indexPath animated:NO]; |
305 |
} |
|
306 |
||
307 |
} |
|
308 |
||
309 |
// action to perform when you want to change a hog hat |
|
3660 | 310 |
-(void) tableView:(UITableView *)aTableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { |
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3660
diff
changeset
|
311 |
if (nil == hogHatViewController) |
3547 | 312 |
hogHatViewController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
3697 | 313 |
|
3547 | 314 |
// cache the dictionary file of the team, so that other controllers can modify it |
315 |
hogHatViewController.teamDictionary = self.teamDictionary; |
|
316 |
hogHatViewController.selectedHog = [indexPath row]; |
|
3697 | 317 |
|
3660 | 318 |
// if we are editing the field undo any change before proceeding |
319 |
EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
|
320 |
[cell cancel:nil]; |
|
3697 | 321 |
|
3547 | 322 |
[self.navigationController pushViewController:hogHatViewController animated:YES]; |
323 |
} |
|
324 |
||
325 |
||
326 |
#pragma mark - |
|
327 |
#pragma mark Memory management |
|
328 |
-(void) didReceiveMemoryWarning { |
|
329 |
[super didReceiveMemoryWarning]; |
|
330 |
if (hogHatViewController.view.superview == nil) |
|
331 |
hogHatViewController = nil; |
|
332 |
if (gravesViewController.view.superview == nil) |
|
333 |
gravesViewController = nil; |
|
334 |
if (voicesViewController.view.superview == nil) |
|
335 |
voicesViewController = nil; |
|
336 |
if (fortsViewController.view.superview == nil) |
|
337 |
fortsViewController = nil; |
|
338 |
if (flagsViewController.view.superview == nil) |
|
339 |
flagsViewController = nil; |
|
340 |
if (levelViewController.view.superview == nil) |
|
341 |
levelViewController = nil; |
|
3660 | 342 |
MSG_MEMCLEAN(); |
3547 | 343 |
} |
344 |
||
345 |
-(void) viewDidUnload { |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3660
diff
changeset
|
346 |
[super viewDidUnload]; |
3547 | 347 |
self.teamDictionary = nil; |
348 |
self.teamName = nil; |
|
349 |
self.normalHogSprite = nil; |
|
350 |
self.secondaryItems = nil; |
|
351 |
hogHatViewController = nil; |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3660
diff
changeset
|
352 |
gravesViewController = nil; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3660
diff
changeset
|
353 |
voicesViewController = nil; |
3547 | 354 |
flagsViewController = nil; |
355 |
fortsViewController = nil; |
|
356 |
levelViewController = nil; |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3660
diff
changeset
|
357 |
MSG_DIDUNLOAD(); |
3547 | 358 |
[super viewDidUnload]; |
359 |
} |
|
360 |
||
361 |
-(void) dealloc { |
|
362 |
[teamDictionary release]; |
|
363 |
[teamName release]; |
|
364 |
[normalHogSprite release]; |
|
365 |
[secondaryItems release]; |
|
366 |
[hogHatViewController release]; |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3660
diff
changeset
|
367 |
[gravesViewController release]; |
3547 | 368 |
[fortsViewController release]; |
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3660
diff
changeset
|
369 |
[voicesViewController release]; |
3547 | 370 |
[flagsViewController release]; |
371 |
[levelViewController release]; |
|
372 |
[super dealloc]; |
|
373 |
} |
|
374 |
||
375 |
||
376 |
@end |
|
377 |