cocoaTouch/OverlayViewController.m
changeset 3463 23c50be687a9
parent 3405 8fdb08497bf1
child 3479 972ae3ec178a
equal deleted inserted replaced
3462:4b36933dce1d 3463:23c50be687a9
     9 #import "OverlayViewController.h"
     9 #import "OverlayViewController.h"
    10 #import "SDL_uikitappdelegate.h"
    10 #import "SDL_uikitappdelegate.h"
    11 #import "PascalImports.h"
    11 #import "PascalImports.h"
    12 #import "CGPointUtils.h"
    12 #import "CGPointUtils.h"
    13 #import "SDL_mouse.h"
    13 #import "SDL_mouse.h"
       
    14 #import "SDL_config_iphoneos.h"
    14 #import "PopoverMenuViewController.h"
    15 #import "PopoverMenuViewController.h"
    15 #import "CommodityFunctions.h"
    16 #import "CommodityFunctions.h"
    16 
    17 
       
    18 #define HIDING_TIME_DEFAULT [NSDate dateWithTimeIntervalSinceNow:2.7]
       
    19 #define HIDING_TIME_NEVER   [NSDate dateWithTimeIntervalSinceNow:10000]
       
    20 
       
    21 
    17 @implementation OverlayViewController
    22 @implementation OverlayViewController
    18 @synthesize popoverController, popupMenu;
    23 @synthesize popoverController, popupMenu, writeChatTextField;
    19 
    24 
    20 
    25 
    21 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    26 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    22 	return rotationManager(interfaceOrientation);
    27 	return rotationManager(interfaceOrientation);
    23 }
    28 }
    31 
    36 
    32 -(void) didRotate:(NSNotification *)notification {	
    37 -(void) didRotate:(NSNotification *)notification {	
    33     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    38     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    34     CGRect rect = [[UIScreen mainScreen] bounds];
    39     CGRect rect = [[UIScreen mainScreen] bounds];
    35     CGRect usefulRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
    40     CGRect usefulRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
    36     UIView *sdlView = [[SDLUIKitDelegate sharedAppDelegate].uiwindow viewWithTag:SDL_VIEW_TAG];
    41     UIView *sdlView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:12345];
    37     
    42     
    38     [UIView beginAnimations:@"rotation" context:NULL];
    43     [UIView beginAnimations:@"rotation" context:NULL];
    39     [UIView setAnimationDuration:0.8f];
    44     [UIView setAnimationDuration:0.8f];
    40     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    45     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    41 	switch (orientation) {
    46 	switch (orientation) {
    42         case UIDeviceOrientationLandscapeLeft:
    47         case UIDeviceOrientationLandscapeLeft:
    43             sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
    48             sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
    44             self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
    49             self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
       
    50             [self chatDisappear];
       
    51             [dimTimer setFireDate:HIDING_TIME_DEFAULT];
    45             HW_setLandscape(YES);
    52             HW_setLandscape(YES);
    46             break;
    53             break;
    47         case UIDeviceOrientationLandscapeRight:
    54         case UIDeviceOrientationLandscapeRight:
    48             sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
    55             sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
    49             self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
    56             self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
       
    57             [self chatDisappear];
       
    58             [dimTimer setFireDate:HIDING_TIME_DEFAULT];
    50             HW_setLandscape(YES);
    59             HW_setLandscape(YES);
    51             break;
    60             break;
    52         case UIDeviceOrientationPortrait:
    61         case UIDeviceOrientationPortrait:
    53             sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(270));
    62             sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(270));
    54             self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
    63             self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
       
    64             [self chatAppear];
       
    65             [self activateOverlay];
       
    66             [dimTimer setFireDate:HIDING_TIME_NEVER];
    55             HW_setLandscape(NO);
    67             HW_setLandscape(NO);
    56             break;
    68             break;
    57         case UIDeviceOrientationPortraitUpsideDown:
    69         case UIDeviceOrientationPortraitUpsideDown:
    58             sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
    70             sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
    59             self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
    71             self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
       
    72             [self chatAppear];
       
    73             [self activateOverlay];
       
    74             [dimTimer setFireDate:HIDING_TIME_NEVER];
    60             HW_setLandscape(NO);
    75             HW_setLandscape(NO);
    61             break;
    76             break;
    62         default:
    77         default:
    63             NSLog(@"warning - Unknown rotation status");
    78             NSLog(@"warning - Unknown rotation status");
    64             break;
    79             break;
    65     }
    80     }
    66     self.view.frame = usefulRect;
    81     self.view.frame = usefulRect;
    67     sdlView.frame = usefulRect;
    82     //sdlView.frame = usefulRect;
    68     [UIView commitAnimations];
    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 }
       
   103 
       
   104 -(void) chatDisappear {
       
   105     writeChatTextField.alpha = 0;
       
   106     [writeChatTextField resignFirstResponder];
       
   107     [dimTimer setFireDate:HIDING_TIME_DEFAULT];
    69 }
   108 }
    70 
   109 
    71 -(void) viewDidLoad {
   110 -(void) viewDidLoad {
    72     isPopoverVisible = NO;
   111     isPopoverVisible = NO;
    73     self.view.alpha = 0;
   112     self.view.alpha = 0;
    87     // listen for dismissal of the popover (see below)
   126     // listen for dismissal of the popover (see below)
    88     [[NSNotificationCenter defaultCenter] addObserver:self
   127     [[NSNotificationCenter defaultCenter] addObserver:self
    89                                              selector:@selector(dismissPopover)
   128                                              selector:@selector(dismissPopover)
    90                                                  name:@"dismissPopover"
   129                                                  name:@"dismissPopover"
    91                                                object:nil];
   130                                                object:nil];
    92     // present the overlay after 2 seconds
       
    93     [NSTimer scheduledTimerWithTimeInterval:2
       
    94                                      target:self
       
    95                                    selector:@selector(showMenuAfterwards)
       
    96                                    userInfo:nil
       
    97                                     repeats:NO];
       
    98 }
       
    99 
       
   100 -(void) viewDidUnload {
       
   101     self.popoverController = nil;
       
   102     self.popupMenu = nil;
       
   103     [super viewDidUnload];
       
   104 }
       
   105 
       
   106 -(void) dealloc {
       
   107 	[dimTimer invalidate];
       
   108     [popupMenu release];
       
   109     [popoverController release];
       
   110     // dimTimer is autoreleased
       
   111     [super dealloc];
       
   112 }
       
   113 
       
   114 // draws the controller overlay after the sdl window has taken control
       
   115 -(void) showMenuAfterwards {
       
   116     [[SDLUIKitDelegate sharedAppDelegate].uiwindow bringSubviewToFront:self.view];
       
   117     
   131     
   118     // need to split paths because iphone doesn't rotate (so we don't need to subscribe to any notification
   132     // need to split paths because iphone doesn't rotate (so we don't need to subscribe to any notification
   119     // nor perform engine actions when rotating
   133     // nor perform engine actions when rotating
   120     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
   134     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
   121         [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];	
   135         [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];	
   132 	[UIView setAnimationDuration:1];
   146 	[UIView setAnimationDuration:1];
   133 	self.view.alpha = 1;
   147 	self.view.alpha = 1;
   134 	[UIView commitAnimations];
   148 	[UIView commitAnimations];
   135 }
   149 }
   136 
   150 
       
   151 -(void) viewDidUnload {
       
   152     self.writeChatTextField = nil;
       
   153     self.popoverController = nil;
       
   154     self.popupMenu = nil;
       
   155     [super viewDidUnload];
       
   156 }
       
   157 
       
   158 -(void) dealloc {
       
   159 	[dimTimer invalidate];
       
   160     [writeChatTextField release];
       
   161     [popupMenu release];
       
   162     [popoverController release];
       
   163     // dimTimer is autoreleased
       
   164     [super dealloc];
       
   165 }
       
   166 
   137 // dim the overlay when there's no more input for a certain amount of time
   167 // dim the overlay when there's no more input for a certain amount of time
   138 -(IBAction) buttonReleased:(id) sender {
   168 -(IBAction) buttonReleased:(id) sender {
   139 	HW_allKeysUp();
   169 	HW_allKeysUp();
   140     [dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2.7]];
   170     [dimTimer setFireDate:HIDING_TIME_DEFAULT];
   141 }
   171 }
   142 
   172 
   143 // nice transition for dimming
   173 // nice transition for dimming, should be called only by the timer himself
   144 -(void) dimOverlay {
   174 -(void) dimOverlay {
   145     [UIView beginAnimations:@"overlay dim" context:NULL];
   175     [UIView beginAnimations:@"overlay dim" context:NULL];
   146    	[UIView setAnimationDuration:0.6];
   176    	[UIView setAnimationDuration:0.6];
   147     self.view.alpha = 0.2;
   177     self.view.alpha = 0.2;
   148 	[UIView commitAnimations];
   178 	[UIView commitAnimations];
   149 }
   179 }
   150 
   180 
   151 // set the overlay visible and put off the timer for enough time
   181 // set the overlay visible and put off the timer for enough time
   152 -(void) activateOverlay {
   182 -(void) activateOverlay {
   153     self.view.alpha = 1;
   183     self.view.alpha = 1;
   154     [dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:1000]];
   184     [dimTimer setFireDate:HIDING_TIME_NEVER];
   155 }
   185 }
   156 
   186 
   157 // issue certain action based on the tag of the button 
   187 // issue certain action based on the tag of the button 
   158 -(IBAction) buttonPressed:(id) sender {
   188 -(IBAction) buttonPressed:(id) sender {
   159     [self activateOverlay];
   189     [self activateOverlay];
   206 
   236 
   207 // show up a popover containing a popupMenuViewController; we hook it with setPopoverContentSize
   237 // show up a popover containing a popupMenuViewController; we hook it with setPopoverContentSize
   208 // on iphone instead just use the tableViewController directly (and implement manually all animations)
   238 // on iphone instead just use the tableViewController directly (and implement manually all animations)
   209 -(IBAction) showPopover{
   239 -(IBAction) showPopover{
   210     isPopoverVisible = YES;
   240     isPopoverVisible = YES;
       
   241     CGRect anchorForPopover;
   211     Class popoverControllerClass = NSClassFromString(@"UIPopoverController");
   242     Class popoverControllerClass = NSClassFromString(@"UIPopoverController");
   212     if (popoverControllerClass) {
   243     if (popoverControllerClass) {
   213 #ifdef __IPHONE_3_2
   244 #ifdef __IPHONE_3_2
   214         popupMenu = [[PopoverMenuViewController alloc] initWithStyle:UITableViewStylePlain];
   245         popupMenu = [[PopoverMenuViewController alloc] initWithStyle:UITableViewStylePlain];
   215         popoverController = [[popoverControllerClass alloc] initWithContentViewController:popupMenu];
   246         popoverController = [[popoverControllerClass alloc] initWithContentViewController:popupMenu];
   216         [popoverController setPopoverContentSize:CGSizeMake(220, 170) animated:YES];
   247         [popoverController setPopoverContentSize:CGSizeMake(220, 170) animated:YES];
   217         [popoverController setPassthroughViews:[NSArray arrayWithObject:self.view]];
   248         [popoverController setPassthroughViews:[NSArray arrayWithObject:self.view]];
       
   249 
       
   250         if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]))
       
   251             anchorForPopover = CGRectMake(960, 0, 220, 32);
       
   252         else
       
   253             anchorForPopover = CGRectMake(736, 0, 220, 32);
   218         
   254         
   219         [popoverController presentPopoverFromRect:CGRectMake(960, 0, 220, 32)
   255         [popoverController presentPopoverFromRect:anchorForPopover
   220                                            inView:self.view
   256                                            inView:self.view
   221                          permittedArrowDirections:UIPopoverArrowDirectionUp 
   257                          permittedArrowDirections:UIPopoverArrowDirectionUp
   222                                          animated:YES];
   258                                          animated:YES];
   223 #endif
   259 #endif
   224     } else {
   260     } else {
   225         popupMenu = [[PopoverMenuViewController alloc] initWithStyle:UITableViewStyleGrouped];
   261         popupMenu = [[PopoverMenuViewController alloc] initWithStyle:UITableViewStyleGrouped];
   226         popupMenu.view.backgroundColor = [UIColor clearColor];
   262         popupMenu.view.backgroundColor = [UIColor clearColor];
   233         [UIView commitAnimations];
   269         [UIView commitAnimations];
   234     }
   270     }
   235     popupMenu.tableView.scrollEnabled = NO;
   271     popupMenu.tableView.scrollEnabled = NO;
   236 }
   272 }
   237 
   273 
   238 // on ipad just dismiss it, on iphone transtion on the right
   274 // on ipad just dismiss it, on iphone transtion to the right
   239 -(void) dismissPopover {
   275 -(void) dismissPopover {
   240     if (YES == isPopoverVisible) {
   276     if (YES == isPopoverVisible) {
   241         isPopoverVisible = NO;
   277         isPopoverVisible = NO;
   242         
   278         
   243         if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
   279         if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
   250             popupMenu.view.frame = CGRectMake(480, 0, 200, 170);
   286             popupMenu.view.frame = CGRectMake(480, 0, 200, 170);
   251             [UIView commitAnimations];
   287             [UIView commitAnimations];
   252         
   288         
   253             [popupMenu.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.35];
   289             [popupMenu.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.35];
   254             [popupMenu performSelector:@selector(release) withObject:nil afterDelay:0.35];
   290             [popupMenu performSelector:@selector(release) withObject:nil afterDelay:0.35];
   255             
       
   256             //[dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2.7]];
       
   257         }
   291         }
   258         [self buttonReleased:nil];
   292         [self buttonReleased:nil];
   259     }
   293     }
   260 }
   294 }
   261 
   295 
       
   296 -(void) textFieldDoneEditing:(id) sender{
       
   297     [sender resignFirstResponder];
       
   298 }
   262 
   299 
   263 #pragma mark -
   300 #pragma mark -
   264 #pragma mark Custom touch event handling
   301 #pragma mark Custom touch event handling
   265 
   302 
   266 #define kMinimumPinchDelta      50
   303 #define kMinimumPinchDelta      50
   270 	NSArray *twoTouches;
   307 	NSArray *twoTouches;
   271 	UITouch *touch = [touches anyObject];
   308 	UITouch *touch = [touches anyObject];
   272     
   309     
   273     if (isPopoverVisible) {
   310     if (isPopoverVisible) {
   274         [self dismissPopover];
   311         [self dismissPopover];
       
   312     }
       
   313     if (writeChatTextField) {
       
   314         [self.writeChatTextField resignFirstResponder];
       
   315         [dimTimer setFireDate:HIDING_TIME_DEFAULT];
   275     }
   316     }
   276     
   317     
   277     gestureStartPoint = [touch locationInView:self.view];
   318     gestureStartPoint = [touch locationInView:self.view];
   278         
   319         
   279 	switch ([touches count]) {
   320 	switch ([touches count]) {