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