3006
|
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"
|
3025
|
10 |
#import "SDL_uikitappdelegate.h"
|
3006
|
11 |
#import "PascalImports.h"
|
3025
|
12 |
#import "SDL_mouse.h"
|
|
13 |
#import "CGPointUtils.h"
|
3006
|
14 |
|
|
15 |
@implementation overlayViewController
|
3015
|
16 |
@synthesize dimTimer;
|
|
17 |
|
3006
|
18 |
|
|
19 |
-(void) didReceiveMemoryWarning {
|
|
20 |
// Releases the view if it doesn't have a superview.
|
|
21 |
[super didReceiveMemoryWarning];
|
|
22 |
|
|
23 |
// Release any cached data, images, etc that aren't in use.
|
|
24 |
}
|
|
25 |
|
3015
|
26 |
-(void) viewDidLoad {
|
|
27 |
dimTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:6]
|
|
28 |
interval:1000
|
|
29 |
target:self
|
|
30 |
selector:@selector(dimOverlay)
|
|
31 |
userInfo:nil
|
|
32 |
repeats:YES];
|
|
33 |
|
|
34 |
[[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode];
|
|
35 |
}
|
|
36 |
|
3006
|
37 |
-(void) viewDidUnload {
|
3015
|
38 |
[dimTimer invalidate];
|
3006
|
39 |
}
|
|
40 |
|
|
41 |
-(void) dealloc {
|
3015
|
42 |
[dimTimer release];
|
3006
|
43 |
[super dealloc];
|
|
44 |
}
|
|
45 |
|
|
46 |
|
|
47 |
// dim the overlay when there's no more input for a certain amount of time
|
|
48 |
-(IBAction) buttonReleased:(id) sender {
|
|
49 |
HW_allKeysUp();
|
3015
|
50 |
[dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2.7]];
|
3006
|
51 |
}
|
|
52 |
|
3015
|
53 |
// nice transition for dimming
|
3006
|
54 |
-(void) dimOverlay {
|
|
55 |
[UIView beginAnimations:@"overlay dim" context:NULL];
|
|
56 |
[UIView setAnimationDuration:0.6];
|
|
57 |
self.view.alpha = 0.2;
|
|
58 |
[UIView commitAnimations];
|
|
59 |
}
|
|
60 |
|
3015
|
61 |
// set the overlay visible and put off the timer for enough time
|
3006
|
62 |
-(void) activateOverlay {
|
|
63 |
self.view.alpha = 1;
|
3015
|
64 |
[dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:1000]];
|
3006
|
65 |
}
|
|
66 |
|
|
67 |
// issue certain action based on the tag of the button
|
|
68 |
-(IBAction) buttonPressed:(id) sender {
|
|
69 |
[self activateOverlay];
|
3015
|
70 |
|
3006
|
71 |
UIButton *theButton = (UIButton*)sender;
|
|
72 |
switch (theButton.tag) {
|
|
73 |
case 0:
|
|
74 |
HW_walkLeft();
|
|
75 |
break;
|
|
76 |
case 1:
|
|
77 |
HW_walkRight();
|
|
78 |
break;
|
|
79 |
case 2:
|
|
80 |
HW_aimUp();
|
|
81 |
break;
|
|
82 |
case 3:
|
|
83 |
HW_aimDown();
|
|
84 |
break;
|
|
85 |
case 4:
|
|
86 |
HW_shoot();
|
|
87 |
break;
|
|
88 |
case 5:
|
3015
|
89 |
HW_jump();
|
3006
|
90 |
break;
|
|
91 |
case 6:
|
3015
|
92 |
HW_backjump();
|
3006
|
93 |
break;
|
|
94 |
default:
|
3015
|
95 |
// HW_chat() HW_tab() HW_pause()
|
3006
|
96 |
break;
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
3025
|
100 |
#pragma mark -
|
|
101 |
#pragma mark Custom SDL_UIView input handling
|
|
102 |
#define kMinimumPinchDelta 50
|
|
103 |
#define kMinimumGestureLength 10
|
|
104 |
#define kMaximumVariance 3
|
|
105 |
|
|
106 |
// we override default touch input to implement our own gestures
|
|
107 |
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
108 |
NSArray *twoTouches;
|
|
109 |
UITouch *touch = [touches anyObject];
|
|
110 |
|
|
111 |
switch ([touches count]) {
|
|
112 |
case 1:
|
|
113 |
gestureStartPoint = [touch locationInView:self.view];
|
|
114 |
initialDistanceForPinching = 0;
|
|
115 |
switch ([touch tapCount]) {
|
|
116 |
case 1:
|
|
117 |
NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y );
|
|
118 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window,
|
|
119 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x);
|
|
120 |
HW_click();
|
|
121 |
break;
|
|
122 |
case 2:
|
|
123 |
HW_ammoMenu();
|
|
124 |
break;
|
|
125 |
default:
|
|
126 |
break;
|
|
127 |
}
|
|
128 |
break;
|
|
129 |
case 2:
|
|
130 |
if (2 == [touch tapCount]) {
|
|
131 |
HW_zoomReset();
|
|
132 |
}
|
|
133 |
|
|
134 |
// pinching
|
|
135 |
twoTouches = [touches allObjects];
|
|
136 |
UITouch *first = [twoTouches objectAtIndex:0];
|
|
137 |
UITouch *second = [twoTouches objectAtIndex:1];
|
|
138 |
initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]);
|
|
139 |
break;
|
|
140 |
default:
|
|
141 |
break;
|
|
142 |
}
|
|
143 |
|
|
144 |
}
|
|
145 |
|
|
146 |
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
147 |
initialDistanceForPinching = 0;
|
|
148 |
gestureStartPoint.x = 0;
|
|
149 |
gestureStartPoint.y = 0;
|
|
150 |
HW_allKeysUp();
|
|
151 |
}
|
|
152 |
|
|
153 |
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
154 |
// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances.
|
|
155 |
// Usually (it seems) all active touches are canceled.
|
|
156 |
[self touchesEnded:touches withEvent:event];
|
|
157 |
}
|
|
158 |
|
|
159 |
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
160 |
NSArray *twoTouches;
|
|
161 |
CGPoint currentPosition;
|
|
162 |
UITouch *touch = [touches anyObject];
|
|
163 |
|
|
164 |
switch ([touches count]) {
|
|
165 |
case 1:
|
|
166 |
currentPosition = [touch locationInView:self.view];
|
|
167 |
// panning
|
|
168 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window,
|
|
169 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x);
|
|
170 |
// remember that we have x and y inverted
|
|
171 |
/* temporarily disabling hog movements for camera panning testing
|
|
172 |
CGFloat vertDiff = gestureStartPoint.x - currentPosition.x;
|
|
173 |
CGFloat horizDiff = gestureStartPoint.y - currentPosition.y;
|
|
174 |
CGFloat deltaX = fabsf(vertDiff);
|
|
175 |
CGFloat deltaY = fabsf(horizDiff);
|
|
176 |
|
|
177 |
if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) {
|
|
178 |
NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x);
|
|
179 |
if (horizDiff > 0) HW_walkLeft();
|
|
180 |
else HW_walkRight();
|
|
181 |
} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){
|
|
182 |
NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y);
|
|
183 |
if (vertDiff < 0) HW_aimUp();
|
|
184 |
else HW_aimDown();
|
|
185 |
}
|
|
186 |
*/
|
|
187 |
break;
|
|
188 |
case 2:
|
|
189 |
twoTouches = [touches allObjects];
|
|
190 |
UITouch *first = [twoTouches objectAtIndex:0];
|
|
191 |
UITouch *second = [twoTouches objectAtIndex:1];
|
|
192 |
CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]);
|
|
193 |
|
|
194 |
if (0 == initialDistanceForPinching)
|
|
195 |
initialDistanceForPinching = currentDistanceOfPinching;
|
|
196 |
|
|
197 |
if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta)
|
|
198 |
HW_zoomOut();
|
|
199 |
else if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta)
|
|
200 |
HW_zoomIn();
|
|
201 |
|
|
202 |
currentDistanceOfPinching = initialDistanceForPinching;
|
|
203 |
break;
|
|
204 |
default:
|
|
205 |
break;
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
|
3006
|
210 |
|
|
211 |
@end
|