hedgewars/uTouch.pas
changeset 6625 2d8c5815292f
parent 6580 6155187bf599
child 6640 813a173cd677
equal deleted inserted replaced
6624:e049b5bb0ad1 6625:2d8c5815292f
    31 type
    31 type
    32     PTouch_Finger = ^Touch_Finger;
    32     PTouch_Finger = ^Touch_Finger;
    33     Touch_Finger = record
    33     Touch_Finger = record
    34         id                       : SDL_FingerId;
    34         id                       : SDL_FingerId;
    35         x,y                      : LongInt;
    35         x,y                      : LongInt;
       
    36         dx,dy                    : LongInt;
    36         historicalX, historicalY : LongInt;
    37         historicalX, historicalY : LongInt;
    37         timeSinceDown            : Longword;
    38         timeSinceDown            : Longword;
    38         end;
    39         end;
    39 
    40 
    40 procedure initModule;
    41 procedure initModule;
    41 
    42 
    42 procedure ProcessTouch;
    43 procedure ProcessTouch;
    43 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId);
    44 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId);
    44 procedure onTouchMotion(x,y: Longword; dx,dy: LongInt; pointerId: SDL_FingerId);
    45 procedure onTouchMotion(x,y: Longword; dx,dy: LongInt; pointerId: SDL_FingerId);
    45 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
    46 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
    46 function convertToCursor(scale: LongInt; xy: LongInt): LongInt;
    47 function convertToCursorX(x: LongInt): LongInt;
       
    48 function convertToCursorY(y: LongInt): LongInt;
       
    49 function convertToCursorDeltaX(x: LongInt): LongInt;
       
    50 function convertToCursorDeltaY(y: LongInt): LongInt;
    47 function addFinger(x,y: Longword; id: SDL_FingerId): PTouch_Finger;
    51 function addFinger(x,y: Longword; id: SDL_FingerId): PTouch_Finger;
       
    52 function updateFinger(x,y,dx,dy: Longword; id: SDL_FingerId): PTouch_Finger;
    48 procedure deleteFinger(id: SDL_FingerId);
    53 procedure deleteFinger(id: SDL_FingerId);
    49 procedure onTouchClick(finger: Touch_Finger);
    54 procedure onTouchClick(finger: Touch_Finger);
    50 procedure onTouchDoubleClick(finger: Touch_Finger);
    55 procedure onTouchDoubleClick(finger: Touch_Finger);
    51 
    56 
    52 function findFinger(id: SDL_FingerId): PTouch_Finger;
    57 function findFinger(id: SDL_FingerId): PTouch_Finger;
   123             begin
   128             begin
   124                 stopFiring:= false;
   129                 stopFiring:= false;
   125                 ParseCommand('+attack', true);
   130                 ParseCommand('+attack', true);
   126                 exit;
   131                 exit;
   127             end;
   132             end;
   128             if (finger^.x < leftButtonBoundary) and (finger^.y < 390) then
   133 {            if (finger^.x < leftButtonBoundary) and (finger^.y < 390) then
   129             begin
   134             begin
   130                 ParseCommand('+left', true);
   135                 ParseCommand('+left', true);
   131                 walkingLeft := true;
   136                 walkingLeft := true;
   132                 exit;
   137                 exit;
   133             end;
   138             end;
   140             if finger^.y < topButtonBoundary then
   145             if finger^.y < topButtonBoundary then
   141             begin
   146             begin
   142                 ParseCommand('hjump', true);
   147                 ParseCommand('hjump', true);
   143                 exit;
   148                 exit;
   144             end;
   149             end;
   145             moveCursor:= true; 
   150  }           moveCursor:= true; 
   146         end;
   151         end;
   147         2:
   152         2:
   148         begin
   153         begin
   149             aiming:= false;
   154             aiming:= false;
   150             stopFiring:= true;
   155             stopFiring:= true;
   157 
   162 
   158 procedure onTouchMotion(x,y: Longword;dx,dy: LongInt; pointerId: SDL_FingerId);
   163 procedure onTouchMotion(x,y: Longword;dx,dy: LongInt; pointerId: SDL_FingerId);
   159 var
   164 var
   160     finger, secondFinger: PTouch_Finger;
   165     finger, secondFinger: PTouch_Finger;
   161     currentPinchDelta, zoom : hwFloat;
   166     currentPinchDelta, zoom : hwFloat;
   162     tmpX, tmpY: LongInt;
   167 begin
   163 begin
   168 finger:= updateFinger(x,y,dx,dy,pointerId);
   164 x := x;
       
   165 y := y;
       
   166 dx := dx;
       
   167 dy := dy;
       
   168 finger:= findFinger(pointerId);
       
   169 tmpX := convertToCursor(cScreenWidth, x);
       
   170 tmpY := convertToCursor(cScreenHeight, y);
       
   171 
   169 
   172 if moveCursor then
   170 if moveCursor then
   173     begin
   171     begin
   174         if invertCursor then
   172         if invertCursor then
   175         begin
   173         begin
   176             CursorPoint.X := CursorPoint.X + (finger^.x - tmpX);
   174             CursorPoint.X := CursorPoint.X - finger^.dx;
   177             CursorPoint.Y := CursorPoint.Y - (finger^.y - tmpY);
   175             CursorPoint.Y := CursorPoint.Y + finger^.dy;
   178         end
   176         end
   179     else
   177     else
   180         begin
   178         begin
   181             CursorPoint.X := CursorPoint.X - (finger^.x - tmpX);
   179             CursorPoint.X := CursorPoint.X + finger^.dx;
   182             CursorPoint.Y := CursorPoint.Y + (finger^.y - tmpY);
   180             CursorPoint.Y := CursorPoint.Y - finger^.dy;
   183         end;
   181         end;
   184         finger^.x := tmpX;
       
   185         finger^.y := tmpY;
       
   186         exit //todo change into switch rather than ugly ifs
   182         exit //todo change into switch rather than ugly ifs
   187     end;
   183     end;
   188     
       
   189     finger^.x := tmpX;
       
   190     finger^.y := tmpY;
       
   191     
   184     
   192     if aiming then 
   185     if aiming then 
   193     begin
   186     begin
   194         aim(finger^);
   187         aim(finger^);
   195         exit
   188         exit
   206            ZoomValue := cMinZoomLevel;
   199            ZoomValue := cMinZoomLevel;
   207     end;
   200     end;
   208 end;
   201 end;
   209 
   202 
   210 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
   203 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
       
   204 var
       
   205     finger: PTouch_Finger;
   211 begin
   206 begin
   212 x := x;
   207 x := x;
   213 y := y;
   208 y := y;
   214 aiming:= false;
   209 aiming:= false;
   215 stopFiring:= true;
   210 stopFiring:= true;
       
   211 finger:= updateFinger(x,y,0,0,pointerId);
       
   212 //Check for onTouchClick event
       
   213 if ((SDL_GetTicks - finger^.timeSinceDown) < clickTime) AND not(fingerHasMoved(finger^)) then
       
   214     onTouchClick(finger^);
       
   215 
   216 deleteFinger(pointerId);
   216 deleteFinger(pointerId);
   217 
   217 
   218 if walkingLeft then
   218 if walkingLeft then
   219     begin
   219     begin
   220         ParseCommand('-left', true);
   220         ParseCommand('-left', true);
   237 procedure onTouchClick(finger: Touch_Finger);
   237 procedure onTouchClick(finger: Touch_Finger);
   238 begin
   238 begin
   239 if (SDL_GetTicks - timeSinceClick < 300) and (DistanceI(finger.X-xTouchClick, finger.Y-yTouchClick) < _30) then
   239 if (SDL_GetTicks - timeSinceClick < 300) and (DistanceI(finger.X-xTouchClick, finger.Y-yTouchClick) < _30) then
   240     begin
   240     begin
   241     onTouchDoubleClick(finger);
   241     onTouchDoubleClick(finger);
       
   242     timeSinceClick:= -1;
   242     exit; 
   243     exit; 
   243     end
   244     end;
   244 else
   245 
   245     begin
   246 xTouchClick:= finger.x;
   246         xTouchClick := finger.x;
   247 yTouchClick:= finger.y;
   247         yTouchClick := finger.y;
   248 timeSinceClick:= SDL_GetTicks;
   248         timeSinceClick := SDL_GetTicks;
   249 
   249     end;
   250 if bShowAmmoMenu then 
   250 
   251     begin
   251     if bShowAmmoMenu then 
   252     CursorPoint.X:= finger.x;
   252     begin
   253     CursorPoint.Y:= finger.y;
   253         doPut(CursorPoint.X, CursorPoint.Y, false); 
   254     doPut(CursorPoint.X, CursorPoint.Y, false); 
   254         exit
   255     WriteToConsole(Format('%d %d', [CursorPoint.X, CursorPoint.Y]));
   255     end;
   256     exit
   256 
   257     end;
   257     if isOnCurrentHog(finger) then
   258 
   258     begin
   259 if isOnCurrentHog(finger) then
   259         bShowAmmoMenu := true;
   260     begin
   260         exit;
   261     bShowAmmoMenu := true;
   261     end;
   262     exit;
   262 
   263     end;
   263     if finger.y < topButtonBoundary then
   264 
   264     begin
   265 if finger.y < topButtonBoundary then
   265         ParseCommand('hjump', true);
   266     begin
   266         exit;
   267     ParseCommand('hjump', true);
       
   268     exit;
   267     end;
   269     end;
   268 end;
   270 end;
   269 
   271 
   270 function addFinger(x,y: Longword; id: SDL_FingerId): PTouch_Finger;
   272 function addFinger(x,y: Longword; id: SDL_FingerId): PTouch_Finger;
   271 var 
   273 var 
   278         for index := length(fingers) div 2 to length(fingers) do
   280         for index := length(fingers) div 2 to length(fingers) do
   279             fingers[index].id := nilFingerId;
   281             fingers[index].id := nilFingerId;
   280     end;
   282     end;
   281     
   283     
   282     
   284     
   283     xCursor := convertToCursor(cScreenWidth, x);
   285     xCursor := convertToCursorX(x);
   284     yCursor := convertToCursor(cScreenHeight, y);
   286     yCursor := convertToCursorY(y);
   285     
   287     
   286     //on removing fingers, all fingers are moved to the left
   288     //on removing fingers, all fingers are moved to the left
   287     //with dynamic arrays being zero based, the new position of the finger is the old pointerCount
   289     //with dynamic arrays being zero based, the new position of the finger is the old pointerCount
   288     fingers[pointerCount].id := id;
   290     fingers[pointerCount].id := id;
   289     fingers[pointerCount].historicalX := xCursor;
   291     fingers[pointerCount].historicalX := xCursor;
   290     fingers[pointerCount].historicalY := yCursor;
   292     fingers[pointerCount].historicalY := yCursor;
   291     fingers[pointerCount].x := xCursor;
   293     fingers[pointerCount].x := xCursor;
   292     fingers[pointerCount].y := yCursor;
   294     fingers[pointerCount].y := yCursor;
       
   295     fingers[pointerCount].dx := 0;
       
   296     fingers[pointerCount].dy := 0;
   293     fingers[pointerCount].timeSinceDown:= SDL_GetTicks;
   297     fingers[pointerCount].timeSinceDown:= SDL_GetTicks;
   294  
   298  
   295     addFinger:= @fingers[pointerCount];
   299     addFinger:= @fingers[pointerCount];
   296     inc(pointerCount);
   300     inc(pointerCount);
   297 end;
   301 end;
   298 
   302 
       
   303 function updateFinger(x,y,dx,dy: Longword; id: SDL_FingerId): PTouch_Finger;
       
   304 begin
       
   305    updateFinger:= findFinger(id);
       
   306 
       
   307    updateFinger^.x:= convertToCursorX(x);
       
   308    updateFinger^.y:= convertToCursorY(y);
       
   309    updateFinger^.dx:= convertToCursorDeltaX(dx);
       
   310    updateFinger^.dy:= convertToCursorDeltaY(dy);
       
   311 end;
       
   312 
   299 procedure deleteFinger(id: SDL_FingerId);
   313 procedure deleteFinger(id: SDL_FingerId);
   300 var
   314 var
   301     index : Longword;
   315     index : Longword;
   302 begin
   316 begin
   303     
   317     
   304     dec(pointerCount);
   318     dec(pointerCount);
   305     for index := 0 to pointerCount do
   319     for index := 0 to pointerCount do
   306     begin
   320     begin
   307         if fingers[index].id = id then
   321         if fingers[index].id = id then
   308         begin
   322         begin
   309             //Check for onTouchClick event
       
   310             if ((SDL_GetTicks - fingers[index].timeSinceDown) < clickTime) AND not(fingerHasMoved(fingers[index])) then
       
   311                 onTouchClick(fingers[index]);
       
   312  
   323  
   313             //put the last finger into the spot of the finger to be removed, 
   324             //put the last finger into the spot of the finger to be removed, 
   314             //so that all fingers are packed to the far left
   325             //so that all fingers are packed to the far left
   315             if  pointerCount <> index then
   326             if  pointerCount <> index then
   316                 begin
   327                 begin
   422             end;
   433             end;
   423 
   434 
   424     end; //if CurrentHedgehog^.Gear <> nil
   435     end; //if CurrentHedgehog^.Gear <> nil
   425 end;
   436 end;
   426 
   437 
   427 function convertToCursor(scale: LongInt; xy: LongInt): LongInt;
   438 //These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system
   428 begin
   439 // 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.
   429     convertToCursor := round(xy/32768*scale)
   440 // the CursorPoint coordinate system goes from -cScreenWidth/2 to cScreenWidth/2 on the x axis 
       
   441 //  and 0 to cScreenHeight on the x axis, (-cScreenWidth, cScreenHeight) being top left,
       
   442 function convertToCursorX(x: LongInt): LongInt;
       
   443 begin
       
   444     convertToCursorX := round((x/32768)*cScreenWidth) - (cScreenWidth shr 1);
       
   445 end;
       
   446 
       
   447 function convertToCursorY(y: LongInt): LongInt;
       
   448 begin
       
   449     convertToCursorY := cScreenHeight - round((y/32768)*cScreenHeight)
       
   450 end;
       
   451 
       
   452 function convertToCursorDeltaX(x: LongInt): LongInt;
       
   453 begin
       
   454     convertToCursorDeltaX := round(x/32768*cScreenWidth)
       
   455 end;
       
   456 
       
   457 function convertToCursorDeltaY(y: LongInt): LongInt;
       
   458 begin
       
   459     convertToCursorDeltaY := round(y/32768*cScreenHeight)
   430 end;
   460 end;
   431 
   461 
   432 function isOnFireButton(finger: Touch_Finger): boolean;
   462 function isOnFireButton(finger: Touch_Finger): boolean;
   433 begin
   463 begin
   434     isOnFireButton:= (finger.x <= fireButtonRight) and (finger.x >= fireButtonLeft) and (finger.y <= fireButtonBottom) and (finger.y >= fireButtonTop);
   464     isOnFireButton:= (finger.x <= fireButtonRight) and (finger.x >= fireButtonLeft) and (finger.y <= fireButtonBottom) and (finger.y >= fireButtonTop);
   448 var
   478 var
   449     x,y : hwFloat;
   479     x,y : hwFloat;
   450 begin
   480 begin
   451     x := _0;
   481     x := _0;
   452     y := _0;
   482     y := _0;
   453     convertToFingerCoord(x, y, CurrentHedgehog^.Gear^.X, CurrentHedgehog^.Gear^.Y);
   483     convertToFingerCoord(x,y, CurrentHedgehog^.Gear^.X, CurrentHedgehog^.Gear^.Y);
       
   484     WriteToConsole(Format('%d %s %d %s', [finger.x, cstr(x), finger.y, cstr(y) ]));
       
   485     
   454     isOnCurrentHog := Distance(int2hwFloat(finger.X)-x, int2hwFloat(finger.Y)-y) < _50;
   486     isOnCurrentHog := Distance(int2hwFloat(finger.X)-x, int2hwFloat(finger.Y)-y) < _50;
   455 end;
   487 end;
   456 
   488 
   457 procedure convertToFingerCoord(var x,y : hwFloat; oldX, oldY: hwFloat);
   489 procedure convertToFingerCoord(var x,y : hwFloat; oldX, oldY: hwFloat);
   458 begin
   490 begin
   459     x := oldX + int2hwFloat(WorldDx + (cScreenWidth div 2));
   491     x := oldX + int2hwFloat(WorldDx);
   460     y := oldY + int2hwFloat(WorldDy);
   492     y := int2hwFloat(cScreenHeight) - (oldY + int2hwFloat(WorldDy));
   461 end;
   493 end;
   462 
   494 
   463 procedure convertToWorldCoord(var x,y: hwFloat; finger: Touch_Finger);
   495 procedure convertToWorldCoord(var x,y: hwFloat; finger: Touch_Finger);
   464 begin
   496 begin
   465 //if x <> nil then 
   497 //if x <> nil then