author | sheepluva |
Tue, 28 Aug 2012 21:59:49 +0200 | |
changeset 7618 | 4d3a32d91028 |
parent 6832 | fae8fd118da9 |
child 8441 | a00b0fa0dbd7 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
6700 | 3 |
* Copyright (c) 2009-2012 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 |
|
5208 | 16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 0211-1307, USA. |
3829 | 17 |
*/ |
18 |
||
3547 | 19 |
|
20 |
#import "LevelViewController.h" |
|
21 |
||
22 |
||
23 |
@implementation LevelViewController |
|
24 |
@synthesize teamDictionary, levelArray, levelSprites, lastIndexPath; |
|
25 |
||
26 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
27 |
return rotationManager(interfaceOrientation); |
|
28 |
} |
|
29 |
||
30 |
#pragma mark - |
|
31 |
#pragma mark View lifecycle |
|
3880 | 32 |
-(void) viewDidLoad { |
3547 | 33 |
[super viewDidLoad]; |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
34 |
srandom(time(NULL)); |
3547 | 35 |
|
36 |
NSArray *array = [[NSArray alloc] initWithObjects: |
|
37 |
NSLocalizedString(@"Brutal",@""), |
|
38 |
NSLocalizedString(@"Aggressive",@""), |
|
39 |
NSLocalizedString(@"Bully",@""), |
|
40 |
NSLocalizedString(@"Average",@""), |
|
41 |
NSLocalizedString(@"Weaky",@""), |
|
42 |
nil]; |
|
43 |
self.levelArray = array; |
|
44 |
[array release]; |
|
3697 | 45 |
|
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
|
46 |
self.title = NSLocalizedString(@"Set difficulty level",@""); |
3547 | 47 |
} |
48 |
||
3880 | 49 |
-(void) viewWillAppear:(BOOL)animated { |
3547 | 50 |
[super viewWillAppear:animated]; |
3880 | 51 |
|
52 |
if ([[[[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0] objectForKey:@"level"] intValue] == 0) |
|
53 |
numberOfSections = 1; |
|
54 |
else |
|
55 |
numberOfSections = 2; |
|
56 |
||
3547 | 57 |
[self.tableView reloadData]; |
58 |
// this moves the tableview to the top |
|
59 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
60 |
} |
|
61 |
||
3880 | 62 |
-(void) viewWillDisappear:(BOOL)animated { |
63 |
// stuff like checking that at least 1 field was selected |
|
64 |
} |
|
65 |
||
3547 | 66 |
#pragma mark - |
67 |
#pragma mark Table view data source |
|
68 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
3880 | 69 |
return numberOfSections; |
3547 | 70 |
} |
71 |
||
3880 | 72 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section { |
73 |
if (section == 0) |
|
74 |
return 1; |
|
75 |
else |
|
76 |
return 5; |
|
3547 | 77 |
} |
78 |
||
79 |
// Customize the appearance of table view cells. |
|
80 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
3880 | 81 |
static NSString *CellIdentifier0 = @"Cell0"; |
82 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
3697 | 83 |
|
3547 | 84 |
NSInteger row = [indexPath row]; |
3880 | 85 |
NSInteger section = [indexPath section]; |
86 |
UITableViewCell *cell; |
|
87 |
||
88 |
if (section == 0) { |
|
89 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
|
90 |
if (cell == nil) { |
|
91 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease]; |
|
92 |
UISwitch *theSwitch = [[UISwitch alloc] init]; |
|
93 |
[theSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged]; |
|
94 |
cell.accessoryView = theSwitch; |
|
95 |
[theSwitch release]; |
|
96 |
} |
|
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
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
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
|
101 |
theSwitch.on = YES; |
3880 | 102 |
cell.textLabel.text = NSLocalizedString(@"Hogs controlled by AI",@""); |
103 |
} else { |
|
104 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
|
105 |
if (cell == nil) |
|
106 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
|
107 |
||
108 |
cell.textLabel.text = [levelArray objectAtIndex:row]; |
|
109 |
NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0]; |
|
110 |
if ([[hog objectForKey:@"level"] intValue] == row+1) { |
|
111 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
112 |
self.lastIndexPath = indexPath; |
|
113 |
} else { |
|
114 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
115 |
} |
|
116 |
||
5982
283be2ca54a7
mad several updates to the resource copying phase in the ios project file, changed paths of some images and added some smaller forts version
koda
parents:
5208
diff
changeset
|
117 |
NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/bot%d.png",[[NSBundle mainBundle] resourcePath],row+1]; |
3880 | 118 |
UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath]; |
119 |
[botlevelPath release]; |
|
120 |
cell.imageView.image = levelImage; |
|
121 |
[levelImage release]; |
|
3547 | 122 |
} |
3880 | 123 |
|
3547 | 124 |
return cell; |
125 |
} |
|
126 |
||
3880 | 127 |
-(void) switchValueChanged:(id) sender { |
128 |
UISwitch *theSwitch = (UISwitch *)sender; |
|
129 |
NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:1]; |
|
130 |
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
|
131 |
NSInteger level; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
132 |
|
3880 | 133 |
if (theSwitch.on) { |
134 |
numberOfSections = 2; |
|
135 |
[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
|
136 |
level = 1 + (random() % ([levelArray count] - 1)); |
3880 | 137 |
} else { |
138 |
numberOfSections = 1; |
|
139 |
[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
|
140 |
level = 0; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
141 |
} |
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
142 |
[sections release]; |
3547 | 143 |
|
3923
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3883
diff
changeset
|
144 |
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
|
145 |
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
|
146 |
[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
|
147 |
|
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
148 |
[self.tableView reloadData]; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
149 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
3547 | 150 |
} |
151 |
||
152 |
||
153 |
#pragma mark - |
|
154 |
#pragma mark Table view delegate |
|
3880 | 155 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
3547 | 156 |
int newRow = [indexPath row]; |
3880 | 157 |
int oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1; |
3697 | 158 |
|
3880 | 159 |
if ([indexPath section] != 0) { |
160 |
if (newRow != oldRow) { |
|
161 |
NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"]; |
|
162 |
||
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
163 |
NSInteger level = newRow + 1; |
3880 | 164 |
for (NSMutableDictionary *hog in hogs) |
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
165 |
[hog setObject:[NSNumber numberWithInt:level] forKey:@"level"]; |
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
166 |
DLog(@"New level is %d",level); |
3880 | 167 |
|
168 |
// tell our boss to write this new stuff on disk |
|
169 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
170 |
[self.tableView reloadData]; |
|
171 |
||
172 |
self.lastIndexPath = indexPath; |
|
173 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3547 | 174 |
} |
175 |
} |
|
176 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
177 |
} |
|
178 |
||
179 |
||
180 |
#pragma mark - |
|
181 |
#pragma mark Memory management |
|
182 |
-(void) didReceiveMemoryWarning { |
|
3971 | 183 |
self.lastIndexPath = nil; |
184 |
MSG_MEMCLEAN(); |
|
3547 | 185 |
[super didReceiveMemoryWarning]; |
186 |
} |
|
187 |
||
188 |
-(void) viewDidUnload { |
|
189 |
self.lastIndexPath = nil; |
|
190 |
self.teamDictionary = nil; |
|
191 |
self.levelArray = nil; |
|
192 |
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
|
193 |
MSG_DIDUNLOAD(); |
3547 | 194 |
[super viewDidUnload]; |
195 |
} |
|
196 |
||
197 |
-(void) dealloc { |
|
5208 | 198 |
releaseAndNil(levelArray); |
199 |
releaseAndNil(levelSprites); |
|
200 |
releaseAndNil(teamDictionary); |
|
201 |
releaseAndNil(lastIndexPath); |
|
3547 | 202 |
[super dealloc]; |
203 |
} |
|
204 |
||
205 |
||
206 |
@end |
|
207 |