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