author | koda |
Tue, 05 Apr 2011 22:21:02 +0200 | |
changeset 5106 | 517bdd3865f1 |
parent 4976 | 088d40d8aba2 |
child 5208 | 878e551f0b4a |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
4976 | 3 |
* Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 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]; |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
39 |
srandom(time(NULL)); |
3547 | 40 |
|
41 |
NSArray *array = [[NSArray alloc] initWithObjects: |
|
42 |
NSLocalizedString(@"Brutal",@""), |
|
43 |
NSLocalizedString(@"Aggressive",@""), |
|
44 |
NSLocalizedString(@"Bully",@""), |
|
45 |
NSLocalizedString(@"Average",@""), |
|
46 |
NSLocalizedString(@"Weaky",@""), |
|
47 |
nil]; |
|
48 |
self.levelArray = array; |
|
49 |
[array release]; |
|
3697 | 50 |
|
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
|
51 |
self.title = NSLocalizedString(@"Set difficulty level",@""); |
3547 | 52 |
} |
53 |
||
3880 | 54 |
-(void) viewWillAppear:(BOOL)animated { |
3547 | 55 |
[super viewWillAppear:animated]; |
3880 | 56 |
|
57 |
if ([[[[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0] objectForKey:@"level"] intValue] == 0) |
|
58 |
numberOfSections = 1; |
|
59 |
else |
|
60 |
numberOfSections = 2; |
|
61 |
||
3547 | 62 |
[self.tableView reloadData]; |
63 |
// this moves the tableview to the top |
|
64 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
65 |
} |
|
66 |
||
3880 | 67 |
-(void) viewWillDisappear:(BOOL)animated { |
68 |
// stuff like checking that at least 1 field was selected |
|
69 |
} |
|
70 |
||
3547 | 71 |
#pragma mark - |
72 |
#pragma mark Table view data source |
|
73 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
3880 | 74 |
return numberOfSections; |
3547 | 75 |
} |
76 |
||
3880 | 77 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section { |
78 |
if (section == 0) |
|
79 |
return 1; |
|
80 |
else |
|
81 |
return 5; |
|
3547 | 82 |
} |
83 |
||
84 |
// Customize the appearance of table view cells. |
|
85 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
3880 | 86 |
static NSString *CellIdentifier0 = @"Cell0"; |
87 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
3697 | 88 |
|
3547 | 89 |
NSInteger row = [indexPath row]; |
3880 | 90 |
NSInteger section = [indexPath section]; |
91 |
UITableViewCell *cell; |
|
92 |
||
93 |
if (section == 0) { |
|
94 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
|
95 |
if (cell == nil) { |
|
96 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease]; |
|
97 |
UISwitch *theSwitch = [[UISwitch alloc] init]; |
|
98 |
[theSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged]; |
|
99 |
cell.accessoryView = theSwitch; |
|
100 |
[theSwitch release]; |
|
101 |
} |
|
4149
51200479f9d8
update ios port with new health modes, increase sd turns, new game modes, fixed bugs that prevented the creation of AI controlled teams, revisited update modes
koda
parents:
3971
diff
changeset
|
102 |
UISwitch *theSwitch = (UISwitch *)cell.accessoryView; |
51200479f9d8
update ios port with new health modes, increase sd turns, new game modes, fixed bugs that prevented the creation of AI controlled teams, revisited update modes
koda
parents:
3971
diff
changeset
|
103 |
if (numberOfSections == 1) |
51200479f9d8
update ios port with new health modes, increase sd turns, new game modes, fixed bugs that prevented the creation of AI controlled teams, revisited update modes
koda
parents:
3971
diff
changeset
|
104 |
theSwitch.on = NO; |
51200479f9d8
update ios port with new health modes, increase sd turns, new game modes, fixed bugs that prevented the creation of AI controlled teams, revisited update modes
koda
parents:
3971
diff
changeset
|
105 |
else |
51200479f9d8
update ios port with new health modes, increase sd turns, new game modes, fixed bugs that prevented the creation of AI controlled teams, revisited update modes
koda
parents:
3971
diff
changeset
|
106 |
theSwitch.on = YES; |
3880 | 107 |
cell.textLabel.text = NSLocalizedString(@"Hogs controlled by AI",@""); |
108 |
} else { |
|
109 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
|
110 |
if (cell == nil) |
|
111 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
|
112 |
||
113 |
cell.textLabel.text = [levelArray objectAtIndex:row]; |
|
114 |
NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0]; |
|
115 |
if ([[hog objectForKey:@"level"] intValue] == row+1) { |
|
116 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
117 |
self.lastIndexPath = indexPath; |
|
118 |
} else { |
|
119 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
120 |
} |
|
121 |
||
122 |
NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/%d.png",BOTLEVELS_DIRECTORY(),row+1]; |
|
123 |
UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath]; |
|
124 |
[botlevelPath release]; |
|
125 |
cell.imageView.image = levelImage; |
|
126 |
[levelImage release]; |
|
3547 | 127 |
} |
3880 | 128 |
|
3547 | 129 |
return cell; |
130 |
} |
|
131 |
||
3880 | 132 |
-(void) switchValueChanged:(id) sender { |
133 |
UISwitch *theSwitch = (UISwitch *)sender; |
|
134 |
NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:1]; |
|
135 |
NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"]; |
|
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
136 |
NSInteger level; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
137 |
|
3880 | 138 |
if (theSwitch.on) { |
139 |
numberOfSections = 2; |
|
140 |
[self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
|
4149
51200479f9d8
update ios port with new health modes, increase sd turns, new game modes, fixed bugs that prevented the creation of AI controlled teams, revisited update modes
koda
parents:
3971
diff
changeset
|
141 |
level = 1 + (random() % ([levelArray count] - 1)); |
3880 | 142 |
} else { |
143 |
numberOfSections = 1; |
|
144 |
[self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
|
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
145 |
level = 0; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
146 |
} |
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
147 |
[sections release]; |
3547 | 148 |
|
3923
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3883
diff
changeset
|
149 |
DLog(@"New level is %d",level); |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
150 |
for (NSMutableDictionary *hog in hogs) |
4149
51200479f9d8
update ios port with new health modes, increase sd turns, new game modes, fixed bugs that prevented the creation of AI controlled teams, revisited update modes
koda
parents:
3971
diff
changeset
|
151 |
[hog setObject:[NSNumber numberWithInt:level] forKey:@"level"]; |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
152 |
|
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
153 |
[self.tableView reloadData]; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
154 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
3547 | 155 |
} |
156 |
||
157 |
||
158 |
#pragma mark - |
|
159 |
#pragma mark Table view delegate |
|
3880 | 160 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
3547 | 161 |
int newRow = [indexPath row]; |
3880 | 162 |
int oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1; |
3697 | 163 |
|
3880 | 164 |
if ([indexPath section] != 0) { |
165 |
if (newRow != oldRow) { |
|
166 |
NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"]; |
|
167 |
||
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
168 |
NSInteger level = newRow + 1; |
3880 | 169 |
for (NSMutableDictionary *hog in hogs) |
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
170 |
[hog setObject:[NSNumber numberWithInt:level] forKey:@"level"]; |
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
171 |
DLog(@"New level is %d",level); |
3880 | 172 |
|
173 |
// tell our boss to write this new stuff on disk |
|
174 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
175 |
[self.tableView reloadData]; |
|
176 |
||
177 |
self.lastIndexPath = indexPath; |
|
178 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3547 | 179 |
} |
180 |
} |
|
181 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
182 |
} |
|
183 |
||
184 |
||
185 |
#pragma mark - |
|
186 |
#pragma mark Memory management |
|
187 |
-(void) didReceiveMemoryWarning { |
|
3971 | 188 |
self.lastIndexPath = nil; |
189 |
MSG_MEMCLEAN(); |
|
3547 | 190 |
[super didReceiveMemoryWarning]; |
191 |
} |
|
192 |
||
193 |
-(void) viewDidUnload { |
|
194 |
self.lastIndexPath = nil; |
|
195 |
self.teamDictionary = nil; |
|
196 |
self.levelArray = nil; |
|
197 |
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
|
198 |
MSG_DIDUNLOAD(); |
3547 | 199 |
[super viewDidUnload]; |
200 |
} |
|
201 |
||
202 |
-(void) dealloc { |
|
203 |
[levelArray release]; |
|
204 |
[levelSprites release]; |
|
205 |
[teamDictionary release]; |
|
206 |
[lastIndexPath release]; |
|
207 |
[super dealloc]; |
|
208 |
} |
|
209 |
||
210 |
||
211 |
@end |
|
212 |