author | Wuzzy <Wuzzy2@mail.ru> |
Wed, 14 Mar 2018 15:19:08 +0100 | |
changeset 13197 | 0bc5f618ca7c |
parent 11790 | ac93cfc17365 |
child 12871 | 2c06b1120749 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
6700 | 3 |
* Copyright (c) 2009-2012 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 |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
8441
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
3829 | 17 |
*/ |
18 |
||
3547 | 19 |
|
20 |
#import "SingleWeaponViewController.h" |
|
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5976
diff
changeset
|
21 |
|
11141
01e8e5a6a8c1
- Fix for crash on loading SingleWeaponViewController in Settings
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
22 |
@interface SingleWeaponViewController () |
01e8e5a6a8c1
- Fix for crash on loading SingleWeaponViewController in Settings
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
23 |
@property (nonatomic, retain) NSString *trPath; |
01e8e5a6a8c1
- Fix for crash on loading SingleWeaponViewController in Settings
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
24 |
@property (nonatomic, retain) NSString *trFileName; |
01e8e5a6a8c1
- Fix for crash on loading SingleWeaponViewController in Settings
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
25 |
@end |
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]; |
8441 | 38 |
|
11141
01e8e5a6a8c1
- Fix for crash on loading SingleWeaponViewController in Settings
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
39 |
self.trPath = [NSString stringWithFormat:@"%@", LOCALE_DIRECTORY()]; |
11219
c51ecb9bcf05
- Retrieving language ID moved to HWUtils
antonc27 <antonc27@mail.ru>
parents:
11200
diff
changeset
|
40 |
self.trFileName = [NSString stringWithFormat:@"%@.txt", [HWUtils languageID]]; |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
41 |
// fill the data structure that we are going to read |
11790
ac93cfc17365
- Pass also userPath to LoadLocaleWrapper
antonc27 <antonc27@mail.ru>
parents:
11219
diff
changeset
|
42 |
LoadLocaleWrapper([self.trPath UTF8String], [DOCUMENTS_FOLDER() UTF8String], [self.trFileName UTF8String]); |
8441 | 43 |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
44 |
quantity = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1)); |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
45 |
probability = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1)); |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
46 |
delay = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1)); |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
47 |
crateness = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1)); |
8441 | 48 |
|
3621 | 49 |
NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()]; |
50 |
UIImage *img = [[UIImage alloc] initWithContentsOfFile:str]; |
|
51 |
self.ammoStoreImage = img; |
|
52 |
[img release]; |
|
8441 | 53 |
|
3659 | 54 |
self.title = NSLocalizedString(@"Edit weapons preferences",@""); |
3547 | 55 |
} |
3621 | 56 |
|
57 |
-(void) viewWillAppear:(BOOL) animated { |
|
58 |
[super viewWillAppear:animated]; |
|
8441 | 59 |
|
3659 | 60 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 61 |
NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile]; |
62 |
[ammoFile release]; |
|
8441 | 63 |
|
3782 | 64 |
self.description = [weapon objectForKey:@"description"]; |
3621 | 65 |
const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String]; |
66 |
const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String]; |
|
67 |
const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String]; |
|
68 |
const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String]; |
|
69 |
[weapon release]; |
|
8441 | 70 |
|
3621 | 71 |
// if the new weaponset is diffrent from the older we need to update it replacing |
72 |
// the missing ammos with 0 quantity |
|
73 |
int oldlen = strlen(tmp1); |
|
74 |
for (int i = 0; i < oldlen; i++) { |
|
75 |
quantity[i] = tmp1[i]; |
|
76 |
probability[i] = tmp2[i]; |
|
77 |
delay[i] = tmp3[i]; |
|
78 |
crateness[i] = tmp4[i]; |
|
79 |
} |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
80 |
for (int i = oldlen; i < HW_getNumberOfWeapons(); i++) { |
3621 | 81 |
quantity[i] = '0'; |
82 |
probability[i] = '0'; |
|
83 |
delay[i] = '0'; |
|
84 |
crateness[i] = '0'; |
|
85 |
} |
|
8441 | 86 |
|
3621 | 87 |
[self.tableView reloadData]; |
3547 | 88 |
} |
3621 | 89 |
|
90 |
-(void) viewWillDisappear:(BOOL) animated { |
|
91 |
[super viewWillDisappear:animated]; |
|
3659 | 92 |
[self saveAmmos]; |
93 |
} |
|
94 |
||
95 |
-(void) saveAmmos { |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
96 |
quantity[HW_getNumberOfWeapons()] = '\0'; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
97 |
probability[HW_getNumberOfWeapons()] = '\0'; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
98 |
delay[HW_getNumberOfWeapons()] = '\0'; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
99 |
crateness[HW_getNumberOfWeapons()] = '\0'; |
8441 | 100 |
|
3621 | 101 |
NSString *quantityStr = [NSString stringWithUTF8String:quantity]; |
102 |
NSString *probabilityStr = [NSString stringWithUTF8String:probability]; |
|
103 |
NSString *delayStr = [NSString stringWithUTF8String:delay]; |
|
104 |
NSString *cratenessStr = [NSString stringWithUTF8String:crateness]; |
|
8441 | 105 |
|
3621 | 106 |
NSDictionary *weapon = [[NSDictionary alloc] initWithObjectsAndKeys: |
107 |
quantityStr,@"ammostore_initialqt", |
|
108 |
probabilityStr,@"ammostore_probability", |
|
109 |
delayStr,@"ammostore_delay", |
|
8441 | 110 |
cratenessStr,@"ammostore_crate", |
3782 | 111 |
self.description,@"description", |
112 |
nil]; |
|
8441 | 113 |
|
3659 | 114 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 115 |
[weapon writeToFile:ammoFile atomically:YES]; |
116 |
[ammoFile release]; |
|
117 |
[weapon release]; |
|
3547 | 118 |
} |
119 |
||
120 |
#pragma mark - |
|
121 |
#pragma mark Table view data source |
|
3621 | 122 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
3659 | 123 |
return 2; |
3547 | 124 |
} |
125 |
||
3621 | 126 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
3659 | 127 |
if (section == 0) |
3782 | 128 |
return 2; |
3659 | 129 |
else |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
130 |
return HW_getNumberOfWeapons(); |
3547 | 131 |
} |
132 |
||
133 |
// Customize the appearance of table view cells. |
|
3659 | 134 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
135 |
static NSString *CellIdentifier0 = @"Cell0"; |
|
136 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
3621 | 137 |
NSInteger row = [indexPath row]; |
3659 | 138 |
UITableViewCell *cell = nil; |
8441 | 139 |
|
3659 | 140 |
if (0 == [indexPath section]) { |
3782 | 141 |
EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
142 |
if (editableCell == nil) { |
|
143 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
|
144 |
reuseIdentifier:CellIdentifier0] autorelease]; |
|
145 |
editableCell.delegate = self; |
|
3659 | 146 |
} |
3782 | 147 |
editableCell.tag = row; |
148 |
editableCell.selectionStyle = UITableViewCellSelectionStyleNone; |
|
149 |
editableCell.imageView.image = nil; |
|
150 |
editableCell.detailTextLabel.text = nil; |
|
8441 | 151 |
|
3782 | 152 |
if (row == 0) { |
153 |
editableCell.textField.text = self.weaponName; |
|
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
3930
diff
changeset
|
154 |
editableCell.textField.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
3782 | 155 |
} else { |
3825 | 156 |
editableCell.minimumCharacters = 0; |
3782 | 157 |
editableCell.textField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; |
158 |
editableCell.textField.text = self.description; |
|
159 |
editableCell.textField.placeholder = NSLocalizedString(@"You can add a description if you wish",@""); |
|
160 |
} |
|
161 |
cell = editableCell; |
|
3659 | 162 |
} else { |
3782 | 163 |
WeaponCellView *weaponCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
164 |
if (weaponCell == nil) { |
|
165 |
weaponCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
|
166 |
weaponCell.delegate = self; |
|
3659 | 167 |
} |
3697 | 168 |
|
6209 | 169 |
CGFloat theScale = [[UIScreen mainScreen] safeScale]; |
170 |
int size = 32 * theScale; |
|
171 |
int corners = 8 * theScale; |
|
172 |
int x = ((row*size)/(int)(self.ammoStoreImage.size.height * theScale))*size; |
|
173 |
int y = (row*size)%(int)(self.ammoStoreImage.size.height * theScale); |
|
3697 | 174 |
|
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
|
175 |
UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, size, size)] makeRoundCornersOfSize:CGSizeMake(corners, corners)]; |
3782 | 176 |
weaponCell.weaponIcon.image = img; |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
177 |
weaponCell.weaponName.text = [NSString stringWithUTF8String:HW_getWeaponNameByIndex(row)]; |
3782 | 178 |
weaponCell.tag = row; |
3697 | 179 |
|
3782 | 180 |
[weaponCell.initialSli setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO]; |
181 |
[weaponCell.probabilitySli setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO]; |
|
182 |
[weaponCell.delaySli setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO]; |
|
183 |
[weaponCell.crateSli setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO]; |
|
184 |
cell = weaponCell; |
|
3547 | 185 |
} |
3697 | 186 |
|
3643
858b20bafb6e
reworked the ammunition configuration page (visually)
koda
parents:
3624
diff
changeset
|
187 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
3547 | 188 |
return cell; |
189 |
} |
|
190 |
||
3659 | 191 |
-(CGFloat) tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
192 |
if (0 == [indexPath section]) |
|
193 |
return aTableView.rowHeight; |
|
194 |
else |
|
6671 | 195 |
return IS_ON_PORTRAIT() ? 208 : 120; |
3659 | 196 |
} |
3547 | 197 |
|
3701 | 198 |
-(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { |
199 |
NSString *sectionTitle = nil; |
|
200 |
switch (section) { |
|
201 |
case 0: |
|
202 |
sectionTitle = NSLocalizedString(@"Weaponset Name", @""); |
|
203 |
break; |
|
204 |
case 1: |
|
205 |
sectionTitle = NSLocalizedString(@"Weapon Ammuntions", @""); |
|
206 |
break; |
|
207 |
default: |
|
208 |
DLog(@"nope"); |
|
209 |
break; |
|
210 |
} |
|
211 |
return sectionTitle; |
|
212 |
} |
|
213 |
||
3547 | 214 |
#pragma mark - |
215 |
#pragma mark Table view delegate |
|
3659 | 216 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
217 |
if (0 == [indexPath section]) { |
|
218 |
EditableCellView *editableCell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
|
219 |
[editableCell replyKeyboard]; |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
#pragma mark - |
|
224 |
#pragma mark editableCellView delegate |
|
225 |
// set the new value |
|
3697 | 226 |
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { |
3782 | 227 |
if (tagValue == 0) { |
228 |
// delete old file |
|
229 |
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL]; |
|
230 |
// update filename |
|
231 |
self.weaponName = textString; |
|
232 |
// save new file |
|
233 |
[self saveAmmos]; |
|
234 |
} else { |
|
235 |
self.description = textString; |
|
236 |
} |
|
3547 | 237 |
} |
238 |
||
3624 | 239 |
#pragma mark - |
240 |
#pragma mark WeaponButtonControllerDelegate |
|
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
241 |
-(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
|
242 |
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
|
243 |
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
|
244 |
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
|
245 |
crateness[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:3] intValue]] characterAtIndex:0]; |
3624 | 246 |
} |
3547 | 247 |
|
248 |
#pragma mark - |
|
249 |
#pragma mark Memory management |
|
3621 | 250 |
-(void) didReceiveMemoryWarning { |
3547 | 251 |
[super didReceiveMemoryWarning]; |
252 |
} |
|
253 |
||
3621 | 254 |
-(void) viewDidUnload { |
3659 | 255 |
free(quantity); quantity = NULL; |
256 |
free(probability); probability = NULL; |
|
257 |
free(delay); delay = NULL; |
|
258 |
free(crateness); crateness = NULL; |
|
3621 | 259 |
[super viewDidUnload]; |
3782 | 260 |
self.description = nil; |
3659 | 261 |
self.weaponName = nil; |
262 |
self.ammoStoreImage = nil; |
|
3621 | 263 |
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
|
264 |
[super viewDidUnload]; |
3547 | 265 |
} |
266 |
||
267 |
||
3621 | 268 |
-(void) dealloc { |
11141
01e8e5a6a8c1
- Fix for crash on loading SingleWeaponViewController in Settings
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
269 |
releaseAndNil(_trPath); |
01e8e5a6a8c1
- Fix for crash on loading SingleWeaponViewController in Settings
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
270 |
releaseAndNil(_trFileName); |
01e8e5a6a8c1
- Fix for crash on loading SingleWeaponViewController in Settings
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
271 |
|
5208 | 272 |
releaseAndNil(weaponName); |
273 |
releaseAndNil(description); |
|
274 |
releaseAndNil(ammoStoreImage); |
|
3547 | 275 |
[super dealloc]; |
276 |
} |
|
277 |
||
278 |
||
279 |
@end |
|
280 |