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