3547
|
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"
|
|
10 |
#import "SDL_uikitappdelegate.h"
|
|
11 |
#import "PascalImports.h"
|
|
12 |
#import "CGPointUtils.h"
|
|
13 |
#import "SDL_mouse.h"
|
|
14 |
#import "SDL_config_iphoneos.h"
|
|
15 |
#import "PopoverMenuViewController.h"
|
|
16 |
#import "CommodityFunctions.h"
|
|
17 |
|
|
18 |
#define HIDING_TIME_DEFAULT [NSDate dateWithTimeIntervalSinceNow:2.7]
|
|
19 |
#define HIDING_TIME_NEVER [NSDate dateWithTimeIntervalSinceNow:10000]
|
|
20 |
|
|
21 |
|
|
22 |
@implementation OverlayViewController
|
|
23 |
@synthesize popoverController, popupMenu, writeChatTextField, spinningWheel;
|
|
24 |
|
|
25 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
|
|
26 |
return rotationManager(interfaceOrientation);
|
|
27 |
}
|
|
28 |
|
|
29 |
|
|
30 |
-(void) didReceiveMemoryWarning {
|
|
31 |
// Releases the view if it doesn't have a superview.
|
|
32 |
[super didReceiveMemoryWarning];
|
|
33 |
// Release any cached data, images, etc that aren't in use.
|
|
34 |
if (popupMenu.view.superview == nil)
|
|
35 |
popupMenu = nil;
|
3617
|
36 |
MSG_MEMCLEAN();
|
3547
|
37 |
}
|
|
38 |
|
|
39 |
-(void) didRotate:(NSNotification *)notification {
|
|
40 |
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
|
|
41 |
CGRect rect = [[UIScreen mainScreen] bounds];
|
|
42 |
CGRect usefulRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
|
|
43 |
UIView *sdlView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:12345];
|
|
44 |
|
|
45 |
[UIView beginAnimations:@"rotation" context:NULL];
|
|
46 |
[UIView setAnimationDuration:0.8f];
|
|
47 |
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
|
|
48 |
switch (orientation) {
|
|
49 |
case UIDeviceOrientationLandscapeLeft:
|
|
50 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
|
|
51 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
|
|
52 |
[self chatDisappear];
|
|
53 |
HW_setLandscape(YES);
|
|
54 |
break;
|
|
55 |
case UIDeviceOrientationLandscapeRight:
|
|
56 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
|
|
57 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
|
|
58 |
[self chatDisappear];
|
|
59 |
HW_setLandscape(YES);
|
|
60 |
break;
|
3551
|
61 |
/*
|
3547
|
62 |
case UIDeviceOrientationPortrait:
|
|
63 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
64 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(270));
|
|
65 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
|
|
66 |
[self chatAppear];
|
|
67 |
HW_setLandscape(NO);
|
|
68 |
}
|
|
69 |
break;
|
|
70 |
case UIDeviceOrientationPortraitUpsideDown:
|
|
71 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
72 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
|
|
73 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
|
|
74 |
[self chatAppear];
|
|
75 |
HW_setLandscape(NO);
|
|
76 |
}
|
|
77 |
break;
|
3551
|
78 |
*/
|
3547
|
79 |
default:
|
|
80 |
break;
|
|
81 |
}
|
|
82 |
self.view.frame = usefulRect;
|
|
83 |
//sdlView.frame = usefulRect;
|
|
84 |
[UIView commitAnimations];
|
|
85 |
}
|
|
86 |
|
|
87 |
-(void) chatAppear {
|
3617
|
88 |
/*
|
3547
|
89 |
if (writeChatTextField == nil) {
|
|
90 |
writeChatTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 768, [UIFont systemFontSize]+8)];
|
|
91 |
writeChatTextField.textColor = [UIColor whiteColor];
|
|
92 |
writeChatTextField.backgroundColor = [UIColor blueColor];
|
|
93 |
writeChatTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
|
|
94 |
writeChatTextField.autocorrectionType = UITextAutocorrectionTypeNo;
|
|
95 |
writeChatTextField.enablesReturnKeyAutomatically = NO;
|
|
96 |
writeChatTextField.keyboardAppearance = UIKeyboardAppearanceDefault;
|
|
97 |
writeChatTextField.keyboardType = UIKeyboardTypeDefault;
|
|
98 |
writeChatTextField.returnKeyType = UIReturnKeyDefault;
|
|
99 |
writeChatTextField.secureTextEntry = NO;
|
|
100 |
[self.view addSubview:writeChatTextField];
|
|
101 |
}
|
|
102 |
writeChatTextField.alpha = 1;
|
|
103 |
[self activateOverlay];
|
|
104 |
[dimTimer setFireDate:HIDING_TIME_NEVER];
|
3617
|
105 |
*/
|
3547
|
106 |
}
|
|
107 |
|
|
108 |
-(void) chatDisappear {
|
3617
|
109 |
/*
|
3547
|
110 |
writeChatTextField.alpha = 0;
|
|
111 |
[writeChatTextField resignFirstResponder];
|
|
112 |
[dimTimer setFireDate:HIDING_TIME_DEFAULT];
|
3617
|
113 |
*/
|
3547
|
114 |
}
|
|
115 |
|
|
116 |
#pragma mark -
|
|
117 |
#pragma mark View Management
|
|
118 |
-(void) viewDidLoad {
|
|
119 |
isPopoverVisible = NO;
|
|
120 |
singleton = self.spinningWheel;
|
|
121 |
canDim = NO;
|
|
122 |
self.view.alpha = 0;
|
|
123 |
self.view.center = CGPointMake(self.view.frame.size.height/2.0, self.view.frame.size.width/2.0);
|
|
124 |
|
|
125 |
|
|
126 |
dimTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:6]
|
|
127 |
interval:1000
|
|
128 |
target:self
|
|
129 |
selector:@selector(dimOverlay)
|
|
130 |
userInfo:nil
|
|
131 |
repeats:YES];
|
|
132 |
|
|
133 |
// add timer too runloop, otherwise it doesn't work
|
|
134 |
[[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode];
|
|
135 |
|
|
136 |
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
|
|
137 |
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
138 |
selector:@selector(didRotate:)
|
3598
|
139 |
name:UIDeviceOrientationDidChangeNotification
|
3547
|
140 |
object:nil];
|
|
141 |
|
|
142 |
[UIView beginAnimations:@"showing overlay" context:NULL];
|
|
143 |
[UIView setAnimationDuration:1];
|
|
144 |
self.view.alpha = 1;
|
|
145 |
[UIView commitAnimations];
|
3598
|
146 |
|
|
147 |
// set initial orientation
|
|
148 |
[self didRotate:[NSNotification notificationWithName:UIDeviceOrientationDidChangeNotification object:nil]];
|
3547
|
149 |
}
|
|
150 |
|
|
151 |
-(void) viewDidUnload {
|
3617
|
152 |
[dimTimer invalidate];
|
|
153 |
[popoverController dismissPopoverAnimated:NO];
|
3547
|
154 |
self.writeChatTextField = nil;
|
|
155 |
self.popoverController = nil;
|
|
156 |
self.popupMenu = nil;
|
|
157 |
self.spinningWheel = nil;
|
|
158 |
[super viewDidUnload];
|
|
159 |
MSG_DIDUNLOAD();
|
|
160 |
}
|
|
161 |
|
|
162 |
-(void) dealloc {
|
|
163 |
[writeChatTextField release];
|
|
164 |
[popupMenu release];
|
|
165 |
[popoverController release];
|
|
166 |
// dimTimer is autoreleased
|
|
167 |
[spinningWheel release];
|
|
168 |
[super dealloc];
|
|
169 |
}
|
|
170 |
|
|
171 |
#pragma mark -
|
|
172 |
#pragma mark Overlay actions and members
|
|
173 |
// dim the overlay when there's no more input for a certain amount of time
|
|
174 |
-(IBAction) buttonReleased:(id) sender {
|
|
175 |
HW_allKeysUp();
|
|
176 |
[dimTimer setFireDate:HIDING_TIME_DEFAULT];
|
|
177 |
}
|
|
178 |
|
|
179 |
// nice transition for dimming, should be called only by the timer himself
|
|
180 |
-(void) dimOverlay {
|
|
181 |
if (canDim) {
|
|
182 |
[UIView beginAnimations:@"overlay dim" context:NULL];
|
|
183 |
[UIView setAnimationDuration:0.6];
|
|
184 |
self.view.alpha = 0.2;
|
|
185 |
[UIView commitAnimations];
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
|
189 |
// set the overlay visible and put off the timer for enough time
|
|
190 |
-(void) activateOverlay {
|
|
191 |
self.view.alpha = 1;
|
|
192 |
[dimTimer setFireDate:HIDING_TIME_NEVER];
|
|
193 |
}
|
|
194 |
|
|
195 |
// issue certain action based on the tag of the button
|
|
196 |
-(IBAction) buttonPressed:(id) sender {
|
|
197 |
[self activateOverlay];
|
|
198 |
if (isPopoverVisible) {
|
|
199 |
[self dismissPopover];
|
|
200 |
}
|
|
201 |
UIButton *theButton = (UIButton *)sender;
|
|
202 |
|
|
203 |
switch (theButton.tag) {
|
|
204 |
case 0:
|
|
205 |
HW_walkLeft();
|
|
206 |
break;
|
|
207 |
case 1:
|
|
208 |
HW_walkRight();
|
|
209 |
break;
|
|
210 |
case 2:
|
|
211 |
HW_aimUp();
|
|
212 |
break;
|
|
213 |
case 3:
|
|
214 |
HW_aimDown();
|
|
215 |
break;
|
|
216 |
case 4:
|
|
217 |
HW_shoot();
|
|
218 |
break;
|
|
219 |
case 5:
|
|
220 |
HW_jump();
|
|
221 |
break;
|
|
222 |
case 6:
|
|
223 |
HW_backjump();
|
|
224 |
break;
|
|
225 |
case 7:
|
|
226 |
HW_tab();
|
|
227 |
break;
|
|
228 |
case 10:
|
|
229 |
[self showPopover];
|
|
230 |
break;
|
|
231 |
default:
|
|
232 |
NSLog(@"Nope");
|
|
233 |
break;
|
|
234 |
}
|
|
235 |
}
|
|
236 |
|
|
237 |
// present a further check before closing game
|
|
238 |
-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
|
|
239 |
if ([actionSheet cancelButtonIndex] != buttonIndex)
|
|
240 |
HW_terminate(NO);
|
|
241 |
else
|
|
242 |
HW_pause();
|
|
243 |
}
|
|
244 |
|
|
245 |
// show up a popover containing a popupMenuViewController; we hook it with setPopoverContentSize
|
|
246 |
// on iphone instead just use the tableViewController directly (and implement manually all animations)
|
|
247 |
-(IBAction) showPopover{
|
|
248 |
isPopoverVisible = YES;
|
|
249 |
|
|
250 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
251 |
if (popupMenu == nil)
|
|
252 |
popupMenu = [[PopoverMenuViewController alloc] initWithStyle:UITableViewStylePlain];
|
|
253 |
if (popoverController == nil) {
|
|
254 |
popoverController = [[UIPopoverController alloc] initWithContentViewController:popupMenu];
|
|
255 |
[popoverController setPopoverContentSize:CGSizeMake(220, 170) animated:YES];
|
|
256 |
[popoverController setPassthroughViews:[NSArray arrayWithObject:self.view]];
|
|
257 |
}
|
3551
|
258 |
|
|
259 |
[popoverController presentPopoverFromRect:CGRectMake(1000, 0, 220, 32)
|
3547
|
260 |
inView:self.view
|
|
261 |
permittedArrowDirections:UIPopoverArrowDirectionUp
|
|
262 |
animated:YES];
|
|
263 |
} else {
|
|
264 |
if (popupMenu == nil) {
|
|
265 |
popupMenu = [[PopoverMenuViewController alloc] initWithStyle:UITableViewStyleGrouped];
|
|
266 |
popupMenu.view.backgroundColor = [UIColor clearColor];
|
|
267 |
popupMenu.view.frame = CGRectMake(480, 0, 200, 170);
|
|
268 |
}
|
|
269 |
[self.view addSubview:popupMenu.view];
|
|
270 |
|
|
271 |
[UIView beginAnimations:@"showing popover" context:NULL];
|
|
272 |
[UIView setAnimationDuration:0.35];
|
|
273 |
popupMenu.view.frame = CGRectMake(280, 0, 200, 170);
|
|
274 |
[UIView commitAnimations];
|
|
275 |
}
|
|
276 |
popupMenu.tableView.scrollEnabled = NO;
|
|
277 |
}
|
|
278 |
|
|
279 |
// on ipad just dismiss it, on iphone transtion to the right
|
|
280 |
-(void) dismissPopover {
|
|
281 |
if (YES == isPopoverVisible) {
|
|
282 |
isPopoverVisible = NO;
|
|
283 |
|
|
284 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
285 |
[popoverController dismissPopoverAnimated:YES];
|
|
286 |
} else {
|
|
287 |
[UIView beginAnimations:@"hiding popover" context:NULL];
|
|
288 |
[UIView setAnimationDuration:0.35];
|
|
289 |
popupMenu.view.frame = CGRectMake(480, 0, 200, 170);
|
|
290 |
[UIView commitAnimations];
|
|
291 |
|
|
292 |
[popupMenu.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.35];
|
|
293 |
}
|
|
294 |
[self buttonReleased:nil];
|
|
295 |
}
|
|
296 |
}
|
|
297 |
|
|
298 |
-(void) textFieldDoneEditing:(id) sender{
|
|
299 |
[sender resignFirstResponder];
|
|
300 |
}
|
|
301 |
|
|
302 |
// this function is called by pascal FinishProgress and removes the spinning wheel when loading is done
|
|
303 |
void spinningWheelDone (void) {
|
3551
|
304 |
[singleton stopAnimating];
|
|
305 |
singleton = nil;
|
3547
|
306 |
canDim = YES;
|
|
307 |
}
|
|
308 |
|
|
309 |
#pragma mark -
|
|
310 |
#pragma mark Custom touch event handling
|
|
311 |
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
312 |
NSArray *twoTouches;
|
|
313 |
UITouch *touch = [touches anyObject];
|
3551
|
314 |
CGRect screen = [[UIScreen mainScreen] bounds];
|
|
315 |
CGPoint currentPosition = [touch locationInView:self.view];
|
3547
|
316 |
|
|
317 |
if (isPopoverVisible) {
|
|
318 |
[self dismissPopover];
|
|
319 |
}
|
|
320 |
if (writeChatTextField) {
|
|
321 |
[self.writeChatTextField resignFirstResponder];
|
|
322 |
[dimTimer setFireDate:HIDING_TIME_DEFAULT];
|
|
323 |
}
|
3598
|
324 |
|
|
325 |
if (currentPosition.y < screen.size.width - 120) {
|
|
326 |
switch ([touches count]) {
|
|
327 |
case 1:
|
|
328 |
DLog(@"X:%d Y:%d", HWX(currentPosition.x), HWY(currentPosition.y));
|
|
329 |
HW_setCursor(HWX(currentPosition.x), HWY(currentPosition.y));
|
|
330 |
break;
|
|
331 |
case 2:
|
|
332 |
if (2 == [touch tapCount]) {
|
|
333 |
HW_ammoMenu();
|
|
334 |
//HW_zoomReset();
|
|
335 |
}
|
|
336 |
|
|
337 |
// pinching
|
|
338 |
twoTouches = [touches allObjects];
|
|
339 |
UITouch *first = [twoTouches objectAtIndex:0];
|
|
340 |
UITouch *second = [twoTouches objectAtIndex:1];
|
|
341 |
initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]);
|
|
342 |
break;
|
|
343 |
default:
|
|
344 |
break;
|
|
345 |
}
|
3547
|
346 |
}
|
|
347 |
}
|
|
348 |
|
|
349 |
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
350 |
initialDistanceForPinching = 0;
|
|
351 |
//HW_allKeysUp();
|
3551
|
352 |
HW_click();
|
3547
|
353 |
}
|
|
354 |
|
|
355 |
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
356 |
// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances
|
|
357 |
[self touchesEnded:touches withEvent:event];
|
|
358 |
}
|
|
359 |
|
|
360 |
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
|
3551
|
361 |
CGRect screen = [[UIScreen mainScreen] bounds];
|
3547
|
362 |
|
|
363 |
NSArray *twoTouches;
|
|
364 |
CGPoint currentPosition;
|
|
365 |
UITouch *touch = [touches anyObject];
|
|
366 |
|
|
367 |
switch ([touches count]) {
|
3551
|
368 |
case 1:
|
|
369 |
if (HW_isAmmoOpen()) {
|
|
370 |
currentPosition = [touch locationInView:self.view];
|
|
371 |
DLog(@"X:%d Y:%d", HWX(currentPosition.x), HWY(currentPosition.y));
|
|
372 |
HW_setCursor(HWX(currentPosition.x), HWY(currentPosition.y));
|
3547
|
373 |
}
|
3551
|
374 |
break;
|
3547
|
375 |
case 2:
|
|
376 |
twoTouches = [touches allObjects];
|
|
377 |
UITouch *first = [twoTouches objectAtIndex:0];
|
|
378 |
UITouch *second = [twoTouches objectAtIndex:1];
|
|
379 |
CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]);
|
|
380 |
const int pinchDelta = 40;
|
|
381 |
|
|
382 |
if (0 != initialDistanceForPinching) {
|
|
383 |
if (currentDistanceOfPinching - initialDistanceForPinching > pinchDelta) {
|
|
384 |
HW_zoomIn();
|
|
385 |
initialDistanceForPinching = currentDistanceOfPinching;
|
|
386 |
}
|
|
387 |
else if (initialDistanceForPinching - currentDistanceOfPinching > pinchDelta) {
|
|
388 |
HW_zoomOut();
|
|
389 |
initialDistanceForPinching = currentDistanceOfPinching;
|
|
390 |
}
|
|
391 |
} else
|
|
392 |
initialDistanceForPinching = currentDistanceOfPinching;
|
|
393 |
|
|
394 |
break;
|
|
395 |
default:
|
|
396 |
break;
|
|
397 |
}
|
|
398 |
}
|
|
399 |
|
|
400 |
|
|
401 |
@end
|