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 |
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];
|
3315
|
52 |
|
|
53 |
// listen if any childController modifies the plist and write it if needed
|
|
54 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil];
|
|
55 |
isWriteNeeded = NO;
|
|
56 |
}
|
|
57 |
|
|
58 |
- (void)viewWillAppear:(BOOL)animated {
|
|
59 |
[super viewWillAppear:animated];
|
3323
|
60 |
|
3315
|
61 |
self.title = [self.teamDictionary objectForKey:@"teamname"];
|
3312
|
62 |
|
|
63 |
// load the images of the hat for aach hog
|
3315
|
64 |
NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
|
|
65 |
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]];
|
|
66 |
for (NSDictionary *hog in hogArray) {
|
3325
|
67 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png", HATS_DIRECTORY(), [hog objectForKey:@"hat"]];
|
3312
|
68 |
|
|
69 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile];
|
3316
|
70 |
[hatFile release];
|
3312
|
71 |
CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32);
|
|
72 |
CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea);
|
|
73 |
[image release];
|
|
74 |
|
|
75 |
UIImage *hatSprite = [[UIImage alloc] initWithCGImage:cgImgage];
|
|
76 |
[array addObject:hatSprite];
|
|
77 |
CGImageRelease(cgImgage);
|
|
78 |
[hatSprite release];
|
|
79 |
}
|
3315
|
80 |
self.hatArray = array;
|
3312
|
81 |
[array release];
|
3315
|
82 |
|
|
83 |
[self.tableView reloadData];
|
3305
|
84 |
}
|
|
85 |
|
3325
|
86 |
// needed by other classes to warn about a user change
|
3315
|
87 |
-(void) setWriteNeeded {
|
|
88 |
isWriteNeeded = YES;
|
|
89 |
}
|
|
90 |
|
|
91 |
// write to disk the team dictionary
|
|
92 |
-(void) viewWillDisappear:(BOOL)animated {
|
3305
|
93 |
[super viewWillDisappear:animated];
|
3315
|
94 |
|
|
95 |
if (isWriteNeeded) {
|
3325
|
96 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title];
|
|
97 |
[self.teamDictionary writeToFile:teamFile atomically:YES];
|
3315
|
98 |
[teamFile release];
|
3325
|
99 |
NSLog(@"writing: %@",teamDictionary);
|
3315
|
100 |
isWriteNeeded = NO;
|
|
101 |
}
|
3305
|
102 |
}
|
3315
|
103 |
|
3305
|
104 |
|
|
105 |
#pragma mark -
|
|
106 |
#pragma mark Table view data source
|
|
107 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
|
|
108 |
return 3;
|
|
109 |
}
|
|
110 |
|
|
111 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
3312
|
112 |
NSInteger rows = 0;
|
3305
|
113 |
switch (section) {
|
3325
|
114 |
case 0: // team name
|
3305
|
115 |
rows = 1;
|
|
116 |
break;
|
3325
|
117 |
case 1: // team members
|
|
118 |
rows = MAX_HOGS;
|
3305
|
119 |
break;
|
3325
|
120 |
case 2: // team details
|
|
121 |
rows = [self.secondaryItems count];
|
3305
|
122 |
break;
|
|
123 |
default:
|
|
124 |
break;
|
|
125 |
}
|
|
126 |
return rows;
|
|
127 |
}
|
|
128 |
|
|
129 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
130 |
static NSString *CellIdentifier = @"Cell";
|
|
131 |
|
|
132 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
133 |
if (cell == nil) {
|
|
134 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
|
|
135 |
reuseIdentifier:CellIdentifier] autorelease];
|
|
136 |
}
|
3325
|
137 |
|
|
138 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
|
|
139 |
|
3315
|
140 |
NSArray *hogArray;
|
3305
|
141 |
NSInteger row = [indexPath row];
|
|
142 |
switch ([indexPath section]) {
|
|
143 |
case 0:
|
3315
|
144 |
cell.textLabel.text = self.title;
|
|
145 |
cell.imageView.image = nil;
|
3305
|
146 |
break;
|
|
147 |
case 1:
|
3315
|
148 |
hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
|
|
149 |
cell.textLabel.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"];
|
|
150 |
cell.imageView.image = [self.hatArray objectAtIndex:row];
|
3305
|
151 |
break;
|
|
152 |
case 2:
|
|
153 |
cell.textLabel.text = [self.secondaryItems objectAtIndex:row];
|
3325
|
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 |
}
|
3305
|
163 |
break;
|
|
164 |
default:
|
|
165 |
break;
|
|
166 |
}
|
|
167 |
|
|
168 |
return cell;
|
|
169 |
}
|
|
170 |
|
|
171 |
|
|
172 |
/*
|
|
173 |
// Override to support conditional editing of the table view.
|
|
174 |
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
175 |
// Return NO if you do not want the specified item to be editable.
|
|
176 |
return YES;
|
|
177 |
}
|
|
178 |
*/
|
|
179 |
|
|
180 |
|
|
181 |
/*
|
|
182 |
// Override to support editing the table view.
|
|
183 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
184 |
|
|
185 |
if (editingStyle == UITableViewCellEditingStyleDelete) {
|
|
186 |
// Delete the row from the data source
|
|
187 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
|
|
188 |
}
|
|
189 |
else if (editingStyle == UITableViewCellEditingStyleInsert) {
|
|
190 |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
|
|
191 |
}
|
|
192 |
}
|
|
193 |
*/
|
|
194 |
|
|
195 |
|
|
196 |
/*
|
|
197 |
// Override to support rearranging the table view.
|
|
198 |
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
|
|
199 |
}
|
|
200 |
*/
|
|
201 |
|
|
202 |
|
|
203 |
/*
|
|
204 |
// Override to support conditional rearranging of the table view.
|
|
205 |
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
206 |
// Return NO if you do not want the item to be re-orderable.
|
|
207 |
return YES;
|
|
208 |
}
|
|
209 |
*/
|
|
210 |
|
|
211 |
|
|
212 |
#pragma mark -
|
|
213 |
#pragma mark Table view delegate
|
|
214 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
3325
|
215 |
NSInteger row = [indexPath row];
|
|
216 |
UITableViewController *nextController;
|
3305
|
217 |
if (1 == [indexPath section]) {
|
|
218 |
if (nil == hogChildController) {
|
|
219 |
hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped];
|
|
220 |
}
|
3315
|
221 |
|
|
222 |
// cache the dictionary file of the team, so that other controllers can modify it
|
|
223 |
hogChildController.teamDictionary = self.teamDictionary;
|
3325
|
224 |
hogChildController.selectedHog = row;
|
3315
|
225 |
|
3325
|
226 |
nextController = hogChildController;
|
3305
|
227 |
}
|
3325
|
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];
|
3305
|
234 |
}
|
|
235 |
|
|
236 |
|
|
237 |
#pragma mark -
|
|
238 |
#pragma mark Memory management
|
|
239 |
-(void) didReceiveMemoryWarning {
|
|
240 |
// Releases the view if it doesn't have a superview.
|
|
241 |
[super didReceiveMemoryWarning];
|
|
242 |
// Relinquish ownership any cached data, images, etc that aren't in use.
|
|
243 |
}
|
|
244 |
|
|
245 |
-(void) viewDidUnload {
|
3325
|
246 |
self.secondaryControllers = nil;
|
|
247 |
self.secondaryItems = nil;
|
3315
|
248 |
self.hatArray = nil;
|
|
249 |
self.teamDictionary = nil;
|
|
250 |
[super viewDidUnload];
|
3305
|
251 |
}
|
|
252 |
|
|
253 |
-(void) dealloc {
|
3325
|
254 |
[secondaryControllers release];
|
3305
|
255 |
[secondaryItems release];
|
3315
|
256 |
[hatArray release];
|
|
257 |
[teamDictionary release];
|
3305
|
258 |
[super dealloc];
|
|
259 |
}
|
|
260 |
|
|
261 |
|
|
262 |
@end
|
|
263 |
|