aiming fixed, inverted cursor on ammo menu, added equal and isZero function to uFloat, changed the way ammo menu opens, you must now click on the hog rather than anywhere on the screen hedgeroid
authorXeli
Thu, 18 Aug 2011 22:37:23 +0200
branchhedgeroid
changeset 5599 2e4b90f33a83
parent 5597 14b3cdb23c2c
child 5601 92a7336043d6
aiming fixed, inverted cursor on ammo menu, added equal and isZero function to uFloat, changed the way ammo menu opens, you must now click on the hog rather than anywhere on the screen
hedgewars/uFloat.pas
hedgewars/uGame.pas
hedgewars/uTouch.pas
--- a/hedgewars/uFloat.pas	Thu Aug 18 15:26:41 2011 +0200
+++ b/hedgewars/uFloat.pas	Thu Aug 18 22:37:23 2011 +0200
@@ -62,6 +62,7 @@
 
 // The implemented operators
 
+operator = (const z1, z2: hwFloat) z:boolean; inline;
 operator + (const z1, z2: hwFloat) z : hwFloat; inline;
 operator - (const z1, z2: hwFloat) z : hwFloat; inline;
 operator - (const z1: hwFloat) z : hwFloat; inline;
@@ -88,7 +89,7 @@
 function AngleCos(const Angle: Longword): hwFloat;
 function SignAs(const num, signum: hwFloat): hwFloat; inline; // Returns an hwFloat with the value of parameter num and the sign of signum.
 function hwSign(r: hwFloat): LongInt; inline; // Returns an integer with value 1 and sign of parameter r.
-
+function isZero(const z: hwFloat): boolean; inline;
 {$IFDEF FPC}
 {$J-}
 {$ENDIF}
@@ -158,10 +159,13 @@
              _40: hwFloat = (isNegative: false; QWordValue:  4294967296 * 40);
              _50: hwFloat = (isNegative: false; QWordValue:  4294967296 * 50);
              _70: hwFloat = (isNegative: false; QWordValue:  4294967296 * 70);
+             _90: hwFloat = (isNegative: false; QWordValue:  4294967296 * 90);
             _128: hwFloat = (isNegative: false; QWordValue:  4294967296 * 128);
+            _180: hwFloat = (isNegative: false; QWordValue:  4294967296 * 180);
             _250: hwFloat = (isNegative: false; QWordValue:  4294967296 * 250);
             _256: hwFloat = (isNegative: false; QWordValue:  4294967296 * 256);
             _300: hwFloat = (isNegative: false; QWordValue:  4294967296 * 300);
+            _360: hwFloat = (isNegative: false; QWordValue:  4294967296 * 360);
             _450: hwFloat = (isNegative: false; QWordValue:  4294967296 * 450);
            _1000: hwFloat = (isNegative: false; QWordValue:  4294967296 * 1000);
            _1024: hwFloat = (isNegative: false; QWordValue:  4294967296 * 1024);
@@ -197,6 +201,14 @@
 if i.isNegative then hwFloat2Float:= -hwFloat2Float;
 end;
 
+operator = (const z1, z2: hwFloat) z:boolean; inline;
+begin
+    z:= true;
+    z:= z and (z1.isNegative = z2.isNegative);
+    z:= z and (z1.QWordValue = z2.QWordValue);
+end;
+
+
 operator + (const z1, z2: hwFloat) z : hwFloat;
 begin
 if z1.isNegative = z2.isNegative then
@@ -403,6 +415,10 @@
                 else AngleCos.QWordValue:= SinTable[Angle - 1024]
 end;
 
+function isZero(const z: hwFloat): boolean; inline; 
+begin
+    isZero := z.QWordValue = 0;
+end;
 {$ENDIF}
 
 end.
--- a/hedgewars/uGame.pas	Thu Aug 18 15:26:41 2011 +0200
+++ b/hedgewars/uGame.pas	Thu Aug 18 22:37:23 2011 +0200
@@ -26,7 +26,7 @@
 ////////////////////
    implementation
 ////////////////////
-uses uKeys, uTeams, uIO, uAI, uGears, uSound, uMobile, uVisualGears, uTypes, uVariables;
+uses uKeys, uTeams, uIO, uAI, uGears, uSound, uMobile, uVisualGears, uTypes, uVariables, uTouch;
 
 procedure DoGameTick(Lag: LongInt);
 var i: LongInt;
@@ -52,6 +52,7 @@
     if not CurrentTeam^.ExtDriven then
        begin
        if CurrentHedgehog^.BotLevel <> 0 then ProcessBot;
+       ProcessTouch;
        ProcessGears
        end else
        begin
--- a/hedgewars/uTouch.pas	Thu Aug 18 15:26:41 2011 +0200
+++ b/hedgewars/uTouch.pas	Thu Aug 18 22:37:23 2011 +0200
@@ -4,10 +4,12 @@
 
 interface
 
-uses sysutils, math, uConsole, uVariables, SDLh, uTypes, uFloat, uConsts, uIO, GLUnit;
+uses sysutils, math, uConsole, uVariables, SDLh, uTypes, uFloat, uConsts, uIO, uCommands, GLUnit, uCommandHandlers;
 
 procedure initModule;
 
+
+procedure ProcessTouch;
 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId);
 procedure onTouchMotion(x,y: Longword; dx,dy: LongInt; pointerId: SDL_FingerId);
 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
@@ -16,7 +18,9 @@
 procedure deleteFinger(id: SDL_FingerId);
 procedure onTouchClick(x,y: Longword; pointerId: SDL_FingerId);
 
+procedure aim(id: SDL_FingerId);
 function isOnCurrentHog(id: SDL_FingerId): boolean;
+procedure convertToWorldCoord(var x,y: hwFloat; id: SDL_FingerId);
 function fingerHasMoved(id: SDL_FingerId): boolean;
 function calculateDelta(id1, id2: SDL_FingerId): hwFloat;
 function getSecondPointer(id: SDL_FingerId): SDL_FingerId;
@@ -37,16 +41,26 @@
 
     invertCursor : boolean;
 
+    //aiming
+    aiming, movingCrosshair: boolean; 
+    crosshairCommand: ShortString;
+    aimingPointerId: SDL_FingerId;
+    targetAngle: LongInt;    
+
 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId);
 begin
-    WriteToConsole('down'); 
     addFinger(x,y,pointerId);
     xyCoord[pointerId*2] := convertToCursor(cScreenWidth,x);
     xyCoord[pointerId*2+1] := convertToCursor(cScreenHeight,y);
+    
    
     case pointerCount of
+        1:
+            if isOnCurrentHog(pointerId) then aiming:= true;
         2:
         begin
+            aiming:= false;
+            
             pinchSize := calculateDelta(pointerId, getSecondPointer(pointerId));
             baseZoomValue := ZoomValue
         end;
@@ -64,6 +78,11 @@
     case pointerCount of
        1:
            begin
+               if aiming then 
+               begin
+                   aim(pointerId);
+                   exit
+               end;
                if invertCursor then
                begin
                    CursorPoint.X := CursorPoint.X - convertToCursor(cScreenWidth,dx);
@@ -81,8 +100,8 @@
                currentPinchDelta := calculateDelta(pointerId, secondId) - pinchSize;
                zoom := currentPinchDelta/cScreenWidth;
                ZoomValue := baseZoomValue - ((hwFloat2Float(zoom) * cMinMaxZoomLevelDelta));
-               //WriteToConsole(Format('Zoom in/out. ZoomValue = %f', [ZoomValue]));
-//              if ZoomValue > cMaxZoomLevel then ZoomValue := cMaxZoomLevel;
+               WriteToConsole(Format('Zoom in/out. ZoomValue = %f, %f', [ZoomValue, cMaxZoomLevel]));
+               if ZoomValue > cMaxZoomLevel then ZoomValue := cMaxZoomLevel;
 //               if ZoomValue < cMinZoomLevel then ZoomValue := cMinZoomLevel;
             end;
     end; //end case pointerCount of
@@ -90,6 +109,7 @@
 
 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
 begin
+    aiming:= false;
     pointerCount := pointerCount-1;
     deleteFinger(pointerId);
 end;
@@ -99,23 +119,16 @@
     if bShowAmmoMenu then 
     begin
         doPut(CursorPoint.X, CursorPoint.Y, false); 
-        invertCursor := true;
         exit
     end;
 
     if isOnCurrentHog(pointerId) then
     begin
     bShowAmmoMenu := true;
-    invertCursor := false;
     end;
     //WriteToConsole(Format('%s, %s : %d, %d', [cstr(CurrentHedgehog^.Gear^.X), cstr(CurrentHedgehog^.Gear^.Y), x-WorldDX, y-WorldDY]));
 end;
 
-function convertToCursor(scale: LongInt; xy: LongInt): LongInt;
-begin
-    convertToCursor := round(xy/32768*scale)
-end;
-
 procedure addFinger(x,y: Longword; id: SDL_FingerId);
 var 
     index, tmp: Longword;
@@ -169,13 +182,87 @@
     if ((SDL_GetTicks - timeSinceDown[id]) < clickTime) AND  not(fingerHasMoved(id)) then onTouchClick(xyCoord[id*2], xyCoord[id*2+1], id);
 end;
 
+procedure ProcessTouch;
+var
+    deltaAngle: LongInt;
+begin
+    invertCursor := not(bShowAmmoMenu);
+    if aiming then
+    begin
+        if CurrentHedgehog^.Gear <> nil then
+        begin
+            deltaAngle:= CurrentHedgehog^.Gear^.Angle - targetAngle;
+            if (deltaAngle <> 0) and not(movingCrosshair) then 
+            begin
+                ParseCommand('+' + crosshairCommand, true);
+                movingCrosshair := true;
+            end
+            else 
+                if movingCrosshair then 
+                begin
+                    ParseCommand('-' + crosshairCommand, true);
+                    movingCrosshair:= false;
+                end;
+        end;
+    end
+    else if movingCrosshair then 
+    begin
+        ParseCommand('-' + crosshairCommand, true);
+        movingCrosshair := false;
+    end;
+end;
+
+procedure aim(id: SDL_FingerId);
+var 
+    hogX, hogY, touchX, touchY, deltaX, deltaY, tmpAngle: hwFloat;
+    tmp: ShortString;
+begin
+    if CurrentHedgehog^.Gear <> nil then
+    begin
+        hogX := CurrentHedgehog^.Gear^.X;
+        hogY := CurrentHedgehog^.Gear^.Y;
+
+        convertToWorldCoord(touchX, touchY, id);
+        deltaX := hwAbs(TouchX-HogX);
+        deltaY := (TouchY-HogY);
+        
+        tmpAngle:= DeltaY / Distance(deltaX, deltaY) *_2048;
+        targetAngle:= (hwRound(tmpAngle) + 2048) div 2;
+
+        tmp := crosshairCommand;
+        if CurrentHedgehog^.Gear^.Angle - targetAngle < 0 then crosshairCommand := 'down'
+        else crosshairCommand:= 'up';
+        if movingCrosshair and (tmp <> crosshairCommand) then 
+        begin
+            ParseCommand('-' + tmp, true);
+            movingCrosshair := false;
+        end;
+
+    end; //if CurrentHedgehog^.Gear <> nil
+end;
+
+function convertToCursor(scale: LongInt; xy: LongInt): LongInt;
+begin
+    convertToCursor := round(xy/32768*scale)
+end;
+
 function isOnCurrentHog(id: SDL_FingerId): boolean;
 var
-     x,y : hwFloat;
+    x,y, fingerX, fingerY : hwFloat;
 begin
     x := CurrentHedgehog^.Gear^.X;
     y := CurrentHedgehog^.Gear^.Y;
-    isOnCurrentHog := Distance(int2hwFloat((xyCoord[id*2] -  WorldDX) - (cScreenWidth div 2))-x, int2hwFloat(xyCoord[id*2+1] - WorldDy)-y) < int2hwFloat(20);
+
+    convertToWorldCoord(fingerX, fingerY, id);
+    isOnCurrentHog := Distance(fingerX-x, fingerY-y) < _20;
+end;
+
+procedure convertToWorldCoord(var x,y: hwFloat; id: SDL_FingerId);
+begin
+//if x <> nil then 
+    x := int2hwFloat((xyCoord[id*2]-WorldDx) - (cScreenWidth div 2));
+//if y <> nil then 
+    y := int2hwFloat(xyCoord[id*2+1]-WorldDy);
 end;
 
 //Method to calculate the distance this finger has moved since the downEvent
@@ -209,7 +296,7 @@
     setLength(historicalXY, 10);    
     for index := Low(xyCoord) to High(xyCoord) do xyCoord[index] := -1;
     for index := Low(pointerIds) to High(pointerIds) do pointerIds[index] := -1;
-
+    movingCrosshair := false;
 end;
 
 begin