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