author | koda |
Fri, 03 Sep 2010 02:03:44 +0200 | |
changeset 3823 | cca9bfb88a24 |
parent 3816 | 7b74aa003f52 |
child 3825 | fd6c20cd90e3 |
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 |
|
3816 | 22 |
@synthesize teamDictionary, normalHogSprite, secondaryItems, moreSecondaryItems, 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]; |
|
3816 | 61 |
|
62 |
// labels for the subtitles |
|
63 |
NSArray *moreArray = [[NSArray alloc] initWithObjects: |
|
64 |
NSLocalizedString(@"Mark the death of your fallen warriors",@""), |
|
65 |
NSLocalizedString(@"Pick a slang your hogs will speak",@""), |
|
66 |
NSLocalizedString(@"Select the team invincible fortress (only valid for fort games)",@""), |
|
67 |
NSLocalizedString(@"Choose a charismatic symbol for your team",@""), |
|
68 |
NSLocalizedString(@"Opt for controlling the team or let the AI lead",@""),nil]; |
|
69 |
self.moreSecondaryItems = moreArray; |
|
70 |
[moreArray release]; |
|
3547 | 71 |
|
72 |
// load the base hog image, drawing will occure in cellForRow... |
|
73 |
NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
|
74 |
UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)]; |
|
75 |
[normalHogFile release]; |
|
76 |
self.normalHogSprite = hogSprite; |
|
77 |
[hogSprite release]; |
|
3697 | 78 |
|
3547 | 79 |
// listen if any childController modifies the plist and write it if needed |
80 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil]; |
|
81 |
isWriteNeeded = NO; |
|
3697 | 82 |
|
3660 | 83 |
self.title = NSLocalizedString(@"Edit team settings",@""); |
3547 | 84 |
} |
85 |
||
86 |
-(void) viewWillAppear:(BOOL)animated { |
|
87 |
[super viewWillAppear:animated]; |
|
3697 | 88 |
|
3660 | 89 |
// load data about the team and write if there has been a change from other childControllers |
3697 | 90 |
if (isWriteNeeded) |
3547 | 91 |
[self writeFile]; |
3697 | 92 |
|
3660 | 93 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.teamName]; |
3547 | 94 |
NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile]; |
95 |
self.teamDictionary = teamDict; |
|
96 |
[teamDict release]; |
|
97 |
[teamFile release]; |
|
3697 | 98 |
|
3547 | 99 |
[self.tableView reloadData]; |
100 |
} |
|
101 |
||
102 |
// write on file if there has been a change |
|
103 |
-(void) viewWillDisappear:(BOOL)animated { |
|
104 |
[super viewWillDisappear:animated]; |
|
105 |
||
3697 | 106 |
if (isWriteNeeded) |
3660 | 107 |
[self writeFile]; |
3547 | 108 |
} |
109 |
||
110 |
#pragma mark - |
|
111 |
// needed by other classes to warn about a user change |
|
112 |
-(void) setWriteNeeded { |
|
113 |
isWriteNeeded = YES; |
|
114 |
} |
|
115 |
||
116 |
-(void) writeFile { |
|
3660 | 117 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.teamName]; |
118 |
[self.teamDictionary writeToFile:teamFile atomically:YES]; |
|
119 |
[teamFile release]; |
|
3547 | 120 |
|
3789
c3eb56754e92
added a smaller version of forts, fixed a couple of regressions
koda
parents:
3783
diff
changeset
|
121 |
//DLog(@"%@",teamDictionary); |
3547 | 122 |
isWriteNeeded = NO; |
123 |
} |
|
124 |
||
125 |
#pragma mark - |
|
126 |
#pragma mark Table view data source |
|
127 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
128 |
return 3; |
|
129 |
} |
|
130 |
||
131 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
132 |
NSInteger rows = 0; |
|
133 |
switch (section) { |
|
134 |
case 0: // team name |
|
135 |
rows = 1; |
|
136 |
break; |
|
137 |
case 1: // team members |
|
138 |
rows = MAX_HOGS; |
|
139 |
break; |
|
140 |
case 2: // team details |
|
141 |
rows = [self.secondaryItems count]; |
|
142 |
break; |
|
143 |
default: |
|
144 |
break; |
|
145 |
} |
|
146 |
return rows; |
|
147 |
} |
|
148 |
||
3660 | 149 |
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
150 |
NSString *sectionTitle = nil; |
|
151 |
switch (section) { |
|
152 |
case 0: |
|
153 |
sectionTitle = NSLocalizedString(@"Team Name", @""); |
|
154 |
break; |
|
155 |
case 1: |
|
156 |
sectionTitle = NSLocalizedString(@"Names and Hats", @""); |
|
157 |
break; |
|
158 |
case 2: |
|
159 |
sectionTitle = NSLocalizedString(@"Team Preferences", @""); |
|
160 |
break; |
|
161 |
default: |
|
162 |
DLog(@"Nope"); |
|
163 |
break; |
|
164 |
} |
|
165 |
return sectionTitle; |
|
166 |
} |
|
167 |
||
3547 | 168 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
169 |
static NSString *CellIdentifier0 = @"Cell0"; |
|
170 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
171 |
static NSString *CellIdentifier2 = @"Cell2"; |
|
3697 | 172 |
|
3547 | 173 |
NSArray *hogArray; |
174 |
UITableViewCell *cell = nil; |
|
3660 | 175 |
EditableCellView *editableCell = nil; |
3547 | 176 |
NSInteger row = [indexPath row]; |
177 |
UIImage *accessoryImage; |
|
3697 | 178 |
|
3547 | 179 |
switch ([indexPath section]) { |
180 |
case 0: |
|
3660 | 181 |
editableCell = (EditableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
182 |
if (editableCell == nil) { |
|
3697 | 183 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
3547 | 184 |
reuseIdentifier:CellIdentifier0] autorelease]; |
3660 | 185 |
editableCell.delegate = self; |
186 |
editableCell.tag = TEAMNAME_TAG; |
|
3547 | 187 |
} |
3697 | 188 |
|
3660 | 189 |
editableCell.imageView.image = nil; |
190 |
editableCell.accessoryType = UITableViewCellAccessoryNone; |
|
191 |
editableCell.textField.text = self.teamName; |
|
192 |
||
193 |
cell = editableCell; |
|
3547 | 194 |
break; |
195 |
case 1: |
|
3660 | 196 |
editableCell = (EditableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
197 |
if (editableCell == nil) { |
|
3697 | 198 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
3547 | 199 |
reuseIdentifier:CellIdentifier1] autorelease]; |
3660 | 200 |
editableCell.delegate = self; |
201 |
editableCell.tag = [indexPath row]; |
|
3547 | 202 |
} |
3697 | 203 |
|
3547 | 204 |
hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
3697 | 205 |
|
3660 | 206 |
// draw the hat on top of the hog |
3547 | 207 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png", HATS_DIRECTORY(), [[hogArray objectAtIndex:row] objectForKey:@"hat"]]; |
208 |
UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
|
209 |
[hatFile release]; |
|
3660 | 210 |
editableCell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)]; |
3547 | 211 |
[hatSprite release]; |
3697 | 212 |
|
3660 | 213 |
editableCell.textField.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"]; |
214 |
editableCell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; |
|
3697 | 215 |
|
3660 | 216 |
cell = editableCell; |
3547 | 217 |
break; |
218 |
case 2: |
|
219 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; |
|
220 |
if (cell == nil) { |
|
3816 | 221 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle |
3547 | 222 |
reuseIdentifier:CellIdentifier2] autorelease]; |
223 |
} |
|
3697 | 224 |
|
3547 | 225 |
cell.textLabel.text = [self.secondaryItems objectAtIndex:row]; |
3816 | 226 |
cell.detailTextLabel.text = [self.moreSecondaryItems objectAtIndex:row]; |
3547 | 227 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
228 |
switch (row) { |
|
229 |
case 0: // grave |
|
230 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
|
231 |
GRAVES_DIRECTORY(),[teamDictionary objectForKey:@"grave"]] |
|
232 |
andCutAt:CGRectMake(0,0,32,32)]; |
|
233 |
cell.imageView.image = accessoryImage; |
|
234 |
[accessoryImage release]; |
|
235 |
break; |
|
3816 | 236 |
case 1: // voice |
237 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/HellishBomb.png", |
|
238 |
GRAPHICS_DIRECTORY()]]; |
|
239 |
cell.imageView.image = accessoryImage; |
|
240 |
[accessoryImage release]; |
|
241 |
break; |
|
3547 | 242 |
case 2: // fort |
3789
c3eb56754e92
added a smaller version of forts, fixed a couple of regressions
koda
parents:
3783
diff
changeset
|
243 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@-preview.png", |
3547 | 244 |
FORTS_DIRECTORY(),[teamDictionary objectForKey:@"fort"]]]; |
3816 | 245 |
cell.imageView.image = [accessoryImage scaleToSize:CGSizeMake(32, 32)]; |
3547 | 246 |
[accessoryImage release]; |
247 |
break; |
|
248 |
case 3: // flags |
|
249 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
|
250 |
FLAGS_DIRECTORY(),[teamDictionary objectForKey:@"flag"]]]; |
|
3816 | 251 |
cell.imageView.image = [accessoryImage scaleToSize:CGSizeMake(32, 32)]; |
3547 | 252 |
[accessoryImage release]; |
253 |
break; |
|
254 |
case 4: // level |
|
255 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%d.png", |
|
256 |
BOTLEVELS_DIRECTORY(),[[[[teamDictionary objectForKey:@"hedgehogs"] |
|
257 |
objectAtIndex:0] objectForKey:@"level"] |
|
258 |
intValue]]]; |
|
3816 | 259 |
cell.imageView.image = [accessoryImage scaleToSize:CGSizeMake(32, 32)]; |
3547 | 260 |
[accessoryImage release]; |
261 |
break; |
|
262 |
default: |
|
263 |
cell.imageView.image = nil; |
|
264 |
break; |
|
265 |
} |
|
266 |
break; |
|
267 |
} |
|
3697 | 268 |
|
3547 | 269 |
return cell; |
270 |
} |
|
271 |
||
272 |
||
273 |
#pragma mark - |
|
274 |
#pragma mark Table view delegate |
|
275 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
276 |
NSInteger row = [indexPath row]; |
|
277 |
NSInteger section = [indexPath section]; |
|
278 |
UITableViewController *nextController = nil; |
|
3697 | 279 |
|
3547 | 280 |
if (2 == section) { |
281 |
switch (row) { |
|
282 |
case 0: // grave |
|
283 |
if (nil == gravesViewController) |
|
284 |
gravesViewController = [[GravesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3697 | 285 |
|
3547 | 286 |
nextController = gravesViewController; |
287 |
break; |
|
288 |
case 1: // voice |
|
289 |
if (nil == voicesViewController) |
|
290 |
voicesViewController = [[VoicesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3697 | 291 |
|
292 |
nextController = voicesViewController; |
|
3547 | 293 |
break; |
294 |
case 2: // fort |
|
295 |
if (nil == fortsViewController) |
|
296 |
fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3697 | 297 |
|
3547 | 298 |
nextController = fortsViewController; |
299 |
break; |
|
300 |
case 3: // flag |
|
3697 | 301 |
if (nil == flagsViewController) |
3547 | 302 |
flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
3697 | 303 |
|
3547 | 304 |
nextController = flagsViewController; |
305 |
break; |
|
306 |
case 4: // level |
|
307 |
if (nil == levelViewController) |
|
308 |
levelViewController = [[LevelViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3697 | 309 |
|
3547 | 310 |
nextController = levelViewController; |
311 |
break; |
|
312 |
} |
|
3697 | 313 |
|
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
|
314 |
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
|
315 |
[nextController setTeamDictionary:teamDictionary]; |
3547 | 316 |
[self.navigationController pushViewController:nextController animated:YES]; |
317 |
} else { |
|
3660 | 318 |
EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
319 |
[cell replyKeyboard]; |
|
3547 | 320 |
[aTableView deselectRowAtIndexPath:indexPath animated:NO]; |
321 |
} |
|
322 |
||
323 |
} |
|
324 |
||
325 |
// action to perform when you want to change a hog hat |
|
3660 | 326 |
-(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
|
327 |
if (nil == hogHatViewController) |
3547 | 328 |
hogHatViewController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
3697 | 329 |
|
3547 | 330 |
// cache the dictionary file of the team, so that other controllers can modify it |
331 |
hogHatViewController.teamDictionary = self.teamDictionary; |
|
332 |
hogHatViewController.selectedHog = [indexPath row]; |
|
3697 | 333 |
|
3660 | 334 |
// if we are editing the field undo any change before proceeding |
335 |
EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
|
336 |
[cell cancel:nil]; |
|
3697 | 337 |
|
3547 | 338 |
[self.navigationController pushViewController:hogHatViewController animated:YES]; |
339 |
} |
|
340 |
||
341 |
||
342 |
#pragma mark - |
|
343 |
#pragma mark Memory management |
|
344 |
-(void) didReceiveMemoryWarning { |
|
345 |
[super didReceiveMemoryWarning]; |
|
346 |
if (hogHatViewController.view.superview == nil) |
|
347 |
hogHatViewController = nil; |
|
348 |
if (gravesViewController.view.superview == nil) |
|
349 |
gravesViewController = nil; |
|
350 |
if (voicesViewController.view.superview == nil) |
|
351 |
voicesViewController = nil; |
|
352 |
if (fortsViewController.view.superview == nil) |
|
353 |
fortsViewController = nil; |
|
354 |
if (flagsViewController.view.superview == nil) |
|
355 |
flagsViewController = nil; |
|
356 |
if (levelViewController.view.superview == nil) |
|
357 |
levelViewController = nil; |
|
3660 | 358 |
MSG_MEMCLEAN(); |
3547 | 359 |
} |
360 |
||
361 |
-(void) viewDidUnload { |
|
3783 | 362 |
[[NSNotificationCenter defaultCenter] removeObserver:self]; |
3547 | 363 |
self.teamDictionary = nil; |
364 |
self.teamName = nil; |
|
365 |
self.normalHogSprite = nil; |
|
366 |
self.secondaryItems = nil; |
|
3816 | 367 |
self.moreSecondaryItems = nil; |
3547 | 368 |
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
|
369 |
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
|
370 |
voicesViewController = nil; |
3547 | 371 |
flagsViewController = nil; |
372 |
fortsViewController = nil; |
|
373 |
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
|
374 |
MSG_DIDUNLOAD(); |
3547 | 375 |
[super viewDidUnload]; |
376 |
} |
|
377 |
||
378 |
-(void) dealloc { |
|
379 |
[teamDictionary release]; |
|
380 |
[teamName release]; |
|
381 |
[normalHogSprite release]; |
|
382 |
[secondaryItems release]; |
|
3816 | 383 |
[moreSecondaryItems release]; |
3547 | 384 |
[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
|
385 |
[gravesViewController release]; |
3547 | 386 |
[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
|
387 |
[voicesViewController release]; |
3547 | 388 |
[flagsViewController release]; |
389 |
[levelViewController release]; |
|
390 |
[super dealloc]; |
|
391 |
} |
|
392 |
||
393 |
||
394 |
@end |
|
395 |