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