project_files/HedgewarsMobile/Classes/OverlayViewController.m
changeset 4028 eb371ada631d
parent 3996 eb549fd864a5
child 4078 da01196fb5ab
equal deleted inserted replaced
4027:6e2bff0c173c 4028:eb371ada631d
    18  * File created on 16/03/2010.
    18  * File created on 16/03/2010.
    19  */
    19  */
    20 
    20 
    21 
    21 
    22 #import "OverlayViewController.h"
    22 #import "OverlayViewController.h"
    23 #import "SDL_uikitappdelegate.h"
       
    24 #import "InGameMenuViewController.h"
    23 #import "InGameMenuViewController.h"
    25 #import "HelpPageViewController.h"
    24 #import "HelpPageViewController.h"
    26 #import "AmmoMenuViewController.h"
    25 #import "AmmoMenuViewController.h"
    27 #import "PascalImports.h"
    26 #import "PascalImports.h"
    28 #import "CommodityFunctions.h"
    27 #import "CommodityFunctions.h"
    29 #import "CGPointUtils.h"
    28 #import "CGPointUtils.h"
    30 #import "SDL_config_iphoneos.h"
    29 #import "SDL_config_iphoneos.h"
    31 #import "SDL_mouse.h"
    30 #import "SDL_mouse.h"
       
    31 #import "ObjcExports.h"
    32 
    32 
    33 #define HIDING_TIME_DEFAULT [NSDate dateWithTimeIntervalSinceNow:2.7]
    33 #define HIDING_TIME_DEFAULT [NSDate dateWithTimeIntervalSinceNow:2.7]
    34 #define HIDING_TIME_NEVER   [NSDate dateWithTimeIntervalSinceNow:10000]
    34 #define HIDING_TIME_NEVER   [NSDate dateWithTimeIntervalSinceNow:10000]
    35 #define doDim()             [dimTimer setFireDate: (IS_DUALHEAD()) ? HIDING_TIME_NEVER : HIDING_TIME_DEFAULT]
    35 #define doDim()             [dimTimer setFireDate: (IS_DUALHEAD()) ? HIDING_TIME_NEVER : HIDING_TIME_DEFAULT]
    36 #define doNotDim()          [dimTimer setFireDate:HIDING_TIME_NEVER]
    36 #define doNotDim()          [dimTimer setFireDate:HIDING_TIME_NEVER]
    37 
    37 
    38 #define CONFIRMATION_TAG 5959
       
    39 #define GRENADE_TAG 9595
       
    40 #define REPLAYBLACKVIEW_TAG 9955
       
    41 #define ACTIVITYINDICATOR_TAG 987654
       
    42 #define ANIMATION_DURATION 0.25
       
    43 #define removeConfirmationInput()   [[self.view viewWithTag:CONFIRMATION_TAG] removeFromSuperview];
    38 #define removeConfirmationInput()   [[self.view viewWithTag:CONFIRMATION_TAG] removeFromSuperview];
    44 
    39 
    45 @implementation OverlayViewController
    40 @implementation OverlayViewController
    46 @synthesize popoverController, popupMenu, helpPage, amvc, isNetGame, useClassicMenu;
    41 @synthesize popoverController, popupMenu, helpPage, amvc, isNetGame, useClassicMenu;
    47 
    42 
   110 
   105 
   111 #pragma mark -
   106 #pragma mark -
   112 #pragma mark View Management
   107 #pragma mark View Management
   113 -(id) initWithCoder:(NSCoder *)aDecoder {
   108 -(id) initWithCoder:(NSCoder *)aDecoder {
   114     if ((self = [super initWithCoder:aDecoder])) {
   109     if ((self = [super initWithCoder:aDecoder])) {
   115         isGameRunning = NO;
   110         objcExportsInit();
   116         isReplay = NO;
       
   117         cachedGrenadeTime = 2;
       
   118 
       
   119         isAttacking = NO;
   111         isAttacking = NO;
   120         wasVisible = NO;
   112         wasVisible = NO;
   121         isPopoverVisible = NO;    // it is called "popover" even on the iphone
   113         isPopoverVisible = NO;    // it is called "popover" even on the iphone
   122     }
   114     }
   123     return self;
   115     return self;
   286 
   278 
   287 #pragma mark -
   279 #pragma mark -
   288 #pragma mark overlay user interaction
   280 #pragma mark overlay user interaction
   289 // nice transition for dimming, should be called only by the timer himself
   281 // nice transition for dimming, should be called only by the timer himself
   290 -(void) dimOverlay {
   282 -(void) dimOverlay {
   291     if (isGameRunning) {
   283     if (isGameRunning()) {
   292         [UIView beginAnimations:@"overlay dim" context:NULL];
   284         [UIView beginAnimations:@"overlay dim" context:NULL];
   293         [UIView setAnimationDuration:0.6];
   285         [UIView setAnimationDuration:0.6];
   294         self.view.alpha = 0.2;
   286         self.view.alpha = 0.2;
   295         [UIView commitAnimations];
   287         [UIView commitAnimations];
   296     }
   288     }
   302     doNotDim();
   294     doNotDim();
   303 }
   295 }
   304 
   296 
   305 // dim the overlay when there's no more input for a certain amount of time
   297 // dim the overlay when there's no more input for a certain amount of time
   306 -(IBAction) buttonReleased:(id) sender {
   298 -(IBAction) buttonReleased:(id) sender {
   307     if (isGameRunning == NO)
   299     if (isGameRunning() == NO)
   308         return;
   300         return;
   309 
   301 
   310     UIButton *theButton = (UIButton *)sender;
   302     UIButton *theButton = (UIButton *)sender;
   311 
   303 
   312     switch (theButton.tag) {
   304     switch (theButton.tag) {
   335 
   327 
   336 // issue certain action based on the tag of the button
   328 // issue certain action based on the tag of the button
   337 -(IBAction) buttonPressed:(id) sender {
   329 -(IBAction) buttonPressed:(id) sender {
   338     [self activateOverlay];
   330     [self activateOverlay];
   339     
   331     
   340     if (isGameRunning == NO)
   332     if (isGameRunning() == NO)
   341         return;
   333         return;
   342     
   334     
   343     if (isPopoverVisible)
   335     if (isPopoverVisible)
   344         [self dismissPopover];
   336         [self dismissPopover];
   345     
   337     
   420     doDim();
   412     doDim();
   421 }
   413 }
   422 
   414 
   423 -(void) setGrenadeTime:(id) sender {
   415 -(void) setGrenadeTime:(id) sender {
   424     UISegmentedControl *theSegment = (UISegmentedControl *)sender;
   416     UISegmentedControl *theSegment = (UISegmentedControl *)sender;
   425     if (cachedGrenadeTime != theSegment.selectedSegmentIndex) {
   417     if (cachedGrenadeTime() != theSegment.selectedSegmentIndex) {
   426         HW_setGrenadeTime(theSegment.selectedSegmentIndex + 1);
   418         HW_setGrenadeTime(theSegment.selectedSegmentIndex + 1);
   427         cachedGrenadeTime = theSegment.selectedSegmentIndex;
   419         setGrenadeTime(theSegment.selectedSegmentIndex);
   428     }
   420     }
   429 }
   421 }
   430 
   422 
   431 #pragma mark -
   423 #pragma mark -
   432 #pragma mark other menu
   424 #pragma mark other menu
   488 #pragma mark Custom touch event handling
   480 #pragma mark Custom touch event handling
   489 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   481 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   490     NSSet *allTouches = [event allTouches];
   482     NSSet *allTouches = [event allTouches];
   491     UITouch *first, *second;
   483     UITouch *first, *second;
   492 
   484 
   493     if (isGameRunning == NO)
   485     if (isGameRunning() == NO)
   494         return;
   486         return;
   495 
   487 
   496     // hide in-game menu
   488     // hide in-game menu
   497     if (isPopoverVisible)
   489     if (isPopoverVisible)
   498         [self dismissPopover];
   490         [self dismissPopover];
   527 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   519 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   528     CGRect screen = [[UIScreen mainScreen] bounds];
   520     CGRect screen = [[UIScreen mainScreen] bounds];
   529     NSSet *allTouches = [event allTouches];
   521     NSSet *allTouches = [event allTouches];
   530     CGPoint currentPosition = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
   522     CGPoint currentPosition = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
   531 
   523 
   532     if (isGameRunning == NO)
   524     if (isGameRunning() == NO)
   533         return;
   525         return;
   534     
   526     
   535     switch ([allTouches count]) {
   527     switch ([allTouches count]) {
   536         case 1:
   528         case 1:
   537             // if we're in the menu we just click in the point
   529             // if we're in the menu we just click in the point
   580                             UISegmentedControl *grenadeTime = [[UISegmentedControl alloc] initWithItems:items];
   572                             UISegmentedControl *grenadeTime = [[UISegmentedControl alloc] initWithItems:items];
   581                             [items release];
   573                             [items release];
   582 
   574 
   583                             [grenadeTime addTarget:self action:@selector(setGrenadeTime:) forControlEvents:UIControlEventValueChanged];
   575                             [grenadeTime addTarget:self action:@selector(setGrenadeTime:) forControlEvents:UIControlEventValueChanged];
   584                             grenadeTime.frame = CGRectMake(screen.size.height / 2 - 125, screen.size.width, 250, 50);
   576                             grenadeTime.frame = CGRectMake(screen.size.height / 2 - 125, screen.size.width, 250, 50);
   585                             grenadeTime.selectedSegmentIndex = cachedGrenadeTime;
   577                             grenadeTime.selectedSegmentIndex = cachedGrenadeTime();
   586                             grenadeTime.tag = GRENADE_TAG;
   578                             grenadeTime.tag = GRENADE_TAG;
   587                             [self.view addSubview:grenadeTime];
   579                             [self.view addSubview:grenadeTime];
   588                             [grenadeTime release];
   580                             [grenadeTime release];
   589 
   581 
   590                             [UIView beginAnimations:@"inserting segmented control" context:NULL];
   582                             [UIView beginAnimations:@"inserting segmented control" context:NULL];
   619     CGRect screen = [[UIScreen mainScreen] bounds];
   611     CGRect screen = [[UIScreen mainScreen] bounds];
   620     NSSet *allTouches = [event allTouches];
   612     NSSet *allTouches = [event allTouches];
   621     int x, y, dx, dy;
   613     int x, y, dx, dy;
   622     UITouch *touch, *first, *second;
   614     UITouch *touch, *first, *second;
   623 
   615 
   624     if (isGameRunning == NO)
   616     if (isGameRunning() == NO)
   625         return;
   617         return;
   626     
   618     
   627     switch ([allTouches count]) {
   619     switch ([allTouches count]) {
   628         case 1:
   620         case 1:
   629             touch = [[allTouches allObjects] objectAtIndex:0];
   621             touch = [[allTouches allObjects] objectAtIndex:0];
   672             DLog(@"Nope");
   664             DLog(@"Nope");
   673             break;
   665             break;
   674     }
   666     }
   675 }
   667 }
   676 
   668 
   677 #pragma mark -
       
   678 #pragma mark Functions called by pascal code
       
   679 void inline setGameRunning(BOOL value) {
       
   680     isGameRunning = value;
       
   681 }
       
   682 
       
   683 // called by uStore from AddProgress
       
   684 void startSpinning() {
       
   685     setGameRunning(NO);
       
   686     UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
       
   687     UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
       
   688     indicator.tag = ACTIVITYINDICATOR_TAG;
       
   689     int offset;
       
   690     if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
       
   691         offset = -120;
       
   692     else
       
   693         offset = 120;
       
   694     if (IS_DUALHEAD())
       
   695         indicator.center = CGPointMake(theWindow.frame.size.width/2, theWindow.frame.size.height/2 + offset);
       
   696     else
       
   697         indicator.center = CGPointMake(theWindow.frame.size.width/2 + offset, theWindow.frame.size.height/2);
       
   698     indicator.hidesWhenStopped = YES;
       
   699     [indicator startAnimating];
       
   700     [theWindow addSubview:indicator];
       
   701     [indicator release];
       
   702 }
       
   703 
       
   704 // called by uStore from FinishProgress and by OverlayViewController by replayBegan
       
   705 void stopSpinning() {
       
   706     UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
       
   707     UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[theWindow viewWithTag:ACTIVITYINDICATOR_TAG];
       
   708     [indicator stopAnimating];
       
   709     HW_zoomSet(1.7);
       
   710     if (isReplay == NO)
       
   711         setGameRunning(YES);
       
   712 }
       
   713 
       
   714 // called by CCHandlers from chNextTurn
       
   715 void clearView() {
       
   716     UIWindow *theWindow = (IS_DUALHEAD()) ? [SDLUIKitDelegate sharedAppDelegate].uiwindow : [[UIApplication sharedApplication] keyWindow];
       
   717     UIButton *theButton = (UIButton *)[theWindow viewWithTag:CONFIRMATION_TAG];
       
   718     UISegmentedControl *theSegment = (UISegmentedControl *)[theWindow viewWithTag:GRENADE_TAG];
       
   719 
       
   720     [UIView beginAnimations:@"remove button" context:NULL];
       
   721     [UIView setAnimationDuration:ANIMATION_DURATION];
       
   722     theButton.alpha = 0;
       
   723     theSegment.alpha = 0;
       
   724     [UIView commitAnimations];
       
   725 
       
   726     if (theButton)
       
   727         [theWindow performSelector:@selector(removeFromSuperview) withObject:theButton afterDelay:ANIMATION_DURATION];
       
   728     if (theSegment)
       
   729         [theWindow performSelector:@selector(removeFromSuperview) withObject:theSegment afterDelay:ANIMATION_DURATION];
       
   730 
       
   731     cachedGrenadeTime = 2;
       
   732 }
       
   733 
       
   734 // called by hwengine
       
   735 void replayBegan() {
       
   736     UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
       
   737     UIView *blackView = [[UIView alloc] initWithFrame:theWindow.frame];
       
   738     blackView.backgroundColor = [UIColor blackColor];
       
   739     blackView.alpha = 0.6;
       
   740     blackView.tag = REPLAYBLACKVIEW_TAG;
       
   741     blackView.exclusiveTouch = NO;
       
   742     blackView.multipleTouchEnabled = NO;
       
   743     blackView.userInteractionEnabled = NO;
       
   744     UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
       
   745     indicator.center = theWindow.center;
       
   746     [indicator startAnimating];
       
   747     [blackView addSubview:indicator];
       
   748     [indicator release];
       
   749     [theWindow addSubview:blackView];
       
   750     [blackView release];
       
   751     isReplay = YES;
       
   752     stopSpinning();
       
   753 }
       
   754 
       
   755 // called by uGame
       
   756 void replayFinished() {
       
   757     UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
       
   758     UIView *blackView = (UIView *)[theWindow viewWithTag:REPLAYBLACKVIEW_TAG];
       
   759     
       
   760     [UIView beginAnimations:@"removing black" context:NULL];
       
   761     [UIView setAnimationDuration:1];
       
   762     blackView.alpha = 0;
       
   763     [UIView commitAnimations];
       
   764     [theWindow performSelector:@selector(removeFromSuperview) withObject:blackView afterDelay:1];
       
   765     
       
   766     setGameRunning(YES);
       
   767     isReplay = NO;
       
   768 }
       
   769 
       
   770 @end
   669 @end