author | koda |
Sun, 24 Jul 2011 19:14:56 +0200 | |
changeset 5451 | e359a79e3d08 |
parent 5208 | 878e551f0b4a |
child 5455 | df05cdb998ed |
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 |
5451
e359a79e3d08
rip out the sync weapons/schemes switch and place it into settings; also perform some runtime check on what can be enabled
koda
parents:
5208
diff
changeset
|
28 |
@synthesize listOfSchemes, listOfWeapons, lastIndexPath_sc, lastIndexPath_we, selectedScheme, selectedWeapon; |
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 |
|
5451
e359a79e3d08
rip out the sync weapons/schemes switch and place it into settings; also perform some runtime check on what can be enabled
koda
parents:
5208
diff
changeset
|
88 |
return 2; |
3547 | 89 |
} |
90 |
||
91 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
3697 | 92 |
if (section == 0) |
3547 | 93 |
return [self.listOfSchemes count]; |
5451
e359a79e3d08
rip out the sync weapons/schemes switch and place it into settings; also perform some runtime check on what can be enabled
koda
parents:
5208
diff
changeset
|
94 |
else |
4287 | 95 |
return [self.listOfWeapons count]; |
3547 | 96 |
} |
97 |
||
98 |
// Customize the appearance of table view cells. |
|
99 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
100 |
static NSString *CellIdentifier = @"Cell"; |
|
101 |
NSInteger row = [indexPath row]; |
|
4287 | 102 |
NSInteger section = [indexPath section]; |
3697 | 103 |
|
3547 | 104 |
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
|
105 |
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
|
106 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; |
3697 | 107 |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
108 |
cell.accessoryView = nil; |
4287 | 109 |
if (0 == section) { |
3547 | 110 |
cell.textLabel.text = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension]; |
3782 | 111 |
NSString *str = [NSString stringWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),[self.listOfSchemes objectAtIndex:row]]; |
112 |
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:str]; |
|
113 |
cell.detailTextLabel.text = [dict objectForKey:@"description"]; |
|
114 |
[dict release]; |
|
3547 | 115 |
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
|
116 |
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
|
117 |
cell.accessoryView = checkbox; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
118 |
[checkbox release]; |
3547 | 119 |
self.lastIndexPath_sc = indexPath; |
120 |
} |
|
5451
e359a79e3d08
rip out the sync weapons/schemes switch and place it into settings; also perform some runtime check on what can be enabled
koda
parents:
5208
diff
changeset
|
121 |
} else { |
3547 | 122 |
cell.textLabel.text = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension]; |
3782 | 123 |
NSString *str = [NSString stringWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]]; |
124 |
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:str]; |
|
125 |
cell.detailTextLabel.text = [dict objectForKey:@"description"]; |
|
126 |
[dict release]; |
|
3547 | 127 |
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
|
128 |
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
|
129 |
cell.accessoryView = checkbox; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
130 |
[checkbox release]; |
3547 | 131 |
self.lastIndexPath_we = indexPath; |
132 |
} |
|
133 |
} |
|
4287 | 134 |
|
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4356
diff
changeset
|
135 |
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
|
136 |
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
|
137 |
cell.detailTextLabel.textColor = [UIColor whiteColor]; |
3547 | 138 |
return cell; |
139 |
} |
|
140 |
||
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
141 |
-(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
|
142 |
return 40.0; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
143 |
} |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
144 |
|
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
145 |
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { |
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3971
diff
changeset
|
146 |
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
|
147 |
NSString *text; |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
148 |
if (section == 0) |
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3971
diff
changeset
|
149 |
text = NSLocalizedString(@"Schemes",@""); |
5451
e359a79e3d08
rip out the sync weapons/schemes switch and place it into settings; also perform some runtime check on what can be enabled
koda
parents:
5208
diff
changeset
|
150 |
else |
4287 | 151 |
text = NSLocalizedString(@"Weapons",@""); |
152 |
||
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3971
diff
changeset
|
153 |
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
|
154 |
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
|
155 |
|
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
|
156 |
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
|
157 |
[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
|
158 |
[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
|
159 |
return theView; |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
160 |
} |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
161 |
|
3547 | 162 |
#pragma mark - |
163 |
#pragma mark Table view delegate |
|
164 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
165 |
NSIndexPath *lastIndexPath; |
|
166 |
if ([indexPath section] == 0) |
|
167 |
lastIndexPath = self.lastIndexPath_sc; |
|
168 |
else |
|
169 |
lastIndexPath = self.lastIndexPath_we; |
|
3697 | 170 |
|
3547 | 171 |
int newRow = [indexPath row]; |
172 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 173 |
|
3547 | 174 |
if (newRow != oldRow) { |
175 |
//TODO: this code works only for a single section table |
|
176 |
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
|
177 |
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
|
178 |
newCell.accessoryView = checkbox; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
179 |
[checkbox release]; |
3547 | 180 |
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
|
181 |
oldCell.accessoryView = nil; |
3697 | 182 |
|
3547 | 183 |
if ([indexPath section] == 0) { |
184 |
self.lastIndexPath_sc = indexPath; |
|
185 |
self.selectedScheme = [self.listOfSchemes objectAtIndex:newRow]; |
|
5451
e359a79e3d08
rip out the sync weapons/schemes switch and place it into settings; also perform some runtime check on what can be enabled
koda
parents:
5208
diff
changeset
|
186 |
|
e359a79e3d08
rip out the sync weapons/schemes switch and place it into settings; also perform some runtime check on what can be enabled
koda
parents:
5208
diff
changeset
|
187 |
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults]; |
e359a79e3d08
rip out the sync weapons/schemes switch and place it into settings; also perform some runtime check on what can be enabled
koda
parents:
5208
diff
changeset
|
188 |
if ([[settings objectForKey:@"sync_ws"] boolValue]) { |
4287 | 189 |
for (NSString *str in self.listOfWeapons) { |
190 |
if ([str isEqualToString:self.selectedScheme]) { |
|
191 |
int index = [self.listOfSchemes indexOfObject:str]; |
|
192 |
self.selectedWeapon = str; |
|
193 |
self.lastIndexPath_we = [NSIndexPath indexPathForRow:index inSection:1]; |
|
194 |
[self.tableView reloadData]; |
|
195 |
break; |
|
196 |
} |
|
197 |
} |
|
198 |
} |
|
3547 | 199 |
} else { |
200 |
self.lastIndexPath_we = indexPath; |
|
201 |
self.selectedWeapon = [self.listOfWeapons objectAtIndex:newRow]; |
|
3697 | 202 |
} |
203 |
||
3547 | 204 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
205 |
} |
|
206 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
207 |
} |
|
208 |
||
4349 | 209 |
-(void) fillSections { |
210 |
if (hideSections == YES) { |
|
211 |
hideSections = NO; |
|
212 |
NSRange range; |
|
213 |
range.location = 0; |
|
5451
e359a79e3d08
rip out the sync weapons/schemes switch and place it into settings; also perform some runtime check on what can be enabled
koda
parents:
5208
diff
changeset
|
214 |
range.length = 2; |
4349 | 215 |
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range]; |
216 |
[self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
|
217 |
self.selectedScheme = @"Default.plist"; |
|
218 |
self.selectedWeapon = @"Default.plist"; |
|
219 |
||
220 |
self.tableView.scrollEnabled = YES; |
|
221 |
||
222 |
[[self.view viewWithTag:LABEL_TAG] removeFromSuperview]; |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
-(void) emptySections { |
|
227 |
hideSections = YES; |
|
228 |
NSRange range; |
|
229 |
range.location = 0; |
|
5451
e359a79e3d08
rip out the sync weapons/schemes switch and place it into settings; also perform some runtime check on what can be enabled
koda
parents:
5208
diff
changeset
|
230 |
range.length = 2; |
4349 | 231 |
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range]; |
232 |
[self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
|
233 |
self.selectedScheme = @"Default.plist"; |
|
234 |
self.selectedWeapon = @"Default.plist"; |
|
235 |
||
236 |
self.tableView.scrollEnabled = NO; |
|
237 |
||
238 |
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 60); |
|
239 |
UILabel *theLabel = createBlueLabel(NSLocalizedString(@"Missions don't need further configuration",@""), frame); |
|
240 |
theLabel.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2); |
|
241 |
theLabel.numberOfLines = 2; |
|
242 |
theLabel.tag = LABEL_TAG; |
|
243 |
||
244 |
[self.view addSubview:theLabel]; |
|
245 |
[theLabel release]; |
|
246 |
} |
|
247 |
||
3547 | 248 |
#pragma mark - |
249 |
#pragma mark Memory management |
|
250 |
-(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
|
251 |
if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) { |
3971 | 252 |
self.lastIndexPath_sc = nil; |
253 |
self.lastIndexPath_we = nil; |
|
254 |
self.listOfSchemes = nil; |
|
255 |
self.listOfWeapons = nil; |
|
256 |
MSG_MEMCLEAN(); |
|
257 |
} |
|
3547 | 258 |
[super didReceiveMemoryWarning]; |
259 |
} |
|
260 |
||
261 |
-(void) viewDidUnload { |
|
262 |
self.listOfSchemes = nil; |
|
263 |
self.listOfWeapons = nil; |
|
264 |
self.lastIndexPath_sc = nil; |
|
265 |
self.lastIndexPath_we = nil; |
|
266 |
self.selectedScheme = nil; |
|
267 |
self.selectedWeapon = nil; |
|
268 |
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
|
269 |
[super viewDidUnload]; |
3547 | 270 |
} |
271 |
||
272 |
||
273 |
-(void) dealloc { |
|
5208 | 274 |
releaseAndNil(listOfSchemes); |
275 |
releaseAndNil(listOfWeapons); |
|
276 |
releaseAndNil(lastIndexPath_sc); |
|
277 |
releaseAndNil(lastIndexPath_we); |
|
278 |
releaseAndNil(selectedScheme); |
|
279 |
releaseAndNil(selectedWeapon); |
|
3547 | 280 |
[super dealloc]; |
281 |
} |
|
282 |
||
283 |
||
284 |
@end |
|
285 |