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 |
-(BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
119 |
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
|
|
120 |
if (![cell.textLabel.text isEqualToString:@"Default"])
|
|
121 |
return YES;
|
|
122 |
else
|
|
123 |
return NO;
|
|
124 |
}
|
|
125 |
|
|
126 |
#pragma mark -
|
|
127 |
#pragma mark Table view delegate
|
|
128 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
129 |
if (childController == nil) {
|
|
130 |
childController = [[SingleWeaponViewController alloc] initWithStyle:UITableViewStyleGrouped];
|
|
131 |
}
|
|
132 |
|
|
133 |
NSInteger row = [indexPath row];
|
|
134 |
NSString *selectedWeaponFile = [self.listOfWeapons objectAtIndex:row];
|
|
135 |
|
|
136 |
// this must be set so childController can load the correct plist
|
|
137 |
childController.title = [selectedWeaponFile stringByDeletingPathExtension];
|
|
138 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
|
|
139 |
|
|
140 |
[self.navigationController pushViewController:childController animated:YES];
|
|
141 |
}
|
|
142 |
|
|
143 |
|
|
144 |
#pragma mark -
|
|
145 |
#pragma mark Memory management
|
|
146 |
-(void)didReceiveMemoryWarning {
|
|
147 |
[super didReceiveMemoryWarning];
|
|
148 |
if (childController.view.superview == nil )
|
|
149 |
childController = nil;
|
|
150 |
}
|
|
151 |
|
|
152 |
-(void) viewDidUnload {
|
|
153 |
self.listOfWeapons = nil;
|
|
154 |
childController = nil;
|
|
155 |
[super viewDidUnload];
|
|
156 |
MSG_DIDUNLOAD();
|
|
157 |
}
|
|
158 |
|
|
159 |
|
|
160 |
-(void) dealloc {
|
|
161 |
[self.listOfWeapons release];
|
|
162 |
[childController release];
|
|
163 |
[super dealloc];
|
|
164 |
}
|
|
165 |
|
|
166 |
|
|
167 |
@end
|
|
168 |
|