author | koda |
Fri, 09 Apr 2010 11:09:26 +0000 | |
changeset 3328 | fe87c2242984 |
parent 3325 | 652a8ebdf667 |
child 3329 | d8e41ee0b3ae |
permissions | -rw-r--r-- |
3305 | 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" |
|
3325 | 11 |
#import "FlagsViewController.h" |
12 |
#import "FortsViewController.h" |
|
13 |
#import "CommodityFunctions.h" |
|
3305 | 14 |
|
15 |
@implementation SingleTeamViewController |
|
3325 | 16 |
@synthesize teamDictionary, hatArray, secondaryItems, secondaryControllers; |
17 |
||
3305 | 18 |
|
3325 | 19 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
20 |
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
|
21 |
} |
|
3305 | 22 |
|
23 |
#pragma mark - |
|
24 |
#pragma mark View lifecycle |
|
25 |
- (void)viewDidLoad { |
|
26 |
[super viewDidLoad]; |
|
27 |
||
28 |
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: |
|
29 |
NSLocalizedString(@"Grave",@""), |
|
30 |
NSLocalizedString(@"Voice",@""), |
|
31 |
NSLocalizedString(@"Fort",@""), |
|
32 |
NSLocalizedString(@"Flag",@""), |
|
33 |
NSLocalizedString(@"Level",@""),nil]; |
|
34 |
self.secondaryItems = array; |
|
35 |
[array release]; |
|
3312 | 36 |
|
3325 | 37 |
// insert controllers here |
38 |
NSMutableArray *controllersArray = [[NSMutableArray alloc] initWithCapacity:[secondaryItems count]]; |
|
39 |
||
40 |
FlagsViewController *flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
41 |
[controllersArray addObject:flagsViewController]; |
|
42 |
[flagsViewController release]; |
|
43 |
||
44 |
FortsViewController *fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
45 |
[controllersArray addObject:fortsViewController]; |
|
46 |
[fortsViewController release]; |
|
47 |
||
48 |
self.secondaryControllers = controllersArray; |
|
49 |
[controllersArray release]; |
|
3315 | 50 |
|
51 |
// listen if any childController modifies the plist and write it if needed |
|
52 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil]; |
|
53 |
isWriteNeeded = NO; |
|
54 |
} |
|
55 |
||
56 |
- (void)viewWillAppear:(BOOL)animated { |
|
57 |
[super viewWillAppear:animated]; |
|
3323 | 58 |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
59 |
// load data about the team and extract info |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
60 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
61 |
if (isWriteNeeded) { |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
62 |
[self.teamDictionary writeToFile:teamFile atomically:YES]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
63 |
NSLog(@"writing: %@",teamDictionary); |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
64 |
isWriteNeeded = NO; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
65 |
} |
3312 | 66 |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
67 |
NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
68 |
self.teamDictionary = teamDict; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
69 |
[teamDict release]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
70 |
|
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
71 |
[teamFile release]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
72 |
|
3312 | 73 |
// load the images of the hat for aach hog |
3315 | 74 |
NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
75 |
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]]; |
|
76 |
for (NSDictionary *hog in hogArray) { |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
77 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png",HATS_DIRECTORY(),[hog objectForKey:@"hat"]]; |
3312 | 78 |
|
79 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile]; |
|
3316 | 80 |
[hatFile release]; |
3312 | 81 |
CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32); |
82 |
CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea); |
|
83 |
[image release]; |
|
84 |
||
85 |
UIImage *hatSprite = [[UIImage alloc] initWithCGImage:cgImgage]; |
|
86 |
[array addObject:hatSprite]; |
|
87 |
CGImageRelease(cgImgage); |
|
88 |
[hatSprite release]; |
|
89 |
} |
|
3315 | 90 |
self.hatArray = array; |
3312 | 91 |
[array release]; |
3315 | 92 |
|
93 |
[self.tableView reloadData]; |
|
3305 | 94 |
} |
95 |
||
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
96 |
-(void) viewWillDisappear:(BOOL)animated { |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
97 |
[super viewWillDisappear:animated]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
98 |
|
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
99 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
100 |
if (isWriteNeeded) { |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
101 |
[self.teamDictionary writeToFile:teamFile atomically:YES]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
102 |
NSLog(@"writing: %@",teamDictionary); |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
103 |
isWriteNeeded = NO; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
104 |
} |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
105 |
|
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
106 |
[teamFile release]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
107 |
} |
3325 | 108 |
// needed by other classes to warn about a user change |
3315 | 109 |
-(void) setWriteNeeded { |
110 |
isWriteNeeded = YES; |
|
111 |
} |
|
112 |
||
3305 | 113 |
#pragma mark - |
114 |
#pragma mark Table view data source |
|
115 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
116 |
return 3; |
|
117 |
} |
|
118 |
||
119 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
3312 | 120 |
NSInteger rows = 0; |
3305 | 121 |
switch (section) { |
3325 | 122 |
case 0: // team name |
3305 | 123 |
rows = 1; |
124 |
break; |
|
3325 | 125 |
case 1: // team members |
126 |
rows = MAX_HOGS; |
|
3305 | 127 |
break; |
3325 | 128 |
case 2: // team details |
129 |
rows = [self.secondaryItems count]; |
|
3305 | 130 |
break; |
131 |
default: |
|
132 |
break; |
|
133 |
} |
|
134 |
return rows; |
|
135 |
} |
|
136 |
||
137 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
138 |
static NSString *CellIdentifier = @"Cell"; |
|
139 |
||
140 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
141 |
if (cell == nil) { |
|
142 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
|
143 |
reuseIdentifier:CellIdentifier] autorelease]; |
|
144 |
} |
|
3325 | 145 |
|
146 |
||
3315 | 147 |
NSArray *hogArray; |
3305 | 148 |
NSInteger row = [indexPath row]; |
149 |
switch ([indexPath section]) { |
|
150 |
case 0: |
|
3315 | 151 |
cell.textLabel.text = self.title; |
152 |
cell.imageView.image = nil; |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
153 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3305 | 154 |
break; |
155 |
case 1: |
|
3315 | 156 |
hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
157 |
cell.textLabel.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"]; |
|
158 |
cell.imageView.image = [self.hatArray objectAtIndex:row]; |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
159 |
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; |
3305 | 160 |
break; |
161 |
case 2: |
|
162 |
cell.textLabel.text = [self.secondaryItems objectAtIndex:row]; |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
163 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3325 | 164 |
switch (row) { |
165 |
case 3: // flags |
|
166 |
cell.imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
|
167 |
FLAGS_DIRECTORY(),[teamDictionary objectForKey:@"flag"]]]; |
|
168 |
break; |
|
169 |
default: |
|
170 |
cell.imageView.image = nil; |
|
171 |
break; |
|
172 |
} |
|
3305 | 173 |
break; |
174 |
default: |
|
175 |
break; |
|
176 |
} |
|
177 |
||
178 |
return cell; |
|
179 |
} |
|
180 |
||
181 |
||
182 |
/* |
|
183 |
// Override to support conditional editing of the table view. |
|
184 |
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
|
185 |
// Return NO if you do not want the specified item to be editable. |
|
186 |
return YES; |
|
187 |
} |
|
188 |
*/ |
|
189 |
||
190 |
/* |
|
191 |
// Override to support editing the table view. |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
192 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
193 |
forRowAtIndexPath:(NSIndexPath *)indexPath { |
3305 | 194 |
|
195 |
if (editingStyle == UITableViewCellEditingStyleDelete) { |
|
196 |
// Delete the row from the data source |
|
197 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; |
|
198 |
} |
|
199 |
else if (editingStyle == UITableViewCellEditingStyleInsert) { |
|
200 |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view |
|
201 |
} |
|
202 |
} |
|
203 |
*/ |
|
204 |
||
205 |
/* |
|
206 |
// Override to support rearranging the table view. |
|
207 |
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { |
|
208 |
} |
|
209 |
*/ |
|
210 |
||
211 |
||
212 |
/* |
|
213 |
// Override to support conditional rearranging of the table view. |
|
214 |
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
|
215 |
// Return NO if you do not want the item to be re-orderable. |
|
216 |
return YES; |
|
217 |
} |
|
218 |
*/ |
|
219 |
||
220 |
||
221 |
#pragma mark - |
|
222 |
#pragma mark Table view delegate |
|
223 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
3325 | 224 |
NSInteger row = [indexPath row]; |
225 |
UITableViewController *nextController; |
|
3305 | 226 |
if (1 == [indexPath section]) { |
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
227 |
//TODO: hog name pref, causes segfault |
3305 | 228 |
} |
3325 | 229 |
if (2 == [indexPath section]) { |
230 |
//TODO: this part should be rewrittend with lazy loading instead of an array of controllers |
|
231 |
nextController = [secondaryControllers objectAtIndex:row%2 ]; //TODO: fix the objectAtIndex |
|
232 |
nextController.title = [secondaryItems objectAtIndex:row]; |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
233 |
[nextController setTeamDictionary:teamDictionary]; |
3325 | 234 |
} |
235 |
[self.navigationController pushViewController:nextController animated:YES]; |
|
3305 | 236 |
} |
237 |
||
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
238 |
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
239 |
if (nil == hogChildController) { |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
240 |
hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
241 |
} |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
242 |
|
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
243 |
// cache the dictionary file of the team, so that other controllers can modify it |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
244 |
hogChildController.teamDictionary = self.teamDictionary; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
245 |
hogChildController.selectedHog = [indexPath row]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
246 |
|
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
247 |
[self.navigationController pushViewController:hogChildController animated:YES]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
248 |
} |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
249 |
|
3305 | 250 |
|
251 |
#pragma mark - |
|
252 |
#pragma mark Memory management |
|
253 |
-(void) didReceiveMemoryWarning { |
|
254 |
// Releases the view if it doesn't have a superview. |
|
255 |
[super didReceiveMemoryWarning]; |
|
256 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
257 |
} |
|
258 |
||
259 |
-(void) viewDidUnload { |
|
3325 | 260 |
self.secondaryControllers = nil; |
261 |
self.secondaryItems = nil; |
|
3315 | 262 |
self.hatArray = nil; |
263 |
self.teamDictionary = nil; |
|
264 |
[super viewDidUnload]; |
|
3305 | 265 |
} |
266 |
||
267 |
-(void) dealloc { |
|
3325 | 268 |
[secondaryControllers release]; |
3305 | 269 |
[secondaryItems release]; |
3315 | 270 |
[hatArray release]; |
271 |
[teamDictionary release]; |
|
3305 | 272 |
[super dealloc]; |
273 |
} |
|
274 |
||
275 |
||
276 |
@end |
|
277 |