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