author | tiyuri@gmail.com |
Thu, 02 Sep 2010 17:30:38 +0100 | |
changeset 3809 | 4fe006966370 |
parent 3782 | dc3531e49e4c |
child 3825 | fd6c20cd90e3 |
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 { |
|
192 |
editableCell.textField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; |
|
193 |
editableCell.textField.text = self.description; |
|
194 |
editableCell.textField.placeholder = NSLocalizedString(@"You can add a description if you wish",@""); |
|
195 |
} |
|
196 |
cell = editableCell; |
|
3659 | 197 |
} else { |
3782 | 198 |
WeaponCellView *weaponCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
199 |
if (weaponCell == nil) { |
|
200 |
weaponCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
|
201 |
weaponCell.delegate = self; |
|
3659 | 202 |
} |
3697 | 203 |
|
3659 | 204 |
int x = ((row*32)/1024)*32; |
205 |
int y = (row*32)%1024; |
|
3697 | 206 |
|
3659 | 207 |
UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, 32, 32)] makeRoundCornersOfSize:CGSizeMake(7, 7)]; |
3782 | 208 |
weaponCell.weaponIcon.image = img; |
209 |
weaponCell.weaponName.text = [ammoNames objectAtIndex:row]; |
|
210 |
weaponCell.tag = row; |
|
3697 | 211 |
|
3782 | 212 |
[weaponCell.initialSli setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO]; |
213 |
[weaponCell.probabilitySli setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO]; |
|
214 |
[weaponCell.delaySli setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO]; |
|
215 |
[weaponCell.crateSli setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO]; |
|
216 |
cell = weaponCell; |
|
3547 | 217 |
} |
3697 | 218 |
|
3643
858b20bafb6e
reworked the ammunition configuration page (visually)
koda
parents:
3624
diff
changeset
|
219 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
3547 | 220 |
return cell; |
221 |
} |
|
222 |
||
3659 | 223 |
-(CGFloat) tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
224 |
if (0 == [indexPath section]) |
|
225 |
return aTableView.rowHeight; |
|
226 |
else |
|
227 |
return 120; |
|
228 |
} |
|
3547 | 229 |
|
3701 | 230 |
-(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { |
231 |
NSString *sectionTitle = nil; |
|
232 |
switch (section) { |
|
233 |
case 0: |
|
234 |
sectionTitle = NSLocalizedString(@"Weaponset Name", @""); |
|
235 |
break; |
|
236 |
case 1: |
|
237 |
sectionTitle = NSLocalizedString(@"Weapon Ammuntions", @""); |
|
238 |
break; |
|
239 |
default: |
|
240 |
DLog(@"nope"); |
|
241 |
break; |
|
242 |
} |
|
243 |
return sectionTitle; |
|
244 |
} |
|
245 |
||
3547 | 246 |
#pragma mark - |
247 |
#pragma mark Table view delegate |
|
3659 | 248 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
249 |
if (0 == [indexPath section]) { |
|
250 |
EditableCellView *editableCell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
|
251 |
[editableCell replyKeyboard]; |
|
252 |
} |
|
253 |
} |
|
254 |
||
255 |
#pragma mark - |
|
256 |
#pragma mark editableCellView delegate |
|
257 |
// set the new value |
|
3697 | 258 |
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { |
3782 | 259 |
if (tagValue == 0) { |
260 |
// delete old file |
|
261 |
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL]; |
|
262 |
// update filename |
|
263 |
self.weaponName = textString; |
|
264 |
// save new file |
|
265 |
[self saveAmmos]; |
|
266 |
} else { |
|
267 |
self.description = textString; |
|
268 |
} |
|
3547 | 269 |
} |
270 |
||
3624 | 271 |
#pragma mark - |
272 |
#pragma mark WeaponButtonControllerDelegate |
|
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
273 |
-(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
|
274 |
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
|
275 |
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
|
276 |
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
|
277 |
crateness[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:3] intValue]] characterAtIndex:0]; |
3624 | 278 |
} |
3547 | 279 |
|
280 |
#pragma mark - |
|
281 |
#pragma mark Memory management |
|
3621 | 282 |
-(void) didReceiveMemoryWarning { |
3547 | 283 |
[super didReceiveMemoryWarning]; |
284 |
} |
|
285 |
||
3621 | 286 |
-(void) viewDidUnload { |
3659 | 287 |
free(quantity); quantity = NULL; |
288 |
free(probability); probability = NULL; |
|
289 |
free(delay); delay = NULL; |
|
290 |
free(crateness); crateness = NULL; |
|
3621 | 291 |
[super viewDidUnload]; |
3782 | 292 |
self.description = nil; |
3659 | 293 |
self.weaponName = nil; |
294 |
self.ammoStoreImage = nil; |
|
295 |
self.ammoNames = nil; |
|
3621 | 296 |
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
|
297 |
[super viewDidUnload]; |
3547 | 298 |
} |
299 |
||
300 |
||
3621 | 301 |
-(void) dealloc { |
3659 | 302 |
[weaponName release]; |
3782 | 303 |
[description release]; |
3659 | 304 |
[ammoStoreImage release]; |
305 |
[ammoNames release]; |
|
3547 | 306 |
[super dealloc]; |
307 |
} |
|
308 |
||
309 |
||
310 |
@end |
|
311 |