author | koda |
Sun, 03 Oct 2010 00:23:05 +0200 | |
changeset 3923 | 694e6f6e0e30 |
parent 3829 | 81db3c85784b |
child 3924 | 2a9ace189288 |
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]; |
|
291 |
UITableViewController *nextController = nil; |
|
3697 | 292 |
|
3547 | 293 |
if (2 == section) { |
294 |
switch (row) { |
|
295 |
case 0: // grave |
|
296 |
if (nil == gravesViewController) |
|
297 |
gravesViewController = [[GravesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3697 | 298 |
|
3547 | 299 |
nextController = gravesViewController; |
300 |
break; |
|
301 |
case 1: // voice |
|
302 |
if (nil == voicesViewController) |
|
303 |
voicesViewController = [[VoicesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3697 | 304 |
|
305 |
nextController = voicesViewController; |
|
3547 | 306 |
break; |
307 |
case 2: // fort |
|
308 |
if (nil == fortsViewController) |
|
309 |
fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3697 | 310 |
|
3547 | 311 |
nextController = fortsViewController; |
312 |
break; |
|
313 |
case 3: // flag |
|
3697 | 314 |
if (nil == flagsViewController) |
3547 | 315 |
flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
3697 | 316 |
|
3547 | 317 |
nextController = flagsViewController; |
318 |
break; |
|
319 |
case 4: // level |
|
320 |
if (nil == levelViewController) |
|
321 |
levelViewController = [[LevelViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3697 | 322 |
|
3547 | 323 |
nextController = levelViewController; |
324 |
break; |
|
325 |
} |
|
3697 | 326 |
|
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 ([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
|
328 |
[nextController setTeamDictionary:teamDictionary]; |
3547 | 329 |
[self.navigationController pushViewController:nextController animated:YES]; |
330 |
} else { |
|
3660 | 331 |
EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
332 |
[cell replyKeyboard]; |
|
3547 | 333 |
[aTableView deselectRowAtIndexPath:indexPath animated:NO]; |
334 |
} |
|
335 |
||
336 |
} |
|
337 |
||
338 |
// action to perform when you want to change a hog hat |
|
3660 | 339 |
-(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
|
340 |
if (nil == hogHatViewController) |
3547 | 341 |
hogHatViewController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
3697 | 342 |
|
3547 | 343 |
// cache the dictionary file of the team, so that other controllers can modify it |
344 |
hogHatViewController.teamDictionary = self.teamDictionary; |
|
345 |
hogHatViewController.selectedHog = [indexPath row]; |
|
3697 | 346 |
|
3660 | 347 |
// if we are editing the field undo any change before proceeding |
348 |
EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
|
349 |
[cell cancel:nil]; |
|
3697 | 350 |
|
3547 | 351 |
[self.navigationController pushViewController:hogHatViewController animated:YES]; |
352 |
} |
|
353 |
||
354 |
||
355 |
#pragma mark - |
|
356 |
#pragma mark Memory management |
|
357 |
-(void) didReceiveMemoryWarning { |
|
358 |
[super didReceiveMemoryWarning]; |
|
359 |
if (hogHatViewController.view.superview == nil) |
|
360 |
hogHatViewController = nil; |
|
361 |
if (gravesViewController.view.superview == nil) |
|
362 |
gravesViewController = nil; |
|
363 |
if (voicesViewController.view.superview == nil) |
|
364 |
voicesViewController = nil; |
|
365 |
if (fortsViewController.view.superview == nil) |
|
366 |
fortsViewController = nil; |
|
367 |
if (flagsViewController.view.superview == nil) |
|
368 |
flagsViewController = nil; |
|
369 |
if (levelViewController.view.superview == nil) |
|
370 |
levelViewController = nil; |
|
3660 | 371 |
MSG_MEMCLEAN(); |
3547 | 372 |
} |
373 |
||
374 |
-(void) viewDidUnload { |
|
3783 | 375 |
[[NSNotificationCenter defaultCenter] removeObserver:self]; |
3547 | 376 |
self.teamDictionary = nil; |
377 |
self.teamName = nil; |
|
378 |
self.normalHogSprite = nil; |
|
379 |
self.secondaryItems = nil; |
|
3816 | 380 |
self.moreSecondaryItems = nil; |
3547 | 381 |
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
|
382 |
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
|
383 |
voicesViewController = nil; |
3547 | 384 |
flagsViewController = nil; |
385 |
fortsViewController = nil; |
|
386 |
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
|
387 |
MSG_DIDUNLOAD(); |
3547 | 388 |
[super viewDidUnload]; |
389 |
} |
|
390 |
||
391 |
-(void) dealloc { |
|
392 |
[teamDictionary release]; |
|
393 |
[teamName release]; |
|
394 |
[normalHogSprite release]; |
|
395 |
[secondaryItems release]; |
|
3816 | 396 |
[moreSecondaryItems release]; |
3547 | 397 |
[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
|
398 |
[gravesViewController release]; |
3547 | 399 |
[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
|
400 |
[voicesViewController release]; |
3547 | 401 |
[flagsViewController release]; |
402 |
[levelViewController release]; |
|
403 |
[super dealloc]; |
|
404 |
} |
|
405 |
||
406 |
||
407 |
@end |
|
408 |