63 NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
63 NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
64 UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)]; |
64 UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)]; |
65 [normalHogFile release]; |
65 [normalHogFile release]; |
66 self.normalHogSprite = hogSprite; |
66 self.normalHogSprite = hogSprite; |
67 [hogSprite release]; |
67 [hogSprite release]; |
68 |
68 |
69 // listen if any childController modifies the plist and write it if needed |
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]; |
70 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil]; |
71 isWriteNeeded = NO; |
71 isWriteNeeded = NO; |
72 |
72 |
73 self.title = NSLocalizedString(@"Edit team settings",@""); |
73 self.title = NSLocalizedString(@"Edit team settings",@""); |
74 } |
74 } |
75 |
75 |
76 -(void) viewWillAppear:(BOOL)animated { |
76 -(void) viewWillAppear:(BOOL)animated { |
77 [super viewWillAppear:animated]; |
77 [super viewWillAppear:animated]; |
78 |
78 |
79 // load data about the team and write if there has been a change from other childControllers |
79 // load data about the team and write if there has been a change from other childControllers |
80 if (isWriteNeeded) |
80 if (isWriteNeeded) |
81 [self writeFile]; |
81 [self writeFile]; |
82 |
82 |
83 NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.teamName]; |
83 NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.teamName]; |
84 NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile]; |
84 NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile]; |
85 self.teamDictionary = teamDict; |
85 self.teamDictionary = teamDict; |
86 [teamDict release]; |
86 [teamDict release]; |
87 [teamFile release]; |
87 [teamFile release]; |
88 |
88 |
89 [self.tableView reloadData]; |
89 [self.tableView reloadData]; |
90 } |
90 } |
91 |
91 |
92 // write on file if there has been a change |
92 // write on file if there has been a change |
93 -(void) viewWillDisappear:(BOOL)animated { |
93 -(void) viewWillDisappear:(BOOL)animated { |
94 [super viewWillDisappear:animated]; |
94 [super viewWillDisappear:animated]; |
95 |
95 |
96 if (isWriteNeeded) |
96 if (isWriteNeeded) |
97 [self writeFile]; |
97 [self writeFile]; |
98 } |
98 } |
99 |
99 |
100 #pragma mark - |
100 #pragma mark - |
101 // needed by other classes to warn about a user change |
101 // needed by other classes to warn about a user change |
157 |
157 |
158 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
158 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
159 static NSString *CellIdentifier0 = @"Cell0"; |
159 static NSString *CellIdentifier0 = @"Cell0"; |
160 static NSString *CellIdentifier1 = @"Cell1"; |
160 static NSString *CellIdentifier1 = @"Cell1"; |
161 static NSString *CellIdentifier2 = @"Cell2"; |
161 static NSString *CellIdentifier2 = @"Cell2"; |
162 |
162 |
163 NSArray *hogArray; |
163 NSArray *hogArray; |
164 UITableViewCell *cell = nil; |
164 UITableViewCell *cell = nil; |
165 EditableCellView *editableCell = nil; |
165 EditableCellView *editableCell = nil; |
166 NSInteger row = [indexPath row]; |
166 NSInteger row = [indexPath row]; |
167 UIImage *accessoryImage; |
167 UIImage *accessoryImage; |
168 |
168 |
169 switch ([indexPath section]) { |
169 switch ([indexPath section]) { |
170 case 0: |
170 case 0: |
171 editableCell = (EditableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
171 editableCell = (EditableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
172 if (editableCell == nil) { |
172 if (editableCell == nil) { |
173 editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
173 editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
174 reuseIdentifier:CellIdentifier0] autorelease]; |
174 reuseIdentifier:CellIdentifier0] autorelease]; |
175 editableCell.delegate = self; |
175 editableCell.delegate = self; |
176 editableCell.tag = TEAMNAME_TAG; |
176 editableCell.tag = TEAMNAME_TAG; |
177 } |
177 } |
178 |
178 |
179 editableCell.imageView.image = nil; |
179 editableCell.imageView.image = nil; |
180 editableCell.accessoryType = UITableViewCellAccessoryNone; |
180 editableCell.accessoryType = UITableViewCellAccessoryNone; |
181 editableCell.textField.text = self.teamName; |
181 editableCell.textField.text = self.teamName; |
182 |
182 |
183 cell = editableCell; |
183 cell = editableCell; |
184 break; |
184 break; |
185 case 1: |
185 case 1: |
186 editableCell = (EditableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
186 editableCell = (EditableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
187 if (editableCell == nil) { |
187 if (editableCell == nil) { |
188 editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
188 editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
189 reuseIdentifier:CellIdentifier1] autorelease]; |
189 reuseIdentifier:CellIdentifier1] autorelease]; |
190 editableCell.delegate = self; |
190 editableCell.delegate = self; |
191 editableCell.tag = [indexPath row]; |
191 editableCell.tag = [indexPath row]; |
192 } |
192 } |
193 |
193 |
194 hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
194 hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
195 |
195 |
196 // draw the hat on top of the hog |
196 // draw the hat on top of the hog |
197 NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png", HATS_DIRECTORY(), [[hogArray objectAtIndex:row] objectForKey:@"hat"]]; |
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)]; |
198 UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
199 [hatFile release]; |
199 [hatFile release]; |
200 editableCell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)]; |
200 editableCell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)]; |
201 [hatSprite release]; |
201 [hatSprite release]; |
202 |
202 |
203 editableCell.textField.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"]; |
203 editableCell.textField.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"]; |
204 editableCell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; |
204 editableCell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; |
205 |
205 |
206 cell = editableCell; |
206 cell = editableCell; |
207 break; |
207 break; |
208 case 2: |
208 case 2: |
209 cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; |
209 cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; |
210 if (cell == nil) { |
210 if (cell == nil) { |
211 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
211 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
212 reuseIdentifier:CellIdentifier2] autorelease]; |
212 reuseIdentifier:CellIdentifier2] autorelease]; |
213 } |
213 } |
214 |
214 |
215 cell.textLabel.text = [self.secondaryItems objectAtIndex:row]; |
215 cell.textLabel.text = [self.secondaryItems objectAtIndex:row]; |
216 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
216 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
217 switch (row) { |
217 switch (row) { |
218 case 0: // grave |
218 case 0: // grave |
219 accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
219 accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
226 accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@L.png", |
226 accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@L.png", |
227 FORTS_DIRECTORY(),[teamDictionary objectForKey:@"fort"]]]; |
227 FORTS_DIRECTORY(),[teamDictionary objectForKey:@"fort"]]]; |
228 cell.imageView.image = [accessoryImage scaleToSize:CGSizeMake(42, 42)]; |
228 cell.imageView.image = [accessoryImage scaleToSize:CGSizeMake(42, 42)]; |
229 [accessoryImage release]; |
229 [accessoryImage release]; |
230 break; |
230 break; |
231 |
231 |
232 case 3: // flags |
232 case 3: // flags |
233 accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
233 accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
234 FLAGS_DIRECTORY(),[teamDictionary objectForKey:@"flag"]]]; |
234 FLAGS_DIRECTORY(),[teamDictionary objectForKey:@"flag"]]]; |
235 cell.imageView.image = accessoryImage; |
235 cell.imageView.image = accessoryImage; |
236 [accessoryImage release]; |
236 [accessoryImage release]; |
247 cell.imageView.image = nil; |
247 cell.imageView.image = nil; |
248 break; |
248 break; |
249 } |
249 } |
250 break; |
250 break; |
251 } |
251 } |
252 |
252 |
253 return cell; |
253 return cell; |
254 } |
254 } |
255 |
255 |
256 |
256 |
257 #pragma mark - |
257 #pragma mark - |
258 #pragma mark Table view delegate |
258 #pragma mark Table view delegate |
259 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
259 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
260 NSInteger row = [indexPath row]; |
260 NSInteger row = [indexPath row]; |
261 NSInteger section = [indexPath section]; |
261 NSInteger section = [indexPath section]; |
262 UITableViewController *nextController = nil; |
262 UITableViewController *nextController = nil; |
263 |
263 |
264 if (2 == section) { |
264 if (2 == section) { |
265 switch (row) { |
265 switch (row) { |
266 case 0: // grave |
266 case 0: // grave |
267 if (nil == gravesViewController) |
267 if (nil == gravesViewController) |
268 gravesViewController = [[GravesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
268 gravesViewController = [[GravesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
269 |
269 |
270 nextController = gravesViewController; |
270 nextController = gravesViewController; |
271 break; |
271 break; |
272 case 1: // voice |
272 case 1: // voice |
273 if (nil == voicesViewController) |
273 if (nil == voicesViewController) |
274 voicesViewController = [[VoicesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
274 voicesViewController = [[VoicesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
275 |
275 |
276 nextController = voicesViewController; |
276 nextController = voicesViewController; |
277 break; |
277 break; |
278 case 2: // fort |
278 case 2: // fort |
279 if (nil == fortsViewController) |
279 if (nil == fortsViewController) |
280 fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
280 fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
281 |
281 |
282 nextController = fortsViewController; |
282 nextController = fortsViewController; |
283 break; |
283 break; |
284 case 3: // flag |
284 case 3: // flag |
285 if (nil == flagsViewController) |
285 if (nil == flagsViewController) |
286 flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
286 flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
287 |
287 |
288 nextController = flagsViewController; |
288 nextController = flagsViewController; |
289 break; |
289 break; |
290 case 4: // level |
290 case 4: // level |
291 if (nil == levelViewController) |
291 if (nil == levelViewController) |
292 levelViewController = [[LevelViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
292 levelViewController = [[LevelViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
293 |
293 |
294 nextController = levelViewController; |
294 nextController = levelViewController; |
295 break; |
295 break; |
296 } |
296 } |
297 |
297 |
298 if ([nextController respondsToSelector:@selector(setTeamDictionary:)]) |
298 if ([nextController respondsToSelector:@selector(setTeamDictionary:)]) |
299 [nextController setTeamDictionary:teamDictionary]; |
299 [nextController setTeamDictionary:teamDictionary]; |
300 [self.navigationController pushViewController:nextController animated:YES]; |
300 [self.navigationController pushViewController:nextController animated:YES]; |
301 } else { |
301 } else { |
302 EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
302 EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
308 |
308 |
309 // action to perform when you want to change a hog hat |
309 // action to perform when you want to change a hog hat |
310 -(void) tableView:(UITableView *)aTableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { |
310 -(void) tableView:(UITableView *)aTableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { |
311 if (nil == hogHatViewController) |
311 if (nil == hogHatViewController) |
312 hogHatViewController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
312 hogHatViewController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
313 |
313 |
314 // cache the dictionary file of the team, so that other controllers can modify it |
314 // cache the dictionary file of the team, so that other controllers can modify it |
315 hogHatViewController.teamDictionary = self.teamDictionary; |
315 hogHatViewController.teamDictionary = self.teamDictionary; |
316 hogHatViewController.selectedHog = [indexPath row]; |
316 hogHatViewController.selectedHog = [indexPath row]; |
317 |
317 |
318 // if we are editing the field undo any change before proceeding |
318 // if we are editing the field undo any change before proceeding |
319 EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
319 EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
320 [cell cancel:nil]; |
320 [cell cancel:nil]; |
321 |
321 |
322 [self.navigationController pushViewController:hogHatViewController animated:YES]; |
322 [self.navigationController pushViewController:hogHatViewController animated:YES]; |
323 } |
323 } |
324 |
324 |
325 |
325 |
326 #pragma mark - |
326 #pragma mark - |