project_files/HedgewarsMobile/Classes/OverlayViewController.m
changeset 4078 da01196fb5ab
parent 4028 eb371ada631d
child 4082 bfe14b38dad1
equal deleted inserted replaced
4076:7ca17cb94992 4078:da01196fb5ab
    56         wasVisible = YES;
    56         wasVisible = YES;
    57     } else
    57     } else
    58         wasVisible = NO;
    58         wasVisible = NO;
    59 
    59 
    60     [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    60     [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
       
    61 
       
    62     UIView *sdlView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:SDL_VIEW_TAG];
       
    63     switch (toInterfaceOrientation) {
       
    64         case UIDeviceOrientationLandscapeLeft:
       
    65             sdlView.transform = CGAffineTransformMakeRotation(degreesToRadians(a));
       
    66             break;
       
    67         case UIDeviceOrientationLandscapeRight:
       
    68             sdlView.transform = CGAffineTransformMakeRotation(degreesToRadians(b));
       
    69             break;
       
    70         default:
       
    71             // a debug log would spam too much
       
    72             break;
       
    73     }
    61 }
    74 }
    62 
    75 
    63 // now restore previous state
    76 // now restore previous state
    64 -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation {
    77 -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation {
    65     if (wasVisible || IS_DUALHEAD())
    78     if (wasVisible || IS_DUALHEAD())
    68         HW_pause();
    81         HW_pause();
    69 
    82 
    70     [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    83     [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    71 }
    84 }
    72 
    85 
    73 // rotate the sdl view according to the orientation -- the uiview is autorotated
    86 // while in dual head the above rotation functions are not called
    74 -(void) didRotate:(NSNotification *)notification {
    87 -(void) dualHeadRotation:(NSNotification *)notification {
    75     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    88     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    76     UIView *sdlView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:SDL_VIEW_TAG];
       
    77     CGRect screenRect = [[UIScreen mainScreen] bounds];
       
    78     
    89     
    79     [UIView beginAnimations:@"rotation" context:NULL];
    90     [UIView beginAnimations:@"rotation" context:NULL];
    80     [UIView setAnimationDuration:0.7];
    91     [UIView setAnimationDuration:0.7];
    81     switch (orientation) {
    92     switch (orientation) {
    82         case UIDeviceOrientationLandscapeLeft:
    93         case UIDeviceOrientationLandscapeLeft:
    83             if (IS_DUALHEAD()) {
    94             self.view.frame = [[UIScreen mainScreen] bounds];
    84                 self.view.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
    95             self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
    85                 self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
       
    86             } else
       
    87                 sdlView.transform = CGAffineTransformMakeRotation(degreesToRadians(a));
       
    88             break;
    96             break;
    89         case UIDeviceOrientationLandscapeRight:
    97         case UIDeviceOrientationLandscapeRight:
    90             if (IS_DUALHEAD()) {
    98             self.view.frame = [[UIScreen mainScreen] bounds];
    91                 self.view.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
    99             self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
    92                 self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
       
    93             } else
       
    94                 sdlView.transform = CGAffineTransformMakeRotation(degreesToRadians(b));
       
    95             break;
   100             break;
    96         default:
   101         default:
    97             // a debug log would spam too much
   102             // a debug log would spam too much
    98             break;
   103             break;
    99     }
   104     }
   100     [UIView commitAnimations];
   105     [UIView commitAnimations];
   101 
       
   102     // for single screens only landscape mode is supported
       
   103     // for dual screen mode the sdlview is not modified, but you can rotate the pad in any direction
       
   104 }
   106 }
   105 
   107 
   106 #pragma mark -
   108 #pragma mark -
   107 #pragma mark View Management
   109 #pragma mark View Management
   108 -(id) initWithCoder:(NSCoder *)aDecoder {
   110 -(id) initWithCoder:(NSCoder *)aDecoder {
   144                 break;
   146                 break;
   145             default:
   147             default:
   146                 DLog(@"Nope");
   148                 DLog(@"Nope");
   147                 break;
   149                 break;
   148         }
   150         }
       
   151         [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
       
   152         [[NSNotificationCenter defaultCenter] addObserver:self
       
   153                                                  selector:@selector(dualHeadRotation:)
       
   154                                                      name:UIDeviceOrientationDidChangeNotification
       
   155                                                    object:nil];
   149     }
   156     }
   150 
   157 
   151     // the timer used to dim the overlay
   158     // the timer used to dim the overlay
   152     dimTimer = [[NSTimer alloc] initWithFireDate:(IS_DUALHEAD()) ? HIDING_TIME_NEVER : [NSDate dateWithTimeIntervalSinceNow:6]
   159     dimTimer = [[NSTimer alloc] initWithFireDate:(IS_DUALHEAD()) ? HIDING_TIME_NEVER : [NSDate dateWithTimeIntervalSinceNow:6]
   153                                         interval:1000
   160                                         interval:1000
   157                                          repeats:YES];
   164                                          repeats:YES];
   158     // add timer to runloop, otherwise it doesn't work
   165     // add timer to runloop, otherwise it doesn't work
   159     [[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode];
   166     [[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode];
   160 
   167 
   161     // become listener of some notifications
   168     // become listener of some notifications
   162     [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
       
   163     [[NSNotificationCenter defaultCenter] addObserver:self
       
   164                                              selector:@selector(didRotate:)
       
   165                                                  name:UIDeviceOrientationDidChangeNotification
       
   166                                                object:nil];
       
   167 
       
   168     [[NSNotificationCenter defaultCenter] addObserver:self
   169     [[NSNotificationCenter defaultCenter] addObserver:self
   169                                              selector:@selector(showHelp:)
   170                                              selector:@selector(showHelp:)
   170                                                  name:@"show help ingame"
   171                                                  name:@"show help ingame"
   171                                                object:nil];
   172                                                object:nil];
   172 
   173