18 * File created on 20/04/2010. |
18 * File created on 20/04/2010. |
19 */ |
19 */ |
20 |
20 |
21 |
21 |
22 #import "TeamConfigViewController.h" |
22 #import "TeamConfigViewController.h" |
23 #import "CommodityFunctions.h" |
23 #import <QuartzCore/QuartzCore.h> |
24 #import "SquareButtonView.h" |
24 #import "SquareButtonView.h" |
25 |
25 |
|
26 |
26 @implementation TeamConfigViewController |
27 @implementation TeamConfigViewController |
27 @synthesize listOfTeams, listOfSelectedTeams, cachedContentsOfDir; |
28 @synthesize tableView, selectedTeamsCount, allTeamsCount, listOfAllTeams, listOfSelectedTeams, cachedContentsOfDir; |
|
29 |
|
30 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
31 return rotationManager(interfaceOrientation); |
|
32 } |
28 |
33 |
29 #pragma mark - |
34 #pragma mark - |
30 #pragma mark View lifecycle |
35 #pragma mark View lifecycle |
31 -(void) viewDidLoad { |
36 -(void) viewDidLoad { |
|
37 UITableView *aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) |
|
38 style:UITableViewStyleGrouped]; |
|
39 aTableView.delegate = self; |
|
40 aTableView.dataSource = self; |
|
41 if (IS_IPAD()) { |
|
42 [aTableView setBackgroundColorForAnyTable:[UIColor darkBlueColorTransparent]]; |
|
43 aTableView.layer.borderColor = [[UIColor darkYellowColor] CGColor]; |
|
44 aTableView.layer.borderWidth = 2.7f; |
|
45 aTableView.layer.cornerRadius = 8; |
|
46 aTableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0); |
|
47 } else { |
|
48 UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:@"background~iphone.png"]; |
|
49 UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage]; |
|
50 [backgroundImage release]; |
|
51 [self.view addSubview:background]; |
|
52 [background release]; |
|
53 [aTableView setBackgroundColorForAnyTable:[UIColor clearColor]]; |
|
54 } |
|
55 |
|
56 aTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite; |
|
57 aTableView.separatorColor = [UIColor whiteColor]; |
|
58 aTableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
|
59 self.tableView = aTableView; |
|
60 [aTableView release]; |
|
61 |
|
62 [self.view addSubview:self.tableView]; |
32 [super viewDidLoad]; |
63 [super viewDidLoad]; |
33 |
|
34 CGSize screenSize = [[UIScreen mainScreen] bounds].size; |
|
35 self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44); |
|
36 |
|
37 if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) { |
|
38 if (IS_IPAD()) |
|
39 [self.tableView setBackgroundView:nil]; |
|
40 else { |
|
41 UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:@"background~iphone.png"]; |
|
42 UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage]; |
|
43 [backgroundImage release]; |
|
44 [self.tableView setBackgroundView:background]; |
|
45 [background release]; |
|
46 } |
|
47 } else |
|
48 self.view.backgroundColor = [UIColor blackColor]; |
|
49 |
|
50 self.tableView.separatorColor = UICOLOR_HW_YELLOW_BODER; |
|
51 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
|
52 } |
64 } |
53 |
65 |
54 -(void) viewWillAppear:(BOOL)animated { |
66 -(void) viewWillAppear:(BOOL)animated { |
55 [super viewWillAppear:animated]; |
|
56 |
|
57 NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL]; |
67 NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL]; |
58 // avoid overwriting selected teams when returning on this view |
|
59 if ([self.cachedContentsOfDir isEqualToArray:contentsOfDir] == NO) { |
68 if ([self.cachedContentsOfDir isEqualToArray:contentsOfDir] == NO) { |
60 NSArray *colors = getAvailableColors(); |
69 self.cachedContentsOfDir = contentsOfDir; |
|
70 NSArray *colors = [HWUtils teamColors]; |
61 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[contentsOfDir count]]; |
71 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[contentsOfDir count]]; |
62 for (int i = 0; i < [contentsOfDir count]; i++) { |
72 for (int i = 0; i < [contentsOfDir count]; i++) { |
63 NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: |
73 NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: |
64 [contentsOfDir objectAtIndex:i],@"team", |
74 [contentsOfDir objectAtIndex:i],@"team", |
65 [NSNumber numberWithInt:4],@"number", |
75 [NSNumber numberWithInt:4],@"number", |
66 [colors objectAtIndex:i%[colors count]],@"color",nil]; |
76 [colors objectAtIndex:i%[colors count]],@"color",nil]; |
67 [array addObject:dict]; |
77 [array addObject:dict]; |
68 [dict release]; |
78 [dict release]; |
69 } |
79 } |
70 self.listOfTeams = array; |
80 self.listOfAllTeams = array; |
71 [array release]; |
81 [array release]; |
72 |
82 |
73 NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithObjects:nil]; |
83 NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithObjects:nil]; |
74 self.listOfSelectedTeams = emptyArray; |
84 self.listOfSelectedTeams = emptyArray; |
75 [emptyArray release]; |
85 [emptyArray release]; |
76 |
86 |
77 selectedTeamsCount = [self.listOfSelectedTeams count]; |
87 self.selectedTeamsCount = [self.listOfSelectedTeams count]; |
78 allTeamsCount = [self.listOfTeams count]; |
88 self.allTeamsCount = [self.listOfAllTeams count]; |
79 |
89 [self.tableView reloadData]; |
80 NSArray *contents = [[NSArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
90 } |
81 self.cachedContentsOfDir = contents; |
91 |
82 [contents release]; |
92 [super viewWillAppear:animated]; |
83 } |
|
84 [self.tableView reloadData]; |
|
85 } |
|
86 |
|
87 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
88 return rotationManager(interfaceOrientation); |
|
89 } |
93 } |
90 |
94 |
91 -(NSInteger) filterNumberOfHogs:(NSInteger) hogs { |
95 -(NSInteger) filterNumberOfHogs:(NSInteger) hogs { |
92 NSInteger numberOfHogs; |
96 NSInteger numberOfHogs; |
93 if (hogs <= HW_getMaxNumberOfHogs() && hogs >= 1) |
97 if (hogs <= HW_getMaxNumberOfHogs() && hogs >= 1) |
99 numberOfHogs = HW_getMaxNumberOfHogs(); |
103 numberOfHogs = HW_getMaxNumberOfHogs(); |
100 } |
104 } |
101 return numberOfHogs; |
105 return numberOfHogs; |
102 } |
106 } |
103 |
107 |
104 -(UIImage *)drawHogsRepeated:(NSInteger) manyTimes { |
|
105 NSString *imgString = [[NSString alloc] initWithFormat:@"%@/hedgehog.png",[[NSBundle mainBundle] resourcePath]]; |
|
106 UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:imgString]; |
|
107 [imgString release]; |
|
108 CGFloat screenScale = getScreenScale(); |
|
109 int w = hogSprite.size.width * screenScale; |
|
110 int h = hogSprite.size.height * screenScale; |
|
111 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
|
112 CGContextRef context = CGBitmapContextCreate(NULL, w * 3, h, 8, 4 * w * 3, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
113 |
|
114 // draw the two images in the current context |
|
115 for (int i = 0; i < manyTimes; i++) |
|
116 CGContextDrawImage(context, CGRectMake(i*8*screenScale, 0, w, h), [hogSprite CGImage]); |
|
117 [hogSprite release]; |
|
118 |
|
119 // Create bitmap image info from pixel data in current context |
|
120 CGImageRef imageRef = CGBitmapContextCreateImage(context); |
|
121 |
|
122 // Create a new UIImage object |
|
123 UIImage *resultImage; |
|
124 if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
|
125 resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp]; |
|
126 else |
|
127 resultImage = [UIImage imageWithCGImage:imageRef]; |
|
128 |
|
129 // Release colorspace, context and bitmap information |
|
130 CGColorSpaceRelease(colorSpace); |
|
131 CGContextRelease(context); |
|
132 CFRelease(imageRef); |
|
133 |
|
134 return resultImage; |
|
135 } |
|
136 |
|
137 #pragma mark - |
108 #pragma mark - |
138 #pragma mark Table view data source |
109 #pragma mark Table view data source |
139 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
110 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
140 return 2; |
111 return 2; |
141 } |
112 } |
142 |
113 |
143 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
114 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
144 if (section == 0) |
115 return (section == 0 ? self.selectedTeamsCount : self.allTeamsCount); |
145 return selectedTeamsCount; |
|
146 else |
|
147 return allTeamsCount; |
|
148 } |
116 } |
149 |
117 |
150 // Customize the appearance of table view cells. |
118 // Customize the appearance of table view cells. |
151 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
119 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
152 static NSString *CellIdentifier0 = @"Cell0"; |
120 static NSString *CellIdentifier0 = @"Cell0"; |
172 [squareButton selectColor:[[selectedRow objectForKey:@"color"] intValue]]; |
140 [squareButton selectColor:[[selectedRow objectForKey:@"color"] intValue]]; |
173 NSNumber *hogNumber = [selectedRow objectForKey:@"number"]; |
141 NSNumber *hogNumber = [selectedRow objectForKey:@"number"]; |
174 [squareButton setTitle:[hogNumber stringValue] forState:UIControlStateNormal]; |
142 [squareButton setTitle:[hogNumber stringValue] forState:UIControlStateNormal]; |
175 squareButton.ownerDictionary = selectedRow; |
143 squareButton.ownerDictionary = selectedRow; |
176 |
144 |
177 cell.imageView.image = [self drawHogsRepeated:[hogNumber intValue]]; |
145 cell.imageView.image = [UIImage drawHogsRepeated:[hogNumber intValue]]; |
178 ((HoldTableViewCell *)cell).delegate = self; |
146 ((HoldTableViewCell *)cell).delegate = self; |
179 } else { |
147 } else { |
180 cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
148 cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
181 if (cell == nil) |
149 if (cell == nil) |
182 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
150 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
183 |
151 |
184 cell.textLabel.text = [[[listOfTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension]; |
152 cell.textLabel.text = [[[self.listOfAllTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension]; |
185 cell.textLabel.backgroundColor = [UIColor clearColor]; |
153 cell.textLabel.backgroundColor = [UIColor clearColor]; |
186 |
154 |
187 NSString *teamPath = [NSString stringWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),cell.textLabel.text]; |
155 NSString *teamPath = [NSString stringWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),cell.textLabel.text]; |
188 NSDictionary *firstHog = [[[NSDictionary dictionaryWithContentsOfFile:teamPath] objectForKey:@"hedgehogs"] objectAtIndex:0]; |
156 NSDictionary *firstHog = [[[NSDictionary dictionaryWithContentsOfFile:teamPath] objectForKey:@"hedgehogs"] objectAtIndex:0]; |
189 if ([[firstHog objectForKey:@"level"] intValue] != 0) { |
157 if ([[firstHog objectForKey:@"level"] intValue] != 0) { |
197 [spriteView release]; |
165 [spriteView release]; |
198 } else |
166 } else |
199 cell.accessoryView = nil; |
167 cell.accessoryView = nil; |
200 } |
168 } |
201 |
169 |
202 cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT; |
170 cell.textLabel.textColor = [UIColor lightYellowColor]; |
203 cell.backgroundColor = UICOLOR_HW_ALMOSTBLACK; |
171 cell.backgroundColor = [UIColor blackColorTransparent]; |
204 cell.selectionStyle = UITableViewCellSelectionStyleNone; |
172 cell.selectionStyle = UITableViewCellSelectionStyleNone; |
205 |
173 |
206 return cell; |
174 return cell; |
207 } |
175 } |
208 |
176 |
209 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { |
177 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { |
210 return 40.0; |
178 return 45.0; |
211 } |
179 } |
212 |
180 |
213 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { |
181 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { |
214 CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30); |
182 CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30); |
215 NSString *text; |
183 NSString *text = (section == 0) ? NSLocalizedString(@"Playing Teams",@"") : NSLocalizedString(@"Available Teams",@""); |
216 if (section == 0) |
184 UILabel *theLabel = [[UILabel alloc] initWithFrame:frame andTitle:text]; |
217 text = NSLocalizedString(@"Playing Teams",@""); |
|
218 else |
|
219 text = NSLocalizedString(@"Available Teams",@""); |
|
220 UILabel *theLabel = createBlueLabel(text, frame); |
|
221 theLabel.center = CGPointMake(self.view.frame.size.width/2, 20); |
185 theLabel.center = CGPointMake(self.view.frame.size.width/2, 20); |
222 |
186 |
223 UIView *theView = [[[UIView alloc] init] autorelease]; |
187 UIView *theView = [[[UIView alloc] init] autorelease]; |
224 [theView addSubview:theLabel]; |
188 [theView addSubview:theLabel]; |
225 [theLabel release]; |
189 [theLabel release]; |
228 |
192 |
229 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { |
193 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { |
230 return IS_IPAD() ? 40 : 20; |
194 return IS_IPAD() ? 40 : 20; |
231 } |
195 } |
232 |
196 |
233 -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section { |
197 -(UIView *)tableView:(UITableView *)aTableView viewForFooterInSection:(NSInteger) section { |
234 NSInteger height = IS_IPAD() ? 40 : 20; |
198 NSInteger height = IS_IPAD() ? 40 : 20; |
235 UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, height)]; |
199 UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aTableView.frame.size.width, height)]; |
236 footer.backgroundColor = [UIColor clearColor]; |
200 footer.backgroundColor = [UIColor clearColor]; |
237 |
201 |
238 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width*80/100, height)]; |
202 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, aTableView.frame.size.width*80/100, height)]; |
239 label.center = CGPointMake(self.tableView.frame.size.width/2, height/2); |
203 label.center = CGPointMake(aTableView.frame.size.width/2, height/2); |
240 label.textAlignment = UITextAlignmentCenter; |
204 label.textAlignment = UITextAlignmentCenter; |
241 label.font = [UIFont italicSystemFontOfSize:12]; |
205 label.font = [UIFont italicSystemFontOfSize:12]; |
242 label.textColor = [UIColor whiteColor]; |
206 label.textColor = [UIColor whiteColor]; |
243 label.numberOfLines = 2; |
207 label.numberOfLines = 2; |
244 if (section == 0) |
208 if (section == 0) |
279 NSInteger increaseNumber = [[selectedRow objectForKey:@"number"] intValue] + 1; |
243 NSInteger increaseNumber = [[selectedRow objectForKey:@"number"] intValue] + 1; |
280 NSNumber *newNumber = [NSNumber numberWithInt:[self filterNumberOfHogs:increaseNumber]]; |
244 NSNumber *newNumber = [NSNumber numberWithInt:[self filterNumberOfHogs:increaseNumber]]; |
281 [squareButton setTitle:[newNumber stringValue] forState:UIControlStateNormal]; |
245 [squareButton setTitle:[newNumber stringValue] forState:UIControlStateNormal]; |
282 [selectedRow setObject:newNumber forKey:@"number"]; |
246 [selectedRow setObject:newNumber forKey:@"number"]; |
283 |
247 |
284 cell.imageView.image = [self drawHogsRepeated:[newNumber intValue]]; |
248 cell.imageView.image = [UIImage drawHogsRepeated:[newNumber intValue]]; |
285 } |
249 } |
286 } |
250 } |
287 |
251 |
288 -(void) holdAction:(NSString *)content { |
252 -(void) holdAction:(NSString *)content onTable:(UITableView *)aTableView { |
289 NSInteger row; |
253 NSInteger row; |
290 for (row = 0; row < [self.listOfSelectedTeams count]; row++) { |
254 for (row = 0; row < [self.listOfSelectedTeams count]; row++) { |
291 NSDictionary *dict = [self.listOfSelectedTeams objectAtIndex:row]; |
255 NSDictionary *dict = [self.listOfSelectedTeams objectAtIndex:row]; |
292 if ([content isEqualToString:[[dict objectForKey:@"team"] stringByDeletingPathExtension]]) |
256 if ([content isEqualToString:[[dict objectForKey:@"team"] stringByDeletingPathExtension]]) |
293 break; |
257 break; |
294 } |
258 } |
295 |
259 |
296 [self.listOfTeams addObject:[self.listOfSelectedTeams objectAtIndex:row]]; |
260 [self.listOfAllTeams addObject:[self.listOfSelectedTeams objectAtIndex:row]]; |
297 [self.listOfSelectedTeams removeObjectAtIndex:row]; |
261 [self.listOfSelectedTeams removeObjectAtIndex:row]; |
298 |
262 |
299 [self.tableView beginUpdates]; |
263 [aTableView beginUpdates]; |
300 [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:0]] withRowAnimation:UITableViewRowAnimationLeft]; |
264 [aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:0]] withRowAnimation:UITableViewRowAnimationLeft]; |
301 [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:allTeamsCount inSection:1]] withRowAnimation:UITableViewRowAnimationLeft]; |
265 [aTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:allTeamsCount inSection:1]] withRowAnimation:UITableViewRowAnimationLeft]; |
302 allTeamsCount++; |
266 self.allTeamsCount++; |
303 selectedTeamsCount--; |
267 self.selectedTeamsCount--; |
304 [self.tableView endUpdates]; |
268 [aTableView endUpdates]; |
305 } |
269 } |
306 |
270 |
307 #pragma mark - |
271 #pragma mark - |
308 #pragma mark Memory management |
272 #pragma mark Memory management |
309 -(void) didReceiveMemoryWarning { |
273 -(void) didReceiveMemoryWarning { |
310 // Relinquish ownership any cached data, images, etc that aren't in use. |
274 if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) { |
|
275 self.listOfSelectedTeams = nil; |
|
276 self.listOfAllTeams = nil; |
|
277 self.tableView = nil; |
|
278 } |
311 self.cachedContentsOfDir = nil; |
279 self.cachedContentsOfDir = nil; |
312 MSG_MEMCLEAN(); |
280 MSG_MEMCLEAN(); |
313 [super didReceiveMemoryWarning]; |
281 [super didReceiveMemoryWarning]; |
314 } |
282 } |
315 |
283 |
316 -(void) viewDidUnload { |
284 -(void) viewDidUnload { |
317 self.listOfTeams = nil; |
285 self.tableView = nil; |
|
286 self.listOfAllTeams = nil; |
318 self.listOfSelectedTeams = nil; |
287 self.listOfSelectedTeams = nil; |
319 self.cachedContentsOfDir = nil; |
288 self.cachedContentsOfDir = nil; |
320 MSG_DIDUNLOAD(); |
289 MSG_DIDUNLOAD(); |
321 [super viewDidUnload]; |
290 [super viewDidUnload]; |
322 } |
291 } |
323 |
292 |
324 |
293 |
325 -(void) dealloc { |
294 -(void) dealloc { |
326 releaseAndNil(listOfTeams); |
295 releaseAndNil(tableView); |
|
296 releaseAndNil(listOfAllTeams); |
327 releaseAndNil(listOfSelectedTeams); |
297 releaseAndNil(listOfSelectedTeams); |
328 releaseAndNil(cachedContentsOfDir); |
298 releaseAndNil(cachedContentsOfDir); |
329 [super dealloc]; |
299 [super dealloc]; |
330 } |
300 } |
331 |
301 |