author | sheepluva |
Wed, 22 Mar 2023 20:42:48 +0100 | |
changeset 15931 | 72f4638ee48b |
parent 13166 | ba5c794adae3 |
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 () |
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
23 |
@property (nonatomic, strong) NSString *trPath; |
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
24 |
@property (nonatomic, strong) NSString *trFileName; |
11141
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 |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
30 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
3621 | 31 |
return rotationManager(interfaceOrientation); |
3547 | 32 |
} |
33 |
||
34 |
#pragma mark - |
|
35 |
#pragma mark View lifecycle |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
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 |
|
12871 | 49 |
NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos_base.png", GRAPHICS_DIRECTORY()]; |
3621 | 50 |
UIImage *img = [[UIImage alloc] initWithContentsOfFile:str]; |
51 |
self.ammoStoreImage = img; |
|
8441 | 52 |
|
3659 | 53 |
self.title = NSLocalizedString(@"Edit weapons preferences",@""); |
3547 | 54 |
} |
3621 | 55 |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
56 |
- (void)viewWillAppear:(BOOL) animated { |
3621 | 57 |
[super viewWillAppear:animated]; |
8441 | 58 |
|
3659 | 59 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 60 |
NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile]; |
8441 | 61 |
|
3782 | 62 |
self.description = [weapon objectForKey:@"description"]; |
3621 | 63 |
const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String]; |
64 |
const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String]; |
|
65 |
const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String]; |
|
66 |
const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String]; |
|
8441 | 67 |
|
3621 | 68 |
// if the new weaponset is diffrent from the older we need to update it replacing |
69 |
// the missing ammos with 0 quantity |
|
70 |
int oldlen = strlen(tmp1); |
|
71 |
for (int i = 0; i < oldlen; i++) { |
|
72 |
quantity[i] = tmp1[i]; |
|
73 |
probability[i] = tmp2[i]; |
|
74 |
delay[i] = tmp3[i]; |
|
75 |
crateness[i] = tmp4[i]; |
|
76 |
} |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
77 |
for (int i = oldlen; i < HW_getNumberOfWeapons(); i++) { |
3621 | 78 |
quantity[i] = '0'; |
79 |
probability[i] = '0'; |
|
80 |
delay[i] = '0'; |
|
81 |
crateness[i] = '0'; |
|
82 |
} |
|
8441 | 83 |
|
3621 | 84 |
[self.tableView reloadData]; |
3547 | 85 |
} |
3621 | 86 |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
87 |
- (void)viewWillDisappear:(BOOL) animated { |
3621 | 88 |
[super viewWillDisappear:animated]; |
3659 | 89 |
[self saveAmmos]; |
90 |
} |
|
91 |
||
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
92 |
- (void)saveAmmos { |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
93 |
quantity[HW_getNumberOfWeapons()] = '\0'; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
94 |
probability[HW_getNumberOfWeapons()] = '\0'; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
95 |
delay[HW_getNumberOfWeapons()] = '\0'; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
96 |
crateness[HW_getNumberOfWeapons()] = '\0'; |
8441 | 97 |
|
3621 | 98 |
NSString *quantityStr = [NSString stringWithUTF8String:quantity]; |
99 |
NSString *probabilityStr = [NSString stringWithUTF8String:probability]; |
|
100 |
NSString *delayStr = [NSString stringWithUTF8String:delay]; |
|
101 |
NSString *cratenessStr = [NSString stringWithUTF8String:crateness]; |
|
8441 | 102 |
|
3621 | 103 |
NSDictionary *weapon = [[NSDictionary alloc] initWithObjectsAndKeys: |
104 |
quantityStr,@"ammostore_initialqt", |
|
105 |
probabilityStr,@"ammostore_probability", |
|
106 |
delayStr,@"ammostore_delay", |
|
8441 | 107 |
cratenessStr,@"ammostore_crate", |
3782 | 108 |
self.description,@"description", |
109 |
nil]; |
|
8441 | 110 |
|
3659 | 111 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 112 |
[weapon writeToFile:ammoFile atomically:YES]; |
3547 | 113 |
} |
114 |
||
115 |
#pragma mark - |
|
116 |
#pragma mark Table view data source |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
117 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
3659 | 118 |
return 2; |
3547 | 119 |
} |
120 |
||
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
121 |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
3659 | 122 |
if (section == 0) |
3782 | 123 |
return 2; |
3659 | 124 |
else |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
125 |
return HW_getNumberOfWeapons(); |
3547 | 126 |
} |
127 |
||
128 |
// Customize the appearance of table view cells. |
|
3659 | 129 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
130 |
static NSString *CellIdentifier0 = @"Cell0"; |
|
131 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
3621 | 132 |
NSInteger row = [indexPath row]; |
3659 | 133 |
UITableViewCell *cell = nil; |
8441 | 134 |
|
3659 | 135 |
if (0 == [indexPath section]) { |
3782 | 136 |
EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
137 |
if (editableCell == nil) { |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
138 |
editableCell = [[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
139 |
reuseIdentifier:CellIdentifier0]; |
3782 | 140 |
editableCell.delegate = self; |
3659 | 141 |
} |
3782 | 142 |
editableCell.tag = row; |
143 |
editableCell.selectionStyle = UITableViewCellSelectionStyleNone; |
|
144 |
editableCell.imageView.image = nil; |
|
145 |
editableCell.detailTextLabel.text = nil; |
|
8441 | 146 |
|
3782 | 147 |
if (row == 0) { |
148 |
editableCell.textField.text = self.weaponName; |
|
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
3930
diff
changeset
|
149 |
editableCell.textField.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
3782 | 150 |
} else { |
3825 | 151 |
editableCell.minimumCharacters = 0; |
3782 | 152 |
editableCell.textField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; |
153 |
editableCell.textField.text = self.description; |
|
154 |
editableCell.textField.placeholder = NSLocalizedString(@"You can add a description if you wish",@""); |
|
155 |
} |
|
156 |
cell = editableCell; |
|
3659 | 157 |
} else { |
3782 | 158 |
WeaponCellView *weaponCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
159 |
if (weaponCell == nil) { |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
160 |
weaponCell = [[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]; |
3782 | 161 |
weaponCell.delegate = self; |
3659 | 162 |
} |
3697 | 163 |
|
6209 | 164 |
CGFloat theScale = [[UIScreen mainScreen] safeScale]; |
165 |
int size = 32 * theScale; |
|
166 |
int corners = 8 * theScale; |
|
167 |
int x = ((row*size)/(int)(self.ammoStoreImage.size.height * theScale))*size; |
|
168 |
int y = (row*size)%(int)(self.ammoStoreImage.size.height * theScale); |
|
3697 | 169 |
|
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
|
170 |
UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, size, size)] makeRoundCornersOfSize:CGSizeMake(corners, corners)]; |
3782 | 171 |
weaponCell.weaponIcon.image = img; |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
172 |
weaponCell.weaponName.text = [NSString stringWithUTF8String:HW_getWeaponNameByIndex(row)]; |
3782 | 173 |
weaponCell.tag = row; |
3697 | 174 |
|
3782 | 175 |
[weaponCell.initialSli setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO]; |
176 |
[weaponCell.probabilitySli setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO]; |
|
177 |
[weaponCell.delaySli setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO]; |
|
178 |
[weaponCell.crateSli setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO]; |
|
179 |
cell = weaponCell; |
|
3547 | 180 |
} |
3697 | 181 |
|
3643
858b20bafb6e
reworked the ammunition configuration page (visually)
koda
parents:
3624
diff
changeset
|
182 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
3547 | 183 |
return cell; |
184 |
} |
|
185 |
||
13166
ba5c794adae3
- Some not-so-important pretty formatting
antonc27 <antonc27@mail.ru>
parents:
12872
diff
changeset
|
186 |
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
3659 | 187 |
if (0 == [indexPath section]) |
188 |
return aTableView.rowHeight; |
|
189 |
else |
|
6671 | 190 |
return IS_ON_PORTRAIT() ? 208 : 120; |
3659 | 191 |
} |
3547 | 192 |
|
13166
ba5c794adae3
- Some not-so-important pretty formatting
antonc27 <antonc27@mail.ru>
parents:
12872
diff
changeset
|
193 |
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { |
3701 | 194 |
NSString *sectionTitle = nil; |
195 |
switch (section) { |
|
196 |
case 0: |
|
197 |
sectionTitle = NSLocalizedString(@"Weaponset Name", @""); |
|
198 |
break; |
|
199 |
case 1: |
|
200 |
sectionTitle = NSLocalizedString(@"Weapon Ammuntions", @""); |
|
201 |
break; |
|
202 |
default: |
|
203 |
DLog(@"nope"); |
|
204 |
break; |
|
205 |
} |
|
206 |
return sectionTitle; |
|
207 |
} |
|
208 |
||
3547 | 209 |
#pragma mark - |
210 |
#pragma mark Table view delegate |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
211 |
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
3659 | 212 |
if (0 == [indexPath section]) { |
213 |
EditableCellView *editableCell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
|
214 |
[editableCell replyKeyboard]; |
|
215 |
} |
|
216 |
} |
|
217 |
||
218 |
#pragma mark - |
|
219 |
#pragma mark editableCellView delegate |
|
220 |
// set the new value |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
221 |
- (void)saveTextFieldValue:(NSString *)textString withTag:(NSInteger)tagValue { |
3782 | 222 |
if (tagValue == 0) { |
223 |
// delete old file |
|
224 |
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL]; |
|
225 |
// update filename |
|
226 |
self.weaponName = textString; |
|
227 |
// save new file |
|
228 |
[self saveAmmos]; |
|
229 |
} else { |
|
230 |
self.description = textString; |
|
231 |
} |
|
3547 | 232 |
} |
233 |
||
3624 | 234 |
#pragma mark - |
235 |
#pragma mark WeaponButtonControllerDelegate |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
236 |
- (void)updateValues:(NSArray *)withArray atIndex:(NSInteger)index { |
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
237 |
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
|
238 |
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
|
239 |
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
|
240 |
crateness[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:3] intValue]] characterAtIndex:0]; |
3624 | 241 |
} |
3547 | 242 |
|
243 |
#pragma mark - |
|
244 |
#pragma mark Memory management |
|
245 |
||
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
12871
diff
changeset
|
246 |
- (void)dealloc { |
3659 | 247 |
free(quantity); quantity = NULL; |
248 |
free(probability); probability = NULL; |
|
249 |
free(delay); delay = NULL; |
|
250 |
free(crateness); crateness = NULL; |
|
3547 | 251 |
} |
252 |
||
253 |
@end |
|
254 |