initial refactoring of ObjcExports and OverlayViewController
authorkoda
Mon, 25 Apr 2011 06:38:59 +0200
changeset 5174 f5294509783e
parent 5173 4056310a161a
child 5175 a3da220dbb3f
initial refactoring of ObjcExports and OverlayViewController
hedgewars/hwengine.pas
hedgewars/uGame.pas
hedgewars/uIO.pas
hedgewars/uMobile.pas
hedgewars/uStore.pas
project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m
project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m
project_files/HedgewarsMobile/Classes/ObjcExports.h
project_files/HedgewarsMobile/Classes/ObjcExports.m
project_files/HedgewarsMobile/Classes/OverlayViewController.h
project_files/HedgewarsMobile/Classes/OverlayViewController.m
--- a/hedgewars/hwengine.pas	Mon Apr 25 02:25:25 2011 +0200
+++ b/hedgewars/hwengine.pas	Mon Apr 25 06:38:59 2011 +0200
@@ -30,7 +30,7 @@
 {$ENDIF}
 
 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uKeys, uSound,
-     uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uRandom, uLandTexture, uCollisions, uMobile,
+     uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uRandom, uLandTexture, uCollisions,
      sysutils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted;
 
 {$IFDEF HWLIBRARY}
@@ -256,10 +256,9 @@
     ShowMainWindow();
 {$ENDIF}
 
-    AddProgress();
-
     ControllerInit(); // has to happen before InitKbdKeyTable to map keys
     InitKbdKeyTable();
+    AddProgress();
 
     LoadLocale(Pathz[ptLocale] + '/en.txt');  // Do an initial load with english
     if (Length(cLocaleFName) > 6) then cLocale := Copy(cLocaleFName,1,5)
@@ -281,10 +280,7 @@
         SendIPCAndWaitReply('C');        // ask for game config
         end
     else
-        begin
         LoadRecordFromFile(recordFileName);
-        uMobile.SaveBeganSynching();
-        end;
 
     ScriptOnGameInit;
 
--- a/hedgewars/uGame.pas	Mon Apr 25 02:25:25 2011 +0200
+++ b/hedgewars/uGame.pas	Mon Apr 25 06:38:59 2011 +0200
@@ -76,7 +76,7 @@
                         GameType:= gmtLocal;
                         AddVisualGear(0, 0, vgtTeamHealthSorter);
                         {$IFDEF IPHONEOS}InitIPC;{$ENDIF}
-                        uMobile.SaveFinishedSynching();
+                        uMobile.SaveFinished();
                         end;
                end
           else ProcessGears
--- a/hedgewars/uIO.pas	Mon Apr 25 02:25:25 2011 +0200
+++ b/hedgewars/uIO.pas	Mon Apr 25 06:38:59 2011 +0200
@@ -41,7 +41,7 @@
 procedure doPut(putX, putY: LongInt; fromAI: boolean);
 
 implementation
-uses uConsole, uConsts, uVariables, uCommands, uUtils, uDebug;
+uses uConsole, uConsts, uVariables, uCommands, uUtils, uDebug, uMobile;
 
 type PCmd = ^TCmd;
      TCmd = packed record
@@ -176,6 +176,8 @@
     s: shortstring absolute buf;
 begin
 
+uMobile.SaveBeganSynching();
+
 // set RDNLY on file open
 filemode:= 0;
 {$I-}
--- a/hedgewars/uMobile.pas	Mon Apr 25 02:25:25 2011 +0200
+++ b/hedgewars/uMobile.pas	Mon Apr 25 06:38:59 2011 +0200
@@ -24,10 +24,10 @@
 {$IFDEF IPHONEOS}
 (*  iOS calls written in ObjcExports.m  *)
 procedure clearView; cdecl; external;
-procedure startSpinning; cdecl; external;
-procedure stopSpinning; cdecl; external;
-procedure replayBegan; cdecl; external;
-procedure replayFinished; cdecl; external;
+procedure startSpinningProgress; cdecl; external;
+procedure stopSpinningProgress; cdecl; external;
+procedure saveBeganSynching; cdecl; external;
+procedure saveFinishedSynching; cdecl; external;
 procedure setGameRunning(arg: boolean); cdecl; external;
 procedure updateVisualsNewTurn; cdecl; external;
 function  isApplePhone: Boolean; cdecl; external;
@@ -36,12 +36,12 @@
 function  isPhone: Boolean; inline;
 procedure performRumble; inline;
 
-procedure AddProgress; inline;
-procedure FinishProgress; inline;
+procedure GameLoading; inline;
+procedure GameLoaded; inline;
 procedure AmmoUpdate; // don't inline
 procedure NewTurnBeginning; inline;
-procedure SaveBeganSynching; inline;
-procedure SaveFinishedSynching; inline;
+procedure SaveBegan; inline;
+procedure SaveFinished; inline;
 
 implementation
 uses uVariables;
@@ -62,17 +62,17 @@
 {$ENDIF}
 end;
 
-procedure AddProgress; inline;
+procedure GameLoading; inline;
 begin
 {$IFDEF IPHONEOS}
-    startSpinning();
+    startSpinningProgress();
 {$ENDIF}
 end;
 
-procedure FinishProgress; inline;
+procedure GameLoaded; inline;
 begin
 {$IFDEF IPHONEOS}
-    stopSpinning();
+    stopSpinningProgress();
 {$ENDIF}
 end;
 
@@ -95,17 +95,17 @@
     AmmoUpdate();
 end;
 
-procedure SaveBeganSynching; inline;
+procedure SaveBegan; inline;
 begin
 {$IFDEF IPHONEOS}
-    replayBegan();
+    saveBeganSynching();
 {$ENDIF}
 end;
 
-procedure SaveFinishedSynching; inline;
+procedure SaveFinished; inline;
 begin
 {$IFDEF IPHONEOS}
-    replayFinished();
+    saveFinishedSynching();
 {$ENDIF}
 end;
 
--- a/hedgewars/uStore.pas	Mon Apr 25 02:25:25 2011 +0200
+++ b/hedgewars/uStore.pas	Mon Apr 25 06:38:59 2011 +0200
@@ -672,7 +672,7 @@
         numsquares:= texsurf^.h div squaresize;
         SDL_FreeSurface(texsurf);
 
-        uMobile.AddProgress();
+        uMobile.GameLoading();
     end;
 
     TryDo(ProgrTex <> nil, 'Error - Progress Texure is nil!', true);
@@ -700,7 +700,7 @@
 begin
     WriteLnToConsole('Freeing progress surface... ');
     FreeTexture(ProgrTex);
-    uMobile.FinishProgress();
+    uMobile.GameLoaded();
 end;
 
 function RenderHelpWindow(caption, subcaption, description, extra: ansistring; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture;
--- a/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m	Mon Apr 25 02:25:25 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m	Mon Apr 25 06:38:59 2011 +0200
@@ -396,6 +396,7 @@
             case 'q':
                 // game ended, can remove the savefile and the trailing overlay (when dualhead)
                 [self gameHasEndedWithStats:statsArray];
+                [statsArray release];
                 break;
             case 'Q':
                 // game exited but not completed, nothing to do (just don't save the message)
--- a/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m	Mon Apr 25 02:25:25 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m	Mon Apr 25 06:38:59 2011 +0200
@@ -24,6 +24,7 @@
 #import "EngineProtocolNetwork.h"
 #import "OverlayViewController.h"
 #import "StatsPageViewController.h"
+#import "ObjcExports.h"
 
 @implementation GameInterfaceBridge
 @synthesize parentController, systemSettings, savePath, overlayController, engineProtocol, ipcPort, gameType;
@@ -60,6 +61,7 @@
 
     [self.overlayController setUseClassicMenu:[[dict objectForKey:@"menu"] boolValue]];
     [self.overlayController setInitialOrientation:[[dict objectForKey:@"orientation"] intValue]];
+    objcExportsInit(self.overlayController);
 
     UIWindow *gameWindow = (IS_DUALHEAD() ? [HedgewarsAppDelegate sharedAppDelegate].uiwindow : [[UIApplication sharedApplication] keyWindow]);
     [gameWindow addSubview:self.overlayController.view];
@@ -156,7 +158,7 @@
                                     [NSNumber numberWithInt:self.parentController.interfaceOrientation],@"orientation",
                                     [self.systemSettings objectForKey:@"menu"],@"menu",
                                     nil];
-    [self performSelector:@selector(displayOverlayLater:) withObject:overlayOptions afterDelay:3];
+    [self performSelector:@selector(displayOverlayLater:) withObject:overlayOptions afterDelay:0.1];
     [overlayOptions release];
 
     // SYSTEMS ARE GO!!
--- a/project_files/HedgewarsMobile/Classes/ObjcExports.h	Mon Apr 25 02:25:25 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/ObjcExports.h	Mon Apr 25 06:38:59 2011 +0200
@@ -19,18 +19,16 @@
  */
 
 
-#define ANIMATION_DURATION 0.25
-#define CONFIRMATION_TAG 5959
-#define GRENADE_TAG 9595
-#define REPLAYBLACKVIEW_TAG 9955
-#define ACTIVITYINDICATOR_TAG 987654
+@class OverlayViewController;
 
-@class AmmoMenuViewController;
-
-void objcExportsInit();
+void objcExportsInit(OverlayViewController *instance);
 BOOL isGameRunning();
 void setGameRunning(BOOL value);
 NSInteger cachedGrenadeTime();
 void clearView();
 void setGrenadeTime(NSInteger value);
-void setAmmoMenuInstance(AmmoMenuViewController *instance);
+
+void startSpinningProgress();
+void stopSpinningProgress();
+void saveBeganSynching();
+void saveFinishedSynching();
--- a/project_files/HedgewarsMobile/Classes/ObjcExports.m	Mon Apr 25 02:25:25 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/ObjcExports.m	Mon Apr 25 06:38:59 2011 +0200
@@ -20,8 +20,8 @@
 
 
 #import "ObjcExports.h"
+#import "OverlayViewController.h"
 #import "AmmoMenuViewController.h"
-#import "AudioToolbox/AudioToolbox.h"
 
 #pragma mark -
 #pragma mark internal variables
@@ -32,13 +32,14 @@
 // cache the grenade time
 NSInteger grenadeTime;
 // the reference to the newMenu instance
-AmmoMenuViewController *amvc_instance;
+OverlayViewController *overlay_instance;
 // the audiosession must be initialized before using properties
 BOOL gAudioSessionInited = NO;
 
 #pragma mark -
 #pragma mark functions called like oop
-void objcExportsInit() {
+void objcExportsInit(OverlayViewController* instance) {
+    overlay_instance = instance;
     gameRunning = NO;
     savedGame = NO;
     grenadeTime = 2;
@@ -60,36 +61,24 @@
     grenadeTime = value;
 }
 
-void inline setAmmoMenuInstance(AmmoMenuViewController *instance) {
-    amvc_instance = instance;
-}
-
 #pragma mark -
 #pragma mark functions called by pascal code
-void startSpinning() {
+void startSpinningProgress() {
     gameRunning = NO;
-    UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
-    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
-    indicator.tag = ACTIVITYINDICATOR_TAG;
-    int offset;
-    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
-        offset = -120;
-    else
-        offset = 120;
-    if (IS_DUALHEAD())
-        indicator.center = CGPointMake(theWindow.frame.size.width/2, theWindow.frame.size.height/2 + offset);
-    else
-        indicator.center = CGPointMake(theWindow.frame.size.width/2 + offset, theWindow.frame.size.height/2);
-    indicator.hidesWhenStopped = YES;
-    [indicator startAnimating];
-    [theWindow addSubview:indicator];
-    [indicator release];
+    overlay_instance.lowerIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
+
+    CGPoint center = overlay_instance.view.center;
+    overlay_instance.lowerIndicator.center = (IS_DUALHEAD() ? CGPointMake(center.y, center.x)
+                                              : CGPointMake(center.y, center.x * 5/3));
+
+    [overlay_instance.lowerIndicator startAnimating];
+    [overlay_instance.view addSubview:overlay_instance.lowerIndicator];
+    [overlay_instance.lowerIndicator release];
 }
 
-void stopSpinning() {
-    UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
-    UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[theWindow viewWithTag:ACTIVITYINDICATOR_TAG];
-    [indicator stopAnimating];
+void stopSpinningProgress() {
+    [overlay_instance.lowerIndicator stopAnimating];
+    [overlay_instance.lowerIndicator removeFromSuperview];
     HW_zoomSet(1.7);
     if (savedGame == NO)
         gameRunning = YES;
@@ -115,49 +104,45 @@
     grenadeTime = 2;
 }
 
-void replayBegan() {
-    UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
-    UIView *blackView = [[UIView alloc] initWithFrame:theWindow.frame];
-    blackView.backgroundColor = [UIColor blackColor];
-    blackView.alpha = 0.6;
-    blackView.tag = REPLAYBLACKVIEW_TAG;
-    blackView.exclusiveTouch = NO;
-    blackView.multipleTouchEnabled = NO;
-    blackView.userInteractionEnabled = NO;
+void saveBeganSynching() {
+    overlay_instance.view.backgroundColor = [UIColor blackColor];
+    overlay_instance.view.alpha = 0.75;
+    overlay_instance.view.userInteractionEnabled = NO;
+
+    overlay_instance.savesIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
 
-    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
-    indicator.center = theWindow.center;
-    [indicator startAnimating];
-    [blackView addSubview:indicator];
-    [indicator release];
-    [theWindow addSubview:blackView];
-    [blackView release];
+    CGPoint center = overlay_instance.view.center;
+    overlay_instance.savesIndicator.center = CGPointMake(center.y, center.x);
+    overlay_instance.savesIndicator.hidesWhenStopped = YES;
+
+    [overlay_instance.savesIndicator startAnimating];
+    [overlay_instance.view addSubview:overlay_instance.savesIndicator];
+    [overlay_instance.savesIndicator release];
 
     savedGame = YES;
-    stopSpinning();
+    stopSpinningProgress();
 }
 
-void replayFinished() {
-    UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
-    UIView *blackView = (UIView *)[theWindow viewWithTag:REPLAYBLACKVIEW_TAG];
+void saveFinishedSynching() {
+    [UIView beginAnimations:@"fading from save synch" context:NULL];
+    [UIView setAnimationDuration:1];
+    overlay_instance.view.backgroundColor = [UIColor clearColor];
+    overlay_instance.view.alpha = 1;
+    overlay_instance.view.userInteractionEnabled = YES;
+    [UIView commitAnimations];
 
-    [UIView beginAnimations:@"removing black" context:NULL];
-    [UIView setAnimationDuration:1];
-    blackView.alpha = 0;
-    [UIView commitAnimations];
-    [theWindow performSelector:@selector(removeFromSuperview) withObject:blackView afterDelay:1];
+    [overlay_instance.savesIndicator stopAnimating];
+    [overlay_instance.savesIndicator performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
 
     gameRunning = YES;
     savedGame = NO;
 }
 
 void updateVisualsNewTurn(void) {
-    [amvc_instance updateAmmoVisuals];
+    [overlay_instance.amvc updateAmmoVisuals];
 }
 
 // dummy function to prevent linkage fail
 int SDL_main(int argc, char **argv) {
     return 0;
 }
-
-
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.h	Mon Apr 25 02:25:25 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.h	Mon Apr 25 06:38:59 2011 +0200
@@ -53,12 +53,18 @@
     
     // dual head support
     NSInteger initialScreenCount;
+
+    // spinning icons low
+    UIActivityIndicatorView *lowerIndicator;
+    UIActivityIndicatorView *savesIndicator;
 }
 
 @property (nonatomic,retain) id popoverController;
 @property (nonatomic,retain) InGameMenuViewController *popupMenu;
 @property (nonatomic,retain) HelpPageViewController *helpPage;
 @property (nonatomic,retain) AmmoMenuViewController *amvc;
+@property (nonatomic,retain) UIActivityIndicatorView *lowerIndicator;
+@property (nonatomic,retain) UIActivityIndicatorView *savesIndicator;
 @property (assign) BOOL useClassicMenu;
 @property (assign) NSInteger initialOrientation;
 @property (assign) NSInteger initialScreenCount;
@@ -77,4 +83,8 @@
 -(void) activateOverlay;
 -(void) removeOverlay;
 
+#define ANIMATION_DURATION 0.25
+#define CONFIRMATION_TAG 5959
+#define GRENADE_TAG 9595
+
 @end
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Mon Apr 25 02:25:25 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Mon Apr 25 06:38:59 2011 +0200
@@ -38,8 +38,9 @@
 #define removeInputWidget() [[self.view viewWithTag:CONFIRMATION_TAG] removeFromSuperview]; \
                             [[self.view viewWithTag:GRENADE_TAG] removeFromSuperview];
 
+
 @implementation OverlayViewController
-@synthesize popoverController, popupMenu, helpPage, amvc, useClassicMenu, initialScreenCount, initialOrientation;
+@synthesize popoverController, popupMenu, helpPage, amvc, useClassicMenu, initialScreenCount, initialOrientation, lowerIndicator, savesIndicator;
 
 #pragma mark -
 #pragma mark rotation
@@ -90,11 +91,12 @@
 #pragma mark View Management
 -(id) initWithCoder:(NSCoder *)aDecoder {
     if ((self = [super initWithCoder:aDecoder])) {
-        objcExportsInit();
         isAttacking = NO;
         isPopoverVisible = NO;
         initialScreenCount = (IS_DUALHEAD() ? 2 : 1);
         initialOrientation = 0;
+        lowerIndicator = nil;
+        savesIndicator = nil;
     }
     return self;
 }
@@ -173,6 +175,8 @@
     [self dismissPopover];
     self.popoverController = nil;
     self.amvc = nil;
+    self.lowerIndicator = nil;
+    self.savesIndicator = nil;
     MSG_DIDUNLOAD();
     [super viewDidUnload];
 }
@@ -184,6 +188,10 @@
         self.helpPage = nil;
     if (self.amvc.view.superview == nil)
         self.amvc = nil;
+    if (self.lowerIndicator.superview == nil)
+        self.lowerIndicator = nil;
+    if (self.savesIndicator.superview == nil)
+        self.savesIndicator = nil;
     if (IS_IPAD())
         if (((UIPopoverController *)self.popoverController).contentViewController.view.superview == nil)
             self.popoverController = nil;
@@ -197,6 +205,8 @@
     [helpPage release];
     [popoverController release];
     [amvc release];
+    [lowerIndicator release];
+    [savesIndicator release];
     // dimTimer is autoreleased
     [super dealloc];
 }
@@ -343,7 +353,7 @@
             if (IS_DUALHEAD() || self.useClassicMenu == NO) {
                 if (self.amvc == nil)
                     self.amvc = [[AmmoMenuViewController alloc] init];
-                setAmmoMenuInstance(amvc);
+
                 if (self.amvc.isVisible) {
                     doDim();
                     [self.amvc disappear];
@@ -353,10 +363,8 @@
                         [self.amvc appearInView:self.view];
                     }
                 }
-            } else {
-                setAmmoMenuInstance(nil);
+            } else
                 HW_ammoMenu();
-            }
             break;
         default:
             DLog(@"Nope");
@@ -506,13 +514,13 @@
 }
 
 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
-    CGRect screen = [[UIScreen mainScreen] bounds];
     NSSet *allTouches = [event allTouches];
-    CGPoint currentPosition = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
-
     if ([self shouldIgnoreTouch:allTouches] == YES)
         return;
 
+    CGRect screen = [[UIScreen mainScreen] bounds];
+    CGPoint currentPosition = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
+
     switch ([allTouches count]) {
         case 1:
             // if we're in the menu we just click in the point
@@ -597,14 +605,14 @@
 }
 
 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
+    NSSet *allTouches = [event allTouches];
+    if ([self shouldIgnoreTouch:allTouches] == YES)
+        return;
+
     CGRect screen = [[UIScreen mainScreen] bounds];
-    NSSet *allTouches = [event allTouches];
     int x, y, dx, dy;
     UITouch *touch, *first, *second;
 
-    if ([self shouldIgnoreTouch:allTouches] == YES)
-        return;
-
     switch ([allTouches count]) {
         case 1:
             touch = [[allTouches allObjects] objectAtIndex:0];