project_files/HedgewarsMobile/Classes/OverlayViewController.m
changeset 3976 abaf741a4e21
parent 3971 5c82ee165ed5
child 3977 9df7b4812da9
equal deleted inserted replaced
3975:3f605cca9215 3976:abaf741a4e21
    43 #define removeConfirmationInput()   [[self.view viewWithTag:CONFIRMATION_TAG] removeFromSuperview];
    43 #define removeConfirmationInput()   [[self.view viewWithTag:CONFIRMATION_TAG] removeFromSuperview];
    44 
    44 
    45 @implementation OverlayViewController
    45 @implementation OverlayViewController
    46 @synthesize popoverController, popupMenu, helpPage, amvc, isNetGame, useClassicMenu;
    46 @synthesize popoverController, popupMenu, helpPage, amvc, isNetGame, useClassicMenu;
    47 
    47 
       
    48 #pragma mark -
       
    49 #pragma mark rotation
    48 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    50 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    49     return rotationManager(interfaceOrientation);
    51     if (IS_DUALHEAD())
    50 }
    52         return YES;                         ////// TEST MEEEEE
    51 
    53     else
       
    54         return rotationManager(interfaceOrientation);
       
    55 }
       
    56 
       
    57 // pause the game and remove objc menus so that animation is smoother
       
    58 -(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation) toInterfaceOrientation duration:(NSTimeInterval) duration{
       
    59     [self dismissPopover];
       
    60     if (HW_isPaused() == NO)
       
    61         HW_pause();
       
    62     if (self.amvc.isVisible && IS_DUALHEAD() == NO) {
       
    63         [self.amvc disappear];
       
    64         wasVisible = YES;
       
    65     } else
       
    66         wasVisible = NO;
       
    67 
       
    68     [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
       
    69 }
       
    70 
       
    71 // now restore previous state
       
    72 -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation {
       
    73     if (wasVisible || IS_DUALHEAD())
       
    74         [self.amvc appearInView:self.view];
       
    75     if (HW_isPaused() == YES)
       
    76         HW_pause();
       
    77 
       
    78     [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
       
    79 }
       
    80 
       
    81 // rotate the sdl view according to the orientation -- the uiview is autorotated
    52 -(void) didRotate:(NSNotification *)notification {
    82 -(void) didRotate:(NSNotification *)notification {
    53     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    83     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    54     CGRect rect = [[UIScreen mainScreen] bounds];
       
    55     CGRect usefulRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
       
    56     UIView *sdlView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:SDL_VIEW_TAG];
    84     UIView *sdlView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:SDL_VIEW_TAG];
    57 
    85 
    58     [UIView beginAnimations:@"rotation" context:NULL];
    86     [UIView beginAnimations:@"rotation" context:NULL];
    59     [UIView setAnimationDuration:0.8f];
    87     [UIView setAnimationDuration:0.7];
    60     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
       
    61     switch (orientation) {
    88     switch (orientation) {
    62         case UIDeviceOrientationLandscapeLeft:
    89         case UIDeviceOrientationLandscapeLeft:
    63             if (IS_DUALHEAD() == NO)
    90             if (IS_DUALHEAD() == NO)
    64                 sdlView.transform = CGAffineTransformMakeRotation(degreesToRadians(180));
    91                 sdlView.transform = CGAffineTransformMakeRotation(degreesToRadians(a));
    65             self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
    92             //self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
    66             break;
    93             break;
    67         case UIDeviceOrientationLandscapeRight:
    94         case UIDeviceOrientationLandscapeRight:
    68             if (IS_DUALHEAD() == NO)
    95             if (IS_DUALHEAD() == NO)
    69                 sdlView.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
    96                 sdlView.transform = CGAffineTransformMakeRotation(degreesToRadians(b));
    70             self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
    97             //self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
    71             break;
    98             break;
    72         case UIDeviceOrientationPortrait:
    99         case UIDeviceOrientationPortrait:
    73             if (IS_DUALHEAD())
   100             if (IS_DUALHEAD())
    74                 self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
   101                 self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
    75             break;
   102             break;
    79             break;
   106             break;
    80         default:
   107         default:
    81             // a debug log would spam too much
   108             // a debug log would spam too much
    82             break;
   109             break;
    83     }
   110     }
    84     if (self.amvc.isVisible)
       
    85         [self.amvc appearInView:self.view];
       
    86     self.view.frame = usefulRect;
       
    87     [UIView commitAnimations];
   111     [UIView commitAnimations];
    88     
   112 
    89     // for single screens only landscape mode is supported
   113     // for single screens only landscape mode is supported
    90     // for dual screen mode the sdlview is not modified, but you can rotate the pad in any direction
   114     // for dual screen mode the sdlview is not modified, but you can rotate the pad in any direction
    91 }
   115 }
    92 
   116 
    93 #pragma mark -
   117 #pragma mark -
    94 #pragma mark View Management
   118 #pragma mark View Management
       
   119 -(id) initWithCoder:(NSCoder *)aDecoder {
       
   120     if ((self = [super initWithCoder:aDecoder])) {
       
   121         isGameRunning = NO;
       
   122         isReplay = NO;
       
   123         cachedGrenadeTime = 2;
       
   124         isAttacking = NO;
       
   125         wasVisible = NO;
       
   126         isPopoverVisible = NO;    // it is called "popover" even on the iphone
       
   127     }
       
   128     return self;
       
   129 }
       
   130 
    95 -(void) viewDidLoad {
   131 -(void) viewDidLoad {
    96     isGameRunning = NO;
       
    97     isReplay = NO;
       
    98     cachedGrenadeTime = 2;
       
    99     isAttacking = NO;
       
   100     
       
   101     // i called it a popover even on the iphone
       
   102     isPopoverVisible = NO;
       
   103     self.view.alpha = 0;
       
   104     self.view.center = CGPointMake(self.view.frame.size.height/2.0, self.view.frame.size.width/2.0);
       
   105 
       
   106     initialScreenCount = [[UIScreen screens] count];
       
   107 
       
   108     if (IS_DUALHEAD()) {
       
   109         // set initial orientation wrt 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             case UIDeviceOrientationPortrait:
       
   118                 self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
       
   119                 break;
       
   120             case UIDeviceOrientationPortraitUpsideDown:
       
   121                 self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(180));
       
   122                 break;
       
   123             default:
       
   124                 DLog(@"Nope");
       
   125                 break;
       
   126         }
       
   127     }
       
   128     CGRect screenRect = [[UIScreen mainScreen] bounds];
   132     CGRect screenRect = [[UIScreen mainScreen] bounds];
   129     self.view.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
   133     self.view.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
   130 
   134     self.view.alpha = 0;
       
   135     self.view.center = CGPointMake(self.view.frame.size.height/2, self.view.frame.size.width/2);
       
   136 
       
   137     // get the number of screens to know the previous state whan a display is connected or detached
       
   138     initialScreenCount = [[UIScreen screens] count];
       
   139     
       
   140     // detrmine the quanitiy and direction of the rotation
       
   141     if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
       
   142         a = 180;
       
   143         b = 0;
       
   144     } else {
       
   145         a = 0;
       
   146         b = 180;
       
   147     }
       
   148 
       
   149     // the timer used to dim the overlay
   131     dimTimer = [[NSTimer alloc] initWithFireDate:(IS_DUALHEAD()) ? HIDING_TIME_NEVER : [NSDate dateWithTimeIntervalSinceNow:6]
   150     dimTimer = [[NSTimer alloc] initWithFireDate:(IS_DUALHEAD()) ? HIDING_TIME_NEVER : [NSDate dateWithTimeIntervalSinceNow:6]
   132                                         interval:1000
   151                                         interval:1000
   133                                           target:self
   152                                           target:self
   134                                         selector:@selector(dimOverlay)
   153                                         selector:@selector(dimOverlay)
   135                                         userInfo:nil
   154                                         userInfo:nil
   136                                          repeats:YES];
   155                                          repeats:YES];
   137 
       
   138     // add timer to runloop, otherwise it doesn't work
   156     // add timer to runloop, otherwise it doesn't work
   139     [[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode];
   157     [[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode];
   140 
   158 
       
   159     // become listener of some notifications
   141     [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
   160     [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
   142     [[NSNotificationCenter defaultCenter] addObserver:self
   161     [[NSNotificationCenter defaultCenter] addObserver:self
   143                                              selector:@selector(didRotate:)
   162                                              selector:@selector(didRotate:)
   144                                                  name:UIDeviceOrientationDidChangeNotification
   163                                                  name:UIDeviceOrientationDidChangeNotification
   145                                                object:nil];
   164                                                object:nil];
   161     
   180     
   162     [[NSNotificationCenter defaultCenter] addObserver:self
   181     [[NSNotificationCenter defaultCenter] addObserver:self
   163                                              selector:@selector(numberOfScreensDecreased)
   182                                              selector:@selector(numberOfScreensDecreased)
   164                                                  name:UIScreenDidDisconnectNotification
   183                                                  name:UIScreenDidDisconnectNotification
   165                                                object:nil];
   184                                                object:nil];
   166     
   185 
       
   186     // present the overlay
   167     [UIView beginAnimations:@"showing overlay" context:NULL];
   187     [UIView beginAnimations:@"showing overlay" context:NULL];
   168     [UIView setAnimationDuration:1];
   188     [UIView setAnimationDuration:1];
   169     self.view.alpha = 1;
   189     self.view.alpha = 1;
   170     [UIView commitAnimations];
   190     [UIView commitAnimations];
   171 }
   191 }
   207     self.helpPage.view.alpha = 1;
   227     self.helpPage.view.alpha = 1;
   208     [UIView commitAnimations];
   228     [UIView commitAnimations];
   209     doNotDim();
   229     doNotDim();
   210 }
   230 }
   211 
   231 
       
   232 -(void) cleanup {
       
   233     [self dismissPopover];
       
   234     HW_terminate(NO);
       
   235     [self.view removeFromSuperview];
       
   236 }
       
   237 
   212 -(void) didReceiveMemoryWarning {
   238 -(void) didReceiveMemoryWarning {
   213     if (self.popupMenu.view.superview == nil)
   239     if (self.popupMenu.view.superview == nil)
   214         self.popupMenu = nil;
   240         self.popupMenu = nil;
   215     if (self.helpPage.view.superview == nil)
   241     if (self.helpPage.view.superview == nil)
   216         self.helpPage = nil;
   242         self.helpPage = nil;
   243     [amvc release];
   269     [amvc release];
   244     // dimTimer is autoreleased
   270     // dimTimer is autoreleased
   245     [super dealloc];
   271     [super dealloc];
   246 }
   272 }
   247 
   273 
   248 -(void) cleanup {
       
   249     [self dismissPopover];
       
   250     HW_terminate(NO);
       
   251     [self.view removeFromSuperview];
       
   252 }
       
   253 
       
   254 #pragma mark -
   274 #pragma mark -
   255 #pragma mark Overlay actions and members
   275 #pragma mark overlay user interaction
   256 // nice transition for dimming, should be called only by the timer himself
   276 // nice transition for dimming, should be called only by the timer himself
   257 -(void) dimOverlay {
   277 -(void) dimOverlay {
   258     if (isGameRunning) {
   278     if (isGameRunning) {
   259         [UIView beginAnimations:@"overlay dim" context:NULL];
   279         [UIView beginAnimations:@"overlay dim" context:NULL];
   260         [UIView setAnimationDuration:0.6];
   280         [UIView setAnimationDuration:0.6];
   379 
   399 
   380 -(void) unsetPreciseStatus {
   400 -(void) unsetPreciseStatus {
   381     HW_preciseSet(NO);
   401     HW_preciseSet(NO);
   382 }
   402 }
   383 
   403 
       
   404 -(void) sendHWClick {
       
   405     HW_click();
       
   406     removeConfirmationInput();
       
   407     doDim();
       
   408 }
       
   409 
       
   410 -(void) setGrenadeTime:(id) sender {
       
   411     UISegmentedControl *theSegment = (UISegmentedControl *)sender;
       
   412     if (cachedGrenadeTime != theSegment.selectedSegmentIndex) {
       
   413         HW_setGrenadeTime(theSegment.selectedSegmentIndex + 1);
       
   414         cachedGrenadeTime = theSegment.selectedSegmentIndex;
       
   415     }
       
   416 }
       
   417 
       
   418 #pragma mark -
       
   419 #pragma mark other menu
   384 // present a further check before closing game
   420 // present a further check before closing game
   385 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
   421 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
   386     if ([actionSheet cancelButtonIndex] != buttonIndex)
   422     if ([actionSheet cancelButtonIndex] != buttonIndex)
   387         [self cleanup];
   423         [self cleanup];
   388     else
   424     else
   472             break;
   508             break;
   473         default:
   509         default:
   474             break;
   510             break;
   475     }
   511     }
   476 }
   512 }
   477 
       
   478     //if (currentPosition.y < screen.size.width - 130 || (currentPosition.x > 130 && currentPosition.x < screen.size.height - 130)) {
       
   479 
   513 
   480 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   514 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   481     CGRect screen = [[UIScreen mainScreen] bounds];
   515     CGRect screen = [[UIScreen mainScreen] bounds];
   482     NSSet *allTouches = [event allTouches];
   516     NSSet *allTouches = [event allTouches];
   483     CGPoint currentPosition = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
   517     CGPoint currentPosition = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
   560         default:
   594         default:
   561             break;
   595             break;
   562     }
   596     }
   563 
   597 
   564     initialDistanceForPinching = 0;
   598     initialDistanceForPinching = 0;
   565 }
       
   566 
       
   567 -(void) sendHWClick {
       
   568     HW_click();
       
   569     removeConfirmationInput();
       
   570     doDim();
       
   571 }
       
   572 
       
   573 -(void) setGrenadeTime:(id) sender {
       
   574     UISegmentedControl *theSegment = (UISegmentedControl *)sender;
       
   575     if (cachedGrenadeTime != theSegment.selectedSegmentIndex) {
       
   576         HW_setGrenadeTime(theSegment.selectedSegmentIndex + 1);
       
   577         cachedGrenadeTime = theSegment.selectedSegmentIndex;
       
   578     }
       
   579 }
   599 }
   580 
   600 
   581 -(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
   601 -(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
   582     [self touchesEnded:touches withEvent:event];
   602     [self touchesEnded:touches withEvent:event];
   583 }
   603 }
   640             break;
   660             break;
   641     }
   661     }
   642 }
   662 }
   643 
   663 
   644 #pragma mark -
   664 #pragma mark -
   645 #pragma mark Functions called by pascal
   665 #pragma mark Functions called by pascal code
   646 void inline setGameRunning(BOOL value) {
   666 void inline setGameRunning(BOOL value) {
   647     isGameRunning = value;
   667     isGameRunning = value;
   648 }
   668 }
   649 
   669 
   650 // called by uStore from AddProgress
   670 // called by uStore from AddProgress