author | koda |
Sat, 31 Jul 2010 11:24:53 +0200 | |
changeset 3697 | d5b30d6373fc |
parent 3662 | a44406f4369b |
child 3701 | 8c449776ebe6 |
permissions | -rw-r--r-- |
3547 | 1 |
// |
2 |
// SingleWeaponViewController.m |
|
3 |
// Hedgewars |
|
4 |
// |
|
5 |
// Created by Vittorio on 19/06/10. |
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "SingleWeaponViewController.h" |
|
3621 | 10 |
#import "WeaponCellView.h" |
11 |
#import "CommodityFunctions.h" |
|
12 |
#import "UIImageExtra.h" |
|
3547 | 13 |
|
14 |
@implementation SingleWeaponViewController |
|
3659 | 15 |
@synthesize weaponName, ammoStoreImage, ammoNames; |
3547 | 16 |
|
3621 | 17 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
18 |
return rotationManager(interfaceOrientation); |
|
3547 | 19 |
} |
20 |
||
21 |
#pragma mark - |
|
22 |
#pragma mark View lifecycle |
|
3621 | 23 |
-(void) viewDidLoad { |
3547 | 24 |
[super viewDidLoad]; |
3697 | 25 |
|
3621 | 26 |
NSArray *array = [[NSArray alloc] initWithObjects: |
27 |
NSLocalizedString(@"Grenade",@""), |
|
28 |
NSLocalizedString(@"Cluster Bomb",@""), |
|
29 |
NSLocalizedString(@"Bazooka",@""), |
|
30 |
NSLocalizedString(@"Homing Bee",@""), |
|
31 |
NSLocalizedString(@"Shotgun",@""), |
|
32 |
NSLocalizedString(@"Pick Hammer",@""), |
|
33 |
NSLocalizedString(@"Skip",@""), |
|
34 |
NSLocalizedString(@"Rope",@""), |
|
35 |
NSLocalizedString(@"Mine",@""), |
|
36 |
NSLocalizedString(@"Deagle",@""), |
|
37 |
NSLocalizedString(@"Dynamite",@""), |
|
38 |
NSLocalizedString(@"Fire Punch",@""), |
|
39 |
NSLocalizedString(@"Slash",@""), |
|
40 |
NSLocalizedString(@"Baseball bat",@""), |
|
41 |
NSLocalizedString(@"Parachute",@""), |
|
42 |
NSLocalizedString(@"Air Attack",@""), |
|
43 |
NSLocalizedString(@"Mines Attack",@""), |
|
44 |
NSLocalizedString(@"Blow Torch",@""), |
|
45 |
NSLocalizedString(@"Construction",@""), |
|
46 |
NSLocalizedString(@"Teleport",@""), |
|
47 |
NSLocalizedString(@"Switch Hedgehog",@""), |
|
48 |
NSLocalizedString(@"Mortar",@""), |
|
49 |
NSLocalizedString(@"Kamikaze",@""), |
|
50 |
NSLocalizedString(@"Cake",@""), |
|
51 |
NSLocalizedString(@"Seduction",@""), |
|
52 |
NSLocalizedString(@"Watermelon Bomb",@""), |
|
53 |
NSLocalizedString(@"Hellish Hand Grenade",@""), |
|
54 |
NSLocalizedString(@"Napalm Attack",@""), |
|
55 |
NSLocalizedString(@"Drill Rocket",@""), |
|
56 |
NSLocalizedString(@"Ballgun",@""), |
|
57 |
NSLocalizedString(@"RC Plane",@""), |
|
58 |
NSLocalizedString(@"Low Gravity",@""), |
|
59 |
NSLocalizedString(@"Extra Damage",@""), |
|
60 |
NSLocalizedString(@"Invulnerable",@""), |
|
61 |
NSLocalizedString(@"Extra Time",@""), |
|
62 |
NSLocalizedString(@"Laser Sight",@""), |
|
63 |
NSLocalizedString(@"Vampirism",@""), |
|
64 |
NSLocalizedString(@"Sniper Rifle",@""), |
|
65 |
NSLocalizedString(@"Flying Saucer",@""), |
|
66 |
NSLocalizedString(@"Molotov Cocktail",@""), |
|
67 |
NSLocalizedString(@"Birdy",@""), |
|
68 |
NSLocalizedString(@"Portable Portal Device",@""), |
|
69 |
NSLocalizedString(@"Piano Attack",@""), |
|
70 |
NSLocalizedString(@"Old Limburger",@""), |
|
71 |
NSLocalizedString(@"Sine Gun",@""), |
|
72 |
NSLocalizedString(@"Flamethrower",@""), |
|
73 |
nil]; |
|
74 |
self.ammoNames = array; |
|
75 |
[array release]; |
|
3547 | 76 |
|
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
77 |
quantity = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1)); |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
78 |
probability = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1)); |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
79 |
delay = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1)); |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
80 |
crateness = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1)); |
3697 | 81 |
|
3621 | 82 |
NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()]; |
83 |
UIImage *img = [[UIImage alloc] initWithContentsOfFile:str]; |
|
84 |
self.ammoStoreImage = img; |
|
85 |
[img release]; |
|
3697 | 86 |
|
3659 | 87 |
self.title = NSLocalizedString(@"Edit weapons preferences",@""); |
3547 | 88 |
} |
3621 | 89 |
|
90 |
-(void) viewWillAppear:(BOOL) animated { |
|
91 |
[super viewWillAppear:animated]; |
|
3697 | 92 |
|
3659 | 93 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 94 |
NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile]; |
95 |
[ammoFile release]; |
|
3697 | 96 |
|
3621 | 97 |
const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String]; |
98 |
const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String]; |
|
99 |
const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String]; |
|
100 |
const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String]; |
|
101 |
[weapon release]; |
|
3697 | 102 |
|
3621 | 103 |
// if the new weaponset is diffrent from the older we need to update it replacing |
104 |
// the missing ammos with 0 quantity |
|
105 |
int oldlen = strlen(tmp1); |
|
106 |
for (int i = 0; i < oldlen; i++) { |
|
107 |
quantity[i] = tmp1[i]; |
|
108 |
probability[i] = tmp2[i]; |
|
109 |
delay[i] = tmp3[i]; |
|
110 |
crateness[i] = tmp4[i]; |
|
111 |
} |
|
3623 | 112 |
for (int i = oldlen; i < CURRENT_AMMOSIZE; i++) { |
3621 | 113 |
quantity[i] = '0'; |
114 |
probability[i] = '0'; |
|
115 |
delay[i] = '0'; |
|
116 |
crateness[i] = '0'; |
|
117 |
} |
|
3697 | 118 |
|
3621 | 119 |
[self.tableView reloadData]; |
3547 | 120 |
} |
3621 | 121 |
|
122 |
-(void) viewWillDisappear:(BOOL) animated { |
|
123 |
[super viewWillDisappear:animated]; |
|
3659 | 124 |
[self saveAmmos]; |
125 |
} |
|
126 |
||
127 |
-(void) saveAmmos { |
|
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
128 |
quantity[CURRENT_AMMOSIZE] = '\0'; |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
129 |
probability[CURRENT_AMMOSIZE] = '\0'; |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
130 |
delay[CURRENT_AMMOSIZE] = '\0'; |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
131 |
crateness[CURRENT_AMMOSIZE] = '\0'; |
3697 | 132 |
|
3621 | 133 |
NSString *quantityStr = [NSString stringWithUTF8String:quantity]; |
134 |
NSString *probabilityStr = [NSString stringWithUTF8String:probability]; |
|
135 |
NSString *delayStr = [NSString stringWithUTF8String:delay]; |
|
136 |
NSString *cratenessStr = [NSString stringWithUTF8String:crateness]; |
|
137 |
||
138 |
NSDictionary *weapon = [[NSDictionary alloc] initWithObjectsAndKeys: |
|
139 |
[NSNumber numberWithInt:CURRENT_AMMOSIZE],@"version", |
|
140 |
quantityStr,@"ammostore_initialqt", |
|
141 |
probabilityStr,@"ammostore_probability", |
|
142 |
delayStr,@"ammostore_delay", |
|
143 |
cratenessStr,@"ammostore_crate", nil]; |
|
144 |
||
3659 | 145 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 146 |
[weapon writeToFile:ammoFile atomically:YES]; |
147 |
[ammoFile release]; |
|
148 |
[weapon release]; |
|
3547 | 149 |
} |
150 |
||
151 |
#pragma mark - |
|
152 |
#pragma mark Table view data source |
|
3621 | 153 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
3659 | 154 |
return 2; |
3547 | 155 |
} |
156 |
||
3621 | 157 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
3659 | 158 |
if (section == 0) |
159 |
return 1; |
|
160 |
else |
|
161 |
return CURRENT_AMMOSIZE; |
|
3547 | 162 |
} |
163 |
||
164 |
// Customize the appearance of table view cells. |
|
3659 | 165 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
166 |
static NSString *CellIdentifier0 = @"Cell0"; |
|
167 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
3621 | 168 |
NSInteger row = [indexPath row]; |
3659 | 169 |
UITableViewCell *cell = nil; |
170 |
||
171 |
if (0 == [indexPath section]) { |
|
172 |
EditableCellView *customCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
|
173 |
if (customCell == nil) { |
|
3697 | 174 |
customCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
3659 | 175 |
reuseIdentifier:CellIdentifier0] autorelease]; |
176 |
customCell.delegate = self; |
|
177 |
} |
|
3697 | 178 |
|
3659 | 179 |
customCell.textField.text = self.weaponName; |
180 |
customCell.detailTextLabel.text = nil; |
|
181 |
customCell.imageView.image = nil; |
|
182 |
customCell.selectionStyle = UITableViewCellSelectionStyleNone; |
|
183 |
cell = customCell; |
|
184 |
} else { |
|
185 |
WeaponCellView *customCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
|
186 |
if (customCell == nil) { |
|
187 |
customCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
|
188 |
customCell.delegate = self; |
|
189 |
} |
|
3697 | 190 |
|
3659 | 191 |
int x = ((row*32)/1024)*32; |
192 |
int y = (row*32)%1024; |
|
3697 | 193 |
|
3659 | 194 |
UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, 32, 32)] makeRoundCornersOfSize:CGSizeMake(7, 7)]; |
195 |
customCell.weaponIcon.image = img; |
|
196 |
customCell.weaponName.text = [ammoNames objectAtIndex:row]; |
|
197 |
customCell.tag = row; |
|
3697 | 198 |
|
3659 | 199 |
[customCell.initialQt setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO]; |
200 |
[customCell.probabilityQt setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO]; |
|
201 |
[customCell.delayQt setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO]; |
|
202 |
[customCell.crateQt setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO]; |
|
203 |
cell = customCell; |
|
3547 | 204 |
} |
3697 | 205 |
|
3643
858b20bafb6e
reworked the ammunition configuration page (visually)
koda
parents:
3624
diff
changeset
|
206 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
3547 | 207 |
return cell; |
208 |
} |
|
209 |
||
3659 | 210 |
-(CGFloat) tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
211 |
if (0 == [indexPath section]) |
|
212 |
return aTableView.rowHeight; |
|
213 |
else |
|
214 |
return 120; |
|
215 |
} |
|
3547 | 216 |
|
217 |
#pragma mark - |
|
218 |
#pragma mark Table view delegate |
|
3659 | 219 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
220 |
if (0 == [indexPath section]) { |
|
221 |
EditableCellView *editableCell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
|
222 |
[editableCell replyKeyboard]; |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
#pragma mark - |
|
227 |
#pragma mark editableCellView delegate |
|
228 |
// set the new value |
|
3697 | 229 |
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { |
3659 | 230 |
// delete old file |
231 |
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL]; |
|
232 |
// update filename |
|
233 |
self.weaponName = textString; |
|
234 |
// save new file |
|
235 |
[self saveAmmos]; |
|
3547 | 236 |
} |
237 |
||
3624 | 238 |
#pragma mark - |
239 |
#pragma mark WeaponButtonControllerDelegate |
|
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
240 |
-(void) updateValues:(NSArray *)withArray atIndex:(NSInteger) index { |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
241 |
quantity[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:0] intValue]] characterAtIndex:0]; |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
242 |
probability[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:1] intValue]] characterAtIndex:0]; |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
243 |
delay[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:2] intValue]] characterAtIndex:0]; |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
244 |
crateness[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:3] intValue]] characterAtIndex:0]; |
3624 | 245 |
} |
3547 | 246 |
|
247 |
#pragma mark - |
|
248 |
#pragma mark Memory management |
|
3621 | 249 |
-(void) didReceiveMemoryWarning { |
3547 | 250 |
[super didReceiveMemoryWarning]; |
251 |
} |
|
252 |
||
3621 | 253 |
-(void) viewDidUnload { |
3659 | 254 |
free(quantity); quantity = NULL; |
255 |
free(probability); probability = NULL; |
|
256 |
free(delay); delay = NULL; |
|
257 |
free(crateness); crateness = NULL; |
|
3621 | 258 |
[super viewDidUnload]; |
3659 | 259 |
self.weaponName = nil; |
260 |
self.ammoStoreImage = nil; |
|
261 |
self.ammoNames = nil; |
|
3621 | 262 |
MSG_DIDUNLOAD(); |
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3660
diff
changeset
|
263 |
[super viewDidUnload]; |
3547 | 264 |
} |
265 |
||
266 |
||
3621 | 267 |
-(void) dealloc { |
3659 | 268 |
[weaponName release]; |
269 |
[ammoStoreImage release]; |
|
270 |
[ammoNames release]; |
|
3547 | 271 |
[super dealloc]; |
272 |
} |
|
273 |
||
274 |
||
275 |
@end |
|
276 |