author | unc0rr |
Thu, 22 Sep 2011 11:44:39 +0400 | |
changeset 5989 | 23407ecb1826 |
parent 5976 | 306cedbeb213 |
child 6078 | 8c0cc07731e5 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
4976 | 3 |
* Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 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 "CommodityFunctions.h" |
24 |
#import "UIImageExtra.h" |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
25 |
#import "PascalImports.h" |
3547 | 26 |
|
27 |
@implementation SingleWeaponViewController |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
28 |
@synthesize weaponName, description, ammoStoreImage; |
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 |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
39 |
NSString *trFilePath = [NSString stringWithFormat:@"%@/en.txt",LOCALE_DIRECTORY()]; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
40 |
// fill the data structure that we are going to read |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
41 |
LoadLocaleWrapper([trFilePath UTF8String]); |
3782 | 42 |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
43 |
quantity = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1)); |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
44 |
probability = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1)); |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
45 |
delay = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1)); |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
46 |
crateness = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1)); |
3782 | 47 |
|
3621 | 48 |
NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()]; |
49 |
UIImage *img = [[UIImage alloc] initWithContentsOfFile:str]; |
|
50 |
self.ammoStoreImage = img; |
|
51 |
[img release]; |
|
3782 | 52 |
|
3659 | 53 |
self.title = NSLocalizedString(@"Edit weapons preferences",@""); |
3547 | 54 |
} |
3621 | 55 |
|
56 |
-(void) viewWillAppear:(BOOL) animated { |
|
57 |
[super viewWillAppear:animated]; |
|
3782 | 58 |
|
3659 | 59 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 60 |
NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile]; |
61 |
[ammoFile release]; |
|
3782 | 62 |
|
63 |
self.description = [weapon objectForKey:@"description"]; |
|
3621 | 64 |
const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String]; |
65 |
const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String]; |
|
66 |
const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String]; |
|
67 |
const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String]; |
|
68 |
[weapon release]; |
|
3782 | 69 |
|
3621 | 70 |
// if the new weaponset is diffrent from the older we need to update it replacing |
71 |
// the missing ammos with 0 quantity |
|
72 |
int oldlen = strlen(tmp1); |
|
73 |
for (int i = 0; i < oldlen; i++) { |
|
74 |
quantity[i] = tmp1[i]; |
|
75 |
probability[i] = tmp2[i]; |
|
76 |
delay[i] = tmp3[i]; |
|
77 |
crateness[i] = tmp4[i]; |
|
78 |
} |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
79 |
for (int i = oldlen; i < HW_getNumberOfWeapons(); i++) { |
3621 | 80 |
quantity[i] = '0'; |
81 |
probability[i] = '0'; |
|
82 |
delay[i] = '0'; |
|
83 |
crateness[i] = '0'; |
|
84 |
} |
|
3782 | 85 |
|
3621 | 86 |
[self.tableView reloadData]; |
3547 | 87 |
} |
3621 | 88 |
|
89 |
-(void) viewWillDisappear:(BOOL) animated { |
|
90 |
[super viewWillDisappear:animated]; |
|
3659 | 91 |
[self saveAmmos]; |
92 |
} |
|
93 |
||
94 |
-(void) saveAmmos { |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
95 |
quantity[HW_getNumberOfWeapons()] = '\0'; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
96 |
probability[HW_getNumberOfWeapons()] = '\0'; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
97 |
delay[HW_getNumberOfWeapons()] = '\0'; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
98 |
crateness[HW_getNumberOfWeapons()] = '\0'; |
3782 | 99 |
|
3621 | 100 |
NSString *quantityStr = [NSString stringWithUTF8String:quantity]; |
101 |
NSString *probabilityStr = [NSString stringWithUTF8String:probability]; |
|
102 |
NSString *delayStr = [NSString stringWithUTF8String:delay]; |
|
103 |
NSString *cratenessStr = [NSString stringWithUTF8String:crateness]; |
|
3782 | 104 |
|
3621 | 105 |
NSDictionary *weapon = [[NSDictionary alloc] initWithObjectsAndKeys: |
106 |
quantityStr,@"ammostore_initialqt", |
|
107 |
probabilityStr,@"ammostore_probability", |
|
108 |
delayStr,@"ammostore_delay", |
|
3782 | 109 |
cratenessStr,@"ammostore_crate", |
110 |
self.description,@"description", |
|
111 |
nil]; |
|
112 |
||
3659 | 113 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 114 |
[weapon writeToFile:ammoFile atomically:YES]; |
115 |
[ammoFile release]; |
|
116 |
[weapon release]; |
|
3547 | 117 |
} |
118 |
||
119 |
#pragma mark - |
|
120 |
#pragma mark Table view data source |
|
3621 | 121 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
3659 | 122 |
return 2; |
3547 | 123 |
} |
124 |
||
3621 | 125 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
3659 | 126 |
if (section == 0) |
3782 | 127 |
return 2; |
3659 | 128 |
else |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
129 |
return HW_getNumberOfWeapons(); |
3547 | 130 |
} |
131 |
||
132 |
// Customize the appearance of table view cells. |
|
3659 | 133 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
134 |
static NSString *CellIdentifier0 = @"Cell0"; |
|
135 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
3621 | 136 |
NSInteger row = [indexPath row]; |
3659 | 137 |
UITableViewCell *cell = nil; |
3782 | 138 |
|
3659 | 139 |
if (0 == [indexPath section]) { |
3782 | 140 |
EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
141 |
if (editableCell == nil) { |
|
142 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
|
143 |
reuseIdentifier:CellIdentifier0] autorelease]; |
|
144 |
editableCell.delegate = self; |
|
3659 | 145 |
} |
3782 | 146 |
editableCell.tag = row; |
147 |
editableCell.selectionStyle = UITableViewCellSelectionStyleNone; |
|
148 |
editableCell.imageView.image = nil; |
|
149 |
editableCell.detailTextLabel.text = nil; |
|
150 |
||
151 |
if (row == 0) { |
|
152 |
editableCell.textField.text = self.weaponName; |
|
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
3930
diff
changeset
|
153 |
editableCell.textField.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
3782 | 154 |
} else { |
3825 | 155 |
editableCell.minimumCharacters = 0; |
3782 | 156 |
editableCell.textField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; |
157 |
editableCell.textField.text = self.description; |
|
158 |
editableCell.textField.placeholder = NSLocalizedString(@"You can add a description if you wish",@""); |
|
159 |
} |
|
160 |
cell = editableCell; |
|
3659 | 161 |
} else { |
3782 | 162 |
WeaponCellView *weaponCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
163 |
if (weaponCell == nil) { |
|
164 |
weaponCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
|
165 |
weaponCell.delegate = self; |
|
3659 | 166 |
} |
3697 | 167 |
|
5976
306cedbeb213
fixed several 'retina display' images and added a bunch more (though just from upscaling the normal ones; might be nice to have 'em redrawn at a real higher resolution)
koda
parents:
5208
diff
changeset
|
168 |
int size = 32 * getScreenScale(); |
306cedbeb213
fixed several 'retina display' images and added a bunch more (though just from upscaling the normal ones; might be nice to have 'em redrawn at a real higher resolution)
koda
parents:
5208
diff
changeset
|
169 |
int corners = 8 * getScreenScale(); |
306cedbeb213
fixed several 'retina display' images and added a bunch more (though just from upscaling the normal ones; might be nice to have 'em redrawn at a real higher resolution)
koda
parents:
5208
diff
changeset
|
170 |
int x = ((row*size)/(int)(self.ammoStoreImage.size.height*getScreenScale()))*size; |
306cedbeb213
fixed several 'retina display' images and added a bunch more (though just from upscaling the normal ones; might be nice to have 'em redrawn at a real higher resolution)
koda
parents:
5208
diff
changeset
|
171 |
int y = (row*size)%(int)(self.ammoStoreImage.size.height*getScreenScale()); |
3697 | 172 |
|
5976
306cedbeb213
fixed several 'retina display' images and added a bunch more (though just from upscaling the normal ones; might be nice to have 'em redrawn at a real higher resolution)
koda
parents:
5208
diff
changeset
|
173 |
UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, size, size)] makeRoundCornersOfSize:CGSizeMake(corners, corners)]; |
3782 | 174 |
weaponCell.weaponIcon.image = img; |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
175 |
weaponCell.weaponName.text = [NSString stringWithUTF8String:HW_getWeaponNameByIndex(row)]; |
3782 | 176 |
weaponCell.tag = row; |
3697 | 177 |
|
3782 | 178 |
[weaponCell.initialSli setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO]; |
179 |
[weaponCell.probabilitySli setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO]; |
|
180 |
[weaponCell.delaySli setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO]; |
|
181 |
[weaponCell.crateSli setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO]; |
|
182 |
cell = weaponCell; |
|
3547 | 183 |
} |
3697 | 184 |
|
3643
858b20bafb6e
reworked the ammunition configuration page (visually)
koda
parents:
3624
diff
changeset
|
185 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
3547 | 186 |
return cell; |
187 |
} |
|
188 |
||
3659 | 189 |
-(CGFloat) tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
190 |
if (0 == [indexPath section]) |
|
191 |
return aTableView.rowHeight; |
|
192 |
else |
|
193 |
return 120; |
|
194 |
} |
|
3547 | 195 |
|
3701 | 196 |
-(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { |
197 |
NSString *sectionTitle = nil; |
|
198 |
switch (section) { |
|
199 |
case 0: |
|
200 |
sectionTitle = NSLocalizedString(@"Weaponset Name", @""); |
|
201 |
break; |
|
202 |
case 1: |
|
203 |
sectionTitle = NSLocalizedString(@"Weapon Ammuntions", @""); |
|
204 |
break; |
|
205 |
default: |
|
206 |
DLog(@"nope"); |
|
207 |
break; |
|
208 |
} |
|
209 |
return sectionTitle; |
|
210 |
} |
|
211 |
||
3547 | 212 |
#pragma mark - |
213 |
#pragma mark Table view delegate |
|
3659 | 214 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
215 |
if (0 == [indexPath section]) { |
|
216 |
EditableCellView *editableCell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
|
217 |
[editableCell replyKeyboard]; |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
#pragma mark - |
|
222 |
#pragma mark editableCellView delegate |
|
223 |
// set the new value |
|
3697 | 224 |
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { |
3782 | 225 |
if (tagValue == 0) { |
226 |
// delete old file |
|
227 |
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL]; |
|
228 |
// update filename |
|
229 |
self.weaponName = textString; |
|
230 |
// save new file |
|
231 |
[self saveAmmos]; |
|
232 |
} else { |
|
233 |
self.description = textString; |
|
234 |
} |
|
3547 | 235 |
} |
236 |
||
3624 | 237 |
#pragma mark - |
238 |
#pragma mark WeaponButtonControllerDelegate |
|
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
239 |
-(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
|
240 |
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
|
241 |
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
|
242 |
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
|
243 |
crateness[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:3] intValue]] characterAtIndex:0]; |
3624 | 244 |
} |
3547 | 245 |
|
246 |
#pragma mark - |
|
247 |
#pragma mark Memory management |
|
3621 | 248 |
-(void) didReceiveMemoryWarning { |
3547 | 249 |
[super didReceiveMemoryWarning]; |
250 |
} |
|
251 |
||
3621 | 252 |
-(void) viewDidUnload { |
3659 | 253 |
free(quantity); quantity = NULL; |
254 |
free(probability); probability = NULL; |
|
255 |
free(delay); delay = NULL; |
|
256 |
free(crateness); crateness = NULL; |
|
3621 | 257 |
[super viewDidUnload]; |
3782 | 258 |
self.description = nil; |
3659 | 259 |
self.weaponName = nil; |
260 |
self.ammoStoreImage = nil; |
|
3621 | 261 |
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
|
262 |
[super viewDidUnload]; |
3547 | 263 |
} |
264 |
||
265 |
||
3621 | 266 |
-(void) dealloc { |
5208 | 267 |
releaseAndNil(weaponName); |
268 |
releaseAndNil(description); |
|
269 |
releaseAndNil(ammoStoreImage); |
|
3547 | 270 |
[super dealloc]; |
271 |
} |
|
272 |
||
273 |
||
274 |
@end |
|
275 |