3547
|
1 |
//
|
|
2 |
// overlayViewController.m
|
|
3 |
// HedgewarsMobile
|
|
4 |
//
|
|
5 |
// Created by Vittorio on 16/03/10.
|
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved.
|
|
7 |
//
|
|
8 |
|
|
9 |
#import "OverlayViewController.h"
|
|
10 |
#import "SDL_uikitappdelegate.h"
|
|
11 |
#import "PascalImports.h"
|
|
12 |
#import "CGPointUtils.h"
|
|
13 |
#import "SDL_mouse.h"
|
|
14 |
#import "SDL_config_iphoneos.h"
|
|
15 |
#import "PopoverMenuViewController.h"
|
|
16 |
#import "CommodityFunctions.h"
|
|
17 |
|
|
18 |
#define HIDING_TIME_DEFAULT [NSDate dateWithTimeIntervalSinceNow:2.7]
|
|
19 |
#define HIDING_TIME_NEVER [NSDate dateWithTimeIntervalSinceNow:10000]
|
|
20 |
|
|
21 |
|
|
22 |
@implementation OverlayViewController
|
|
23 |
@synthesize popoverController, popupMenu, writeChatTextField, spinningWheel;
|
|
24 |
|
|
25 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
|
|
26 |
return rotationManager(interfaceOrientation);
|
|
27 |
}
|
|
28 |
|
|
29 |
|
|
30 |
-(void) didReceiveMemoryWarning {
|
|
31 |
// Releases the view if it doesn't have a superview.
|
|
32 |
[super didReceiveMemoryWarning];
|
|
33 |
// Release any cached data, images, etc that aren't in use.
|
|
34 |
if (popupMenu.view.superview == nil)
|
|
35 |
popupMenu = nil;
|
|
36 |
}
|
|
37 |
|
|
38 |
-(void) didRotate:(NSNotification *)notification {
|
|
39 |
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
|
|
40 |
CGRect rect = [[UIScreen mainScreen] bounds];
|
|
41 |
CGRect usefulRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
|
|
42 |
UIView *sdlView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:12345];
|
|
43 |
|
|
44 |
[UIView beginAnimations:@"rotation" context:NULL];
|
|
45 |
[UIView setAnimationDuration:0.8f];
|
|
46 |
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
|
|
47 |
switch (orientation) {
|
|
48 |
case UIDeviceOrientationLandscapeLeft:
|
|
49 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
|
|
50 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
|
|
51 |
[self chatDisappear];
|
|
52 |
HW_setLandscape(YES);
|
|
53 |
break;
|
|
54 |
case UIDeviceOrientationLandscapeRight:
|
|
55 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
|
|
56 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
|
|
57 |
[self chatDisappear];
|
|
58 |
HW_setLandscape(YES);
|
|
59 |
break;
|
3551
|
60 |
/*
|
3547
|
61 |
case UIDeviceOrientationPortrait:
|
|
62 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
63 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(270));
|
|
64 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
|
|
65 |
[self chatAppear];
|
|
66 |
HW_setLandscape(NO);
|
|
67 |
}
|
|
68 |
break;
|
|
69 |
case UIDeviceOrientationPortraitUpsideDown:
|
|
70 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
71 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
|
|
72 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
|
|
73 |
[self chatAppear];
|
|
74 |
HW_setLandscape(NO);
|
|
75 |
}
|
|
76 |
break;
|
3551
|
77 |
*/
|
3547
|
78 |
default:
|
|
79 |
break;
|
|
80 |
}
|
|
81 |
self.view.frame = usefulRect;
|
|
82 |
//sdlView.frame = usefulRect;
|
|
83 |
[UIView commitAnimations];
|
|
84 |
}
|
|
85 |
|
|
86 |
-(void) chatAppear {
|
|
87 |
if (writeChatTextField == nil) {
|
|
88 |
writeChatTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 768, [UIFont systemFontSize]+8)];
|
|
89 |
writeChatTextField.textColor = [UIColor whiteColor];
|
|
90 |
writeChatTextField.backgroundColor = [UIColor blueColor];
|
|
91 |
writeChatTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
|
|
92 |
writeChatTextField.autocorrectionType = UITextAutocorrectionTypeNo;
|
|
93 |
writeChatTextField.enablesReturnKeyAutomatically = NO;
|
|
94 |
writeChatTextField.keyboardAppearance = UIKeyboardAppearanceDefault;
|
|
95 |
writeChatTextField.keyboardType = UIKeyboardTypeDefault;
|
|
96 |
writeChatTextField.returnKeyType = UIReturnKeyDefault;
|
|
97 |
writeChatTextField.secureTextEntry = NO;
|
|
98 |
[self.view addSubview:writeChatTextField];
|
|
99 |
}
|
|
100 |
writeChatTextField.alpha = 1;
|
|
101 |
[self activateOverlay];
|
|
102 |
[dimTimer setFireDate:HIDING_TIME_NEVER];
|
|
103 |
}
|
|
104 |
|
|
105 |
-(void) chatDisappear {
|
|
106 |
writeChatTextField.alpha = 0;
|
|
107 |
[writeChatTextField resignFirstResponder];
|
|
108 |
[dimTimer setFireDate:HIDING_TIME_DEFAULT];
|
|
109 |
}
|
|
110 |
|
|
111 |
#pragma mark -
|
|
112 |
#pragma mark View Management
|
|
113 |
-(void) viewDidLoad {
|
|
114 |
isPopoverVisible = NO;
|
|
115 |
singleton = self.spinningWheel;
|
|
116 |
canDim = NO;
|
|
117 |
self.view.alpha = 0;
|
|
118 |
self.view.center = CGPointMake(self.view.frame.size.height/2.0, self.view.frame.size.width/2.0);
|
|
119 |
|
|
120 |
|
|
121 |
dimTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:6]
|
|
122 |
interval:1000
|
|
123 |
target:self
|
|
124 |
selector:@selector(dimOverlay)
|
|
125 |
userInfo:nil
|
|
126 |
repeats:YES];
|
|
127 |
|
|
128 |
// add timer too runloop, otherwise it doesn't work
|
|
129 |
[[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode];
|
|
130 |
|
|
131 |
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
|
|
132 |
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
133 |
selector:@selector(didRotate:)
|
3598
|
134 |
name:UIDeviceOrientationDidChangeNotification
|
3547
|
135 |
object:nil];
|
|
136 |
|
|
137 |
[UIView beginAnimations:@"showing overlay" context:NULL];
|
|
138 |
[UIView setAnimationDuration:1];
|
|
139 |
self.view.alpha = 1;
|
|
140 |
[UIView commitAnimations];
|
3598
|
141 |
|
|
142 |
// set initial orientation
|
|
143 |
[self didRotate:[NSNotification notificationWithName:UIDeviceOrientationDidChangeNotification object:nil]];
|
|
144 |
|
|
145 |
// to put the slider vertical...
|
|
146 |
//slider.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
|
3547
|
147 |
}
|
|
148 |
|
|
149 |
-(void) viewDidUnload {
|
|
150 |
self.writeChatTextField = nil;
|
3599
|
151 |
[popoverController dismissPopoverAnimated:NO];
|
3547
|
152 |
self.popoverController = nil;
|
|
153 |
self.popupMenu = nil;
|
|
154 |
self.spinningWheel = nil;
|
|
155 |
[super viewDidUnload];
|
|
156 |
MSG_DIDUNLOAD();
|
|
157 |
}
|
|
158 |
|
|
159 |
-(void) dealloc {
|
|
160 |
[dimTimer invalidate];
|
|
161 |
[writeChatTextField release];
|
|
162 |
[popupMenu release];
|
|
163 |
[popoverController release];
|
|
164 |
// dimTimer is autoreleased
|
|
165 |
[spinningWheel release];
|
|
166 |
[super dealloc];
|
|
167 |
}
|
|
168 |
|
|
169 |
#pragma mark -
|
|
170 |
#pragma mark Overlay actions and members
|
|
171 |
// dim the overlay when there's no more input for a certain amount of time
|
|
172 |
-(IBAction) buttonReleased:(id) sender {
|
|
173 |
HW_allKeysUp();
|
|
174 |
[dimTimer setFireDate:HIDING_TIME_DEFAULT];
|
|
175 |
}
|
|
176 |
|
|
177 |
// nice transition for dimming, should be called only by the timer himself
|
|
178 |
-(void) dimOverlay {
|
|
179 |
if (canDim) {
|
|
180 |
[UIView beginAnimations:@"overlay dim" context:NULL];
|
|
181 |
[UIView setAnimationDuration:0.6];
|
|
182 |
self.view.alpha = 0.2;
|
|
183 |
[UIView commitAnimations];
|
|
184 |
}
|
|
185 |
}
|
|
186 |
|
|
187 |
// set the overlay visible and put off the timer for enough time
|
|
188 |
-(void) activateOverlay {
|
|
189 |
self.view.alpha = 1;
|
|
190 |
[dimTimer setFireDate:HIDING_TIME_NEVER];
|
|
191 |
}
|
|
192 |
|
|
193 |
// issue certain action based on the tag of the button
|
|
194 |
-(IBAction) buttonPressed:(id) sender {
|
|
195 |
[self activateOverlay];
|
|
196 |
if (isPopoverVisible) {
|
|
197 |
[self dismissPopover];
|
|
198 |
}
|
|
199 |
UIButton *theButton = (UIButton *)sender;
|
|
200 |
|
|
201 |
switch (theButton.tag) {
|
|
202 |
case 0:
|
|
203 |
HW_walkLeft();
|
|
204 |
break;
|
|
205 |
case 1:
|
|
206 |
HW_walkRight();
|
|
207 |
break;
|
|
208 |
case 2:
|
|
209 |
HW_aimUp();
|
|
210 |
break;
|
|
211 |
case 3:
|
|
212 |
HW_aimDown();
|
|
213 |
break;
|
|
214 |
case 4:
|
|
215 |
HW_shoot();
|
|
216 |
break;
|
|
217 |
case 5:
|
|
218 |
HW_jump();
|
|
219 |
break;
|
|
220 |
case 6:
|
|
221 |
HW_backjump();
|
|
222 |
break;
|
|
223 |
case 7:
|
|
224 |
HW_tab();
|
|
225 |
break;
|
|
226 |
case 10:
|
|
227 |
[self showPopover];
|
|
228 |
break;
|
|
229 |
default:
|
|
230 |
NSLog(@"Nope");
|
|
231 |
break;
|
|
232 |
}
|
|
233 |
}
|
|
234 |
|
|
235 |
// present a further check before closing game
|
|
236 |
-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
|
|
237 |
if ([actionSheet cancelButtonIndex] != buttonIndex)
|
|
238 |
HW_terminate(NO);
|
|
239 |
else
|
|
240 |
HW_pause();
|
|
241 |
}
|
|
242 |
|
|
243 |
// show up a popover containing a popupMenuViewController; we hook it with setPopoverContentSize
|
|
244 |
// on iphone instead just use the tableViewController directly (and implement manually all animations)
|
|
245 |
-(IBAction) showPopover{
|
|
246 |
isPopoverVisible = YES;
|
|
247 |
|
|
248 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
249 |
if (popupMenu == nil)
|
|
250 |
popupMenu = [[PopoverMenuViewController alloc] initWithStyle:UITableViewStylePlain];
|
|
251 |
if (popoverController == nil) {
|
|
252 |
popoverController = [[UIPopoverController alloc] initWithContentViewController:popupMenu];
|
|
253 |
[popoverController setPopoverContentSize:CGSizeMake(220, 170) animated:YES];
|
|
254 |
[popoverController setPassthroughViews:[NSArray arrayWithObject:self.view]];
|
|
255 |
}
|
3551
|
256 |
|
|
257 |
[popoverController presentPopoverFromRect:CGRectMake(1000, 0, 220, 32)
|
3547
|
258 |
inView:self.view
|
|
259 |
permittedArrowDirections:UIPopoverArrowDirectionUp
|
|
260 |
animated:YES];
|
|
261 |
} else {
|
|
262 |
if (popupMenu == nil) {
|
|
263 |
popupMenu = [[PopoverMenuViewController alloc] initWithStyle:UITableViewStyleGrouped];
|
|
264 |
popupMenu.view.backgroundColor = [UIColor clearColor];
|
|
265 |
popupMenu.view.frame = CGRectMake(480, 0, 200, 170);
|
|
266 |
}
|
|
267 |
[self.view addSubview:popupMenu.view];
|
|
268 |
|
|
269 |
[UIView beginAnimations:@"showing popover" context:NULL];
|
|
270 |
[UIView setAnimationDuration:0.35];
|
|
271 |
popupMenu.view.frame = CGRectMake(280, 0, 200, 170);
|
|
272 |
[UIView commitAnimations];
|
|
273 |
}
|
|
274 |
popupMenu.tableView.scrollEnabled = NO;
|
|
275 |
}
|
|
276 |
|
|
277 |
// on ipad just dismiss it, on iphone transtion to the right
|
|
278 |
-(void) dismissPopover {
|
|
279 |
if (YES == isPopoverVisible) {
|
|
280 |
isPopoverVisible = NO;
|
|
281 |
|
|
282 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
283 |
[popoverController dismissPopoverAnimated:YES];
|
|
284 |
} else {
|
|
285 |
[UIView beginAnimations:@"hiding popover" context:NULL];
|
|
286 |
[UIView setAnimationDuration:0.35];
|
|
287 |
popupMenu.view.frame = CGRectMake(480, 0, 200, 170);
|
|
288 |
[UIView commitAnimations];
|
|
289 |
|
|
290 |
[popupMenu.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.35];
|
|
291 |
}
|
|
292 |
[self buttonReleased:nil];
|
|
293 |
}
|
|
294 |
}
|
|
295 |
|
|
296 |
-(void) textFieldDoneEditing:(id) sender{
|
|
297 |
[sender resignFirstResponder];
|
|
298 |
}
|
|
299 |
|
|
300 |
// this function is called by pascal FinishProgress and removes the spinning wheel when loading is done
|
|
301 |
void spinningWheelDone (void) {
|
3551
|
302 |
[singleton stopAnimating];
|
|
303 |
singleton = nil;
|
3547
|
304 |
canDim = YES;
|
|
305 |
}
|
|
306 |
|
|
307 |
#pragma mark -
|
|
308 |
#pragma mark Custom touch event handling
|
|
309 |
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
310 |
NSArray *twoTouches;
|
|
311 |
UITouch *touch = [touches anyObject];
|
3551
|
312 |
CGRect screen = [[UIScreen mainScreen] bounds];
|
|
313 |
CGPoint currentPosition = [touch locationInView:self.view];
|
3547
|
314 |
|
|
315 |
if (isPopoverVisible) {
|
|
316 |
[self dismissPopover];
|
|
317 |
}
|
|
318 |
if (writeChatTextField) {
|
|
319 |
[self.writeChatTextField resignFirstResponder];
|
|
320 |
[dimTimer setFireDate:HIDING_TIME_DEFAULT];
|
|
321 |
}
|
3598
|
322 |
|
|
323 |
if (currentPosition.y < screen.size.width - 120) {
|
|
324 |
switch ([touches count]) {
|
|
325 |
case 1:
|
|
326 |
DLog(@"X:%d Y:%d", HWX(currentPosition.x), HWY(currentPosition.y));
|
|
327 |
HW_setCursor(HWX(currentPosition.x), HWY(currentPosition.y));
|
|
328 |
break;
|
|
329 |
case 2:
|
|
330 |
if (2 == [touch tapCount]) {
|
|
331 |
HW_ammoMenu();
|
|
332 |
//HW_zoomReset();
|
|
333 |
}
|
|
334 |
|
|
335 |
// pinching
|
|
336 |
twoTouches = [touches allObjects];
|
|
337 |
UITouch *first = [twoTouches objectAtIndex:0];
|
|
338 |
UITouch *second = [twoTouches objectAtIndex:1];
|
|
339 |
initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]);
|
|
340 |
break;
|
|
341 |
default:
|
|
342 |
break;
|
|
343 |
}
|
3547
|
344 |
}
|
|
345 |
}
|
|
346 |
|
|
347 |
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
348 |
initialDistanceForPinching = 0;
|
|
349 |
//HW_allKeysUp();
|
3551
|
350 |
HW_click();
|
3547
|
351 |
}
|
|
352 |
|
|
353 |
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
354 |
// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances
|
|
355 |
[self touchesEnded:touches withEvent:event];
|
|
356 |
}
|
|
357 |
|
|
358 |
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
|
3551
|
359 |
CGRect screen = [[UIScreen mainScreen] bounds];
|
3547
|
360 |
|
|
361 |
NSArray *twoTouches;
|
|
362 |
CGPoint currentPosition;
|
|
363 |
UITouch *touch = [touches anyObject];
|
|
364 |
|
|
365 |
switch ([touches count]) {
|
3551
|
366 |
case 1:
|
|
367 |
if (HW_isAmmoOpen()) {
|
|
368 |
currentPosition = [touch locationInView:self.view];
|
|
369 |
DLog(@"X:%d Y:%d", HWX(currentPosition.x), HWY(currentPosition.y));
|
|
370 |
HW_setCursor(HWX(currentPosition.x), HWY(currentPosition.y));
|
3547
|
371 |
}
|
3551
|
372 |
break;
|
3547
|
373 |
case 2:
|
|
374 |
twoTouches = [touches allObjects];
|
|
375 |
UITouch *first = [twoTouches objectAtIndex:0];
|
|
376 |
UITouch *second = [twoTouches objectAtIndex:1];
|
|
377 |
CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]);
|
|
378 |
const int pinchDelta = 40;
|
|
379 |
|
|
380 |
if (0 != initialDistanceForPinching) {
|
|
381 |
if (currentDistanceOfPinching - initialDistanceForPinching > pinchDelta) {
|
|
382 |
HW_zoomIn();
|
|
383 |
initialDistanceForPinching = currentDistanceOfPinching;
|
|
384 |
}
|
|
385 |
else if (initialDistanceForPinching - currentDistanceOfPinching > pinchDelta) {
|
|
386 |
HW_zoomOut();
|
|
387 |
initialDistanceForPinching = currentDistanceOfPinching;
|
|
388 |
}
|
|
389 |
} else
|
|
390 |
initialDistanceForPinching = currentDistanceOfPinching;
|
|
391 |
|
|
392 |
break;
|
|
393 |
default:
|
|
394 |
break;
|
|
395 |
}
|
|
396 |
}
|
|
397 |
|
|
398 |
|
|
399 |
@end
|