3339
|
1 |
//
|
3352
|
2 |
// LevelViewController.m
|
3339
|
3 |
// HedgewarsMobile
|
|
4 |
//
|
|
5 |
// Created by Vittorio on 02/04/10.
|
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved.
|
|
7 |
//
|
|
8 |
|
3340
|
9 |
#import "LevelViewController.h"
|
3339
|
10 |
#import "CommodityFunctions.h"
|
|
11 |
|
|
12 |
|
|
13 |
@implementation LevelViewController
|
|
14 |
@synthesize teamDictionary, levelArray, levelSprites, lastIndexPath;
|
|
15 |
|
|
16 |
|
|
17 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
|
|
18 |
return rotationManager(interfaceOrientation);
|
|
19 |
}
|
|
20 |
|
|
21 |
|
|
22 |
#pragma mark -
|
|
23 |
#pragma mark View lifecycle
|
|
24 |
- (void)viewDidLoad {
|
|
25 |
[super viewDidLoad];
|
|
26 |
|
|
27 |
NSArray *array = [[NSArray alloc] initWithObjects:
|
3340
|
28 |
NSLocalizedString(@"Human",@""),
|
3352
|
29 |
NSLocalizedString(@"Brutal",@""),
|
|
30 |
NSLocalizedString(@"Aggressive",@""),
|
|
31 |
NSLocalizedString(@"Bully",@""),
|
3340
|
32 |
NSLocalizedString(@"Average",@""),
|
3352
|
33 |
NSLocalizedString(@"Weaky",@""),
|
|
34 |
nil];
|
3339
|
35 |
self.levelArray = array;
|
|
36 |
[array release];
|
3352
|
37 |
|
|
38 |
NSMutableArray *sprites = [[NSMutableArray alloc] initWithCapacity:[levelArray count]];
|
|
39 |
for (int i = 0; i < [levelArray count]; i++) {
|
|
40 |
NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/%d.png",BOTLEVELS_DIRECTORY(),i];
|
|
41 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:botlevelPath];
|
|
42 |
[botlevelPath release];
|
|
43 |
[sprites addObject:image];
|
|
44 |
[image release];
|
3339
|
45 |
}
|
3352
|
46 |
self.levelSprites = sprites;
|
3339
|
47 |
[sprites release];
|
3352
|
48 |
|
3339
|
49 |
}
|
|
50 |
|
|
51 |
- (void)viewWillAppear:(BOOL)animated {
|
|
52 |
[super viewWillAppear:animated];
|
|
53 |
|
|
54 |
// this moves the tableview to the top
|
|
55 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
|
|
56 |
}
|
|
57 |
|
|
58 |
/*
|
|
59 |
- (void)viewDidAppear:(BOOL)animated {
|
|
60 |
[super viewDidAppear:animated];
|
|
61 |
}
|
|
62 |
*/
|
|
63 |
/*
|
|
64 |
- (void)viewWillDisappear:(BOOL)animated {
|
|
65 |
[super viewWillDisappear:animated];
|
|
66 |
}
|
|
67 |
*/
|
|
68 |
/*
|
|
69 |
- (void)viewDidDisappear:(BOOL)animated {
|
|
70 |
[super viewDidDisappear:animated];
|
|
71 |
}
|
|
72 |
*/
|
|
73 |
|
|
74 |
|
|
75 |
#pragma mark -
|
|
76 |
#pragma mark Table view data source
|
|
77 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
|
|
78 |
return 1;
|
|
79 |
}
|
|
80 |
|
|
81 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
82 |
return [self.levelArray count];
|
|
83 |
}
|
|
84 |
|
|
85 |
// Customize the appearance of table view cells.
|
3340
|
86 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
3339
|
87 |
static NSString *CellIdentifier = @"Cell";
|
|
88 |
|
3340
|
89 |
NSInteger row = [indexPath row];
|
3339
|
90 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
91 |
if (cell == nil) {
|
|
92 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
|
|
93 |
}
|
|
94 |
|
3340
|
95 |
cell.textLabel.text = [levelArray objectAtIndex:row];
|
|
96 |
NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0];
|
|
97 |
if ([[hog objectForKey:@"level"] intValue] == row) {
|
3339
|
98 |
cell.accessoryType = UITableViewCellAccessoryCheckmark;
|
|
99 |
self.lastIndexPath = indexPath;
|
|
100 |
} else {
|
|
101 |
cell.accessoryType = UITableViewCellAccessoryNone;
|
|
102 |
}
|
|
103 |
|
3340
|
104 |
cell.imageView.image = [levelSprites objectAtIndex:row];
|
3339
|
105 |
return cell;
|
|
106 |
}
|
|
107 |
|
|
108 |
|
|
109 |
/*
|
|
110 |
// Override to support conditional editing of the table view.
|
|
111 |
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
112 |
// Return NO if you do not want the specified item to be editable.
|
|
113 |
return YES;
|
|
114 |
}
|
|
115 |
*/
|
|
116 |
|
|
117 |
|
|
118 |
/*
|
|
119 |
// Override to support editing the table view.
|
|
120 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
121 |
|
|
122 |
if (editingStyle == UITableViewCellEditingStyleDelete) {
|
|
123 |
// Delete the row from the data source
|
|
124 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
|
|
125 |
}
|
|
126 |
else if (editingStyle == UITableViewCellEditingStyleInsert) {
|
|
127 |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
|
|
128 |
}
|
|
129 |
}
|
|
130 |
*/
|
|
131 |
|
|
132 |
|
|
133 |
/*
|
|
134 |
// Override to support rearranging the table view.
|
|
135 |
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
|
|
136 |
}
|
|
137 |
*/
|
|
138 |
|
|
139 |
|
|
140 |
/*
|
|
141 |
// Override to support conditional rearranging of the table view.
|
|
142 |
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
143 |
// Return NO if you do not want the item to be re-orderable.
|
|
144 |
return YES;
|
|
145 |
}
|
|
146 |
*/
|
|
147 |
|
|
148 |
|
|
149 |
#pragma mark -
|
|
150 |
#pragma mark Table view delegate
|
|
151 |
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
152 |
int newRow = [indexPath row];
|
|
153 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
|
|
154 |
|
|
155 |
if (newRow != oldRow) {
|
3340
|
156 |
NSMutableArray *hogs = [teamDictionary objectForKey:@"hedgehogs"];
|
|
157 |
|
|
158 |
for (NSMutableDictionary *hog in hogs) {
|
|
159 |
[hog setObject:[NSNumber numberWithInt:newRow] forKey:@"level"];
|
|
160 |
}
|
3339
|
161 |
|
|
162 |
// tell our boss to write this new stuff on disk
|
|
163 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
|
|
164 |
[self.tableView reloadData];
|
|
165 |
|
|
166 |
self.lastIndexPath = indexPath;
|
|
167 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
|
3340
|
168 |
}
|
3339
|
169 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
170 |
[self.navigationController popViewControllerAnimated:YES];
|
|
171 |
}
|
|
172 |
|
|
173 |
|
|
174 |
#pragma mark -
|
|
175 |
#pragma mark Memory management
|
3340
|
176 |
-(void) didReceiveMemoryWarning {
|
3339
|
177 |
// Releases the view if it doesn't have a superview.
|
|
178 |
[super didReceiveMemoryWarning];
|
|
179 |
// Relinquish ownership any cached data, images, etc that aren't in use.
|
|
180 |
}
|
|
181 |
|
3340
|
182 |
-(void) viewDidUnload {
|
3339
|
183 |
[super viewDidUnload];
|
|
184 |
self.lastIndexPath = nil;
|
|
185 |
self.teamDictionary = nil;
|
|
186 |
self.levelArray = nil;
|
|
187 |
self.levelSprites = nil;
|
|
188 |
}
|
|
189 |
|
3340
|
190 |
-(void) dealloc {
|
3339
|
191 |
[levelArray release];
|
|
192 |
[levelSprites release];
|
|
193 |
[teamDictionary release];
|
|
194 |
[lastIndexPath release];
|
|
195 |
[super dealloc];
|
|
196 |
}
|
|
197 |
|
|
198 |
|
|
199 |
@end
|
|
200 |
|