1 /* |
|
2 * Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
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 03/10/2010. |
|
19 */ |
|
20 |
|
21 |
|
22 #import "AmmoMenuViewController.h" |
|
23 #import <QuartzCore/QuartzCore.h> |
|
24 |
|
25 |
|
26 #define BTNS_PER_ROW 9 |
|
27 #define DEFAULT_DESCRIPTION IS_IPAD() ? \ |
|
28 NSLocalizedString(@"Hold your finger on a weapon to see what it does.\nYou can move this window anywhere on the screen.",@"") : \ |
|
29 NSLocalizedString(@"Hold your finger on a weapon to see what it does.\nTap anywhere to dismiss.",@"") |
|
30 |
|
31 @implementation AmmoMenuViewController |
|
32 @synthesize imagesArray, buttonsArray, nameLabel, extraLabel, captionLabel, isVisible; |
|
33 |
|
34 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
35 return rotationManager(interfaceOrientation); |
|
36 } |
|
37 |
|
38 #pragma mark - |
|
39 #pragma mark view handling |
|
40 -(void) viewDidLoad { |
|
41 self.view.frame = CGRectMake(0, 0, 480, 320); |
|
42 self.view.backgroundColor = [UIColor blackColor]; |
|
43 self.view.layer.borderColor = [[UIColor whiteColor] CGColor]; |
|
44 self.view.layer.borderWidth = 1.3f; |
|
45 [self.view.layer setCornerRadius:10]; |
|
46 [self.view.layer setMasksToBounds:YES]; |
|
47 self.view.autoresizingMask = UIViewAutoresizingNone; |
|
48 placingPoint = CGPointMake(-1, -1); |
|
49 |
|
50 self.isVisible = NO; |
|
51 delay = (uint8_t *)calloc(HW_getNumberOfWeapons(), sizeof(uint8_t)); |
|
52 HW_getAmmoDelays(delay); |
|
53 |
|
54 shouldUpdateImage = (BOOL *)calloc(HW_getNumberOfWeapons(), sizeof(BOOL)); |
|
55 |
|
56 [super viewDidLoad]; |
|
57 } |
|
58 |
|
59 -(void) viewWillAppear:(BOOL)animated { |
|
60 [self updateAmmoVisuals]; |
|
61 [super viewWillAppear:animated]; |
|
62 } |
|
63 |
|
64 -(void) appearInView:(UIView *)container { |
|
65 [self viewWillAppear:YES]; |
|
66 [container addSubview:self.view]; |
|
67 |
|
68 if (placingPoint.x == -1 || placingPoint.y == -1) |
|
69 placingPoint = container.center; |
|
70 self.view.center = placingPoint; |
|
71 |
|
72 self.isVisible = YES; |
|
73 if (IS_IPAD() == NO) |
|
74 HW_pause(); |
|
75 } |
|
76 |
|
77 -(void) disappear { |
|
78 if (self.isVisible) |
|
79 [self.view removeFromSuperview]; |
|
80 self.isVisible = NO; |
|
81 placingPoint = self.view.center; |
|
82 if (IS_IPAD() == NO) |
|
83 HW_pauseToggle(); |
|
84 } |
|
85 |
|
86 #pragma mark - |
|
87 #pragma mark drawing |
|
88 -(void) loadLabels { |
|
89 int x = 12; |
|
90 int y = (HW_getNumberOfWeapons()/BTNS_PER_ROW)*44 + 18; |
|
91 UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 200, 20)]; |
|
92 name.backgroundColor = [UIColor clearColor]; |
|
93 name.textColor = [UIColor darkYellowColor]; |
|
94 name.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
|
95 self.nameLabel = name; |
|
96 [self.view addSubview:self.nameLabel]; |
|
97 [name release]; |
|
98 |
|
99 UILabel *caption = [[UILabel alloc] initWithFrame:CGRectMake(x+200, y, 220, 20)]; |
|
100 caption.backgroundColor = [UIColor clearColor]; |
|
101 caption.textColor = [UIColor whiteColor]; |
|
102 caption.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]; |
|
103 caption.adjustsFontSizeToFitWidth = YES; |
|
104 caption.minimumFontSize = 8; |
|
105 self.captionLabel = caption; |
|
106 [self.view addSubview:self.captionLabel]; |
|
107 [caption release]; |
|
108 |
|
109 UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(x+2, self.view.frame.size.height-50, 415, 53)]; |
|
110 description.backgroundColor = [UIColor clearColor]; |
|
111 description.textColor = [UIColor whiteColor]; |
|
112 description.text = DEFAULT_DESCRIPTION; |
|
113 description.font = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]]; |
|
114 description.adjustsFontSizeToFitWidth = YES; |
|
115 description.minimumFontSize = 8; |
|
116 description.numberOfLines = 0; |
|
117 self.extraLabel = description; |
|
118 [self.view addSubview:self.extraLabel]; |
|
119 [description release]; |
|
120 } |
|
121 |
|
122 -(void) loadAmmoStuff:(id) object { |
|
123 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
124 NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()]; |
|
125 UIImage *ammoStoreImage = [[UIImage alloc] initWithContentsOfFile:str]; |
|
126 CGFloat theScale = [[UIScreen mainScreen] safeScale]; |
|
127 |
|
128 NSMutableArray *imgs = [[NSMutableArray alloc] initWithCapacity:HW_getNumberOfWeapons()]; |
|
129 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:HW_getNumberOfWeapons()]; |
|
130 int i, j, e; |
|
131 for (i = 0, j = 0, e = 0; i < HW_getNumberOfWeapons(); i++) { |
|
132 int x, y; |
|
133 float w, radius; |
|
134 |
|
135 // move utilities aside and make 'em rounded |
|
136 if (HW_isWeaponAnEffect(i)) { |
|
137 x = 432; |
|
138 y = 20 + 48*e++; |
|
139 w = 1.5; |
|
140 radius = 22; |
|
141 } else { |
|
142 x = 10+(j%BTNS_PER_ROW)*44; |
|
143 y = 10+(j/BTNS_PER_ROW)*44; |
|
144 if (j / BTNS_PER_ROW % 2 != 0) |
|
145 x += 20; |
|
146 w = 1; |
|
147 radius = 6; |
|
148 j++; |
|
149 } |
|
150 |
|
151 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; |
|
152 button.frame = CGRectMake(x, y, 40, 40); |
|
153 button.tag = i; |
|
154 button.layer.borderColor = [[UIColor lightYellowColor] CGColor]; |
|
155 button.layer.borderWidth = w; |
|
156 [button.layer setCornerRadius:radius]; |
|
157 [button.layer setMasksToBounds:YES]; |
|
158 [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown]; |
|
159 [button addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpInside]; |
|
160 [button addTarget:self action:@selector(buttonCancelled:) forControlEvents:UIControlEventTouchUpOutside|UIControlEventTouchCancel]; |
|
161 [button setTitleColor:[UIColor lightYellowColor] forState:UIControlStateNormal]; |
|
162 button.titleLabel.backgroundColor = [UIColor blackColor]; |
|
163 button.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]]; |
|
164 [button.titleLabel.layer setCornerRadius:3]; |
|
165 [button.titleLabel.layer setMasksToBounds:YES]; |
|
166 button.titleLabel.layer.borderColor = [[UIColor whiteColor] CGColor]; |
|
167 button.titleLabel.layer.borderWidth = 1; |
|
168 [self.view addSubview:button]; |
|
169 [array addObject:button]; |
|
170 |
|
171 int size = 32 * theScale; |
|
172 int x_src = ((i*size)/(int)(ammoStoreImage.size.height * theScale))*size; |
|
173 int y_src = (i*size)%(int)(ammoStoreImage.size.height * theScale); |
|
174 UIImage *img = [ammoStoreImage cutAt:CGRectMake(x_src, y_src, size, size)]; |
|
175 [imgs addObject:img]; |
|
176 } |
|
177 [self performSelectorOnMainThread:@selector(setButtonsArray:) withObject:array waitUntilDone:NO]; |
|
178 [array release]; |
|
179 |
|
180 [self performSelectorOnMainThread:@selector(setImagesArray:) withObject:imgs waitUntilDone:NO]; |
|
181 [imgs release]; |
|
182 [ammoStoreImage release]; |
|
183 |
|
184 [self performSelectorOnMainThread:@selector(loadLabels) withObject:nil waitUntilDone:NO]; |
|
185 |
|
186 [self performSelectorOnMainThread:@selector(updateAmmoVisuals) withObject:nil waitUntilDone:YES]; |
|
187 UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)object; |
|
188 [spinner stopAnimating]; |
|
189 [pool drain]; |
|
190 } |
|
191 |
|
192 -(void) updateAmmoVisuals { |
|
193 if (self.buttonsArray == nil || self.imagesArray == nil) { |
|
194 UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; |
|
195 spinner.hidesWhenStopped = YES; |
|
196 spinner.center = self.view.center; |
|
197 [spinner startAnimating]; |
|
198 [self.view addSubview:spinner]; |
|
199 [NSThread detachNewThreadSelector:@selector(loadAmmoStuff:) toTarget:self withObject:spinner]; |
|
200 [spinner release]; |
|
201 return; |
|
202 } |
|
203 |
|
204 int *loadout = (int *)calloc(HW_getNumberOfWeapons(), sizeof(int)); |
|
205 int res = HW_getAmmoCounts(loadout); |
|
206 int turns = HW_getTurnsForCurrentTeam(); |
|
207 |
|
208 if (res == 0) { |
|
209 self.view.userInteractionEnabled = YES; |
|
210 |
|
211 for (int i = 0; i < HW_getNumberOfWeapons(); i++) { |
|
212 UIButton *button = [self.buttonsArray objectAtIndex:i]; |
|
213 if (loadout[i] > 0) { |
|
214 if (delay[i]-turns >= 0) { |
|
215 button.layer.borderColor = [[UIColor lightGrayColor] CGColor]; |
|
216 [button setTitle:[NSString stringWithFormat:@" %d ",delay[i]-turns+1] forState:UIControlStateNormal]; |
|
217 if (button.currentBackgroundImage == nil || shouldUpdateImage[i] == NO) { |
|
218 UIImage *img = [self.imagesArray objectAtIndex:i]; |
|
219 [button setBackgroundImage:[img convertToGrayScale] forState:UIControlStateNormal]; |
|
220 shouldUpdateImage[i] = YES; |
|
221 } |
|
222 } else { |
|
223 button.layer.borderColor = [[UIColor lightYellowColor] CGColor]; |
|
224 [button setTitle:nil forState:UIControlStateNormal]; |
|
225 if (button.currentBackgroundImage == nil || shouldUpdateImage[i] == YES) { |
|
226 UIImage *img = [self.imagesArray objectAtIndex:i]; |
|
227 [button setBackgroundImage:img forState:UIControlStateNormal]; |
|
228 shouldUpdateImage[i] = NO; |
|
229 } |
|
230 } |
|
231 button.enabled = YES; |
|
232 } else { |
|
233 if (button.enabled == YES) |
|
234 [button setBackgroundImage:nil forState:UIControlStateNormal]; |
|
235 button.layer.borderColor = [[UIColor darkGrayColor] CGColor]; |
|
236 button.enabled = NO; |
|
237 shouldUpdateImage[i] = NO; |
|
238 } |
|
239 } |
|
240 } else { |
|
241 self.view.userInteractionEnabled = NO; |
|
242 } |
|
243 |
|
244 free(loadout); |
|
245 loadout = NULL; |
|
246 } |
|
247 |
|
248 #pragma mark - |
|
249 #pragma mark user interaction |
|
250 -(void) buttonPressed:(id) sender { |
|
251 UIButton *theButton = (UIButton *)sender; |
|
252 if (self.nameLabel == nil || self.extraLabel == nil) |
|
253 [self loadLabels]; |
|
254 |
|
255 self.nameLabel.text = [NSString stringWithUTF8String:HW_getWeaponNameByIndex(theButton.tag)]; |
|
256 // description contains a lot of unnecessary stuff, we clean it by removing .|, !| and ?| |
|
257 NSString *description = [NSString stringWithUTF8String:HW_getWeaponDescriptionByIndex(theButton.tag)]; |
|
258 NSArray *elements = [description componentsSeparatedByString:@".|"]; |
|
259 NSArray *purgedElements = [[elements objectAtIndex:0] componentsSeparatedByString:@"!|"]; |
|
260 NSArray *morePurgedElements = [[purgedElements objectAtIndex:0] componentsSeparatedByString:@"?|"]; |
|
261 self.extraLabel.text = [[[morePurgedElements objectAtIndex:0] stringByReplacingOccurrencesOfString:@"|" withString:@" "] stringByAppendingString:@"."]; |
|
262 if (theButton.currentTitle != nil) |
|
263 self.captionLabel.text = NSLocalizedString(@"This weapon is locked",@""); |
|
264 else |
|
265 self.captionLabel.text = [NSString stringWithUTF8String:HW_getWeaponCaptionByIndex(theButton.tag)]; |
|
266 |
|
267 self.nameLabel.backgroundColor = [UIColor blackColor]; |
|
268 self.captionLabel.backgroundColor = [UIColor blackColor]; |
|
269 self.extraLabel.backgroundColor = [UIColor blackColor]; |
|
270 |
|
271 int y, x = 8; |
|
272 // display labels on top for lower buttons |
|
273 if (theButton.tag > 41) |
|
274 y = 5; |
|
275 else |
|
276 y = (HW_getNumberOfWeapons()/BTNS_PER_ROW)*40; |
|
277 |
|
278 self.nameLabel.frame = CGRectMake(x, y, 200, 20); |
|
279 self.captionLabel.frame = CGRectMake(x+200, y, 220, 20); |
|
280 self.extraLabel.frame = CGRectMake(x+2, y+20, 415, 53); |
|
281 } |
|
282 |
|
283 -(void) buttonCancelled:(id) sender { |
|
284 self.nameLabel.text = nil; |
|
285 self.extraLabel.text = nil; |
|
286 self.captionLabel.text = nil; |
|
287 self.extraLabel.backgroundColor = [UIColor clearColor]; |
|
288 self.captionLabel.backgroundColor = [UIColor clearColor]; |
|
289 self.nameLabel.backgroundColor = [UIColor clearColor]; |
|
290 } |
|
291 |
|
292 -(void) buttonReleased:(id) sender { |
|
293 UIButton *theButton = (UIButton *)sender; |
|
294 if (self.nameLabel == nil || self.extraLabel == nil) |
|
295 [self loadLabels]; |
|
296 |
|
297 if (theButton.currentTitle == nil) { |
|
298 HW_setWeapon(theButton.tag); |
|
299 [AudioManagerController playClickSound]; |
|
300 if (IS_DUALHEAD() == NO) |
|
301 [self disappear]; |
|
302 } |
|
303 [self buttonCancelled:sender]; |
|
304 } |
|
305 |
|
306 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
|
307 NSSet *allTouches = [event allTouches]; |
|
308 |
|
309 if (IS_IPAD() && [touches count] == 1) { |
|
310 self.view.layer.borderWidth = 3.5; |
|
311 currentPoint = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view]; |
|
312 } |
|
313 |
|
314 if (IS_IPAD() == NO) |
|
315 [self disappear]; |
|
316 } |
|
317 |
|
318 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
|
319 self.view.layer.borderWidth = 1.3; |
|
320 } |
|
321 |
|
322 // better window dragging implementation by |
|
323 // http://iphonedevelopertips.com/graphics/drag-an-image-within-the-bounds-of-superview.html |
|
324 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { |
|
325 if (IS_IPAD() && [touches count] == 1) { |
|
326 // Get active location upon move |
|
327 CGPoint activePoint = [[touches anyObject] locationInView:self.view]; |
|
328 |
|
329 // Determine new point based on where the touch is now located |
|
330 CGPoint newPoint = CGPointMake(self.view.center.x + (activePoint.x - currentPoint.x), |
|
331 self.view.center.y + (activePoint.y - currentPoint.y)); |
|
332 |
|
333 // Make sure we stay within the bounds of the parent view |
|
334 float midPointX = CGRectGetMidX(self.view.bounds); |
|
335 if (newPoint.x > self.view.superview.bounds.size.width - midPointX) |
|
336 newPoint.x = self.view.superview.bounds.size.width - midPointX; |
|
337 else if (newPoint.x < midPointX) |
|
338 newPoint.x = midPointX; |
|
339 |
|
340 float midPointY = CGRectGetMidY(self.view.bounds); |
|
341 if (newPoint.y > self.view.superview.bounds.size.height - midPointY) |
|
342 newPoint.y = self.view.superview.bounds.size.height - midPointY; |
|
343 else if (newPoint.y < midPointY) |
|
344 newPoint.y = midPointY; |
|
345 |
|
346 self.view.center = newPoint; |
|
347 } |
|
348 } |
|
349 |
|
350 -(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { |
|
351 [self touchesEnded:touches withEvent:event]; |
|
352 } |
|
353 |
|
354 #pragma mark - |
|
355 #pragma mark memory |
|
356 -(void) didReceiveMemoryWarning { |
|
357 self.imagesArray = nil; |
|
358 if (self.isVisible == NO) |
|
359 self.buttonsArray = nil; |
|
360 self.nameLabel = nil; |
|
361 self.extraLabel = nil; |
|
362 self.captionLabel = nil; |
|
363 MSG_MEMCLEAN(); |
|
364 [super didReceiveMemoryWarning]; |
|
365 } |
|
366 |
|
367 -(void) viewDidUnload { |
|
368 self.imagesArray = nil; |
|
369 self.buttonsArray = nil; |
|
370 self.nameLabel = nil; |
|
371 self.extraLabel = nil; |
|
372 self.captionLabel = nil; |
|
373 free(delay); |
|
374 delay = NULL; |
|
375 free(shouldUpdateImage); |
|
376 shouldUpdateImage = NULL; |
|
377 MSG_DIDUNLOAD(); |
|
378 [super viewDidUnload]; |
|
379 } |
|
380 |
|
381 -(void) dealloc { |
|
382 releaseAndNil(nameLabel); |
|
383 releaseAndNil(extraLabel); |
|
384 releaseAndNil(captionLabel); |
|
385 releaseAndNil(imagesArray); |
|
386 releaseAndNil(buttonsArray); |
|
387 [super dealloc]; |
|
388 } |
|
389 |
|
390 @end |
|