hedgewars/uTouch.pas
branchwebgl
changeset 9521 8054d9d775fd
parent 8330 aaefa587e277
parent 9380 563ab624522c
child 10108 c68cf030eded
--- a/hedgewars/uTouch.pas	Fri Oct 11 11:55:31 2013 +0200
+++ b/hedgewars/uTouch.pas	Fri Oct 11 17:43:13 2013 +0200
@@ -31,16 +31,17 @@
 procedure ProcessTouch;
 procedure NewTurnBeginning;
 
-procedure onTouchDown(x,y: Longword; pointerId: TSDL_FingerId);
-procedure onTouchMotion(x,y: Longword; dx,dy: LongInt; pointerId: TSDL_FingerId);
-procedure onTouchUp(x,y: Longword; pointerId: TSDL_FingerId);
+procedure onTouchDown(x, y: Single; pointerId: TSDL_FingerId);
+procedure onTouchMotion(x, y, dx, dy: Single; pointerId: TSDL_FingerId);
+procedure onTouchUp(x, y: Single; pointerId: TSDL_FingerId);
+
 function convertToCursorX(x: LongInt): LongInt;
 function convertToCursorY(y: LongInt): LongInt;
-function convertToCursorDeltaX(x: LongInt): LongInt;
-function convertToCursorDeltaY(y: LongInt): LongInt;
+
 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data;
 function updateFinger(x,y,dx,dy: Longword; id: TSDL_FingerId): PTouch_Data;
 procedure deleteFinger(id: TSDL_FingerId);
+
 procedure onTouchClick(finger: TTouch_Data);
 procedure onTouchDoubleClick(finger: TTouch_Data);
 procedure onTouchLongClick(finger: TTouch_Data);
@@ -62,7 +63,6 @@
 
 const
     clickTime = 200;
-    nilFingerId = High(TSDL_FingerId);
     baseRectSize = 96;
 
 var
@@ -88,12 +88,15 @@
     buttonsDown: Longword;
     targetting, targetted: boolean; //true when targetting an airstrike or the like
 
-procedure onTouchDown(x,y: Longword; pointerId: TSDL_FingerId);
+procedure onTouchDown(x, y: Single; pointerId: TSDL_FingerId);
 var
     finger: PTouch_Data;
+    xr, yr: LongWord;
 begin
-{$IFDEF USE_TOUCH_INTERFACE}
-finger := addFinger(x,y,pointerId);
+xr:= round(x * cScreenWidth);
+yr:= round(y * cScreenHeight);
+
+finger:= addFinger(xr, yr, pointerId);
 
 inc(buttonsDown);//inc buttonsDown, if we don't see a button down we'll dec it
 
@@ -177,15 +180,22 @@
             end;
         end;
     end;
-{$ENDIF}
 end;
 
-procedure onTouchMotion(x,y: Longword;dx,dy: LongInt; pointerId: TSDL_FingerId);
+procedure onTouchMotion(x, y, dx, dy: Single; pointerId: TSDL_FingerId);
 var
     finger, secondFinger: PTouch_Data;
-    currentPinchDelta, zoom : single;
+    currentPinchDelta, zoom : Single;
+    xr, yr, dxr, dyr: LongWord;
 begin
-finger:= updateFinger(x,y,dx,dy,pointerId);
+xr:= round(x * cScreenWidth);
+yr:= round(y * cScreenHeight);
+dxr:= round(dx * cScreenWidth);
+dyr:= round(dy * cScreenHeight);
+
+finger:= updateFinger(xr, yr, dxr, dyr, pointerId);
+if finger = nil then
+    exit;
 
 if moveCursor then
     begin
@@ -222,22 +232,26 @@
 
 end;
 
-procedure onTouchUp(x,y: Longword; pointerId: TSDL_FingerId);
+procedure onTouchUp(x,y: Single; pointerId: TSDL_FingerId);
 var
     finger: PTouch_Data;
     widget: POnScreenWidget;
+    xr, yr: LongWord;
 begin
-{$IFDEF USE_TOUCH_INTERFACE}
-x := x;
-y := y;
-finger:= updateFinger(x,y,0,0,pointerId);
+xr:= round(x * cScreenWidth);
+yr:= round(y * cScreenHeight);
+
+finger:= updateFinger(xr, yr, 0, 0, pointerId);
+if finger = nil then
+    exit;
+
 //Check for onTouchClick event
 if not(fingerHasMoved(finger^)) then
     begin
     if (RealTicks - finger^.timeSinceDown) < clickTime then
         onTouchClick(finger^)
     else
-            onTouchLongClick(finger^);
+        onTouchLongClick(finger^);
     end;
 
 if aimingCrosshair then
@@ -284,7 +298,6 @@
     AddCaption('Press the target button to mark the target', cWhiteColor, capgrpAmmoInfo);
 
 deleteFinger(pointerId);
-{$ENDIF}
 end;
 
 procedure onTouchDoubleClick(finger: TTouch_Data);
@@ -294,14 +307,11 @@
 
 procedure onTouchLongClick(finger: TTouch_Data);
 begin
-{$IFDEF USE_TOUCH_INTERFACE}
 if isOnWidget(jumpWidget, finger) then
     begin
     ParseTeamCommand('ljump');
     exit;
     end;
-
-{$ENDIF}
 end;
 
 procedure onTouchClick(finger: TTouch_Data);
@@ -330,7 +340,6 @@
     exit;
     end;
 
-{$IFDEF USE_TOUCH_INTERFACE}
 if isOnCurrentHog(finger) or isOnWidget(AMWidget, finger) then
     begin
     bShowAmmoMenu := true;
@@ -342,7 +351,6 @@
     ParseTeamCommand('hjump');
     exit;
     end;
-{$ENDIF}
 end;
 
 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data;
@@ -350,14 +358,13 @@
     xCursor, yCursor, index : LongInt;
 begin
     //Check array sizes
-    if length(fingers) < Integer(pointerCount) then
+    if length(fingers) < pointerCount then
     begin
         setLength(fingers, length(fingers)*2);
         for index := length(fingers) div 2 to length(fingers) do
             fingers[index].id := nilFingerId;
     end;
 
-
     xCursor := convertToCursorX(x);
     yCursor := convertToCursorY(y);
 
@@ -377,14 +384,21 @@
     inc(pointerCount);
 end;
 
-function updateFinger(x,y,dx,dy: Longword; id: TSDL_FingerId): PTouch_Data;
+function updateFinger(x, y, dx, dy: Longword; id: TSDL_FingerId): PTouch_Data;
+var finger : PTouch_Data;
 begin
-   updateFinger:= findFinger(id);
+    finger:= findFinger(id);
 
-   updateFinger^.x:= convertToCursorX(x);
-   updateFinger^.y:= convertToCursorY(y);
-   updateFinger^.dx:= convertToCursorDeltaX(dx);
-   updateFinger^.dy:= convertToCursorDeltaY(dy);
+    if finger <> nil then
+        begin
+        finger^.x:= convertToCursorX(x);
+        finger^.y:= convertToCursorY(y);
+        finger^.dx:= dx;
+        finger^.dy:= dy;
+        end
+    else
+        WriteLnToConsole('finger ' + inttostr(id) + ' not found');
+    updateFinger:= finger
 end;
 
 procedure deleteFinger(id: TSDL_FingerId);
@@ -409,9 +423,9 @@
                 fingers[index].historicalY := fingers[pointerCount].historicalY;
                 fingers[index].timeSinceDown := fingers[pointerCount].timeSinceDown;
 
-                fingers[pointerCount].id := nilFingerId;
+                fingers[pointerCount].id := 0;
             end
-        else fingers[index].id := nilFingerId;
+        else fingers[index].id := 0;
             break;
         end;
     end;
@@ -497,12 +511,13 @@
 var
     index: LongWord;
 begin
-    for index := 0 to High(fingers) do
+    for index:= 0 to length(fingers) do
         if fingers[index].id = id then
             begin
-            findFinger := @fingers[index];
-            break;
+            findFinger:= @fingers[index];
+            exit;
             end;
+    findFinger:= nil;
 end;
 
 procedure aim(finger: TTouch_Data);
@@ -525,64 +540,49 @@
 end;
 
 // These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system:
-// - the SDL coordinate system goes from 0 to 32768 on the x axis and 0 to 32768 on the y axis, (0,0) being top left;
+// - the SDL coordinate system is proportional to the screen and values are normalized in the onTouch* functions
 // - the CursorPoint coordinate system goes from -cScreenWidth/2 to cScreenWidth/2 on the x axis
 //   and 0 to cScreenHeight on the x axis, (-cScreenWidth, cScreenHeight) being top left.
 function convertToCursorX(x: LongInt): LongInt;
 begin
-    convertToCursorX := round((x/32768)*cScreenWidth) - (cScreenWidth shr 1);
+    convertToCursorX:= x - cScreenWidth shr 1;
 end;
 
 function convertToCursorY(y: LongInt): LongInt;
 begin
-    convertToCursorY := cScreenHeight - round((y/32768)*cScreenHeight)
-end;
-
-function convertToCursorDeltaX(x: LongInt): LongInt;
-begin
-    convertToCursorDeltaX := round(x/32768*cScreenWidth)
-end;
-
-function convertToCursorDeltaY(y: LongInt): LongInt;
-begin
-    convertToCursorDeltaY := round(y/32768*cScreenHeight)
+    convertToCursorY:= cScreenHeight - y;
 end;
 
 function isOnCrosshair(finger: TTouch_Data): boolean;
 var
-    x,y : LongInt;
+    x, y: LongInt;
 begin
-    x := 0;//avoid compiler hint
-    y := 0;
+    x:= 0;
+    y:= 0;
     convertToFingerCoord(x, y, CrosshairX, CrosshairY);
-    isOnCrosshair:= isOnRect((x-HalfRectSize), (y-HalfRectSize), RectSize, RectSize, finger);
-    printFinger(finger);
-    WriteLnToConsole(inttostr(finger.x) + '   ' + inttostr(x));
-    WriteLnToConsole(inttostr(x) + '  ' + inttostr(y) + '   ' + inttostr(round(mobileRecord.getScreenDPI() * 10)));
+    isOnCrosshair:= isOnRect(x - HalfRectSize, y - HalfRectSize, RectSize, RectSize, finger);
 end;
 
 function isOnCurrentHog(finger: TTouch_Data): boolean;
 var
-    x,y : LongInt;
+    x, y: LongInt;
 begin
-    x := 0;
-    y := 0;
-    convertToFingerCoord(x,y, hwRound(CurrentHedgehog^.Gear^.X), hwRound(CurrentHedgehog^.Gear^.Y));
-    isOnCurrentHog:= isOnRect((x-HalfRectSize), (y-HalfRectSize), RectSize, RectSize, finger);
+    x:= 0;
+    y:= 0;
+    convertToFingerCoord(x, y, hwRound(CurrentHedgehog^.Gear^.X), hwRound(CurrentHedgehog^.Gear^.Y));
+    isOnCurrentHog:= isOnRect(x - HalfRectSize, y - HalfRectSize, RectSize, RectSize, finger);
 end;
 
-procedure convertToFingerCoord(var x,y : LongInt; oldX, oldY: LongInt);
+procedure convertToFingerCoord(var x, y : LongInt; oldX, oldY: LongInt);
 begin
     x := oldX + WorldDx;
-    y := cScreenHeight - (oldY + WorldDy);
+    y := cScreenHeight - oldY - WorldDy;
 end;
 
 procedure convertToWorldCoord(var x,y: LongInt; finger: TTouch_Data);
 begin
-//if x <> nil then
-    x := finger.x-WorldDx;
-//if y <> nil then
-    y := (cScreenHeight - finger.y)-WorldDy;
+    x := finger.x - WorldDx;
+    y := cScreenHeight - finger.y - WorldDy;
 end;
 
 //Method to calculate the distance this finger has moved since the downEvent
@@ -626,7 +626,8 @@
 
 procedure printFinger(finger: TTouch_Data);
 begin
-    WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));
+    WriteLnToConsole(Format('id: %d, x: %d y: %d (rel x: %d rel y: %d), time: %d',
+                            [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));
 end;
 
 procedure initModule;
@@ -635,13 +636,14 @@
     //uRenderCoordScaleX, uRenderCoordScaleY: Longword;
 begin
     buttonsDown:= 0;
+    pointerCount:= 0;
 
     setLength(fingers, 4);
-    for index := 0 to High(fingers) do
-        fingers[index].id := nilFingerId;
+    for index := 0 to length(fingers) do
+        fingers[index].id := 0;
 
-    rectSize:= round(baseRectSize * mobileRecord.getScreenDPI());
-    halfRectSize:= rectSize shl 1;
+    rectSize:= baseRectSize;
+    halfRectSize:= baseRectSize shr 1;
 end;
 
 procedure freeModule;