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