author | koda |
Sun, 13 Mar 2011 03:43:18 +0100 | |
changeset 5002 | a9c44a8ffec8 |
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 13/06/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "SchemeWeaponConfigViewController.h" |
|
23 |
#import "CommodityFunctions.h" |
|
24 |
||
4349 | 25 |
#define LABEL_TAG 57423 |
26 |
||
3547 | 27 |
@implementation SchemeWeaponConfigViewController |
4287 | 28 |
@synthesize listOfSchemes, listOfWeapons, lastIndexPath_sc, lastIndexPath_we, selectedScheme, selectedWeapon, syncSwitch; |
3547 | 29 |
|
30 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
31 |
return rotationManager(interfaceOrientation); |
|
32 |
} |
|
33 |
||
34 |
#pragma mark - |
|
35 |
#pragma mark View lifecycle |
|
36 |
-(void) viewDidLoad { |
|
37 |
[super viewDidLoad]; |
|
38 |
||
39 |
CGSize screenSize = [[UIScreen mainScreen] bounds].size; |
|
40 |
self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44); |
|
3697 | 41 |
|
3971 | 42 |
self.selectedScheme = nil; |
43 |
self.selectedWeapon = nil; |
|
3697 | 44 |
|
4244 | 45 |
if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) { |
46 |
if (IS_IPAD()) |
|
47 |
[self.tableView setBackgroundView:nil]; |
|
48 |
else { |
|
4356 | 49 |
UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:@"background~iphone.png"]; |
4244 | 50 |
UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage]; |
51 |
[backgroundImage release]; |
|
52 |
[self.tableView setBackgroundView:background]; |
|
53 |
[background release]; |
|
54 |
} |
|
55 |
} else { |
|
56 |
self.view.backgroundColor = [UIColor blackColor]; |
|
57 |
} |
|
58 |
||
3917 | 59 |
self.tableView.separatorColor = UICOLOR_HW_YELLOW_BODER; |
3659 | 60 |
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
3547 | 61 |
} |
62 |
||
63 |
-(void) viewWillAppear:(BOOL) animated { |
|
64 |
[super viewWillAppear:animated]; |
|
65 |
||
66 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL]; |
|
67 |
self.listOfSchemes = contentsOfDir; |
|
3697 | 68 |
|
3971 | 69 |
if (self.selectedScheme == nil && [listOfSchemes containsObject:@"Default.plist"]) |
3952
d6412423da45
moved some utilities to a separate column with round buttons
koda
parents:
3917
diff
changeset
|
70 |
self.selectedScheme = @"Default.plist"; |
d6412423da45
moved some utilities to a separate column with round buttons
koda
parents:
3917
diff
changeset
|
71 |
|
3547 | 72 |
contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL]; |
73 |
self.listOfWeapons = contentsOfDir; |
|
3952
d6412423da45
moved some utilities to a separate column with round buttons
koda
parents:
3917
diff
changeset
|
74 |
|
3971 | 75 |
if (self.selectedWeapon == nil && [listOfWeapons containsObject:@"Default.plist"]) |
3952
d6412423da45
moved some utilities to a separate column with round buttons
koda
parents:
3917
diff
changeset
|
76 |
self.selectedWeapon = @"Default.plist"; |
d6412423da45
moved some utilities to a separate column with round buttons
koda
parents:
3917
diff
changeset
|
77 |
|
3547 | 78 |
[self.tableView reloadData]; |
79 |
} |
|
80 |
||
81 |
||
82 |
#pragma mark - |
|
83 |
#pragma mark Table view data source |
|
84 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
4349 | 85 |
if (hideSections) |
86 |
return 0; |
|
87 |
else |
|
88 |
return 3; |
|
3547 | 89 |
} |
90 |
||
91 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
3697 | 92 |
if (section == 0) |
3547 | 93 |
return [self.listOfSchemes count]; |
4287 | 94 |
else if (section == 1) |
95 |
return [self.listOfWeapons count]; |
|
3547 | 96 |
else |
4287 | 97 |
return 1; |
3547 | 98 |
} |
99 |
||
100 |
// Customize the appearance of table view cells. |
|
101 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
102 |
static NSString *CellIdentifier = @"Cell"; |
|
103 |
NSInteger row = [indexPath row]; |
|
4287 | 104 |
NSInteger section = [indexPath section]; |
3697 | 105 |
|
3547 | 106 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3780
diff
changeset
|
107 |
if (cell == nil) |
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3780
diff
changeset
|
108 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; |
3697 | 109 |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
110 |
cell.accessoryView = nil; |
4287 | 111 |
if (0 == section) { |
3547 | 112 |
cell.textLabel.text = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension]; |
3782 | 113 |
NSString *str = [NSString stringWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),[self.listOfSchemes objectAtIndex:row]]; |
114 |
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:str]; |
|
115 |
cell.detailTextLabel.text = [dict objectForKey:@"description"]; |
|
116 |
[dict release]; |
|
3547 | 117 |
if ([[self.listOfSchemes objectAtIndex:row] isEqualToString:self.selectedScheme]) { |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
118 |
UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
119 |
cell.accessoryView = checkbox; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
120 |
[checkbox release]; |
3547 | 121 |
self.lastIndexPath_sc = indexPath; |
122 |
} |
|
4287 | 123 |
} else if (1 == section) { |
3547 | 124 |
cell.textLabel.text = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension]; |
3782 | 125 |
NSString *str = [NSString stringWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]]; |
126 |
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:str]; |
|
127 |
cell.detailTextLabel.text = [dict objectForKey:@"description"]; |
|
128 |
[dict release]; |
|
3547 | 129 |
if ([[self.listOfWeapons objectAtIndex:row] isEqualToString:self.selectedWeapon]) { |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
130 |
UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
131 |
cell.accessoryView = checkbox; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
132 |
[checkbox release]; |
3547 | 133 |
self.lastIndexPath_we = indexPath; |
134 |
} |
|
4287 | 135 |
} else { |
136 |
if (self.syncSwitch == nil) { |
|
137 |
UISwitch *theSwitch = [[UISwitch alloc] init]; |
|
138 |
[theSwitch setOn:YES]; |
|
139 |
self.syncSwitch = theSwitch; |
|
140 |
[theSwitch release]; |
|
141 |
} |
|
142 |
cell.textLabel.text = IS_IPAD() ? NSLocalizedString(@"Sync Schemes",@"") : NSLocalizedString(@"Sync Schemes and Weapons",@""); |
|
143 |
cell.detailTextLabel.text = IS_IPAD() ? nil : NSLocalizedString(@"Choosing a Scheme will select its associated Weapon",@""); |
|
144 |
cell.detailTextLabel.adjustsFontSizeToFitWidth = YES; |
|
145 |
cell.accessoryView = self.syncSwitch; |
|
3547 | 146 |
} |
4287 | 147 |
|
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4356
diff
changeset
|
148 |
cell.backgroundColor = UICOLOR_HW_ALMOSTBLACK; |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
149 |
cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3780
diff
changeset
|
150 |
cell.detailTextLabel.textColor = [UIColor whiteColor]; |
3547 | 151 |
return cell; |
152 |
} |
|
153 |
||
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
154 |
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
155 |
return 40.0; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
156 |
} |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
157 |
|
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
158 |
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { |
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3971
diff
changeset
|
159 |
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30); |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3971
diff
changeset
|
160 |
NSString *text; |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
161 |
if (section == 0) |
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3971
diff
changeset
|
162 |
text = NSLocalizedString(@"Schemes",@""); |
4287 | 163 |
else if (section == 1) |
164 |
text = NSLocalizedString(@"Weapons",@""); |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
165 |
else |
4287 | 166 |
text = NSLocalizedString(@"Options",@""); |
167 |
||
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3971
diff
changeset
|
168 |
UILabel *theLabel = createBlueLabel(text, frame); |
3780
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3703
diff
changeset
|
169 |
theLabel.center = CGPointMake(self.view.frame.size.width/2, 20); |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
170 |
|
3780
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3703
diff
changeset
|
171 |
UIView *theView = [[[UIView alloc] init] autorelease]; |
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3703
diff
changeset
|
172 |
[theView addSubview:theLabel]; |
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3703
diff
changeset
|
173 |
[theLabel release]; |
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3703
diff
changeset
|
174 |
return theView; |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
175 |
} |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
176 |
|
3547 | 177 |
#pragma mark - |
178 |
#pragma mark Table view delegate |
|
179 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
180 |
NSIndexPath *lastIndexPath; |
|
181 |
if ([indexPath section] == 0) |
|
182 |
lastIndexPath = self.lastIndexPath_sc; |
|
183 |
else |
|
184 |
lastIndexPath = self.lastIndexPath_we; |
|
3697 | 185 |
|
3547 | 186 |
int newRow = [indexPath row]; |
187 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 188 |
|
3547 | 189 |
if (newRow != oldRow) { |
190 |
//TODO: this code works only for a single section table |
|
191 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
192 |
UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
193 |
newCell.accessoryView = checkbox; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
194 |
[checkbox release]; |
3547 | 195 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
196 |
oldCell.accessoryView = nil; |
3697 | 197 |
|
3547 | 198 |
if ([indexPath section] == 0) { |
199 |
self.lastIndexPath_sc = indexPath; |
|
200 |
self.selectedScheme = [self.listOfSchemes objectAtIndex:newRow]; |
|
4287 | 201 |
if (self.syncSwitch.on) { |
202 |
for (NSString *str in self.listOfWeapons) { |
|
203 |
if ([str isEqualToString:self.selectedScheme]) { |
|
204 |
int index = [self.listOfSchemes indexOfObject:str]; |
|
205 |
self.selectedWeapon = str; |
|
206 |
self.lastIndexPath_we = [NSIndexPath indexPathForRow:index inSection:1]; |
|
207 |
[self.tableView reloadData]; |
|
208 |
break; |
|
209 |
} |
|
210 |
} |
|
211 |
} |
|
3547 | 212 |
} else { |
213 |
self.lastIndexPath_we = indexPath; |
|
214 |
self.selectedWeapon = [self.listOfWeapons objectAtIndex:newRow]; |
|
3697 | 215 |
} |
216 |
||
3547 | 217 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
218 |
} |
|
219 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
220 |
} |
|
221 |
||
4349 | 222 |
-(void) fillSections { |
223 |
if (hideSections == YES) { |
|
224 |
hideSections = NO; |
|
225 |
NSRange range; |
|
226 |
range.location = 0; |
|
227 |
range.length = 3; |
|
228 |
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range]; |
|
229 |
[self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
|
230 |
self.selectedScheme = @"Default.plist"; |
|
231 |
self.selectedWeapon = @"Default.plist"; |
|
232 |
||
233 |
self.tableView.scrollEnabled = YES; |
|
234 |
||
235 |
[[self.view viewWithTag:LABEL_TAG] removeFromSuperview]; |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
-(void) emptySections { |
|
240 |
hideSections = YES; |
|
241 |
NSRange range; |
|
242 |
range.location = 0; |
|
243 |
range.length = 3; |
|
244 |
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range]; |
|
245 |
[self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
|
246 |
self.selectedScheme = @"Default.plist"; |
|
247 |
self.selectedWeapon = @"Default.plist"; |
|
248 |
||
249 |
self.tableView.scrollEnabled = NO; |
|
250 |
||
251 |
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 60); |
|
252 |
UILabel *theLabel = createBlueLabel(NSLocalizedString(@"Missions don't need further configuration",@""), frame); |
|
253 |
theLabel.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2); |
|
254 |
theLabel.numberOfLines = 2; |
|
255 |
theLabel.tag = LABEL_TAG; |
|
256 |
||
257 |
[self.view addSubview:theLabel]; |
|
258 |
[theLabel release]; |
|
259 |
} |
|
260 |
||
3547 | 261 |
#pragma mark - |
262 |
#pragma mark Memory management |
|
263 |
-(void) didReceiveMemoryWarning { |
|
5002
a9c44a8ffec8
we get our own AppDelegate, subclassing SDL_UikitAppDelegate so that we don't have to mess with sdl build system
koda
parents:
4976
diff
changeset
|
264 |
if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) { |
3971 | 265 |
self.lastIndexPath_sc = nil; |
266 |
self.lastIndexPath_we = nil; |
|
267 |
self.listOfSchemes = nil; |
|
268 |
self.listOfWeapons = nil; |
|
4287 | 269 |
self.syncSwitch = nil; |
3971 | 270 |
MSG_MEMCLEAN(); |
271 |
} |
|
3547 | 272 |
[super didReceiveMemoryWarning]; |
273 |
} |
|
274 |
||
275 |
-(void) viewDidUnload { |
|
276 |
self.listOfSchemes = nil; |
|
277 |
self.listOfWeapons = nil; |
|
278 |
self.lastIndexPath_sc = nil; |
|
279 |
self.lastIndexPath_we = nil; |
|
280 |
self.selectedScheme = nil; |
|
281 |
self.selectedWeapon = nil; |
|
4287 | 282 |
self.syncSwitch = nil; |
3547 | 283 |
MSG_DIDUNLOAD(); |
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3659
diff
changeset
|
284 |
[super viewDidUnload]; |
3547 | 285 |
} |
286 |
||
287 |
||
288 |
-(void) dealloc { |
|
289 |
[listOfSchemes release]; |
|
290 |
[listOfWeapons release]; |
|
291 |
[lastIndexPath_sc release]; |
|
292 |
[lastIndexPath_we release]; |
|
293 |
[selectedScheme release]; |
|
294 |
[selectedWeapon release]; |
|
4287 | 295 |
[syncSwitch release]; |
3547 | 296 |
[super dealloc]; |
297 |
} |
|
298 |
||
299 |
||
300 |
@end |
|
301 |