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