author | koda |
Mon, 30 Aug 2010 05:42:03 +0200 | |
changeset 3791 | 98072b3871c1 |
parent 3737 | 2ba6ac8a114b |
child 3829 | 81db3c85784b |
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]; |
|
3697 | 24 |
|
3547 | 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]; |
|
3697 | 31 |
|
3547 | 32 |
} |
33 |
||
34 |
-(void) viewWillAppear:(BOOL) animated { |
|
35 |
[super viewWillAppear:animated]; |
|
3697 | 36 |
|
3547 | 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]; |
|
3697 | 41 |
|
3547 | 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]; |
|
3697 | 49 |
|
3547 | 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]]; |
|
3697 | 68 |
|
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
|
69 |
createWeaponNamed([fileName stringByDeletingPathExtension], 0); |
3697 | 70 |
|
3547 | 71 |
[self.listOfWeapons addObject:fileName]; |
72 |
[fileName release]; |
|
3697 | 73 |
|
3547 | 74 |
// order the array alphabetically, so schemes will keep their position |
75 |
[self.listOfWeapons sortUsingSelector:@selector(compare:)]; |
|
3697 | 76 |
|
3547 | 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"; |
|
3697 | 92 |
|
3547 | 93 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
94 |
if (cell == nil) { |
|
95 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
96 |
} |
|
3697 | 97 |
|
98 |
NSUInteger row = [indexPath row]; |
|
99 |
NSString *rowString = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension]; |
|
100 |
cell.textLabel.text = rowString; |
|
3547 | 101 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3697 | 102 |
|
3547 | 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]; |
|
3697 | 109 |
|
3547 | 110 |
NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]]; |
111 |
[[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL]; |
|
112 |
[schemeFile release]; |
|
3697 | 113 |
|
3547 | 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 |
} |
|
3697 | 124 |
|
3547 | 125 |
NSInteger row = [indexPath row]; |
126 |
NSString *selectedWeaponFile = [self.listOfWeapons objectAtIndex:row]; |
|
3697 | 127 |
|
3547 | 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 |