author | koda |
Sun, 09 Oct 2011 00:24:58 +0200 | |
changeset 6107 | 0741c0f0203e |
parent 6078 | 8c0cc07731e5 |
child 6209 | 074ab6ebcb3e |
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" |
|
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
|
23 |
|
3547 | 24 |
|
25 |
@implementation SingleWeaponViewController |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
26 |
@synthesize weaponName, description, ammoStoreImage; |
3547 | 27 |
|
3621 | 28 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
29 |
return rotationManager(interfaceOrientation); |
|
3547 | 30 |
} |
31 |
||
32 |
#pragma mark - |
|
33 |
#pragma mark View lifecycle |
|
3621 | 34 |
-(void) viewDidLoad { |
3547 | 35 |
[super viewDidLoad]; |
3782 | 36 |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
37 |
NSString *trFilePath = [NSString stringWithFormat:@"%@/en.txt",LOCALE_DIRECTORY()]; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
38 |
// 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
|
39 |
LoadLocaleWrapper([trFilePath UTF8String]); |
3782 | 40 |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
41 |
quantity = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1)); |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
42 |
probability = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1)); |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
43 |
delay = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1)); |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
44 |
crateness = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1)); |
3782 | 45 |
|
3621 | 46 |
NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()]; |
47 |
UIImage *img = [[UIImage alloc] initWithContentsOfFile:str]; |
|
48 |
self.ammoStoreImage = img; |
|
49 |
[img release]; |
|
3782 | 50 |
|
3659 | 51 |
self.title = NSLocalizedString(@"Edit weapons preferences",@""); |
3547 | 52 |
} |
3621 | 53 |
|
54 |
-(void) viewWillAppear:(BOOL) animated { |
|
55 |
[super viewWillAppear:animated]; |
|
3782 | 56 |
|
3659 | 57 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 58 |
NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile]; |
59 |
[ammoFile release]; |
|
3782 | 60 |
|
61 |
self.description = [weapon objectForKey:@"description"]; |
|
3621 | 62 |
const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String]; |
63 |
const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String]; |
|
64 |
const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String]; |
|
65 |
const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String]; |
|
66 |
[weapon release]; |
|
3782 | 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 |
} |
|
3782 | 83 |
|
3621 | 84 |
[self.tableView reloadData]; |
3547 | 85 |
} |
3621 | 86 |
|
87 |
-(void) viewWillDisappear:(BOOL) animated { |
|
88 |
[super viewWillDisappear:animated]; |
|
3659 | 89 |
[self saveAmmos]; |
90 |
} |
|
91 |
||
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'; |
3782 | 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]; |
|
3782 | 102 |
|
3621 | 103 |
NSDictionary *weapon = [[NSDictionary alloc] initWithObjectsAndKeys: |
104 |
quantityStr,@"ammostore_initialqt", |
|
105 |
probabilityStr,@"ammostore_probability", |
|
106 |
delayStr,@"ammostore_delay", |
|
3782 | 107 |
cratenessStr,@"ammostore_crate", |
108 |
self.description,@"description", |
|
109 |
nil]; |
|
110 |
||
3659 | 111 |
NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName]; |
3621 | 112 |
[weapon writeToFile:ammoFile atomically:YES]; |
113 |
[ammoFile release]; |
|
114 |
[weapon release]; |
|
3547 | 115 |
} |
116 |
||
117 |
#pragma mark - |
|
118 |
#pragma mark Table view data source |
|
3621 | 119 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
3659 | 120 |
return 2; |
3547 | 121 |
} |
122 |
||
3621 | 123 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
3659 | 124 |
if (section == 0) |
3782 | 125 |
return 2; |
3659 | 126 |
else |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
127 |
return HW_getNumberOfWeapons(); |
3547 | 128 |
} |
129 |
||
130 |
// Customize the appearance of table view cells. |
|
3659 | 131 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
132 |
static NSString *CellIdentifier0 = @"Cell0"; |
|
133 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
3621 | 134 |
NSInteger row = [indexPath row]; |
3659 | 135 |
UITableViewCell *cell = nil; |
3782 | 136 |
|
3659 | 137 |
if (0 == [indexPath section]) { |
3782 | 138 |
EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
139 |
if (editableCell == nil) { |
|
140 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault |
|
141 |
reuseIdentifier:CellIdentifier0] autorelease]; |
|
142 |
editableCell.delegate = self; |
|
3659 | 143 |
} |
3782 | 144 |
editableCell.tag = row; |
145 |
editableCell.selectionStyle = UITableViewCellSelectionStyleNone; |
|
146 |
editableCell.imageView.image = nil; |
|
147 |
editableCell.detailTextLabel.text = nil; |
|
148 |
||
149 |
if (row == 0) { |
|
150 |
editableCell.textField.text = self.weaponName; |
|
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
3930
diff
changeset
|
151 |
editableCell.textField.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
3782 | 152 |
} else { |
3825 | 153 |
editableCell.minimumCharacters = 0; |
3782 | 154 |
editableCell.textField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; |
155 |
editableCell.textField.text = self.description; |
|
156 |
editableCell.textField.placeholder = NSLocalizedString(@"You can add a description if you wish",@""); |
|
157 |
} |
|
158 |
cell = editableCell; |
|
3659 | 159 |
} else { |
3782 | 160 |
WeaponCellView *weaponCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
161 |
if (weaponCell == nil) { |
|
162 |
weaponCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
|
163 |
weaponCell.delegate = self; |
|
3659 | 164 |
} |
3697 | 165 |
|
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
|
166 |
int size = 32 * [[UIScreen mainScreen] scale]; |
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
|
167 |
int corners = 8 * [[UIScreen mainScreen] scale]; |
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
|
168 |
int x = ((row*size)/(int)(self.ammoStoreImage.size.height * [[UIScreen mainScreen] scale]))*size; |
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
|
169 |
int y = (row*size)%(int)(self.ammoStoreImage.size.height * [[UIScreen mainScreen] scale]); |
3697 | 170 |
|
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
|
171 |
UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, size, size)] makeRoundCornersOfSize:CGSizeMake(corners, corners)]; |
3782 | 172 |
weaponCell.weaponIcon.image = img; |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
173 |
weaponCell.weaponName.text = [NSString stringWithUTF8String:HW_getWeaponNameByIndex(row)]; |
3782 | 174 |
weaponCell.tag = row; |
3697 | 175 |
|
3782 | 176 |
[weaponCell.initialSli setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO]; |
177 |
[weaponCell.probabilitySli setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO]; |
|
178 |
[weaponCell.delaySli setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO]; |
|
179 |
[weaponCell.crateSli setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO]; |
|
180 |
cell = weaponCell; |
|
3547 | 181 |
} |
3697 | 182 |
|
3643
858b20bafb6e
reworked the ammunition configuration page (visually)
koda
parents:
3624
diff
changeset
|
183 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
3547 | 184 |
return cell; |
185 |
} |
|
186 |
||
3659 | 187 |
-(CGFloat) tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
188 |
if (0 == [indexPath section]) |
|
189 |
return aTableView.rowHeight; |
|
190 |
else |
|
191 |
return 120; |
|
192 |
} |
|
3547 | 193 |
|
3701 | 194 |
-(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { |
195 |
NSString *sectionTitle = nil; |
|
196 |
switch (section) { |
|
197 |
case 0: |
|
198 |
sectionTitle = NSLocalizedString(@"Weaponset Name", @""); |
|
199 |
break; |
|
200 |
case 1: |
|
201 |
sectionTitle = NSLocalizedString(@"Weapon Ammuntions", @""); |
|
202 |
break; |
|
203 |
default: |
|
204 |
DLog(@"nope"); |
|
205 |
break; |
|
206 |
} |
|
207 |
return sectionTitle; |
|
208 |
} |
|
209 |
||
3547 | 210 |
#pragma mark - |
211 |
#pragma mark Table view delegate |
|
3659 | 212 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
213 |
if (0 == [indexPath section]) { |
|
214 |
EditableCellView *editableCell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath]; |
|
215 |
[editableCell replyKeyboard]; |
|
216 |
} |
|
217 |
} |
|
218 |
||
219 |
#pragma mark - |
|
220 |
#pragma mark editableCellView delegate |
|
221 |
// set the new value |
|
3697 | 222 |
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { |
3782 | 223 |
if (tagValue == 0) { |
224 |
// delete old file |
|
225 |
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL]; |
|
226 |
// update filename |
|
227 |
self.weaponName = textString; |
|
228 |
// save new file |
|
229 |
[self saveAmmos]; |
|
230 |
} else { |
|
231 |
self.description = textString; |
|
232 |
} |
|
3547 | 233 |
} |
234 |
||
3624 | 235 |
#pragma mark - |
236 |
#pragma mark WeaponButtonControllerDelegate |
|
3644
42c5684289ae
finished ammo configuration (which is the last page missing in config \o/)
koda
parents:
3643
diff
changeset
|
237 |
-(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
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
crateness[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:3] intValue]] characterAtIndex:0]; |
3624 | 242 |
} |
3547 | 243 |
|
244 |
#pragma mark - |
|
245 |
#pragma mark Memory management |
|
3621 | 246 |
-(void) didReceiveMemoryWarning { |
3547 | 247 |
[super didReceiveMemoryWarning]; |
248 |
} |
|
249 |
||
3621 | 250 |
-(void) viewDidUnload { |
3659 | 251 |
free(quantity); quantity = NULL; |
252 |
free(probability); probability = NULL; |
|
253 |
free(delay); delay = NULL; |
|
254 |
free(crateness); crateness = NULL; |
|
3621 | 255 |
[super viewDidUnload]; |
3782 | 256 |
self.description = nil; |
3659 | 257 |
self.weaponName = nil; |
258 |
self.ammoStoreImage = nil; |
|
3621 | 259 |
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
|
260 |
[super viewDidUnload]; |
3547 | 261 |
} |
262 |
||
263 |
||
3621 | 264 |
-(void) dealloc { |
5208 | 265 |
releaseAndNil(weaponName); |
266 |
releaseAndNil(description); |
|
267 |
releaseAndNil(ammoStoreImage); |
|
3547 | 268 |
[super dealloc]; |
269 |
} |
|
270 |
||
271 |
||
272 |
@end |
|
273 |