use a button instead of label and simplify a lot touch interaction
authorkoda
Mon, 12 Jul 2010 01:42:43 +0200
changeset 3639 b5cdbcc89b61
parent 3638 33ee433749ba
child 3640 54676a34b9ad
use a button instead of label and simplify a lot touch interaction
hedgewars/CCHandlers.inc
project_files/HedgewarsMobile/Classes/OverlayViewController.h
project_files/HedgewarsMobile/Classes/OverlayViewController.m
project_files/HedgewarsMobile/Classes/PascalImports.h
project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj
--- a/hedgewars/CCHandlers.inc	Sun Jul 11 18:39:01 2010 +0200
+++ b/hedgewars/CCHandlers.inc	Mon Jul 12 01:42:43 2010 +0200
@@ -439,13 +439,22 @@
     Message:= Message or gm_Switch
 end;
 
+{$IFDEF IPHONEOS}
+procedure clearView; cdecl; external;
+{$ENDIF}
+
 procedure chNextTurn(var s: shortstring);
 begin
-s:= s; // avoid compiler hint
-TryDo(AllInactive, '/nextturn called when not all gears are inactive', true);
+    s:= s; // avoid compiler hint
+    TryDo(AllInactive, '/nextturn called when not all gears are inactive', true);
 
-if not CurrentTeam^.ExtDriven then SendIPC('N');
-{$IFDEF DEBUGFILE}AddFileLog('Doing SwitchHedgehog: time '+inttostr(GameTicks));{$ENDIF}
+    if not CurrentTeam^.ExtDriven then SendIPC('N');
+{$IFDEF DEBUGFILE}
+    AddFileLog('Doing SwitchHedgehog: time '+inttostr(GameTicks));
+{$ENDIF}
+{$IFDEF IPHONEOS}
+    clearView();
+{$ENDIF}
 end;
 
 procedure chSay(var s: shortstring);
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.h	Sun Jul 11 18:39:01 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.h	Mon Jul 12 01:42:43 2010 +0200
@@ -9,7 +9,8 @@
 #import <UIKit/UIKit.h>
 
 #define CONFIRMATION_TAG 5959
-#define removeConfirmationInput() [[self.view viewWithTag:CONFIRMATION_TAG] removeFromSuperview]
+#define ANIMATION_DURATION 0.25
+#define removeConfirmationInput()   [[self.view viewWithTag:CONFIRMATION_TAG] removeFromSuperview]; 
 
 @class PopoverMenuViewController;
 
@@ -22,10 +23,7 @@
     PopoverMenuViewController *popupMenu;
     BOOL isPopoverVisible;
     
-    // touch section
-    BOOL isSingleClick;
     CGFloat initialDistanceForPinching;
-    CGPoint pointWhereToClick;
 }
 
 @property (nonatomic,retain) id popoverController;
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Sun Jul 11 18:39:01 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Mon Jul 12 01:42:43 2010 +0200
@@ -340,7 +340,7 @@
 #pragma mark Custom touch event handling
 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     NSSet *allTouches = [event allTouches];
-    UITouch *touch, *first, *second;
+    UITouch *first, *second;
     
     if (isPopoverVisible) {
         [self dismissPopover];
@@ -353,18 +353,9 @@
     */
     
     switch ([allTouches count]) {
-        case 1:
-            touch = [[allTouches allObjects] objectAtIndex:0];
-            CGPoint currentPosition = [touch locationInView:self.view];
-            
-            //DLog(@"X:%d Y:%d", HWX(currentPosition.x), HWY(currentPosition.y));
-            // this is a single touch/tap
-            isSingleClick = YES;
-            // save were the click event will take place
-            pointWhereToClick = currentPosition;
-            
+        case 1:            
             removeConfirmationInput();
-            if (2 == [touch tapCount])
+            if (2 == [[[allTouches allObjects] objectAtIndex:0] tapCount])
                 HW_zoomReset();
             break;
         case 2:                
@@ -383,49 +374,51 @@
 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
     CGRect screen = [[UIScreen mainScreen] bounds];
     NSSet *allTouches = [event allTouches];
-    UITouch *touch;
+    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
             if (HW_isAmmoOpen()) {
-                // if we're in the menu we just click in the point
-                HW_setCursor(HWX(pointWhereToClick.x), HWY(pointWhereToClick.y));
+                HW_setCursor(HWX(currentPosition.x), HWY(currentPosition.y));
+                // this click doesn't need any wrapping because the ammoMenu already limits the cursor
                 HW_click();
             } else 
-                if (isSingleClick) {
-                    // if they tapped in the screen we trick the system so that camera doesn't move
-                    HW_saveCursor(FALSE);
-                    HW_setCursor(HWX(pointWhereToClick.x), HWY(pointWhereToClick.y));
-                    HW_click();
-                    HW_saveCursor(TRUE);
+                // if weapon requires a further click, ask for tapping again
+                if (HW_isWeaponRequiringClick()) {
+                    // here don't have to wrap thanks to isCursorVisible magic
+                    HW_setCursor(HWX(currentPosition.x), HWY(currentPosition.y));
                     
-                    // and remove the label (if any)
-                    removeConfirmationInput();
-                } else {
-                    // if weapon requires a further click, ask for tapping again
-                    if (HW_isWeaponRequiringClick()) {
-                        touch = [[allTouches allObjects] objectAtIndex:0];
-                        CGPoint currentPosition = [touch locationInView:self.view];
-                        UILabel *tapAgain = [[UILabel alloc] initWithFrame:CGRectMake(currentPosition.x-100, currentPosition.y + 10, 200, 25)];
-                        tapAgain.text = NSLocalizedString(@"Tap again to confirm",@"from the overlay");
-                        tapAgain.backgroundColor = [UIColor clearColor];
-                        tapAgain.tag = CONFIRMATION_TAG;
-                        tapAgain.textColor = [UIColor blueColor];
-                        tapAgain.textAlignment = UITextAlignmentCenter;
-                        tapAgain.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
-                        [self.view addSubview:tapAgain];
-                        [tapAgain release];
-                    }
+                    // draw the button at the last touched point (which is the current position)
+                    UIButton *tapAgain = [UIButton buttonWithType:UIButtonTypeRoundedRect];
+                    tapAgain.frame = CGRectMake(currentPosition.x - 90, currentPosition.y + 15, 180, 30);
+                    tapAgain.tag = CONFIRMATION_TAG;
+                    tapAgain.alpha = 0;
+                    [tapAgain addTarget:self action:@selector(sendHWClick) forControlEvents:UIControlEventTouchUpInside];
+                    [tapAgain setTitle:NSLocalizedString(@"Tap again to confirm",@"from the overlay") forState:UIControlStateNormal];
+                    [self.view addSubview:tapAgain];
+                    
+                    // animation ftw!
+                    [UIView beginAnimations:@"inserting button" context:NULL]; 
+                    [UIView setAnimationDuration:ANIMATION_DURATION];
+                    [self.view viewWithTag:CONFIRMATION_TAG].alpha = 1;
+                    [UIView commitAnimations];
                 }
             break;
         case 2:
             HW_allKeysUp();
             break;
+        default:
+            DLog(@"too many touches");
+            break;
     }
     
-    pointWhereToClick = CGPointZero;
     initialDistanceForPinching = 0;
-    isSingleClick = NO;
+}
+
+-(void) sendHWClick {
+    HW_click();
+    removeConfirmationInput();
 }
 
 -(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
@@ -442,11 +435,9 @@
         case 1:
             touch = [[allTouches allObjects] objectAtIndex:0];
             CGPoint currentPosition = [touch locationInView:self.view];
-            isSingleClick = NO;
+
             if (HW_isAmmoOpen()) {
-                // saves the point on which to select the ammo
-                pointWhereToClick = currentPosition;
-                // moves the cursor over
+                // moves the cursor around
                 HW_setCursor(HWX(currentPosition.x), HWY(currentPosition.y));
             } else {
                 DLog(@"x: %f y: %f -> X:%d Y:%d", currentPosition.x, currentPosition.y, HWX(currentPosition.x), HWY(currentPosition.y));
@@ -477,6 +468,7 @@
     }
 }
 
+
 // called from AddProgress and FinishProgress (respectively)
 void startSpinning() {
     isGameRunning = NO;
@@ -496,4 +488,14 @@
     isGameRunning = YES;
 }
 
+void clearView() {
+    UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
+    UIButton *theButton = (UIButton *)[theWindow viewWithTag:CONFIRMATION_TAG];
+    [UIView beginAnimations:@"remove button" context:NULL];
+    [UIView setAnimationDuration:ANIMATION_DURATION];
+    theButton.alpha = 0;
+    [UIView commitAnimations];
+    [theWindow performSelector:@selector(removeFromSuperview) withObject:theButton afterDelay:0.3];
+}
+
 @end
--- a/project_files/HedgewarsMobile/Classes/PascalImports.h	Sun Jul 11 18:39:01 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/PascalImports.h	Mon Jul 12 01:42:43 2010 +0200
@@ -25,6 +25,7 @@
     void HW_versionInfo(short int*, char**);
 
     void HW_click(void);
+    
     void HW_zoomIn(void);
     void HW_zoomOut(void);
     void HW_zoomReset(void);
--- a/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Sun Jul 11 18:39:01 2010 +0200
+++ b/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Mon Jul 12 01:42:43 2010 +0200
@@ -94,7 +94,6 @@
 		61798829114AA34C00BA94A9 /* uGame.pas in Sources */ = {isa = PBXBuildFile; fileRef = 617987FB114AA34C00BA94A9 /* uGame.pas */; };
 		6179882A114AA34C00BA94A9 /* uGears.pas in Sources */ = {isa = PBXBuildFile; fileRef = 617987FC114AA34C00BA94A9 /* uGears.pas */; };
 		6179882B114AA34C00BA94A9 /* uIO.pas in Sources */ = {isa = PBXBuildFile; fileRef = 617987FD114AA34C00BA94A9 /* uIO.pas */; };
-		6179882C114AA34C00BA94A9 /* uKeys.pas in Sources */ = {isa = PBXBuildFile; fileRef = 617987FE114AA34C00BA94A9 /* uKeys.pas */; };
 		6179882D114AA34C00BA94A9 /* uLand.pas in Sources */ = {isa = PBXBuildFile; fileRef = 617987FF114AA34C00BA94A9 /* uLand.pas */; };
 		6179882E114AA34C00BA94A9 /* uLandGraphics.pas in Sources */ = {isa = PBXBuildFile; fileRef = 61798800114AA34C00BA94A9 /* uLandGraphics.pas */; };
 		6179882F114AA34C00BA94A9 /* uLandObjects.pas in Sources */ = {isa = PBXBuildFile; fileRef = 61798801114AA34C00BA94A9 /* uLandObjects.pas */; };
@@ -122,6 +121,7 @@
 		619C09EA11E8B8D600F1DF16 /* title_small.png in Resources */ = {isa = PBXBuildFile; fileRef = 619C09E911E8B8D600F1DF16 /* title_small.png */; };
 		61A1188511683A8C00359010 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 61A117FE1168322700359010 /* CoreGraphics.framework */; };
 		61A118D311683CD100359010 /* HedgewarsTitle.png in Resources */ = {isa = PBXBuildFile; fileRef = 611FD9CB1155A28C00C2203D /* HedgewarsTitle.png */; };
+		61B3D71C11EA6F2700EC7420 /* uKeys.pas in Sources */ = {isa = PBXBuildFile; fileRef = 617987FE114AA34C00BA94A9 /* uKeys.pas */; };
 		61C3255B1179A384001E70B1 /* OpenAL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 61C3255A1179A384001E70B1 /* OpenAL.framework */; };
 		61C325901179A732001E70B1 /* OpenAL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 61C3255A1179A384001E70B1 /* OpenAL.framework */; };
 		61C325A31179A7AD001E70B1 /* libopenalbridge.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 61C3251D1179A300001E70B1 /* libopenalbridge.a */; };
@@ -1131,7 +1131,6 @@
 				61798829114AA34C00BA94A9 /* uGame.pas in Sources */,
 				6179882A114AA34C00BA94A9 /* uGears.pas in Sources */,
 				6179882B114AA34C00BA94A9 /* uIO.pas in Sources */,
-				6179882C114AA34C00BA94A9 /* uKeys.pas in Sources */,
 				6179882D114AA34C00BA94A9 /* uLand.pas in Sources */,
 				6179882E114AA34C00BA94A9 /* uLandGraphics.pas in Sources */,
 				6179882F114AA34C00BA94A9 /* uLandObjects.pas in Sources */,
@@ -1179,6 +1178,7 @@
 				6163EE7E11CC2600001C0453 /* SingleWeaponViewController.m in Sources */,
 				61E1F4F811D004240016A5AA /* adler32.pas in Sources */,
 				61F904D711DF7DA30068B24D /* WeaponCellView.m in Sources */,
+				61B3D71C11EA6F2700EC7420 /* uKeys.pas in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};