author | koda |
Wed, 22 Dec 2010 03:56:12 +0100 | |
changeset 4607 | 7f683c2357a0 |
parent 4287 | 7dbdc862097c |
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 19/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "WeaponSettingsViewController.h" |
|
4281 | 23 |
#import "CreationChamber.h" |
3547 | 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]; |
3697 | 85 |
|
3547 | 86 |
// order the array alphabetically, so schemes will keep their position |
87 |
[self.listOfWeapons sortUsingSelector:@selector(compare:)]; |
|
4287 | 88 |
[self.tableView reloadData]; |
3697 | 89 |
|
4287 | 90 |
NSInteger index = [self.listOfWeapons indexOfObject:fileName]; |
91 |
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; |
|
92 |
[fileName release]; |
|
3547 | 93 |
} |
94 |
||
95 |
#pragma mark - |
|
96 |
#pragma mark Table view data source |
|
97 |
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
|
98 |
return 1; |
|
99 |
} |
|
100 |
||
101 |
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
102 |
return [self.listOfWeapons count]; |
|
103 |
} |
|
104 |
||
105 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
106 |
static NSString *CellIdentifier = @"Cell"; |
|
3697 | 107 |
|
3547 | 108 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
109 |
if (cell == nil) { |
|
110 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
111 |
} |
|
3697 | 112 |
|
113 |
NSUInteger row = [indexPath row]; |
|
114 |
NSString *rowString = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension]; |
|
115 |
cell.textLabel.text = rowString; |
|
3547 | 116 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3697 | 117 |
|
3547 | 118 |
return cell; |
119 |
} |
|
120 |
||
121 |
// delete the row and the file |
|
122 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
123 |
NSUInteger row = [indexPath row]; |
|
3697 | 124 |
|
3547 | 125 |
NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]]; |
126 |
[[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL]; |
|
127 |
[schemeFile release]; |
|
3697 | 128 |
|
3547 | 129 |
[self.listOfWeapons removeObjectAtIndex:row]; |
130 |
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
|
131 |
} |
|
132 |
||
133 |
#pragma mark - |
|
134 |
#pragma mark Table view delegate |
|
135 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
136 |
if (childController == nil) { |
|
137 |
childController = [[SingleWeaponViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
138 |
} |
|
3697 | 139 |
|
3547 | 140 |
NSInteger row = [indexPath row]; |
141 |
NSString *selectedWeaponFile = [self.listOfWeapons objectAtIndex:row]; |
|
3697 | 142 |
|
3547 | 143 |
// this must be set so childController can load the correct plist |
3659 | 144 |
childController.weaponName = [selectedWeaponFile stringByDeletingPathExtension]; |
3547 | 145 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
146 |
||
147 |
[self.navigationController pushViewController:childController animated:YES]; |
|
148 |
} |
|
149 |
||
150 |
||
151 |
#pragma mark - |
|
152 |
#pragma mark Memory management |
|
153 |
-(void)didReceiveMemoryWarning { |
|
154 |
[super didReceiveMemoryWarning]; |
|
155 |
if (childController.view.superview == nil ) |
|
156 |
childController = nil; |
|
157 |
} |
|
158 |
||
159 |
-(void) viewDidUnload { |
|
160 |
self.listOfWeapons = nil; |
|
161 |
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
|
162 |
MSG_DIDUNLOAD(); |
3547 | 163 |
[super viewDidUnload]; |
164 |
} |
|
165 |
||
166 |
||
167 |
-(void) dealloc { |
|
168 |
[self.listOfWeapons release]; |
|
169 |
[childController release]; |
|
170 |
[super dealloc]; |
|
171 |
} |
|
172 |
||
173 |
||
174 |
@end |
|
175 |