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