author | koda |
Sun, 19 Sep 2010 02:25:31 +0200 | |
changeset 3880 | 24810907e853 |
parent 3829 | 81db3c85784b |
child 3883 | 5934ddf9ed5d |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 |
* Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
* |
|
18 |
* File created on 02/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "LevelViewController.h" |
|
23 |
#import "CommodityFunctions.h" |
|
24 |
||
25 |
||
26 |
@implementation LevelViewController |
|
27 |
@synthesize teamDictionary, levelArray, levelSprites, lastIndexPath; |
|
28 |
||
29 |
||
30 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
31 |
return rotationManager(interfaceOrientation); |
|
32 |
} |
|
33 |
||
34 |
||
35 |
#pragma mark - |
|
36 |
#pragma mark View lifecycle |
|
3880 | 37 |
-(void) viewDidLoad { |
3547 | 38 |
[super viewDidLoad]; |
39 |
||
40 |
NSArray *array = [[NSArray alloc] initWithObjects: |
|
41 |
NSLocalizedString(@"Brutal",@""), |
|
42 |
NSLocalizedString(@"Aggressive",@""), |
|
43 |
NSLocalizedString(@"Bully",@""), |
|
44 |
NSLocalizedString(@"Average",@""), |
|
45 |
NSLocalizedString(@"Weaky",@""), |
|
46 |
nil]; |
|
47 |
self.levelArray = array; |
|
48 |
[array release]; |
|
3697 | 49 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3547
diff
changeset
|
50 |
self.title = NSLocalizedString(@"Set difficulty level",@""); |
3547 | 51 |
} |
52 |
||
3880 | 53 |
-(void) viewWillAppear:(BOOL)animated { |
3547 | 54 |
[super viewWillAppear:animated]; |
3880 | 55 |
|
56 |
if ([[[[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0] objectForKey:@"level"] intValue] == 0) |
|
57 |
numberOfSections = 1; |
|
58 |
else |
|
59 |
numberOfSections = 2; |
|
60 |
||
3547 | 61 |
[self.tableView reloadData]; |
62 |
// this moves the tableview to the top |
|
63 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
64 |
} |
|
65 |
||
3880 | 66 |
-(void) viewWillDisappear:(BOOL)animated { |
67 |
// stuff like checking that at least 1 field was selected |
|
68 |
} |
|
69 |
||
3547 | 70 |
#pragma mark - |
71 |
#pragma mark Table view data source |
|
72 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
3880 | 73 |
return numberOfSections; |
3547 | 74 |
} |
75 |
||
3880 | 76 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section { |
77 |
if (section == 0) |
|
78 |
return 1; |
|
79 |
else |
|
80 |
return 5; |
|
3547 | 81 |
} |
82 |
||
83 |
// Customize the appearance of table view cells. |
|
84 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
3880 | 85 |
static NSString *CellIdentifier0 = @"Cell0"; |
86 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
3697 | 87 |
|
3547 | 88 |
NSInteger row = [indexPath row]; |
3880 | 89 |
NSInteger section = [indexPath section]; |
90 |
UITableViewCell *cell; |
|
91 |
||
92 |
if (section == 0) { |
|
93 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
|
94 |
if (cell == nil) { |
|
95 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease]; |
|
96 |
UISwitch *theSwitch = [[UISwitch alloc] init]; |
|
97 |
if (numberOfSections == 1) |
|
98 |
theSwitch.on = NO; |
|
99 |
else |
|
100 |
theSwitch.on = YES; |
|
101 |
[theSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged]; |
|
102 |
cell.accessoryView = theSwitch; |
|
103 |
[theSwitch release]; |
|
104 |
} |
|
105 |
cell.textLabel.text = NSLocalizedString(@"Hogs controlled by AI",@""); |
|
106 |
} else { |
|
107 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
|
108 |
if (cell == nil) |
|
109 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
|
110 |
||
111 |
cell.textLabel.text = [levelArray objectAtIndex:row]; |
|
112 |
NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0]; |
|
113 |
if ([[hog objectForKey:@"level"] intValue] == row+1) { |
|
114 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
115 |
self.lastIndexPath = indexPath; |
|
116 |
} else { |
|
117 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
118 |
} |
|
119 |
||
120 |
NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/%d.png",BOTLEVELS_DIRECTORY(),row+1]; |
|
121 |
UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath]; |
|
122 |
[botlevelPath release]; |
|
123 |
cell.imageView.image = levelImage; |
|
124 |
[levelImage release]; |
|
3547 | 125 |
} |
3880 | 126 |
|
3547 | 127 |
return cell; |
128 |
} |
|
129 |
||
3880 | 130 |
-(void) switchValueChanged:(id) sender { |
131 |
UISwitch *theSwitch = (UISwitch *)sender; |
|
132 |
NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:1]; |
|
133 |
NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"]; |
|
3547 | 134 |
|
3880 | 135 |
if (theSwitch.on) { |
136 |
numberOfSections = 2; |
|
137 |
[self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
|
138 |
for (NSMutableDictionary *hog in hogs) |
|
139 |
[hog setObject:[NSNumber numberWithInt:4] forKey:@"level"]; |
|
140 |
||
141 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
142 |
[self.tableView reloadData]; |
|
143 |
} else { |
|
144 |
numberOfSections = 1; |
|
145 |
[self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
|
146 |
||
147 |
for (NSMutableDictionary *hog in hogs) |
|
148 |
[hog setObject:[NSNumber numberWithInt:0] forKey:@"level"]; |
|
3547 | 149 |
|
3880 | 150 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
3697 | 151 |
} |
3880 | 152 |
[sections release]; |
3547 | 153 |
} |
154 |
||
155 |
||
156 |
#pragma mark - |
|
157 |
#pragma mark Table view delegate |
|
3880 | 158 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
3547 | 159 |
int newRow = [indexPath row]; |
3880 | 160 |
int oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1; |
3697 | 161 |
|
3880 | 162 |
if ([indexPath section] != 0) { |
163 |
if (newRow != oldRow) { |
|
164 |
NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"]; |
|
165 |
||
166 |
for (NSMutableDictionary *hog in hogs) |
|
167 |
[hog setObject:[NSNumber numberWithInt:newRow+1] forKey:@"level"]; |
|
168 |
||
169 |
// tell our boss to write this new stuff on disk |
|
170 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
171 |
[self.tableView reloadData]; |
|
172 |
||
173 |
self.lastIndexPath = indexPath; |
|
174 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3547 | 175 |
} |
3880 | 176 |
[self.navigationController popViewControllerAnimated:YES]; |
3547 | 177 |
} |
178 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
179 |
} |
|
180 |
||
181 |
||
182 |
#pragma mark - |
|
183 |
#pragma mark Memory management |
|
184 |
-(void) didReceiveMemoryWarning { |
|
185 |
// Releases the view if it doesn't have a superview. |
|
186 |
[super didReceiveMemoryWarning]; |
|
187 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
188 |
} |
|
189 |
||
190 |
-(void) viewDidUnload { |
|
191 |
self.lastIndexPath = nil; |
|
192 |
self.teamDictionary = nil; |
|
193 |
self.levelArray = nil; |
|
194 |
self.levelSprites = nil; |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3547
diff
changeset
|
195 |
MSG_DIDUNLOAD(); |
3547 | 196 |
[super viewDidUnload]; |
197 |
} |
|
198 |
||
199 |
-(void) dealloc { |
|
200 |
[levelArray release]; |
|
201 |
[levelSprites release]; |
|
202 |
[teamDictionary release]; |
|
203 |
[lastIndexPath release]; |
|
204 |
[super dealloc]; |
|
205 |
} |
|
206 |
||
207 |
||
208 |
@end |
|
209 |