author | koda |
Mon, 04 Oct 2010 00:00:42 +0200 | |
changeset 3924 | 2a9ace189288 |
parent 3829 | 81db3c85784b |
child 3926 | 668b71f31e51 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 |
* Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
* |
|
18 |
* File created on 02/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "SingleTeamViewController.h" |
|
23 |
#import "HogHatViewController.h" |
|
24 |
#import "GravesViewController.h" |
|
25 |
#import "VoicesViewController.h" |
|
26 |
#import "FortsViewController.h" |
|
27 |
#import "FlagsViewController.h" |
|
28 |
#import "LevelViewController.h" |
|
29 |
#import "CommodityFunctions.h" |
|
30 |
#import "UIImageExtra.h" |
|
31 |
||
3660 | 32 |
#define TEAMNAME_TAG 78789 |
3547 | 33 |
|
34 |
@implementation SingleTeamViewController |
|
3816 | 35 |
@synthesize teamDictionary, normalHogSprite, secondaryItems, moreSecondaryItems, teamName; |
3547 | 36 |
|
37 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
38 |
return rotationManager(interfaceOrientation); |
|
39 |
} |
|
40 |
||
41 |
#pragma mark - |
|
3660 | 42 |
#pragma mark editableCellViewDelegate methods |
3547 | 43 |
// set the new value |
3660 | 44 |
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { |
45 |
if (TEAMNAME_TAG == tagValue) { |
|
46 |
// delete old file |
|
47 |
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.teamName] error:NULL]; |
|
48 |
// update filename |
|
49 |
self.teamName = textString; |
|
50 |
// save new file |
|
51 |
[self writeFile]; |
|
52 |
} else { |
|
53 |
// replace the old value with the new one |
|
54 |
NSMutableDictionary *hog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:tagValue]; |
|
55 |
[hog setObject:textString forKey:@"hogname"]; |
|
3547 | 56 |
isWriteNeeded = YES; |
57 |
} |
|
58 |
} |
|
59 |
||
60 |
#pragma mark - |
|
61 |
#pragma mark View lifecycle |
|
62 |
-(void) viewDidLoad { |
|
63 |
[super viewDidLoad]; |
|
3697 | 64 |
|
3547 | 65 |
// labels for the entries |
66 |
NSArray *array = [[NSArray alloc] initWithObjects: |
|
67 |
NSLocalizedString(@"Grave",@""), |
|
68 |
NSLocalizedString(@"Voice",@""), |
|
69 |
NSLocalizedString(@"Fort",@""), |
|
70 |
NSLocalizedString(@"Flag",@""), |
|
71 |
NSLocalizedString(@"Level",@""),nil]; |
|
72 |
self.secondaryItems = array; |
|
73 |
[array release]; |
|
3816 | 74 |
|
75 |
// labels for the subtitles |
|
76 |
NSArray *moreArray = [[NSArray alloc] initWithObjects: |
|
77 |
NSLocalizedString(@"Mark the death of your fallen warriors",@""), |
|
78 |
NSLocalizedString(@"Pick a slang your hogs will speak",@""), |
|
79 |
NSLocalizedString(@"Select the team invincible fortress (only valid for fort games)",@""), |
|
80 |
NSLocalizedString(@"Choose a charismatic symbol for your team",@""), |
|
81 |
NSLocalizedString(@"Opt for controlling the team or let the AI lead",@""),nil]; |
|
82 |
self.moreSecondaryItems = moreArray; |
|
83 |
[moreArray release]; |
|
3547 | 84 |
|
85 |
// load the base hog image, drawing will occure in cellForRow... |
|
86 |
NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
|
87 |
UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)]; |
|
88 |
[normalHogFile release]; |
|
89 |
self.normalHogSprite = hogSprite; |
|
90 |
[hogSprite release]; |
|
3697 | 91 |
|
3547 | 92 |
// listen if any childController modifies the plist and write it if needed |
93 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil]; |
|
94 |
isWriteNeeded = NO; |
|
3697 | 95 |
|
3660 | 96 |
self.title = NSLocalizedString(@"Edit team settings",@""); |
3547 | 97 |
} |
98 |
||
99 |
-(void) viewWillAppear:(BOOL)animated { |
|
100 |
[super viewWillAppear:animated]; |
|
3697 | 101 |
|
3660 | 102 |
// load data about the team and write if there has been a change from other childControllers |
3697 | 103 |
if (isWriteNeeded) |
3547 | 104 |
[self writeFile]; |
3697 | 105 |
|
3660 | 106 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.teamName]; |
3547 | 107 |
NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile]; |
108 |
self.teamDictionary = teamDict; |
|
109 |
[teamDict release]; |
|
110 |
[teamFile release]; |
|
3697 | 111 |
|
3547 | 112 |
[self.tableView reloadData]; |
113 |
} |
|
114 |
||
115 |
// write on file if there has been a change |
|
116 |
-(void) viewWillDisappear:(BOOL)animated { |
|
117 |
[super viewWillDisappear:animated]; |
|
118 |
||
3697 | 119 |
if (isWriteNeeded) |
3660 | 120 |
[self writeFile]; |
3547 | 121 |
} |
122 |
||
123 |
#pragma mark - |
|
124 |
// needed by other classes to warn about a user change |
|
125 |
-(void) setWriteNeeded { |
|
126 |
isWriteNeeded = YES; |
|
127 |
} |
|
128 |
||
129 |
-(void) writeFile { |
|
3660 | 130 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.teamName]; |
131 |
[self.teamDictionary writeToFile:teamFile atomically:YES]; |
|
132 |
[teamFile release]; |
|
3547 | 133 |
|
3789
c3eb56754e92
added a smaller version of forts, fixed a couple of regressions
koda
parents:
3783
diff
changeset
|
134 |
//DLog(@"%@",teamDictionary); |
3547 | 135 |
isWriteNeeded = NO; |
136 |
} |
|
137 |
||
138 |
#pragma mark - |
|
139 |
#pragma mark Table view data source |
|
140 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
141 |
return 3; |
|
142 |
} |
|
143 |
||
144 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
145 |
NSInteger rows = 0; |
|
146 |
switch (section) { |
|
147 |
case 0: // team name |
|
148 |
rows = 1; |
|
149 |
break; |
|
150 |
case 1: // team members |
|
151 |
rows = MAX_HOGS; |
|
152 |
break; |
|
153 |
case 2: // team details |
|
154 |
rows = [self.secondaryItems count]; |
|
155 |
break; |
|
156 |
default: |
|
157 |
break; |
|
158 |
} |
|
159 |
return rows; |
|
160 |
} |
|
161 |
||
3660 | 162 |
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
163 |
NSString *sectionTitle = nil; |
|
164 |
switch (section) { |
|
165 |
case 0: |
|
166 |
sectionTitle = NSLocalizedString(@"Team Name", @""); |
|
167 |
break; |
|
168 |
case 1: |
|
169 |
sectionTitle = NSLocalizedString(@"Names and Hats", @""); |
|
170 |
break; |
|
171 |
case 2: |
|
172 |
sectionTitle = NSLocalizedString(@"Team Preferences", @""); |
|
173 |
break; |
|
174 |
default: |
|
175 |
DLog(@"Nope"); |
|
176 |
break; |
|
177 |
} |
|
178 |
return sectionTitle; |
|
179 |
} |
|
180 |
||
3547 | 181 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
182 |
static NSString *CellIdentifier0 = @"Cell0"; |
|
183 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
184 |
static NSString *CellIdentifier2 = @"Cell2"; |
|
3697 | 185 |
|
3547 | 186 |
NSArray *hogArray; |
187 |
UITableViewCell *cell = nil; |
|
3660 | 188 |
EditableCellView *editableCell = nil; |
3547 | 189 |
NSInteger row = [indexPath row]; |
190 |
UIImage *accessoryImage; |
|
3697 | 191 |
|
3547 | 192 |
switch ([indexPath section]) { |
193 |
case 0: |
|
3660 | 194 |
editableCell = (EditableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
195 |
if (editableCell == nil) { |
|
3697 | 196 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
3547 | 197 |
reuseIdentifier:CellIdentifier0] autorelease]; |
3660 | 198 |
editableCell.delegate = self; |
199 |
editableCell.tag = TEAMNAME_TAG; |
|
3547 | 200 |
} |
3697 | 201 |
|
3660 | 202 |
editableCell.imageView.image = nil; |
203 |
editableCell.accessoryType = UITableViewCellAccessoryNone; |
|
204 |
editableCell.textField.text = self.teamName; |
|
205 |
||
206 |
cell = editableCell; |
|
3547 | 207 |
break; |
208 |
case 1: |
|
3660 | 209 |
editableCell = (EditableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
210 |
if (editableCell == nil) { |
|
3697 | 211 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
3547 | 212 |
reuseIdentifier:CellIdentifier1] autorelease]; |
3660 | 213 |
editableCell.delegate = self; |
214 |
editableCell.tag = [indexPath row]; |
|
3547 | 215 |
} |
3697 | 216 |
|
3547 | 217 |
hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
3697 | 218 |
|
3660 | 219 |
// draw the hat on top of the hog |
3547 | 220 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png", HATS_DIRECTORY(), [[hogArray objectAtIndex:row] objectForKey:@"hat"]]; |
221 |
UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
|
222 |
[hatFile release]; |
|
3660 | 223 |
editableCell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)]; |
3547 | 224 |
[hatSprite release]; |
3697 | 225 |
|
3660 | 226 |
editableCell.textField.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"]; |
227 |
editableCell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; |
|
3697 | 228 |
|
3660 | 229 |
cell = editableCell; |
3547 | 230 |
break; |
231 |
case 2: |
|
232 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; |
|
233 |
if (cell == nil) { |
|
3816 | 234 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle |
3547 | 235 |
reuseIdentifier:CellIdentifier2] autorelease]; |
236 |
} |
|
3697 | 237 |
|
3547 | 238 |
cell.textLabel.text = [self.secondaryItems objectAtIndex:row]; |
3816 | 239 |
cell.detailTextLabel.text = [self.moreSecondaryItems objectAtIndex:row]; |
3547 | 240 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
241 |
switch (row) { |
|
242 |
case 0: // grave |
|
243 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
|
244 |
GRAVES_DIRECTORY(),[teamDictionary objectForKey:@"grave"]] |
|
245 |
andCutAt:CGRectMake(0,0,32,32)]; |
|
246 |
cell.imageView.image = accessoryImage; |
|
247 |
[accessoryImage release]; |
|
248 |
break; |
|
3816 | 249 |
case 1: // voice |
250 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/HellishBomb.png", |
|
251 |
GRAPHICS_DIRECTORY()]]; |
|
252 |
cell.imageView.image = accessoryImage; |
|
253 |
[accessoryImage release]; |
|
254 |
break; |
|
3547 | 255 |
case 2: // fort |
3789
c3eb56754e92
added a smaller version of forts, fixed a couple of regressions
koda
parents:
3783
diff
changeset
|
256 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@-preview.png", |
3547 | 257 |
FORTS_DIRECTORY(),[teamDictionary objectForKey:@"fort"]]]; |
3816 | 258 |
cell.imageView.image = [accessoryImage scaleToSize:CGSizeMake(32, 32)]; |
3547 | 259 |
[accessoryImage release]; |
260 |
break; |
|
261 |
case 3: // flags |
|
262 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
|
263 |
FLAGS_DIRECTORY(),[teamDictionary objectForKey:@"flag"]]]; |
|
3825 | 264 |
cell.imageView.image = [accessoryImage scaleToSize:CGSizeMake(27, 19)]; |
3547 | 265 |
[accessoryImage release]; |
266 |
break; |
|
267 |
case 4: // level |
|
268 |
accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%d.png", |
|
269 |
BOTLEVELS_DIRECTORY(),[[[[teamDictionary objectForKey:@"hedgehogs"] |
|
270 |
objectAtIndex:0] objectForKey:@"level"] |
|
271 |
intValue]]]; |
|
3816 | 272 |
cell.imageView.image = [accessoryImage scaleToSize:CGSizeMake(32, 32)]; |
3547 | 273 |
[accessoryImage release]; |
274 |
break; |
|
275 |
default: |
|
276 |
cell.imageView.image = nil; |
|
277 |
break; |
|
278 |
} |
|
279 |
break; |
|
280 |
} |
|
3697 | 281 |
|
3547 | 282 |
return cell; |
283 |
} |
|
284 |
||
285 |
||
286 |
#pragma mark - |
|
287 |
#pragma mark Table view delegate |
|
288 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
289 |
NSInteger row = [indexPath row]; |
|
290 |
NSInteger section = [indexPath section]; |
|
3697 | 291 |
|
3547 | 292 |
if (2 == section) { |
293 |
switch (row) { |
|
294 |
case 0: // grave |
|
295 |
if (nil == gravesViewController) |
|
296 |
gravesViewController = [[GravesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3924 | 297 |
|
298 |
[gravesViewController setTeamDictionary:teamDictionary]; |
|
299 |
[self.navigationController pushViewController:gravesViewController animated:YES]; |
|
3547 | 300 |
break; |
301 |
case 1: // voice |
|
302 |
if (nil == voicesViewController) |
|
303 |
voicesViewController = [[VoicesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3924 | 304 |
|
305 |
[voicesViewController setTeamDictionary:teamDictionary]; |
|
306 |
[self.navigationController pushViewController:voicesViewController animated:YES]; |
|
3547 | 307 |
break; |
308 |
case 2: // fort |
|
309 |
if (nil == fortsViewController) |
|
310 |
fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3924 | 311 |
|
312 |
[fortsViewController setTeamDictionary:teamDictionary]; |
|
313 |
[self.navigationController pushViewController:fortsViewController animated:YES]; |
|
3547 | 314 |
break; |
315 |
case 3: // flag |
|
3697 | 316 |
if (nil == flagsViewController) |
3547 | 317 |
flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
3924 | 318 |
|
319 |
[flagsViewController setTeamDictionary:teamDictionary]; |
|
320 |
[self.navigationController pushViewController:flagsViewController animated:YES]; |
|
3547 | 321 |
break; |
322 |
case 4: // level |
|
323 |
if (nil == levelViewController) |
|
324 |
levelViewController = [[LevelViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3924 | 325 |
|
326 |
[levelViewController setTeamDictionary:teamDictionary]; |
|
327 |
[self.navigationController pushViewController:levelViewController animated:YES]; |
|
328 |
break; |
|
329 |
default: |
|
330 |
DLog(@"Nope"); |
|
3547 | 331 |
break; |
332 |
} |
|
333 |
} else { |
|
3660 | 334 |
EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
335 |
[cell replyKeyboard]; |
|
3547 | 336 |
[aTableView deselectRowAtIndexPath:indexPath animated:NO]; |
337 |
} |
|
338 |
||
339 |
} |
|
340 |
||
341 |
// action to perform when you want to change a hog hat |
|
3660 | 342 |
-(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
|
343 |
if (nil == hogHatViewController) |
3547 | 344 |
hogHatViewController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
3697 | 345 |
|
3547 | 346 |
// cache the dictionary file of the team, so that other controllers can modify it |
347 |
hogHatViewController.teamDictionary = self.teamDictionary; |
|
348 |
hogHatViewController.selectedHog = [indexPath row]; |
|
3697 | 349 |
|
3660 | 350 |
// if we are editing the field undo any change before proceeding |
351 |
EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
|
352 |
[cell cancel:nil]; |
|
3697 | 353 |
|
3547 | 354 |
[self.navigationController pushViewController:hogHatViewController animated:YES]; |
355 |
} |
|
356 |
||
357 |
||
358 |
#pragma mark - |
|
359 |
#pragma mark Memory management |
|
360 |
-(void) didReceiveMemoryWarning { |
|
361 |
[super didReceiveMemoryWarning]; |
|
362 |
if (hogHatViewController.view.superview == nil) |
|
363 |
hogHatViewController = nil; |
|
364 |
if (gravesViewController.view.superview == nil) |
|
365 |
gravesViewController = nil; |
|
366 |
if (voicesViewController.view.superview == nil) |
|
367 |
voicesViewController = nil; |
|
368 |
if (fortsViewController.view.superview == nil) |
|
369 |
fortsViewController = nil; |
|
370 |
if (flagsViewController.view.superview == nil) |
|
371 |
flagsViewController = nil; |
|
372 |
if (levelViewController.view.superview == nil) |
|
373 |
levelViewController = nil; |
|
3660 | 374 |
MSG_MEMCLEAN(); |
3547 | 375 |
} |
376 |
||
377 |
-(void) viewDidUnload { |
|
3783 | 378 |
[[NSNotificationCenter defaultCenter] removeObserver:self]; |
3547 | 379 |
self.teamDictionary = nil; |
380 |
self.teamName = nil; |
|
381 |
self.normalHogSprite = nil; |
|
382 |
self.secondaryItems = nil; |
|
3816 | 383 |
self.moreSecondaryItems = nil; |
3547 | 384 |
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
|
385 |
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
|
386 |
voicesViewController = nil; |
3547 | 387 |
flagsViewController = nil; |
388 |
fortsViewController = nil; |
|
389 |
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
|
390 |
MSG_DIDUNLOAD(); |
3547 | 391 |
[super viewDidUnload]; |
392 |
} |
|
393 |
||
394 |
-(void) dealloc { |
|
395 |
[teamDictionary release]; |
|
396 |
[teamName release]; |
|
397 |
[normalHogSprite release]; |
|
398 |
[secondaryItems release]; |
|
3816 | 399 |
[moreSecondaryItems release]; |
3547 | 400 |
[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
|
401 |
[gravesViewController release]; |
3547 | 402 |
[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
|
403 |
[voicesViewController release]; |
3547 | 404 |
[flagsViewController release]; |
405 |
[levelViewController release]; |
|
406 |
[super dealloc]; |
|
407 |
} |
|
408 |
||
409 |
||
410 |
@end |
|
411 |