author | koda |
Mon, 23 Aug 2010 00:46:06 +0200 | |
changeset 3753 | bb5a0252fb56 |
parent 3737 | 2ba6ac8a114b |
child 3766 | 36b625111609 |
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 |
|
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]; |
|
3547 | 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)); |
3697 | 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]; |
|
3697 | 89 |
|
3659 | 90 |
self.title = NSLocalizedString(@"Edit weapons preferences",@""); |
3547 | 91 |
} |
3621 | 92 |
|
93 |
-(void) viewWillAppear:(BOOL) animated { |
|
94 |
[super viewWillAppear:animated]; |
|
3697 | 95 |
|
3659 | 96 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 97 |
NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile]; |
98 |
[ammoFile release]; |
|
3697 | 99 |
|
3621 | 100 |
const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String]; |
101 |
const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String]; |
|
102 |
const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String]; |
|
103 |
const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String]; |
|
104 |
[weapon release]; |
|
3697 | 105 |
|
3621 | 106 |
// if the new weaponset is diffrent from the older we need to update it replacing |
107 |
// the missing ammos with 0 quantity |
|
108 |
int oldlen = strlen(tmp1); |
|
109 |
for (int i = 0; i < oldlen; i++) { |
|
110 |
quantity[i] = tmp1[i]; |
|
111 |
probability[i] = tmp2[i]; |
|
112 |
delay[i] = tmp3[i]; |
|
113 |
crateness[i] = tmp4[i]; |
|
114 |
} |
|
3623 | 115 |
for (int i = oldlen; i < CURRENT_AMMOSIZE; i++) { |
3621 | 116 |
quantity[i] = '0'; |
117 |
probability[i] = '0'; |
|
118 |
delay[i] = '0'; |
|
119 |
crateness[i] = '0'; |
|
120 |
} |
|
3697 | 121 |
|
3621 | 122 |
[self.tableView reloadData]; |
3547 | 123 |
} |
3621 | 124 |
|
125 |
-(void) viewWillDisappear:(BOOL) animated { |
|
126 |
[super viewWillDisappear:animated]; |
|
3659 | 127 |
[self saveAmmos]; |
128 |
} |
|
129 |
||
130 |
-(void) saveAmmos { |
|
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
131 |
quantity[CURRENT_AMMOSIZE] = '\0'; |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
132 |
probability[CURRENT_AMMOSIZE] = '\0'; |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
133 |
delay[CURRENT_AMMOSIZE] = '\0'; |
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
134 |
crateness[CURRENT_AMMOSIZE] = '\0'; |
3697 | 135 |
|
3621 | 136 |
NSString *quantityStr = [NSString stringWithUTF8String:quantity]; |
137 |
NSString *probabilityStr = [NSString stringWithUTF8String:probability]; |
|
138 |
NSString *delayStr = [NSString stringWithUTF8String:delay]; |
|
139 |
NSString *cratenessStr = [NSString stringWithUTF8String:crateness]; |
|
140 |
||
141 |
NSDictionary *weapon = [[NSDictionary alloc] initWithObjectsAndKeys: |
|
142 |
[NSNumber numberWithInt:CURRENT_AMMOSIZE],@"version", |
|
143 |
quantityStr,@"ammostore_initialqt", |
|
144 |
probabilityStr,@"ammostore_probability", |
|
145 |
delayStr,@"ammostore_delay", |
|
146 |
cratenessStr,@"ammostore_crate", nil]; |
|
147 |
||
3659 | 148 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 149 |
[weapon writeToFile:ammoFile atomically:YES]; |
150 |
[ammoFile release]; |
|
151 |
[weapon release]; |
|
3547 | 152 |
} |
153 |
||
154 |
#pragma mark - |
|
155 |
#pragma mark Table view data source |
|
3621 | 156 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
3659 | 157 |
return 2; |
3547 | 158 |
} |
159 |
||
3621 | 160 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
3659 | 161 |
if (section == 0) |
162 |
return 1; |
|
163 |
else |
|
164 |
return CURRENT_AMMOSIZE; |
|
3547 | 165 |
} |
166 |
||
167 |
// Customize the appearance of table view cells. |
|
3659 | 168 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
169 |
static NSString *CellIdentifier0 = @"Cell0"; |
|
170 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
3621 | 171 |
NSInteger row = [indexPath row]; |
3659 | 172 |
UITableViewCell *cell = nil; |
173 |
||
174 |
if (0 == [indexPath section]) { |
|
175 |
EditableCellView *customCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
|
176 |
if (customCell == nil) { |
|
3697 | 177 |
customCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
3659 | 178 |
reuseIdentifier:CellIdentifier0] autorelease]; |
179 |
customCell.delegate = self; |
|
180 |
} |
|
3697 | 181 |
|
3659 | 182 |
customCell.textField.text = self.weaponName; |
183 |
customCell.detailTextLabel.text = nil; |
|
184 |
customCell.imageView.image = nil; |
|
185 |
customCell.selectionStyle = UITableViewCellSelectionStyleNone; |
|
186 |
cell = customCell; |
|
187 |
} else { |
|
188 |
WeaponCellView *customCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
|
189 |
if (customCell == nil) { |
|
190 |
customCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
|
191 |
customCell.delegate = self; |
|
192 |
} |
|
3697 | 193 |
|
3659 | 194 |
int x = ((row*32)/1024)*32; |
195 |
int y = (row*32)%1024; |
|
3697 | 196 |
|
3659 | 197 |
UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, 32, 32)] makeRoundCornersOfSize:CGSizeMake(7, 7)]; |
198 |
customCell.weaponIcon.image = img; |
|
199 |
customCell.weaponName.text = [ammoNames objectAtIndex:row]; |
|
200 |
customCell.tag = row; |
|
3697 | 201 |
|
3659 | 202 |
[customCell.initialQt setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO]; |
203 |
[customCell.probabilityQt setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO]; |
|
204 |
[customCell.delayQt setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO]; |
|
205 |
[customCell.crateQt setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO]; |
|
206 |
cell = customCell; |
|
3547 | 207 |
} |
3697 | 208 |
|
3643
858b20bafb6e
reworked the ammunition configuration page (visually)
koda
parents:
3624
diff
changeset
|
209 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
3547 | 210 |
return cell; |
211 |
} |
|
212 |
||
3659 | 213 |
-(CGFloat) tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
214 |
if (0 == [indexPath section]) |
|
215 |
return aTableView.rowHeight; |
|
216 |
else |
|
217 |
return 120; |
|
218 |
} |
|
3547 | 219 |
|
3701 | 220 |
-(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { |
221 |
NSString *sectionTitle = nil; |
|
222 |
switch (section) { |
|
223 |
case 0: |
|
224 |
sectionTitle = NSLocalizedString(@"Weaponset Name", @""); |
|
225 |
break; |
|
226 |
case 1: |
|
227 |
sectionTitle = NSLocalizedString(@"Weapon Ammuntions", @""); |
|
228 |
break; |
|
229 |
default: |
|
230 |
DLog(@"nope"); |
|
231 |
break; |
|
232 |
} |
|
233 |
return sectionTitle; |
|
234 |
} |
|
235 |
||
3547 | 236 |
#pragma mark - |
237 |
#pragma mark Table view delegate |
|
3659 | 238 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
239 |
if (0 == [indexPath section]) { |
|
240 |
EditableCellView *editableCell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
|
241 |
[editableCell replyKeyboard]; |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
#pragma mark - |
|
246 |
#pragma mark editableCellView delegate |
|
247 |
// set the new value |
|
3697 | 248 |
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { |
3659 | 249 |
// delete old file |
250 |
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL]; |
|
251 |
// update filename |
|
252 |
self.weaponName = textString; |
|
253 |
// save new file |
|
254 |
[self saveAmmos]; |
|
3547 | 255 |
} |
256 |
||
3624 | 257 |
#pragma mark - |
258 |
#pragma mark WeaponButtonControllerDelegate |
|
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
259 |
-(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
|
260 |
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
|
261 |
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
|
262 |
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
|
263 |
crateness[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:3] intValue]] characterAtIndex:0]; |
3624 | 264 |
} |
3547 | 265 |
|
266 |
#pragma mark - |
|
267 |
#pragma mark Memory management |
|
3621 | 268 |
-(void) didReceiveMemoryWarning { |
3547 | 269 |
[super didReceiveMemoryWarning]; |
270 |
} |
|
271 |
||
3621 | 272 |
-(void) viewDidUnload { |
3659 | 273 |
free(quantity); quantity = NULL; |
274 |
free(probability); probability = NULL; |
|
275 |
free(delay); delay = NULL; |
|
276 |
free(crateness); crateness = NULL; |
|
3621 | 277 |
[super viewDidUnload]; |
3659 | 278 |
self.weaponName = nil; |
279 |
self.ammoStoreImage = nil; |
|
280 |
self.ammoNames = nil; |
|
3621 | 281 |
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
|
282 |
[super viewDidUnload]; |
3547 | 283 |
} |
284 |
||
285 |
||
3621 | 286 |
-(void) dealloc { |
3659 | 287 |
[weaponName release]; |
288 |
[ammoStoreImage release]; |
|
289 |
[ammoNames release]; |
|
3547 | 290 |
[super dealloc]; |
291 |
} |
|
292 |
||
293 |
||
294 |
@end |
|
295 |