author | nemo |
Sun, 04 Sep 2011 13:36:59 -0400 | |
changeset 5771 | e3a047e84e19 |
parent 5208 | 878e551f0b4a |
child 5984 | 6fd40d866342 |
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 19/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "SchemeSettingsViewController.h" |
|
4281 | 23 |
#import "CreationChamber.h" |
3547 | 24 |
#import "SingleSchemeViewController.h" |
25 |
||
26 |
@implementation SchemeSettingsViewController |
|
27 |
@synthesize listOfSchemes; |
|
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:
3903
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]; |
|
44 |
} |
|
45 |
||
46 |
-(void) viewWillAppear:(BOOL) animated { |
|
47 |
[super viewWillAppear:animated]; |
|
3697 | 48 |
|
3547 | 49 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL]; |
50 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
|
51 |
self.listOfSchemes = array; |
|
52 |
[array release]; |
|
3697 | 53 |
|
3547 | 54 |
[self.tableView reloadData]; |
55 |
} |
|
56 |
||
57 |
// modifies the navigation bar to add the "Add" and "Done" buttons |
|
58 |
-(void) toggleEdit:(id) sender { |
|
59 |
BOOL isEditing = self.tableView.editing; |
|
60 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
3697 | 61 |
|
3547 | 62 |
if (isEditing) { |
63 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")]; |
|
64 |
[self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered]; |
|
65 |
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; |
|
66 |
} else { |
|
67 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the scheme panel")]; |
|
68 |
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; |
|
69 |
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel") |
|
70 |
style:UIBarButtonItemStyleBordered |
|
71 |
target:self |
|
72 |
action:@selector(addScheme:)]; |
|
73 |
self.navigationItem.leftBarButtonItem = addButton; |
|
74 |
[addButton release]; |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
-(void) addScheme:(id) sender { |
|
79 |
NSString *fileName = [[NSString alloc] initWithFormat:@"Scheme %u.plist", [self.listOfSchemes count]]; |
|
3697 | 80 |
|
3547 | 81 |
createSchemeNamed([fileName stringByDeletingPathExtension]); |
3697 | 82 |
|
3547 | 83 |
[self.listOfSchemes addObject:fileName]; |
3697 | 84 |
|
3547 | 85 |
// order the array alphabetically, so schemes will keep their position |
86 |
[self.listOfSchemes sortUsingSelector:@selector(compare:)]; |
|
4287 | 87 |
[self.tableView reloadData]; |
3697 | 88 |
|
4287 | 89 |
NSInteger index = [self.listOfSchemes indexOfObject:fileName]; |
90 |
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; |
|
91 |
[fileName release]; |
|
3547 | 92 |
} |
93 |
||
94 |
#pragma mark - |
|
95 |
#pragma mark Table view data source |
|
96 |
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
|
97 |
return 1; |
|
98 |
} |
|
99 |
||
100 |
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
101 |
return [self.listOfSchemes count]; |
|
102 |
} |
|
103 |
||
104 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
105 |
static NSString *CellIdentifier = @"Cell"; |
|
3697 | 106 |
|
3547 | 107 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
108 |
if (cell == nil) { |
|
109 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
110 |
} |
|
3697 | 111 |
|
112 |
NSUInteger row = [indexPath row]; |
|
113 |
NSString *rowString = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension]; |
|
114 |
cell.textLabel.text = rowString; |
|
3547 | 115 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3697 | 116 |
|
3547 | 117 |
return cell; |
118 |
} |
|
119 |
||
120 |
// delete the row and the file |
|
121 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
122 |
NSUInteger row = [indexPath row]; |
|
3697 | 123 |
|
3547 | 124 |
NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),[self.listOfSchemes objectAtIndex:row]]; |
125 |
[[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL]; |
|
126 |
[schemeFile release]; |
|
3697 | 127 |
|
3547 | 128 |
[self.listOfSchemes removeObjectAtIndex:row]; |
129 |
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
|
130 |
} |
|
131 |
||
132 |
#pragma mark - |
|
133 |
#pragma mark Table view delegate |
|
134 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
135 |
if (childController == nil) { |
|
136 |
childController = [[SingleSchemeViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
137 |
} |
|
3697 | 138 |
|
3547 | 139 |
NSInteger row = [indexPath row]; |
140 |
NSString *selectedSchemeFile = [self.listOfSchemes objectAtIndex:row]; |
|
3697 | 141 |
|
3547 | 142 |
// this must be set so childController can load the correct plist |
3659 | 143 |
childController.schemeName = [selectedSchemeFile stringByDeletingPathExtension]; |
3547 | 144 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
145 |
||
146 |
[self.navigationController pushViewController:childController animated:YES]; |
|
147 |
} |
|
148 |
||
149 |
||
150 |
#pragma mark - |
|
151 |
#pragma mark Memory management |
|
152 |
-(void)didReceiveMemoryWarning { |
|
153 |
[super didReceiveMemoryWarning]; |
|
154 |
if (childController.view.superview == nil ) |
|
155 |
childController = nil; |
|
3659 | 156 |
MSG_MEMCLEAN(); |
3547 | 157 |
} |
158 |
||
159 |
-(void) viewDidUnload { |
|
160 |
self.listOfSchemes = 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 { |
|
5208 | 168 |
releaseAndNil(listOfSchemes); |
169 |
releaseAndNil(childController); |
|
3547 | 170 |
[super dealloc]; |
171 |
} |
|
172 |
||
173 |
||
174 |
@end |
|
175 |