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