author | koda |
Sat, 04 Sep 2010 02:09:24 +0200 | |
changeset 3825 | fd6c20cd90e3 |
parent 3782 | dc3531e49e4c |
child 3829 | 81db3c85784b |
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 |
|
3782 | 15 |
@synthesize weaponName, description, 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]; |
3782 | 25 |
|
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:
3701
diff
changeset
|
26 |
// also increment CURRENT_AMMOSIZE in CommodityFunctions.h |
3621 | 27 |
NSArray *array = [[NSArray alloc] initWithObjects: |
28 |
NSLocalizedString(@"Grenade",@""), |
|
29 |
NSLocalizedString(@"Cluster Bomb",@""), |
|
30 |
NSLocalizedString(@"Bazooka",@""), |
|
31 |
NSLocalizedString(@"Homing Bee",@""), |
|
32 |
NSLocalizedString(@"Shotgun",@""), |
|
33 |
NSLocalizedString(@"Pick Hammer",@""), |
|
34 |
NSLocalizedString(@"Skip",@""), |
|
35 |
NSLocalizedString(@"Rope",@""), |
|
36 |
NSLocalizedString(@"Mine",@""), |
|
37 |
NSLocalizedString(@"Deagle",@""), |
|
38 |
NSLocalizedString(@"Dynamite",@""), |
|
39 |
NSLocalizedString(@"Fire Punch",@""), |
|
40 |
NSLocalizedString(@"Slash",@""), |
|
41 |
NSLocalizedString(@"Baseball bat",@""), |
|
42 |
NSLocalizedString(@"Parachute",@""), |
|
43 |
NSLocalizedString(@"Air Attack",@""), |
|
44 |
NSLocalizedString(@"Mines Attack",@""), |
|
45 |
NSLocalizedString(@"Blow Torch",@""), |
|
46 |
NSLocalizedString(@"Construction",@""), |
|
47 |
NSLocalizedString(@"Teleport",@""), |
|
48 |
NSLocalizedString(@"Switch Hedgehog",@""), |
|
49 |
NSLocalizedString(@"Mortar",@""), |
|
50 |
NSLocalizedString(@"Kamikaze",@""), |
|
51 |
NSLocalizedString(@"Cake",@""), |
|
52 |
NSLocalizedString(@"Seduction",@""), |
|
53 |
NSLocalizedString(@"Watermelon Bomb",@""), |
|
54 |
NSLocalizedString(@"Hellish Hand Grenade",@""), |
|
55 |
NSLocalizedString(@"Napalm Attack",@""), |
|
56 |
NSLocalizedString(@"Drill Rocket",@""), |
|
57 |
NSLocalizedString(@"Ballgun",@""), |
|
58 |
NSLocalizedString(@"RC Plane",@""), |
|
59 |
NSLocalizedString(@"Low Gravity",@""), |
|
60 |
NSLocalizedString(@"Extra Damage",@""), |
|
61 |
NSLocalizedString(@"Invulnerable",@""), |
|
62 |
NSLocalizedString(@"Extra Time",@""), |
|
63 |
NSLocalizedString(@"Laser Sight",@""), |
|
64 |
NSLocalizedString(@"Vampirism",@""), |
|
65 |
NSLocalizedString(@"Sniper Rifle",@""), |
|
66 |
NSLocalizedString(@"Flying Saucer",@""), |
|
67 |
NSLocalizedString(@"Molotov Cocktail",@""), |
|
68 |
NSLocalizedString(@"Birdy",@""), |
|
69 |
NSLocalizedString(@"Portable Portal Device",@""), |
|
70 |
NSLocalizedString(@"Piano Attack",@""), |
|
71 |
NSLocalizedString(@"Old Limburger",@""), |
|
72 |
NSLocalizedString(@"Sine Gun",@""), |
|
73 |
NSLocalizedString(@"Flamethrower",@""), |
|
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:
3701
diff
changeset
|
74 |
NSLocalizedString(@"Sticky Mine",@""), |
2ba6ac8a114b
reworked the initialization functions, now it should be safe to update and no more need of spinning wheel at first launch
koda
parents:
3701
diff
changeset
|
75 |
NSLocalizedString(@"Hammer",@""), |
3621 | 76 |
nil]; |
77 |
self.ammoNames = array; |
|
78 |
[array release]; |
|
3782 | 79 |
|
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
80 |
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
|
81 |
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
|
82 |
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
|
83 |
crateness = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1)); |
3782 | 84 |
|
3621 | 85 |
NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()]; |
86 |
UIImage *img = [[UIImage alloc] initWithContentsOfFile:str]; |
|
87 |
self.ammoStoreImage = img; |
|
88 |
[img release]; |
|
3782 | 89 |
|
3659 | 90 |
self.title = NSLocalizedString(@"Edit weapons preferences",@""); |
3547 | 91 |
} |
3621 | 92 |
|
93 |
-(void) viewWillAppear:(BOOL) animated { |
|
94 |
[super viewWillAppear:animated]; |
|
3782 | 95 |
|
3659 | 96 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 97 |
NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile]; |
98 |
[ammoFile release]; |
|
3782 | 99 |
|
100 |
self.description = [weapon objectForKey:@"description"]; |
|
3621 | 101 |
const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String]; |
102 |
const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String]; |
|
103 |
const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String]; |
|
104 |
const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String]; |
|
105 |
[weapon release]; |
|
3782 | 106 |
|
3621 | 107 |
// if the new weaponset is diffrent from the older we need to update it replacing |
108 |
// the missing ammos with 0 quantity |
|
109 |
int oldlen = strlen(tmp1); |
|
110 |
for (int i = 0; i < oldlen; i++) { |
|
111 |
quantity[i] = tmp1[i]; |
|
112 |
probability[i] = tmp2[i]; |
|
113 |
delay[i] = tmp3[i]; |
|
114 |
crateness[i] = tmp4[i]; |
|
115 |
} |
|
3623 | 116 |
for (int i = oldlen; i < CURRENT_AMMOSIZE; i++) { |
3621 | 117 |
quantity[i] = '0'; |
118 |
probability[i] = '0'; |
|
119 |
delay[i] = '0'; |
|
120 |
crateness[i] = '0'; |
|
121 |
} |
|
3782 | 122 |
|
3621 | 123 |
[self.tableView reloadData]; |
3547 | 124 |
} |
3621 | 125 |
|
126 |
-(void) viewWillDisappear:(BOOL) animated { |
|
127 |
[super viewWillDisappear:animated]; |
|
3659 | 128 |
[self saveAmmos]; |
129 |
} |
|
130 |
||
131 |
-(void) saveAmmos { |
|
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
132 |
quantity[CURRENT_AMMOSIZE] = '\0'; |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
133 |
probability[CURRENT_AMMOSIZE] = '\0'; |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
134 |
delay[CURRENT_AMMOSIZE] = '\0'; |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
135 |
crateness[CURRENT_AMMOSIZE] = '\0'; |
3782 | 136 |
|
3621 | 137 |
NSString *quantityStr = [NSString stringWithUTF8String:quantity]; |
138 |
NSString *probabilityStr = [NSString stringWithUTF8String:probability]; |
|
139 |
NSString *delayStr = [NSString stringWithUTF8String:delay]; |
|
140 |
NSString *cratenessStr = [NSString stringWithUTF8String:crateness]; |
|
3782 | 141 |
|
3621 | 142 |
NSDictionary *weapon = [[NSDictionary alloc] initWithObjectsAndKeys: |
143 |
[NSNumber numberWithInt:CURRENT_AMMOSIZE],@"version", |
|
144 |
quantityStr,@"ammostore_initialqt", |
|
145 |
probabilityStr,@"ammostore_probability", |
|
146 |
delayStr,@"ammostore_delay", |
|
3782 | 147 |
cratenessStr,@"ammostore_crate", |
148 |
self.description,@"description", |
|
149 |
nil]; |
|
150 |
||
3659 | 151 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 152 |
[weapon writeToFile:ammoFile atomically:YES]; |
153 |
[ammoFile release]; |
|
154 |
[weapon release]; |
|
3547 | 155 |
} |
156 |
||
157 |
#pragma mark - |
|
158 |
#pragma mark Table view data source |
|
3621 | 159 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
3659 | 160 |
return 2; |
3547 | 161 |
} |
162 |
||
3621 | 163 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
3659 | 164 |
if (section == 0) |
3782 | 165 |
return 2; |
3659 | 166 |
else |
167 |
return CURRENT_AMMOSIZE; |
|
3547 | 168 |
} |
169 |
||
170 |
// Customize the appearance of table view cells. |
|
3659 | 171 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
172 |
static NSString *CellIdentifier0 = @"Cell0"; |
|
173 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
3621 | 174 |
NSInteger row = [indexPath row]; |
3659 | 175 |
UITableViewCell *cell = nil; |
3782 | 176 |
|
3659 | 177 |
if (0 == [indexPath section]) { |
3782 | 178 |
EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
179 |
if (editableCell == nil) { |
|
180 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
|
181 |
reuseIdentifier:CellIdentifier0] autorelease]; |
|
182 |
editableCell.delegate = self; |
|
3659 | 183 |
} |
3782 | 184 |
editableCell.tag = row; |
185 |
editableCell.selectionStyle = UITableViewCellSelectionStyleNone; |
|
186 |
editableCell.imageView.image = nil; |
|
187 |
editableCell.detailTextLabel.text = nil; |
|
188 |
||
189 |
if (row == 0) { |
|
190 |
editableCell.textField.text = self.weaponName; |
|
191 |
} else { |
|
3825 | 192 |
editableCell.minimumCharacters = 0; |
3782 | 193 |
editableCell.textField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; |
194 |
editableCell.textField.text = self.description; |
|
195 |
editableCell.textField.placeholder = NSLocalizedString(@"You can add a description if you wish",@""); |
|
196 |
} |
|
197 |
cell = editableCell; |
|
3659 | 198 |
} else { |
3782 | 199 |
WeaponCellView *weaponCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
200 |
if (weaponCell == nil) { |
|
201 |
weaponCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
|
202 |
weaponCell.delegate = self; |
|
3659 | 203 |
} |
3697 | 204 |
|
3659 | 205 |
int x = ((row*32)/1024)*32; |
206 |
int y = (row*32)%1024; |
|
3697 | 207 |
|
3659 | 208 |
UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, 32, 32)] makeRoundCornersOfSize:CGSizeMake(7, 7)]; |
3782 | 209 |
weaponCell.weaponIcon.image = img; |
210 |
weaponCell.weaponName.text = [ammoNames objectAtIndex:row]; |
|
211 |
weaponCell.tag = row; |
|
3697 | 212 |
|
3782 | 213 |
[weaponCell.initialSli setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO]; |
214 |
[weaponCell.probabilitySli setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO]; |
|
215 |
[weaponCell.delaySli setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO]; |
|
216 |
[weaponCell.crateSli setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO]; |
|
217 |
cell = weaponCell; |
|
3547 | 218 |
} |
3697 | 219 |
|
3643
858b20bafb6e
reworked the ammunition configuration page (visually)
koda
parents:
3624
diff
changeset
|
220 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
3547 | 221 |
return cell; |
222 |
} |
|
223 |
||
3659 | 224 |
-(CGFloat) tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
225 |
if (0 == [indexPath section]) |
|
226 |
return aTableView.rowHeight; |
|
227 |
else |
|
228 |
return 120; |
|
229 |
} |
|
3547 | 230 |
|
3701 | 231 |
-(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { |
232 |
NSString *sectionTitle = nil; |
|
233 |
switch (section) { |
|
234 |
case 0: |
|
235 |
sectionTitle = NSLocalizedString(@"Weaponset Name", @""); |
|
236 |
break; |
|
237 |
case 1: |
|
238 |
sectionTitle = NSLocalizedString(@"Weapon Ammuntions", @""); |
|
239 |
break; |
|
240 |
default: |
|
241 |
DLog(@"nope"); |
|
242 |
break; |
|
243 |
} |
|
244 |
return sectionTitle; |
|
245 |
} |
|
246 |
||
3547 | 247 |
#pragma mark - |
248 |
#pragma mark Table view delegate |
|
3659 | 249 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
250 |
if (0 == [indexPath section]) { |
|
251 |
EditableCellView *editableCell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
|
252 |
[editableCell replyKeyboard]; |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
#pragma mark - |
|
257 |
#pragma mark editableCellView delegate |
|
258 |
// set the new value |
|
3697 | 259 |
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { |
3782 | 260 |
if (tagValue == 0) { |
261 |
// delete old file |
|
262 |
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL]; |
|
263 |
// update filename |
|
264 |
self.weaponName = textString; |
|
265 |
// save new file |
|
266 |
[self saveAmmos]; |
|
267 |
} else { |
|
268 |
self.description = textString; |
|
269 |
} |
|
3547 | 270 |
} |
271 |
||
3624 | 272 |
#pragma mark - |
273 |
#pragma mark WeaponButtonControllerDelegate |
|
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
274 |
-(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
|
275 |
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
|
276 |
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
|
277 |
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
|
278 |
crateness[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:3] intValue]] characterAtIndex:0]; |
3624 | 279 |
} |
3547 | 280 |
|
281 |
#pragma mark - |
|
282 |
#pragma mark Memory management |
|
3621 | 283 |
-(void) didReceiveMemoryWarning { |
3547 | 284 |
[super didReceiveMemoryWarning]; |
285 |
} |
|
286 |
||
3621 | 287 |
-(void) viewDidUnload { |
3659 | 288 |
free(quantity); quantity = NULL; |
289 |
free(probability); probability = NULL; |
|
290 |
free(delay); delay = NULL; |
|
291 |
free(crateness); crateness = NULL; |
|
3621 | 292 |
[super viewDidUnload]; |
3782 | 293 |
self.description = nil; |
3659 | 294 |
self.weaponName = nil; |
295 |
self.ammoStoreImage = nil; |
|
296 |
self.ammoNames = nil; |
|
3621 | 297 |
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
|
298 |
[super viewDidUnload]; |
3547 | 299 |
} |
300 |
||
301 |
||
3621 | 302 |
-(void) dealloc { |
3659 | 303 |
[weaponName release]; |
3782 | 304 |
[description release]; |
3659 | 305 |
[ammoStoreImage release]; |
306 |
[ammoNames release]; |
|
3547 | 307 |
[super dealloc]; |
308 |
} |
|
309 |
||
310 |
||
311 |
@end |
|
312 |