author | nemo |
Sat, 05 Nov 2011 20:09:48 -0400 | |
changeset 6294 | 34aa727d4a25 |
parent 6078 | 8c0cc07731e5 |
child 6700 | e04da46ee43c |
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 |
|
5208 | 16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 0211-1307, USA. |
3829 | 17 |
* |
18 |
* File created on 02/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "LevelViewController.h" |
|
23 |
||
24 |
||
25 |
@implementation LevelViewController |
|
26 |
@synthesize teamDictionary, levelArray, levelSprites, lastIndexPath; |
|
27 |
||
28 |
||
29 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
30 |
return rotationManager(interfaceOrientation); |
|
31 |
} |
|
32 |
||
33 |
||
34 |
#pragma mark - |
|
35 |
#pragma mark View lifecycle |
|
3880 | 36 |
-(void) viewDidLoad { |
3547 | 37 |
[super viewDidLoad]; |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
38 |
srandom(time(NULL)); |
3547 | 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 |
[theSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged]; |
|
98 |
cell.accessoryView = theSwitch; |
|
99 |
[theSwitch release]; |
|
100 |
} |
|
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
|
101 |
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
|
102 |
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
|
103 |
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
|
104 |
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
|
105 |
theSwitch.on = YES; |
3880 | 106 |
cell.textLabel.text = NSLocalizedString(@"Hogs controlled by AI",@""); |
107 |
} else { |
|
108 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
|
109 |
if (cell == nil) |
|
110 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
|
111 |
||
112 |
cell.textLabel.text = [levelArray objectAtIndex:row]; |
|
113 |
NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0]; |
|
114 |
if ([[hog objectForKey:@"level"] intValue] == row+1) { |
|
115 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
116 |
self.lastIndexPath = indexPath; |
|
117 |
} else { |
|
118 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
119 |
} |
|
120 |
||
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
|
121 |
NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/bot%d.png",[[NSBundle mainBundle] resourcePath],row+1]; |
3880 | 122 |
UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath]; |
123 |
[botlevelPath release]; |
|
124 |
cell.imageView.image = levelImage; |
|
125 |
[levelImage release]; |
|
3547 | 126 |
} |
3880 | 127 |
|
3547 | 128 |
return cell; |
129 |
} |
|
130 |
||
3880 | 131 |
-(void) switchValueChanged:(id) sender { |
132 |
UISwitch *theSwitch = (UISwitch *)sender; |
|
133 |
NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:1]; |
|
134 |
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
|
135 |
NSInteger level; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
136 |
|
3880 | 137 |
if (theSwitch.on) { |
138 |
numberOfSections = 2; |
|
139 |
[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
|
140 |
level = 1 + (random() % ([levelArray count] - 1)); |
3880 | 141 |
} else { |
142 |
numberOfSections = 1; |
|
143 |
[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
|
144 |
level = 0; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
145 |
} |
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
146 |
[sections release]; |
3547 | 147 |
|
3923
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3883
diff
changeset
|
148 |
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
|
149 |
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
|
150 |
[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
|
151 |
|
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
152 |
[self.tableView reloadData]; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
153 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
3547 | 154 |
} |
155 |
||
156 |
||
157 |
#pragma mark - |
|
158 |
#pragma mark Table view delegate |
|
3880 | 159 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
3547 | 160 |
int newRow = [indexPath row]; |
3880 | 161 |
int oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1; |
3697 | 162 |
|
3880 | 163 |
if ([indexPath section] != 0) { |
164 |
if (newRow != oldRow) { |
|
165 |
NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"]; |
|
166 |
||
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
167 |
NSInteger level = newRow + 1; |
3880 | 168 |
for (NSMutableDictionary *hog in hogs) |
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
169 |
[hog setObject:[NSNumber numberWithInt:level] forKey:@"level"]; |
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
170 |
DLog(@"New level is %d",level); |
3880 | 171 |
|
172 |
// tell our boss to write this new stuff on disk |
|
173 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
174 |
[self.tableView reloadData]; |
|
175 |
||
176 |
self.lastIndexPath = indexPath; |
|
177 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3547 | 178 |
} |
179 |
} |
|
180 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
181 |
} |
|
182 |
||
183 |
||
184 |
#pragma mark - |
|
185 |
#pragma mark Memory management |
|
186 |
-(void) didReceiveMemoryWarning { |
|
3971 | 187 |
self.lastIndexPath = nil; |
188 |
MSG_MEMCLEAN(); |
|
3547 | 189 |
[super didReceiveMemoryWarning]; |
190 |
} |
|
191 |
||
192 |
-(void) viewDidUnload { |
|
193 |
self.lastIndexPath = nil; |
|
194 |
self.teamDictionary = nil; |
|
195 |
self.levelArray = nil; |
|
196 |
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
|
197 |
MSG_DIDUNLOAD(); |
3547 | 198 |
[super viewDidUnload]; |
199 |
} |
|
200 |
||
201 |
-(void) dealloc { |
|
5208 | 202 |
releaseAndNil(levelArray); |
203 |
releaseAndNil(levelSprites); |
|
204 |
releaseAndNil(teamDictionary); |
|
205 |
releaseAndNil(lastIndexPath); |
|
3547 | 206 |
[super dealloc]; |
207 |
} |
|
208 |
||
209 |
||
210 |
@end |
|
211 |