author | smxx |
Fri, 26 Mar 2010 00:37:36 +0000 | |
changeset 3067 | 330e60f5dde8 |
parent 3063 | 0092dc37fbd6 |
child 3073 | c22b395b6a2e |
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" |
3063 | 14 |
#import "SettingsViewController.h" |
15 |
#import "popupMenuViewController.h" |
|
3006 | 16 |
|
17 |
@implementation overlayViewController |
|
3015 | 18 |
@synthesize dimTimer; |
19 |
||
3006 | 20 |
|
21 |
-(void) didReceiveMemoryWarning { |
|
22 |
// Releases the view if it doesn't have a superview. |
|
23 |
[super didReceiveMemoryWarning]; |
|
24 |
||
25 |
// Release any cached data, images, etc that aren't in use. |
|
26 |
} |
|
27 |
||
3015 | 28 |
-(void) viewDidLoad { |
3027 | 29 |
self.view.alpha = 0; |
3034 | 30 |
|
31 |
// needed for rotation to work on os < 3.2 |
|
32 |
self.view.center = CGPointMake(self.view.frame.size.height/2.0, self.view.frame.size.width/2.0); |
|
33 |
self.view.transform = CGAffineTransformRotate(self.view.transform, (M_PI/2.0)); |
|
3027 | 34 |
|
3015 | 35 |
dimTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:6] |
36 |
interval:1000 |
|
37 |
target:self |
|
38 |
selector:@selector(dimOverlay) |
|
39 |
userInfo:nil |
|
40 |
repeats:YES]; |
|
41 |
||
42 |
[[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
|
43 |
|
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
44 |
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showMenuAfterwards) userInfo:nil repeats:NO]; |
3015 | 45 |
} |
46 |
||
3034 | 47 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
3027 | 48 |
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
49 |
} |
|
50 |
||
3006 | 51 |
-(void) viewDidUnload { |
3015 | 52 |
[dimTimer invalidate]; |
3006 | 53 |
} |
54 |
||
55 |
-(void) dealloc { |
|
3029
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
56 |
// dimTimer is autoreleased |
3006 | 57 |
[super dealloc]; |
58 |
} |
|
59 |
||
3027 | 60 |
// draws the controller overlay after the sdl window has taken control |
61 |
-(void) showMenuAfterwards { |
|
62 |
[[SDLUIKitDelegate sharedAppDelegate].uiwindow bringSubviewToFront:self.view]; |
|
63 |
||
64 |
[UIView beginAnimations:@"showing overlay" context:NULL]; |
|
65 |
[UIView setAnimationDuration:1]; |
|
66 |
self.view.alpha = 1; |
|
67 |
[UIView commitAnimations]; |
|
68 |
} |
|
3006 | 69 |
|
70 |
// dim the overlay when there's no more input for a certain amount of time |
|
71 |
-(IBAction) buttonReleased:(id) sender { |
|
72 |
HW_allKeysUp(); |
|
3015 | 73 |
[dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2.7]]; |
3006 | 74 |
} |
75 |
||
3015 | 76 |
// nice transition for dimming |
3006 | 77 |
-(void) dimOverlay { |
78 |
[UIView beginAnimations:@"overlay dim" context:NULL]; |
|
79 |
[UIView setAnimationDuration:0.6]; |
|
80 |
self.view.alpha = 0.2; |
|
81 |
[UIView commitAnimations]; |
|
82 |
} |
|
83 |
||
3015 | 84 |
// set the overlay visible and put off the timer for enough time |
3006 | 85 |
-(void) activateOverlay { |
86 |
self.view.alpha = 1; |
|
3015 | 87 |
[dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:1000]]; |
3006 | 88 |
} |
89 |
||
90 |
// issue certain action based on the tag of the button |
|
91 |
-(IBAction) buttonPressed:(id) sender { |
|
92 |
[self activateOverlay]; |
|
3015 | 93 |
|
3063 | 94 |
UIButton *theButton = (UIButton *)sender; |
3006 | 95 |
switch (theButton.tag) { |
96 |
case 0: |
|
97 |
HW_walkLeft(); |
|
98 |
break; |
|
99 |
case 1: |
|
100 |
HW_walkRight(); |
|
101 |
break; |
|
102 |
case 2: |
|
103 |
HW_aimUp(); |
|
104 |
break; |
|
105 |
case 3: |
|
106 |
HW_aimDown(); |
|
107 |
break; |
|
108 |
case 4: |
|
109 |
HW_shoot(); |
|
110 |
break; |
|
111 |
case 5: |
|
3015 | 112 |
HW_jump(); |
3006 | 113 |
break; |
114 |
case 6: |
|
3015 | 115 |
HW_backjump(); |
3006 | 116 |
break; |
3063 | 117 |
case 7: |
118 |
HW_pause(); |
|
119 |
break; |
|
120 |
case 8: |
|
121 |
HW_chat(); |
|
122 |
break; |
|
3006 | 123 |
default: |
3063 | 124 |
NSLog(@"Nope"); |
3015 | 125 |
// HW_chat() HW_tab() HW_pause() |
3006 | 126 |
break; |
127 |
} |
|
128 |
} |
|
129 |
||
3063 | 130 |
-(IBAction) showPopover{ |
131 |
//UIViewController *content = [[UIViewController alloc] initWithNibName: nil bundle:nil]; |
|
132 |
//CGRect rectArea = CGRectMake(0, 0, 320, 480); |
|
133 |
//content.view.frame = rectArea; |
|
134 |
//settings.view.frame = rectArea; |
|
135 |
popupMenuViewController *popupMenu = [[UIViewController alloc] initWithNibName:@"popupMenuViewController" bundle:nil]; |
|
136 |
||
137 |
UIButton *buttonPause = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
|
138 |
buttonPause.tag = 7; |
|
139 |
buttonPause.frame = CGRectMake(100, 170, 170, 30); |
|
140 |
[buttonPause setTitle:@"Pause Game" forState:UIControlStateNormal]; |
|
141 |
[buttonPause addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; |
|
142 |
[popupMenu.view addSubview:buttonPause]; |
|
143 |
||
144 |
UIButton *buttonChat = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
|
145 |
buttonChat.tag = 8; |
|
146 |
buttonChat.frame = CGRectMake(100, 220, 170, 30); |
|
147 |
[buttonChat setTitle:@"Chat" forState:UIControlStateNormal]; |
|
148 |
[buttonChat addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; |
|
149 |
[popupMenu.view addSubview: buttonChat]; |
|
150 |
||
151 |
||
152 |
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:popupMenu]; |
|
153 |
//[aPopover setPopoverContentSize:CGSizeMake(320, 480) animated:YES]; |
|
154 |
||
155 |
[aPopover presentPopoverFromRect: CGRectMake(1024, 0, 320, 480) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES]; |
|
156 |
//UIBarButtonItem *sender = [[useless items] objectAtIndex:1]; |
|
157 |
//[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES]; |
|
158 |
//aPopover.popoverContentSize = CGSizeMake(320, 480); |
|
159 |
||
160 |
/*SettingsViewController *settings = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil]; |
|
161 |
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:settings]; |
|
162 |
[self.view addSubview:popover.contentViewController.view];*/ |
|
163 |
} |
|
164 |
||
165 |
||
3025 | 166 |
#pragma mark - |
167 |
#pragma mark Custom SDL_UIView input handling |
|
168 |
#define kMinimumPinchDelta 50 |
|
169 |
#define kMinimumGestureLength 10 |
|
170 |
#define kMaximumVariance 3 |
|
171 |
||
172 |
// we override default touch input to implement our own gestures |
|
173 |
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
|
174 |
NSArray *twoTouches; |
|
175 |
UITouch *touch = [touches anyObject]; |
|
176 |
||
177 |
switch ([touches count]) { |
|
178 |
case 1: |
|
179 |
gestureStartPoint = [touch locationInView:self.view]; |
|
180 |
initialDistanceForPinching = 0; |
|
181 |
switch ([touch tapCount]) { |
|
182 |
case 1: |
|
183 |
NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y ); |
|
184 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
185 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
186 |
HW_click(); |
|
187 |
break; |
|
188 |
case 2: |
|
189 |
HW_ammoMenu(); |
|
190 |
break; |
|
191 |
default: |
|
192 |
break; |
|
193 |
} |
|
194 |
break; |
|
195 |
case 2: |
|
196 |
if (2 == [touch tapCount]) { |
|
197 |
HW_zoomReset(); |
|
198 |
} |
|
199 |
||
200 |
// pinching |
|
201 |
twoTouches = [touches allObjects]; |
|
202 |
UITouch *first = [twoTouches objectAtIndex:0]; |
|
203 |
UITouch *second = [twoTouches objectAtIndex:1]; |
|
204 |
initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
205 |
break; |
|
206 |
default: |
|
207 |
break; |
|
208 |
} |
|
209 |
||
210 |
} |
|
211 |
||
212 |
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
|
213 |
initialDistanceForPinching = 0; |
|
214 |
gestureStartPoint.x = 0; |
|
215 |
gestureStartPoint.y = 0; |
|
216 |
HW_allKeysUp(); |
|
217 |
} |
|
218 |
||
219 |
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { |
|
220 |
// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances. |
|
221 |
[self touchesEnded:touches withEvent:event]; |
|
222 |
} |
|
223 |
||
224 |
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { |
|
225 |
NSArray *twoTouches; |
|
226 |
CGPoint currentPosition; |
|
227 |
UITouch *touch = [touches anyObject]; |
|
228 |
||
229 |
switch ([touches count]) { |
|
230 |
case 1: |
|
231 |
currentPosition = [touch locationInView:self.view]; |
|
232 |
// panning |
|
233 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
234 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
235 |
// remember that we have x and y inverted |
|
236 |
/* temporarily disabling hog movements for camera panning testing |
|
237 |
CGFloat vertDiff = gestureStartPoint.x - currentPosition.x; |
|
238 |
CGFloat horizDiff = gestureStartPoint.y - currentPosition.y; |
|
239 |
CGFloat deltaX = fabsf(vertDiff); |
|
240 |
CGFloat deltaY = fabsf(horizDiff); |
|
241 |
||
242 |
if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { |
|
243 |
NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x); |
|
244 |
if (horizDiff > 0) HW_walkLeft(); |
|
245 |
else HW_walkRight(); |
|
246 |
} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){ |
|
247 |
NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y); |
|
248 |
if (vertDiff < 0) HW_aimUp(); |
|
249 |
else HW_aimDown(); |
|
250 |
} |
|
251 |
*/ |
|
252 |
break; |
|
253 |
case 2: |
|
254 |
twoTouches = [touches allObjects]; |
|
255 |
UITouch *first = [twoTouches objectAtIndex:0]; |
|
256 |
UITouch *second = [twoTouches objectAtIndex:1]; |
|
257 |
CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
258 |
||
259 |
if (0 == initialDistanceForPinching) |
|
260 |
initialDistanceForPinching = currentDistanceOfPinching; |
|
261 |
||
262 |
if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta) |
|
263 |
HW_zoomOut(); |
|
264 |
else if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta) |
|
265 |
HW_zoomIn(); |
|
266 |
||
267 |
currentDistanceOfPinching = initialDistanceForPinching; |
|
268 |
break; |
|
269 |
default: |
|
270 |
break; |
|
271 |
} |
|
272 |
} |
|
273 |
||
274 |
||
3006 | 275 |
|
276 |
@end |