author | antonc27 <antonc27@mail.ru> |
Wed, 14 Oct 2015 21:25:49 +0200 | |
branch | ios-revival |
changeset 11206 | 2e80c9861818 |
parent 11148 | 064a53861759 |
child 11217 | e68b3e392091 |
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 |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
8441
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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]; |
34 |
||
35 |
NSArray *array = [[NSArray alloc] initWithObjects: |
|
36 |
NSLocalizedString(@"Brutal",@""), |
|
37 |
NSLocalizedString(@"Aggressive",@""), |
|
38 |
NSLocalizedString(@"Bully",@""), |
|
39 |
NSLocalizedString(@"Average",@""), |
|
40 |
NSLocalizedString(@"Weaky",@""), |
|
41 |
nil]; |
|
42 |
self.levelArray = array; |
|
43 |
[array release]; |
|
3697 | 44 |
|
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
|
45 |
self.title = NSLocalizedString(@"Set difficulty level",@""); |
3547 | 46 |
} |
47 |
||
3880 | 48 |
-(void) viewWillAppear:(BOOL)animated { |
3547 | 49 |
[super viewWillAppear:animated]; |
8441 | 50 |
|
3880 | 51 |
if ([[[[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0] objectForKey:@"level"] intValue] == 0) |
52 |
numberOfSections = 1; |
|
53 |
else |
|
54 |
numberOfSections = 2; |
|
8441 | 55 |
|
3547 | 56 |
[self.tableView reloadData]; |
57 |
// this moves the tableview to the top |
|
58 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
59 |
} |
|
60 |
||
3880 | 61 |
-(void) viewWillDisappear:(BOOL)animated { |
8441 | 62 |
// stuff like checking that at least 1 field was selected |
3880 | 63 |
} |
64 |
||
3547 | 65 |
#pragma mark - |
66 |
#pragma mark Table view data source |
|
67 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
3880 | 68 |
return numberOfSections; |
3547 | 69 |
} |
70 |
||
3880 | 71 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section { |
72 |
if (section == 0) |
|
73 |
return 1; |
|
74 |
else |
|
75 |
return 5; |
|
3547 | 76 |
} |
77 |
||
78 |
// Customize the appearance of table view cells. |
|
79 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
3880 | 80 |
static NSString *CellIdentifier0 = @"Cell0"; |
81 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
3697 | 82 |
|
3547 | 83 |
NSInteger row = [indexPath row]; |
3880 | 84 |
NSInteger section = [indexPath section]; |
85 |
UITableViewCell *cell; |
|
8441 | 86 |
|
3880 | 87 |
if (section == 0) { |
88 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
|
89 |
if (cell == nil) { |
|
90 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease]; |
|
91 |
UISwitch *theSwitch = [[UISwitch alloc] init]; |
|
92 |
[theSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged]; |
|
93 |
cell.accessoryView = theSwitch; |
|
94 |
[theSwitch release]; |
|
95 |
} |
|
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
|
96 |
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
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
theSwitch.on = YES; |
3880 | 101 |
cell.textLabel.text = NSLocalizedString(@"Hogs controlled by AI",@""); |
102 |
} else { |
|
103 |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
|
104 |
if (cell == nil) |
|
105 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
|
8441 | 106 |
|
3880 | 107 |
cell.textLabel.text = [levelArray objectAtIndex:row]; |
108 |
NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0]; |
|
109 |
if ([[hog objectForKey:@"level"] intValue] == row+1) { |
|
110 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
111 |
self.lastIndexPath = indexPath; |
|
112 |
} else { |
|
113 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
114 |
} |
|
8441 | 115 |
|
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
|
116 |
NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/bot%d.png",[[NSBundle mainBundle] resourcePath],row+1]; |
3880 | 117 |
UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath]; |
118 |
[botlevelPath release]; |
|
119 |
cell.imageView.image = levelImage; |
|
120 |
[levelImage release]; |
|
3547 | 121 |
} |
8441 | 122 |
|
3547 | 123 |
return cell; |
124 |
} |
|
125 |
||
3880 | 126 |
-(void) switchValueChanged:(id) sender { |
127 |
UISwitch *theSwitch = (UISwitch *)sender; |
|
128 |
NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:1]; |
|
129 |
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
|
130 |
NSInteger level; |
8441 | 131 |
|
3880 | 132 |
if (theSwitch.on) { |
133 |
numberOfSections = 2; |
|
134 |
[self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
|
11206
2e80c9861818
- Better random number generation for front-end: random() replaced with arc4random_uniform()
antonc27 <antonc27@mail.ru>
parents:
11148
diff
changeset
|
135 |
level = 1 + arc4random_uniform((int)[levelArray count] - 1); |
3880 | 136 |
} else { |
137 |
numberOfSections = 1; |
|
138 |
[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
|
139 |
level = 0; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
140 |
} |
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
141 |
[sections release]; |
3547 | 142 |
|
11148
064a53861759
- Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
143 |
DLog(@"New level is %ld", (long)level); |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
144 |
for (NSMutableDictionary *hog in hogs) |
11148
064a53861759
- Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
145 |
[hog setObject:[NSNumber numberWithInteger:level] forKey:@"level"]; |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
146 |
|
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
147 |
[self.tableView reloadData]; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3880
diff
changeset
|
148 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
3547 | 149 |
} |
150 |
||
151 |
||
152 |
#pragma mark - |
|
153 |
#pragma mark Table view delegate |
|
3880 | 154 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
11148
064a53861759
- Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
155 |
NSInteger newRow = [indexPath row]; |
064a53861759
- Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
156 |
NSInteger oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1; |
3697 | 157 |
|
8441 | 158 |
if ([indexPath section] != 0) { |
3880 | 159 |
if (newRow != oldRow) { |
160 |
NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"]; |
|
8441 | 161 |
|
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4149
diff
changeset
|
162 |
NSInteger level = newRow + 1; |
3880 | 163 |
for (NSMutableDictionary *hog in hogs) |
11148
064a53861759
- Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
164 |
[hog setObject:[NSNumber numberWithInteger:level] forKey:@"level"]; |
064a53861759
- Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
165 |
DLog(@"New level is %ld", (long)level); |
8441 | 166 |
|
3880 | 167 |
// tell our boss to write this new stuff on disk |
168 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
169 |
[self.tableView reloadData]; |
|
8441 | 170 |
|
3880 | 171 |
self.lastIndexPath = indexPath; |
172 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3547 | 173 |
} |
174 |
} |
|
175 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
176 |
} |
|
177 |
||
178 |
||
179 |
#pragma mark - |
|
180 |
#pragma mark Memory management |
|
181 |
-(void) didReceiveMemoryWarning { |
|
3971 | 182 |
self.lastIndexPath = nil; |
183 |
MSG_MEMCLEAN(); |
|
3547 | 184 |
[super didReceiveMemoryWarning]; |
185 |
} |
|
186 |
||
187 |
-(void) viewDidUnload { |
|
188 |
self.lastIndexPath = nil; |
|
189 |
self.teamDictionary = nil; |
|
190 |
self.levelArray = nil; |
|
191 |
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
|
192 |
MSG_DIDUNLOAD(); |
3547 | 193 |
[super viewDidUnload]; |
194 |
} |
|
195 |
||
196 |
-(void) dealloc { |
|
5208 | 197 |
releaseAndNil(levelArray); |
198 |
releaseAndNil(levelSprites); |
|
199 |
releaseAndNil(teamDictionary); |
|
200 |
releaseAndNil(lastIndexPath); |
|
3547 | 201 |
[super dealloc]; |
202 |
} |
|
203 |
||
204 |
||
205 |
@end |
|
206 |