project_files/HedgewarsMobile/Classes/OverlayViewController.m
changeset 5662 99083392cd4f
parent 5208 878e551f0b4a
child 6000 dbcebcd3d79f
equal deleted inserted replaced
5660:1647244b3ffe 5662:99083392cd4f
    33 #define doDim()             [dimTimer setFireDate: (IS_DUALHEAD()) ? HIDING_TIME_NEVER : HIDING_TIME_DEFAULT]
    33 #define doDim()             [dimTimer setFireDate: (IS_DUALHEAD()) ? HIDING_TIME_NEVER : HIDING_TIME_DEFAULT]
    34 #define doNotDim()          [dimTimer setFireDate:HIDING_TIME_NEVER]
    34 #define doNotDim()          [dimTimer setFireDate:HIDING_TIME_NEVER]
    35 
    35 
    36 
    36 
    37 @implementation OverlayViewController
    37 @implementation OverlayViewController
    38 @synthesize popoverController, popupMenu, helpPage, amvc, initialScreenCount, initialOrientation,
    38 @synthesize popoverController, popupMenu, helpPage, amvc, initialScreenCount, lowerIndicator, savesIndicator,
    39             lowerIndicator, savesIndicator, confirmButton, grenadeTimeSegment;
    39             confirmButton, grenadeTimeSegment;
    40 
    40 
    41 #pragma mark -
    41 #pragma mark -
    42 #pragma mark rotation
    42 #pragma mark rotation
    43 
    43 
    44 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    44 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    45     return rotationManager(interfaceOrientation);
    45     return rotationManager(interfaceOrientation);
    46 }
       
    47 
       
    48 // while in dual head the above rotation functions are not called
       
    49 -(void) handleRotationEvent:(NSNotification *)notification {
       
    50     if (isGameRunning() == NO)
       
    51         return;
       
    52 
       
    53     UIView *sdlView = nil;
       
    54     for (UIView *oneView in [[[UIApplication sharedApplication] keyWindow] subviews])
       
    55         if ([oneView isMemberOfClass:[SDL_uikitopenglview class]]) {
       
    56             sdlView = (UIView *)oneView;
       
    57             break;
       
    58         }
       
    59 
       
    60     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
       
    61     NSInteger angle_left = (self.initialOrientation == UIInterfaceOrientationLandscapeLeft) ? 180 : 0;
       
    62     NSInteger angle_right = (self.initialOrientation == UIInterfaceOrientationLandscapeLeft) ? 0 : 180;
       
    63 
       
    64     NSString *model = getModelType();
       
    65     if (IS_VERY_POWERFUL(model)) {
       
    66         [UIView beginAnimations:@"overlay rotation" context:NULL];
       
    67         [UIView setAnimationDuration:0.7];
       
    68     }
       
    69     switch (orientation) {
       
    70         case UIDeviceOrientationLandscapeLeft:
       
    71             self.view.frame = [[UIScreen mainScreen] bounds];
       
    72             self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
       
    73             if (IS_DUALHEAD() == NO)
       
    74                 sdlView.transform = CGAffineTransformMakeRotation(degreesToRadians(angle_left));
       
    75             break;
       
    76         case UIDeviceOrientationLandscapeRight:
       
    77             self.view.frame = [[UIScreen mainScreen] bounds];
       
    78             self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
       
    79             if (IS_DUALHEAD() == NO)
       
    80                 sdlView.transform = CGAffineTransformMakeRotation(degreesToRadians(angle_right));
       
    81             break;
       
    82         default:
       
    83             // a debug log would spam too much
       
    84             break;
       
    85     }
       
    86     if (IS_VERY_POWERFUL(model))
       
    87         [UIView commitAnimations];
       
    88 }
    46 }
    89 
    47 
    90 #pragma mark -
    48 #pragma mark -
    91 #pragma mark View Management
    49 #pragma mark View Management
    92 -(id) initWithCoder:(NSCoder *)aDecoder {
    50 -(id) initWithCoder:(NSCoder *)aDecoder {
    93     if ((self = [super initWithCoder:aDecoder])) {
    51     if ((self = [super initWithCoder:aDecoder])) {
    94         isAttacking = NO;
    52         isAttacking = NO;
    95         isPopoverVisible = NO;
    53         isPopoverVisible = NO;
    96         initialScreenCount = (IS_DUALHEAD() ? 2 : 1);
    54         initialScreenCount = (IS_DUALHEAD() ? 2 : 1);
    97         initialOrientation = 0;
       
    98         lowerIndicator = nil;
    55         lowerIndicator = nil;
    99         savesIndicator = nil;
    56         savesIndicator = nil;
   100     }
    57     }
   101     return self;
    58     return self;
   102 }
    59 }
   103 
    60 
   104 -(void) viewDidLoad {
    61 -(void) viewDidLoad {
   105     CGRect screenRect = [[UIScreen mainScreen] bounds];
    62     // fill all the screen available as sdlview disables autoresizing
   106     self.view.frame = CGRectMake(0, 0, screenRect.size.height, screenRect.size.width);
    63     CGRect rect = [[UIScreen mainScreen] bounds];
   107     self.view.center = CGPointMake(self.view.frame.size.height/2, self.view.frame.size.width/2);
    64     self.view.frame = CGRectMake(0, 0, rect.size.height, rect.size.width);
   108 
    65 
   109     // set initial orientation of the controller orientation
       
   110     switch (self.interfaceOrientation) {
       
   111         case UIDeviceOrientationLandscapeLeft:
       
   112             self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
       
   113             break;
       
   114         case UIDeviceOrientationLandscapeRight:
       
   115             self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
       
   116             break;
       
   117         default:
       
   118             break;
       
   119     }
       
   120     [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
       
   121     [[NSNotificationCenter defaultCenter] addObserver:self
       
   122                                              selector:@selector(handleRotationEvent:)
       
   123                                                  name:UIDeviceOrientationDidChangeNotification
       
   124                                                object:nil];
       
   125     
       
   126     // the timer used to dim the overlay
    66     // the timer used to dim the overlay
   127     dimTimer = [[NSTimer alloc] initWithFireDate:(IS_DUALHEAD()) ? HIDING_TIME_NEVER : [NSDate dateWithTimeIntervalSinceNow:6]
    67     dimTimer = [[NSTimer alloc] initWithFireDate:(IS_DUALHEAD()) ? HIDING_TIME_NEVER : [NSDate dateWithTimeIntervalSinceNow:6]
   128                                         interval:1000
    68                                         interval:1000
   129                                           target:self
    69                                           target:self
   130                                         selector:@selector(dimOverlay)
    70                                         selector:@selector(dimOverlay)
   137     [[NSNotificationCenter defaultCenter] addObserver:self
    77     [[NSNotificationCenter defaultCenter] addObserver:self
   138                                              selector:@selector(showHelp:)
    78                                              selector:@selector(showHelp:)
   139                                                  name:@"show help ingame"
    79                                                  name:@"show help ingame"
   140                                                object:nil];
    80                                                object:nil];
   141 
    81 
   142     // for iOS >= 3.2
    82     if (IS_IPAD()) {
   143     if ([UIScreen respondsToSelector:@selector(screens)]) {
       
   144         [[NSNotificationCenter defaultCenter] addObserver:self
    83         [[NSNotificationCenter defaultCenter] addObserver:self
   145                                                  selector:@selector(numberOfScreensIncreased)
    84                                                  selector:@selector(numberOfScreensIncreased)
   146                                                      name:UIScreenDidConnectNotification
    85                                                      name:UIScreenDidConnectNotification
   147                                                    object:nil];
    86                                                    object:nil];
   148         
    87         
   159     self.view.alpha = 1;
    98     self.view.alpha = 1;
   160     [UIView commitAnimations];
    99     [UIView commitAnimations];
   161 }
   100 }
   162 
   101 
   163 -(void) viewDidUnload {
   102 -(void) viewDidUnload {
   164     [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
       
   165     [[NSNotificationCenter defaultCenter] removeObserver:self];
   103     [[NSNotificationCenter defaultCenter] removeObserver:self];
   166 
   104 
   167     [NSObject cancelPreviousPerformRequestsWithTarget:self
   105     [NSObject cancelPreviousPerformRequestsWithTarget:self
   168                                              selector:@selector(unsetPreciseStatus)
   106                                              selector:@selector(unsetPreciseStatus)
   169                                                object:nil];
   107                                                object:nil];
   405     [self.view addSubview:helpPage.view];
   343     [self.view addSubview:helpPage.view];
   406     [UIView beginAnimations:@"helpingame" context:NULL];
   344     [UIView beginAnimations:@"helpingame" context:NULL];
   407     self.helpPage.view.alpha = 1;
   345     self.helpPage.view.alpha = 1;
   408     [UIView commitAnimations];
   346     [UIView commitAnimations];
   409     doNotDim();
   347     doNotDim();
   410 }
       
   411 
       
   412 // present a further check before closing game
       
   413 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
       
   414     if ([actionSheet cancelButtonIndex] != buttonIndex)
       
   415         HW_terminate(NO);
       
   416     else
       
   417         HW_pause();
       
   418 }
   348 }
   419 
   349 
   420 // show up a popover containing a popupMenuViewController; we hook it with setPopoverContentSize
   350 // show up a popover containing a popupMenuViewController; we hook it with setPopoverContentSize
   421 // on iphone instead just use the tableViewController directly (and implement manually all animations)
   351 // on iphone instead just use the tableViewController directly (and implement manually all animations)
   422 -(IBAction) showPopover{
   352 -(IBAction) showPopover{