author | koda |
Mon, 11 Oct 2010 03:28:15 +0200 | |
changeset 3952 | d6412423da45 |
parent 3916 | e7d665a4ef42 |
child 4281 | e033cf015b2c |
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 19/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "WeaponSettingsViewController.h" |
|
23 |
#import "CommodityFunctions.h" |
|
24 |
#import "SingleWeaponViewController.h" |
|
25 |
||
26 |
@implementation WeaponSettingsViewController |
|
27 |
@synthesize listOfWeapons; |
|
28 |
||
29 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
30 |
return rotationManager(interfaceOrientation); |
|
31 |
} |
|
32 |
||
33 |
#pragma mark - |
|
34 |
#pragma mark View lifecycle |
|
35 |
-(void) viewDidLoad { |
|
36 |
[super viewDidLoad]; |
|
3697 | 37 |
|
3916
e7d665a4ef42
implemented endless turns support and added Timeless scheme (also fixed a crasher)
koda
parents:
3829
diff
changeset
|
38 |
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"") |
3547 | 39 |
style:UIBarButtonItemStyleBordered |
40 |
target:self |
|
41 |
action:@selector(toggleEdit:)]; |
|
42 |
self.navigationItem.rightBarButtonItem = editButton; |
|
43 |
[editButton release]; |
|
3697 | 44 |
|
3547 | 45 |
} |
46 |
||
47 |
-(void) viewWillAppear:(BOOL) animated { |
|
48 |
[super viewWillAppear:animated]; |
|
3697 | 49 |
|
3547 | 50 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL]; |
51 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
|
52 |
self.listOfWeapons = array; |
|
53 |
[array release]; |
|
3697 | 54 |
|
3547 | 55 |
[self.tableView reloadData]; |
56 |
} |
|
57 |
||
58 |
// modifies the navigation bar to add the "Add" and "Done" buttons |
|
59 |
-(void) toggleEdit:(id) sender { |
|
60 |
BOOL isEditing = self.tableView.editing; |
|
61 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
3697 | 62 |
|
3547 | 63 |
if (isEditing) { |
64 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")]; |
|
65 |
[self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered]; |
|
66 |
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; |
|
67 |
} else { |
|
68 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the scheme panel")]; |
|
69 |
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; |
|
70 |
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel") |
|
71 |
style:UIBarButtonItemStyleBordered |
|
72 |
target:self |
|
73 |
action:@selector(addWeapon:)]; |
|
74 |
self.navigationItem.leftBarButtonItem = addButton; |
|
75 |
[addButton release]; |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
-(void) addWeapon:(id) sender { |
|
80 |
NSString *fileName = [[NSString alloc] initWithFormat:@"Weapon %u.plist", [self.listOfWeapons count]]; |
|
3697 | 81 |
|
3737
2ba6ac8a114b
reworked the initialization functions, now it should be safe to update and no more need of spinning wheel at first launch
koda
parents:
3697
diff
changeset
|
82 |
createWeaponNamed([fileName stringByDeletingPathExtension], 0); |
3697 | 83 |
|
3547 | 84 |
[self.listOfWeapons addObject:fileName]; |
85 |
[fileName release]; |
|
3697 | 86 |
|
3547 | 87 |
// order the array alphabetically, so schemes will keep their position |
88 |
[self.listOfWeapons sortUsingSelector:@selector(compare:)]; |
|
3697 | 89 |
|
3547 | 90 |
[self.tableView reloadData]; |
91 |
} |
|
92 |
||
93 |
#pragma mark - |
|
94 |
#pragma mark Table view data source |
|
95 |
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
|
96 |
return 1; |
|
97 |
} |
|
98 |
||
99 |
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
100 |
return [self.listOfWeapons count]; |
|
101 |
} |
|
102 |
||
103 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
104 |
static NSString *CellIdentifier = @"Cell"; |
|
3697 | 105 |
|
3547 | 106 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
107 |
if (cell == nil) { |
|
108 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
109 |
} |
|
3697 | 110 |
|
111 |
NSUInteger row = [indexPath row]; |
|
112 |
NSString *rowString = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension]; |
|
113 |
cell.textLabel.text = rowString; |
|
3547 | 114 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3697 | 115 |
|
3547 | 116 |
return cell; |
117 |
} |
|
118 |
||
119 |
// delete the row and the file |
|
120 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
121 |
NSUInteger row = [indexPath row]; |
|
3697 | 122 |
|
3547 | 123 |
NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]]; |
124 |
[[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL]; |
|
125 |
[schemeFile release]; |
|
3697 | 126 |
|
3547 | 127 |
[self.listOfWeapons removeObjectAtIndex:row]; |
128 |
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
|
129 |
} |
|
130 |
||
131 |
#pragma mark - |
|
132 |
#pragma mark Table view delegate |
|
133 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
134 |
if (childController == nil) { |
|
135 |
childController = [[SingleWeaponViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
136 |
} |
|
3697 | 137 |
|
3547 | 138 |
NSInteger row = [indexPath row]; |
139 |
NSString *selectedWeaponFile = [self.listOfWeapons objectAtIndex:row]; |
|
3697 | 140 |
|
3547 | 141 |
// this must be set so childController can load the correct plist |
3659 | 142 |
childController.weaponName = [selectedWeaponFile stringByDeletingPathExtension]; |
3547 | 143 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
144 |
||
145 |
[self.navigationController pushViewController:childController animated:YES]; |
|
146 |
} |
|
147 |
||
148 |
||
149 |
#pragma mark - |
|
150 |
#pragma mark Memory management |
|
151 |
-(void)didReceiveMemoryWarning { |
|
152 |
[super didReceiveMemoryWarning]; |
|
153 |
if (childController.view.superview == nil ) |
|
154 |
childController = nil; |
|
155 |
} |
|
156 |
||
157 |
-(void) viewDidUnload { |
|
158 |
self.listOfWeapons = nil; |
|
159 |
childController = nil; |
|
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
|
160 |
MSG_DIDUNLOAD(); |
3547 | 161 |
[super viewDidUnload]; |
162 |
} |
|
163 |
||
164 |
||
165 |
-(void) dealloc { |
|
166 |
[self.listOfWeapons release]; |
|
167 |
[childController release]; |
|
168 |
[super dealloc]; |
|
169 |
} |
|
170 |
||
171 |
||
172 |
@end |
|
173 |