hedgewars/uTouch.pas
changeset 6640 813a173cd677
parent 6625 2d8c5815292f
child 6648 025473a2c420
equal deleted inserted replaced
6639:2b29832557c2 6640:813a173cd677
    56 
    56 
    57 function findFinger(id: SDL_FingerId): PTouch_Finger;
    57 function findFinger(id: SDL_FingerId): PTouch_Finger;
    58 procedure aim(finger: Touch_Finger);
    58 procedure aim(finger: Touch_Finger);
    59 function isOnCrosshair(finger: Touch_Finger): boolean;
    59 function isOnCrosshair(finger: Touch_Finger): boolean;
    60 function isOnCurrentHog(finger: Touch_Finger): boolean;
    60 function isOnCurrentHog(finger: Touch_Finger): boolean;
    61 function isOnFireButton(finger: Touch_Finger): boolean;
       
    62 procedure convertToWorldCoord(var x,y: hwFloat; finger: Touch_Finger);
    61 procedure convertToWorldCoord(var x,y: hwFloat; finger: Touch_Finger);
    63 procedure convertToFingerCoord(var x,y: hwFloat; oldX, oldY: hwFloat);
    62 procedure convertToFingerCoord(var x,y: hwFloat; oldX, oldY: hwFloat);
    64 function fingerHasMoved(finger: Touch_Finger): boolean;
    63 function fingerHasMoved(finger: Touch_Finger): boolean;
    65 function calculateDelta(finger1, finger2: Touch_Finger): hwFloat;
    64 function calculateDelta(finger1, finger2: Touch_Finger): hwFloat;
    66 function getSecondFinger(finger: Touch_Finger): PTouch_Finger;
    65 function getSecondFinger(finger: Touch_Finger): PTouch_Finger;
       
    66 function isOnRect(x,y,w,h: LongInt; finger: Touch_Finger): boolean;
    67 procedure printFinger(finger: Touch_Finger);
    67 procedure printFinger(finger: Touch_Finger);
    68 implementation
    68 implementation
    69 
    69 
    70 const
    70 const
    71     clicktime = 200;
    71     clicktime = 200;
    72     nilFingerId = High(SDL_FingerId);
    72     nilFingerId = High(SDL_FingerId);
    73 
    73 
    74 var
    74 var
    75     fireButtonLeft, fireButtonRight, fireButtonTop, fireButtonBottom : LongInt;
       
    76         
       
    77 
       
    78 
       
    79     leftButtonBoundary  : LongInt;
       
    80     rightButtonBoundary : LongInt;
       
    81     topButtonBoundary   : LongInt;
       
    82     
       
    83     pointerCount : Longword;
    75     pointerCount : Longword;
    84     fingers: array of Touch_Finger;
    76     fingers: array of Touch_Finger;
    85     moveCursor : boolean;
    77     moveCursor : boolean;
    86     invertCursor : boolean;
    78     invertCursor : boolean;
    87 
    79 
    90 
    82 
    91     //Pinch to zoom 
    83     //Pinch to zoom 
    92     pinchSize : hwFloat;
    84     pinchSize : hwFloat;
    93     baseZoomValue: GLFloat;
    85     baseZoomValue: GLFloat;
    94 
    86 
    95 
       
    96     //aiming
    87     //aiming
    97     aiming, movingCrosshair: boolean; 
    88     aiming, movingCrosshair: boolean;
       
    89     aimingUp, aimingDown: boolean; 
    98     crosshairCommand: ShortString;
    90     crosshairCommand: ShortString;
    99     targetAngle: LongInt;
    91     targetAngle: LongInt;
   100     stopFiring: boolean;
    92     stopFiring: boolean;
   101 
    93 
   102     //moving
    94     //moving
   122             begin
   114             begin
   123                 aiming:= true;
   115                 aiming:= true;
   124                 exit;
   116                 exit;
   125             end;
   117             end;
   126 
   118 
   127             if isOnFireButton(finger^) then
   119             if isOnRect(fireButtonX, fireButtonY, fireButtonW, fireButtonH, finger^) then
   128             begin
   120             begin
   129                 stopFiring:= false;
   121                 stopFiring:= false;
   130                 ParseCommand('+attack', true);
   122                 ParseCommand('+attack', true);
   131                 exit;
   123                 exit;
   132             end;
   124             end;
   133 {            if (finger^.x < leftButtonBoundary) and (finger^.y < 390) then
   125             if isOnRect(arrowLeftX, arrowLeftY, arrowLeftW, arrowLeftH, finger^) then
   134             begin
   126             begin
   135                 ParseCommand('+left', true);
   127                 ParseCommand('+left', true);
   136                 walkingLeft := true;
   128                 walkingLeft := true;
   137                 exit;
   129                 exit;
   138             end;
   130             end;
   139             if finger^.x > rightButtonBoundary then
   131             if isOnRect(arrowRightX, arrowRightY, arrowRightW, arrowRightH, finger^) then
   140             begin
   132             begin
   141                 ParseCommand('+right', true);
   133                 ParseCommand('+right', true);
   142                 walkingRight:= true;
   134                 walkingRight:= true;
   143                 exit;
   135                 exit;
   144             end;
   136             end;
   145             if finger^.y < topButtonBoundary then
   137             if isOnRect(arrowUpX, arrowUpY, arrowUpW, arrowUpH, finger^) then
       
   138             begin
       
   139                 ParseCommand('+up', true);
       
   140                 aimingUp:= true;
       
   141                 exit;
       
   142             end;
       
   143             if isOnRect(arrowDownX, arrowDownY, arrowUpW, arrowUpH, finger^) then
       
   144             begin
       
   145                 ParseCommand('+down', true);
       
   146                 aimingDown:= true;
       
   147                 exit;
       
   148             end;
       
   149 
       
   150             if isOnRect(backjumpX, backjumpY, backjumpW, backjumpH, finger^) then
   146             begin
   151             begin
   147                 ParseCommand('hjump', true);
   152                 ParseCommand('hjump', true);
   148                 exit;
   153                 exit;
   149             end;
   154             end;
   150  }           moveCursor:= true; 
   155             if isOnRect(forwardjumpX, forwardjumpY, forwardjumpW, forwardjumpH, finger^) then
       
   156             begin
       
   157                 ParseCommand('ljump', true);
       
   158                 exit;
       
   159             end;
       
   160 
       
   161           
       
   162             moveCursor:= true; 
   151         end;
   163         end;
   152         2:
   164         2:
   153         begin
   165         begin
   154             aiming:= false;
   166             aiming:= false;
   155             stopFiring:= true;
   167             stopFiring:= true;
   219     begin
   231     begin
   220         ParseCommand('-left', true);
   232         ParseCommand('-left', true);
   221         walkingLeft := false;
   233         walkingLeft := false;
   222     end;
   234     end;
   223 
   235 
   224     if walkingRight then
   236 if walkingRight then
   225     begin
   237     begin
   226         ParseCommand('-right', true);
   238         ParseCommand('-right', true);
   227         walkingRight := false;
   239         walkingRight := false;
   228     end;
   240     end;
       
   241 
       
   242 if aimingUp then
       
   243     begin
       
   244     ParseCommand('-up', true);
       
   245     aimingUp:= false;
       
   246     end;
       
   247 if aimingDown then
       
   248     begin
       
   249     ParseCommand('-down', true);
       
   250     aimingDown:= false;
       
   251     end;
       
   252 
       
   253 
   229 end;
   254 end;
   230 
   255 
   231 procedure onTouchDoubleClick(finger: Touch_Finger);
   256 procedure onTouchDoubleClick(finger: Touch_Finger);
   232 begin
   257 begin
   233 finger := finger;//avoid compiler hint
   258 finger := finger;//avoid compiler hint
   234 ParseCommand('ljump', true);
   259 //ParseCommand('ljump', true);
   235 end;
   260 end;
   236 
   261 
   237 procedure onTouchClick(finger: Touch_Finger);
   262 procedure onTouchClick(finger: Touch_Finger);
   238 begin
   263 begin
   239 if (SDL_GetTicks - timeSinceClick < 300) and (DistanceI(finger.X-xTouchClick, finger.Y-yTouchClick) < _30) then
   264 if (SDL_GetTicks - timeSinceClick < 300) and (DistanceI(finger.X-xTouchClick, finger.Y-yTouchClick) < _30) then
   250 if bShowAmmoMenu then 
   275 if bShowAmmoMenu then 
   251     begin
   276     begin
   252     CursorPoint.X:= finger.x;
   277     CursorPoint.X:= finger.x;
   253     CursorPoint.Y:= finger.y;
   278     CursorPoint.Y:= finger.y;
   254     doPut(CursorPoint.X, CursorPoint.Y, false); 
   279     doPut(CursorPoint.X, CursorPoint.Y, false); 
   255     WriteToConsole(Format('%d %d', [CursorPoint.X, CursorPoint.Y]));
       
   256     exit
   280     exit
   257     end;
   281     end;
   258 
   282 
   259 if isOnCurrentHog(finger) then
   283 if isOnCurrentHog(finger) then
   260     begin
   284     begin
   261     bShowAmmoMenu := true;
   285     bShowAmmoMenu := true;
   262     exit;
   286     exit;
   263     end;
   287     end;
   264 
   288 
   265 if finger.y < topButtonBoundary then
   289 {if finger.y < topButtonBoundary then
   266     begin
   290     begin
   267     ParseCommand('hjump', true);
   291     ParseCommand('hjump', true);
   268     exit;
   292     exit;
   269     end;
   293     end;}
   270 end;
   294 end;
   271 
   295 
   272 function addFinger(x,y: Longword; id: SDL_FingerId): PTouch_Finger;
   296 function addFinger(x,y: Longword; id: SDL_FingerId): PTouch_Finger;
   273 var 
   297 var 
   274     xCursor, yCursor, index : LongInt;
   298     xCursor, yCursor, index : LongInt;
   421         tmpAngle:= DeltaY / Distance(deltaX, deltaY) *_2048;
   445         tmpAngle:= DeltaY / Distance(deltaX, deltaY) *_2048;
   422         targetAngle:= (hwRound(tmpAngle) + 2048) div 2;
   446         targetAngle:= (hwRound(tmpAngle) + 2048) div 2;
   423 
   447 
   424         tmp := crosshairCommand;
   448         tmp := crosshairCommand;
   425         if CurrentHedgehog^.Gear^.Angle - targetAngle < 0 then
   449         if CurrentHedgehog^.Gear^.Angle - targetAngle < 0 then
   426             crosshairCommand := 'down'
   450             crosshairCommand := 'up'
   427         else
   451         else
   428             crosshairCommand:= 'up';
   452             crosshairCommand:= 'down';
   429         if movingCrosshair and (tmp <> crosshairCommand) then 
   453         if movingCrosshair and (tmp <> crosshairCommand) then 
   430             begin
   454             begin
   431             ParseCommand('-' + tmp, true);
   455             ParseCommand('-' + tmp, true);
   432             movingCrosshair := false;
   456             movingCrosshair := false;
   433             end;
   457             end;
   457 function convertToCursorDeltaY(y: LongInt): LongInt;
   481 function convertToCursorDeltaY(y: LongInt): LongInt;
   458 begin
   482 begin
   459     convertToCursorDeltaY := round(y/32768*cScreenHeight)
   483     convertToCursorDeltaY := round(y/32768*cScreenHeight)
   460 end;
   484 end;
   461 
   485 
   462 function isOnFireButton(finger: Touch_Finger): boolean;
       
   463 begin
       
   464     isOnFireButton:= (finger.x <= fireButtonRight) and (finger.x >= fireButtonLeft) and (finger.y <= fireButtonBottom) and (finger.y >= fireButtonTop);
       
   465 end;
       
   466 
       
   467 function isOnCrosshair(finger: Touch_Finger): boolean;
   486 function isOnCrosshair(finger: Touch_Finger): boolean;
   468 var
   487 var
   469     x,y : hwFloat;
   488     x,y : hwFloat;
   470 begin
   489 begin
   471     x := _0;//avoid compiler hint
   490     x := _0;//avoid compiler hint
   479     x,y : hwFloat;
   498     x,y : hwFloat;
   480 begin
   499 begin
   481     x := _0;
   500     x := _0;
   482     y := _0;
   501     y := _0;
   483     convertToFingerCoord(x,y, CurrentHedgehog^.Gear^.X, CurrentHedgehog^.Gear^.Y);
   502     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     
       
   486     isOnCurrentHog := Distance(int2hwFloat(finger.X)-x, int2hwFloat(finger.Y)-y) < _50;
   503     isOnCurrentHog := Distance(int2hwFloat(finger.X)-x, int2hwFloat(finger.Y)-y) < _50;
   487 end;
   504 end;
   488 
   505 
   489 procedure convertToFingerCoord(var x,y : hwFloat; oldX, oldY: hwFloat);
   506 procedure convertToFingerCoord(var x,y : hwFloat; oldX, oldY: hwFloat);
   490 begin
   507 begin
   519         getSecondFinger := @fingers[1]
   536         getSecondFinger := @fingers[1]
   520     else
   537     else
   521         getSecondFinger := @fingers[0];
   538         getSecondFinger := @fingers[0];
   522 end;
   539 end;
   523 
   540 
       
   541 function isOnRect(x,y,w,h: LongInt; finger: Touch_Finger): boolean;
       
   542 begin
       
   543 WriteToConsole(Format('(%d,%d) (%d, %d) %d',[finger.x, finger.y, x,y, w]));
       
   544 isOnRect:= (finger.x > x)   and
       
   545            (finger.x < x+w) and
       
   546            (cScreenHeight - finger.y > y)   and
       
   547            (cScreenHeight - finger.y < (y+w));
       
   548 end;
       
   549 
   524 procedure printFinger(finger: Touch_Finger);
   550 procedure printFinger(finger: Touch_Finger);
   525 begin
   551 begin
   526     WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));
   552     WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));
   527 end;
   553 end;
   528 
   554 
   534     movingCrosshair := false;
   560     movingCrosshair := false;
   535     stopFiring:= false;
   561     stopFiring:= false;
   536     walkingLeft := false;
   562     walkingLeft := false;
   537     walkingRight := false;
   563     walkingRight := false;
   538 
   564 
   539     leftButtonBoundary := cScreenWidth div 4;
       
   540     rightButtonBoundary := cScreenWidth div 4*3;
       
   541     topButtonBoundary := cScreenHeight div 6;
       
   542     
       
   543     setLength(fingers, 4);
   565     setLength(fingers, 4);
   544     for index := 0 to High(fingers) do 
   566     for index := 0 to High(fingers) do 
   545         fingers[index].id := nilFingerId;
   567         fingers[index].id := nilFingerId;
   546 
       
   547 
       
   548     //uRenderCoordScaleX := Round(cScreenWidth/0.8 * 2);
       
   549     fireButtonLeft := Round(cScreenWidth*0.01);
       
   550     fireButtonRight := Round(fireButtonLeft + (spritesData[sprFireButton].Width*0.4));
       
   551     fireButtonBottom := Round(cScreenHeight*0.99);
       
   552     fireButtonTop := fireButtonBottom - Round(spritesData[sprFireButton].Height*0.4);
       
   553 end;
   568 end;
   554 
   569 
   555 begin
   570 begin
   556 end.
   571 end.