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];
|
|
51 |
|
|
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];
|
|
68 |
|
|
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;
|
3660
|
72 |
|
|
73 |
self.title = NSLocalizedString(@"Edit team settings",@"");
|
3547
|
74 |
}
|
|
75 |
|
|
76 |
-(void) viewWillAppear:(BOOL)animated {
|
|
77 |
[super viewWillAppear:animated];
|
|
78 |
|
3660
|
79 |
// load data about the team and write if there has been a change from other childControllers
|
3547
|
80 |
if (isWriteNeeded)
|
|
81 |
[self writeFile];
|
|
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];
|
3660
|
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 |
|
|
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";
|
|
162 |
|
|
163 |
NSArray *hogArray;
|
|
164 |
UITableViewCell *cell = nil;
|
3660
|
165 |
EditableCellView *editableCell = nil;
|
3547
|
166 |
NSInteger row = [indexPath row];
|
|
167 |
UIImage *accessoryImage;
|
|
168 |
|
|
169 |
switch ([indexPath section]) {
|
|
170 |
case 0:
|
3660
|
171 |
editableCell = (EditableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
|
|
172 |
if (editableCell == nil) {
|
|
173 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault
|
3547
|
174 |
reuseIdentifier:CellIdentifier0] autorelease];
|
3660
|
175 |
editableCell.delegate = self;
|
|
176 |
editableCell.tag = TEAMNAME_TAG;
|
3547
|
177 |
}
|
|
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) {
|
|
188 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault
|
3547
|
189 |
reuseIdentifier:CellIdentifier1] autorelease];
|
3660
|
190 |
editableCell.delegate = self;
|
|
191 |
editableCell.tag = [indexPath row];
|
3547
|
192 |
}
|
|
193 |
|
|
194 |
hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
|
|
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];
|
|
202 |
|
3660
|
203 |
editableCell.textField.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"];
|
|
204 |
editableCell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
|
|
205 |
|
|
206 |
cell = editableCell;
|
3547
|
207 |
break;
|
|
208 |
case 2:
|
|
209 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
|
|
210 |
if (cell == nil) {
|
|
211 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
|
|
212 |
reuseIdentifier:CellIdentifier2] autorelease];
|
|
213 |
}
|
|
214 |
|
|
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;
|
|
231 |
|
|
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 |
}
|
|
252 |
|
|
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;
|
|
263 |
|
|
264 |
if (2 == section) {
|
|
265 |
switch (row) {
|
|
266 |
case 0: // grave
|
|
267 |
if (nil == gravesViewController)
|
|
268 |
gravesViewController = [[GravesViewController alloc] initWithStyle:UITableViewStyleGrouped];
|
|
269 |
|
|
270 |
nextController = gravesViewController;
|
|
271 |
break;
|
|
272 |
case 1: // voice
|
|
273 |
if (nil == voicesViewController)
|
|
274 |
voicesViewController = [[VoicesViewController alloc] initWithStyle:UITableViewStyleGrouped];
|
|
275 |
|
|
276 |
nextController = voicesViewController;
|
|
277 |
break;
|
|
278 |
case 2: // fort
|
|
279 |
if (nil == fortsViewController)
|
|
280 |
fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped];
|
|
281 |
|
|
282 |
nextController = fortsViewController;
|
|
283 |
break;
|
|
284 |
case 3: // flag
|
|
285 |
if (nil == flagsViewController)
|
|
286 |
flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped];
|
|
287 |
|
|
288 |
nextController = flagsViewController;
|
|
289 |
break;
|
|
290 |
case 4: // level
|
|
291 |
if (nil == levelViewController)
|
|
292 |
levelViewController = [[LevelViewController alloc] initWithStyle:UITableViewStyleGrouped];
|
|
293 |
|
|
294 |
nextController = levelViewController;
|
|
295 |
break;
|
|
296 |
}
|
|
297 |
|
|
298 |
nextController.title = [secondaryItems objectAtIndex:row];
|
|
299 |
[nextController setTeamDictionary:teamDictionary];
|
|
300 |
[self.navigationController pushViewController:nextController animated:YES];
|
3659
|
301 |
[nextController release];
|
3547
|
302 |
} else {
|
3660
|
303 |
EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath];
|
|
304 |
[cell replyKeyboard];
|
3547
|
305 |
[aTableView deselectRowAtIndexPath:indexPath animated:NO];
|
|
306 |
}
|
|
307 |
|
|
308 |
}
|
|
309 |
|
|
310 |
// action to perform when you want to change a hog hat
|
3660
|
311 |
-(void) tableView:(UITableView *)aTableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
|
3547
|
312 |
if (nil == hogHatViewController) {
|
|
313 |
hogHatViewController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped];
|
|
314 |
}
|
|
315 |
|
|
316 |
// cache the dictionary file of the team, so that other controllers can modify it
|
|
317 |
hogHatViewController.teamDictionary = self.teamDictionary;
|
|
318 |
hogHatViewController.selectedHog = [indexPath row];
|
|
319 |
|
3660
|
320 |
// if we are editing the field undo any change before proceeding
|
|
321 |
EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath];
|
|
322 |
[cell cancel:nil];
|
|
323 |
|
3547
|
324 |
[self.navigationController pushViewController:hogHatViewController animated:YES];
|
|
325 |
}
|
|
326 |
|
|
327 |
|
|
328 |
#pragma mark -
|
|
329 |
#pragma mark Memory management
|
|
330 |
-(void) didReceiveMemoryWarning {
|
|
331 |
[super didReceiveMemoryWarning];
|
|
332 |
if (hogHatViewController.view.superview == nil)
|
|
333 |
hogHatViewController = nil;
|
|
334 |
if (gravesViewController.view.superview == nil)
|
|
335 |
gravesViewController = nil;
|
|
336 |
if (voicesViewController.view.superview == nil)
|
|
337 |
voicesViewController = nil;
|
|
338 |
if (fortsViewController.view.superview == nil)
|
|
339 |
fortsViewController = nil;
|
|
340 |
if (flagsViewController.view.superview == nil)
|
|
341 |
flagsViewController = nil;
|
|
342 |
if (levelViewController.view.superview == nil)
|
|
343 |
levelViewController = nil;
|
3660
|
344 |
MSG_MEMCLEAN();
|
3547
|
345 |
}
|
|
346 |
|
|
347 |
-(void) viewDidUnload {
|
|
348 |
self.teamDictionary = nil;
|
|
349 |
self.teamName = nil;
|
|
350 |
self.normalHogSprite = nil;
|
|
351 |
self.secondaryItems = nil;
|
|
352 |
hogHatViewController = nil;
|
|
353 |
flagsViewController = nil;
|
|
354 |
fortsViewController = nil;
|
|
355 |
gravesViewController = nil;
|
|
356 |
levelViewController = nil;
|
|
357 |
[super viewDidUnload];
|
|
358 |
MSG_DIDUNLOAD();
|
|
359 |
}
|
|
360 |
|
|
361 |
-(void) dealloc {
|
|
362 |
[teamDictionary release];
|
|
363 |
[teamName release];
|
|
364 |
[normalHogSprite release];
|
|
365 |
[secondaryItems release];
|
|
366 |
[hogHatViewController release];
|
|
367 |
[fortsViewController release];
|
|
368 |
[gravesViewController release];
|
|
369 |
[flagsViewController release];
|
|
370 |
[levelViewController release];
|
|
371 |
[super dealloc];
|
|
372 |
}
|
|
373 |
|
|
374 |
|
|
375 |
@end
|
|
376 |
|