6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
7 // |
7 // |
8 |
8 |
9 #import "SingleTeamViewController.h" |
9 #import "SingleTeamViewController.h" |
10 #import "HogHatViewController.h" |
10 #import "HogHatViewController.h" |
|
11 #import "FlagsViewController.h" |
|
12 #import "FortsViewController.h" |
|
13 #import "CommodityFunctions.h" |
11 |
14 |
12 @implementation SingleTeamViewController |
15 @implementation SingleTeamViewController |
13 @synthesize teamDictionary, hatArray, secondaryItems; |
16 @synthesize teamDictionary, hatArray, secondaryItems, secondaryControllers; |
14 |
17 |
|
18 |
|
19 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
20 return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
|
21 } |
15 |
22 |
16 #pragma mark - |
23 #pragma mark - |
17 #pragma mark View lifecycle |
24 #pragma mark View lifecycle |
18 - (void)viewDidLoad { |
25 - (void)viewDidLoad { |
19 [super viewDidLoad]; |
26 [super viewDidLoad]; |
20 |
|
21 // Uncomment the following line to preserve selection between presentations. |
|
22 //self.clearsSelectionOnViewWillAppear = NO; |
|
23 |
|
24 // Uncomment the following line to display an Edit button in the navigation bar for this view controller. |
|
25 // self.navigationItem.rightBarButtonItem = self.editButtonItem; |
|
26 |
27 |
27 NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: |
28 NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: |
28 NSLocalizedString(@"Color",@""), |
|
29 NSLocalizedString(@"Grave",@""), |
29 NSLocalizedString(@"Grave",@""), |
30 NSLocalizedString(@"Voice",@""), |
30 NSLocalizedString(@"Voice",@""), |
31 NSLocalizedString(@"Fort",@""), |
31 NSLocalizedString(@"Fort",@""), |
32 NSLocalizedString(@"Flag",@""), |
32 NSLocalizedString(@"Flag",@""), |
33 NSLocalizedString(@"Level",@""),nil]; |
33 NSLocalizedString(@"Level",@""),nil]; |
34 self.secondaryItems = array; |
34 self.secondaryItems = array; |
35 [array release]; |
35 [array release]; |
36 |
36 |
37 |
37 // insert controllers here |
|
38 NSMutableArray *controllersArray = [[NSMutableArray alloc] initWithCapacity:[secondaryItems count]]; |
|
39 |
|
40 FlagsViewController *flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
41 flagsViewController.teamDictionary = self.teamDictionary; |
|
42 [controllersArray addObject:flagsViewController]; |
|
43 [flagsViewController release]; |
|
44 |
|
45 FortsViewController *fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
46 fortsViewController.teamDictionary = self.teamDictionary; |
|
47 [controllersArray addObject:fortsViewController]; |
|
48 [fortsViewController release]; |
|
49 |
|
50 self.secondaryControllers = controllersArray; |
|
51 [controllersArray release]; |
38 |
52 |
39 // listen if any childController modifies the plist and write it if needed |
53 // listen if any childController modifies the plist and write it if needed |
40 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil]; |
54 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil]; |
41 isWriteNeeded = NO; |
55 isWriteNeeded = NO; |
42 } |
56 } |
48 |
62 |
49 // load the images of the hat for aach hog |
63 // load the images of the hat for aach hog |
50 NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
64 NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
51 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]]; |
65 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]]; |
52 for (NSDictionary *hog in hogArray) { |
66 for (NSDictionary *hog in hogArray) { |
53 NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/Data/Graphics/Hats/%@.png",[[NSBundle mainBundle] resourcePath],[hog objectForKey:@"hat"]]; |
67 NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png", HATS_DIRECTORY(), [hog objectForKey:@"hat"]]; |
54 |
68 |
55 UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile]; |
69 UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile]; |
56 [hatFile release]; |
70 [hatFile release]; |
57 CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32); |
71 CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32); |
58 CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea); |
72 CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea); |
64 [hatSprite release]; |
78 [hatSprite release]; |
65 } |
79 } |
66 self.hatArray = array; |
80 self.hatArray = array; |
67 [array release]; |
81 [array release]; |
68 |
82 |
69 [self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
70 [self.tableView reloadData]; |
83 [self.tableView reloadData]; |
71 } |
84 } |
72 |
85 |
73 /* |
86 // needed by other classes to warn about a user change |
74 - (void)viewDidAppear:(BOOL)animated { |
|
75 [super viewDidAppear:animated]; |
|
76 } |
|
77 */ |
|
78 |
|
79 -(void) setWriteNeeded { |
87 -(void) setWriteNeeded { |
80 isWriteNeeded = YES; |
88 isWriteNeeded = YES; |
81 } |
89 } |
82 |
90 |
83 // write to disk the team dictionary |
91 // write to disk the team dictionary |
84 -(void) viewWillDisappear:(BOOL)animated { |
92 -(void) viewWillDisappear:(BOOL)animated { |
85 [super viewWillDisappear:animated]; |
93 [super viewWillDisappear:animated]; |
86 |
94 |
87 if (isWriteNeeded) { |
95 if (isWriteNeeded) { |
88 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
96 NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title]; |
89 NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/Teams/%@.plist",[paths objectAtIndex:0],self.title]; |
97 [self.teamDictionary writeToFile:teamFile atomically:YES]; |
90 [self.teamDictionary writeToFile: teamFile atomically:YES]; |
|
91 [teamFile release]; |
98 [teamFile release]; |
92 |
99 NSLog(@"writing: %@",teamDictionary); |
93 isWriteNeeded = NO; |
100 isWriteNeeded = NO; |
94 } |
101 } |
95 } |
|
96 |
|
97 /* |
|
98 - (void)viewDidDisappear:(BOOL)animated { |
|
99 [super viewDidDisappear:animated]; |
|
100 } |
|
101 */ |
|
102 |
|
103 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
104 return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
|
105 } |
102 } |
106 |
103 |
107 |
104 |
108 #pragma mark - |
105 #pragma mark - |
109 #pragma mark Table view data source |
106 #pragma mark Table view data source |
135 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
132 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
136 if (cell == nil) { |
133 if (cell == nil) { |
137 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
134 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
138 reuseIdentifier:CellIdentifier] autorelease]; |
135 reuseIdentifier:CellIdentifier] autorelease]; |
139 } |
136 } |
|
137 |
|
138 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
|
139 |
140 NSArray *hogArray; |
140 NSArray *hogArray; |
141 NSInteger row = [indexPath row]; |
141 NSInteger row = [indexPath row]; |
142 switch ([indexPath section]) { |
142 switch ([indexPath section]) { |
143 case 0: |
143 case 0: |
144 cell.textLabel.text = self.title; |
144 cell.textLabel.text = self.title; |
145 cell.imageView.image = nil; |
145 cell.imageView.image = nil; |
146 break; |
146 break; |
147 case 1: |
147 case 1: |
148 hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
148 hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
149 cell.textLabel.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"]; |
149 cell.textLabel.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"]; |
150 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
|
151 cell.imageView.image = [self.hatArray objectAtIndex:row]; |
150 cell.imageView.image = [self.hatArray objectAtIndex:row]; |
152 break; |
151 break; |
153 case 2: |
152 case 2: |
154 cell.textLabel.text = [self.secondaryItems objectAtIndex:row]; |
153 cell.textLabel.text = [self.secondaryItems objectAtIndex:row]; |
155 cell.imageView.image = nil; |
154 switch (row) { |
|
155 case 3: // flags |
|
156 cell.imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
|
157 FLAGS_DIRECTORY(),[teamDictionary objectForKey:@"flag"]]]; |
|
158 break; |
|
159 default: |
|
160 cell.imageView.image = nil; |
|
161 break; |
|
162 } |
156 break; |
163 break; |
157 default: |
164 default: |
158 break; |
165 break; |
159 } |
166 } |
160 |
167 |
203 |
210 |
204 |
211 |
205 #pragma mark - |
212 #pragma mark - |
206 #pragma mark Table view delegate |
213 #pragma mark Table view delegate |
207 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
214 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
215 NSInteger row = [indexPath row]; |
|
216 UITableViewController *nextController; |
208 if (1 == [indexPath section]) { |
217 if (1 == [indexPath section]) { |
209 if (nil == hogChildController) { |
218 if (nil == hogChildController) { |
210 hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
219 hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
211 } |
220 } |
212 |
221 |
213 // cache the dictionary file of the team, so that other controllers can modify it |
222 // cache the dictionary file of the team, so that other controllers can modify it |
214 hogChildController.teamDictionary = self.teamDictionary; |
223 hogChildController.teamDictionary = self.teamDictionary; |
215 hogChildController.selectedHog = [indexPath row]; |
224 hogChildController.selectedHog = row; |
216 |
225 |
217 [self.navigationController pushViewController:hogChildController animated:YES]; |
226 nextController = hogChildController; |
218 } |
227 } |
|
228 if (2 == [indexPath section]) { |
|
229 //TODO: this part should be rewrittend with lazy loading instead of an array of controllers |
|
230 nextController = [secondaryControllers objectAtIndex:row%2 ]; //TODO: fix the objectAtIndex |
|
231 nextController.title = [secondaryItems objectAtIndex:row]; |
|
232 } |
|
233 [self.navigationController pushViewController:nextController animated:YES]; |
219 } |
234 } |
220 |
235 |
221 |
236 |
222 #pragma mark - |
237 #pragma mark - |
223 #pragma mark Memory management |
238 #pragma mark Memory management |
226 [super didReceiveMemoryWarning]; |
241 [super didReceiveMemoryWarning]; |
227 // Relinquish ownership any cached data, images, etc that aren't in use. |
242 // Relinquish ownership any cached data, images, etc that aren't in use. |
228 } |
243 } |
229 |
244 |
230 -(void) viewDidUnload { |
245 -(void) viewDidUnload { |
|
246 self.secondaryControllers = nil; |
|
247 self.secondaryItems = nil; |
231 self.hatArray = nil; |
248 self.hatArray = nil; |
232 self.secondaryItems = nil; |
|
233 self.teamDictionary = nil; |
249 self.teamDictionary = nil; |
234 [super viewDidUnload]; |
250 [super viewDidUnload]; |
235 } |
251 } |
236 |
252 |
237 |
|
238 -(void) dealloc { |
253 -(void) dealloc { |
|
254 [secondaryControllers release]; |
239 [secondaryItems release]; |
255 [secondaryItems release]; |
240 [hatArray release]; |
256 [hatArray release]; |
241 [teamDictionary release]; |
257 [teamDictionary release]; |
242 [super dealloc]; |
258 [super dealloc]; |
243 } |
259 } |