hedgewars/uTouch.pas
changeset 6651 4103354b7599
parent 6648 025473a2c420
child 6654 120e95c10532
equal deleted inserted replaced
6650:56a0b7bf6c15 6651:4103354b7599
    83     //Pinch to zoom 
    83     //Pinch to zoom 
    84     pinchSize : hwFloat;
    84     pinchSize : hwFloat;
    85     baseZoomValue: GLFloat;
    85     baseZoomValue: GLFloat;
    86 
    86 
    87     //aiming
    87     //aiming
    88     aiming, movingCrosshair: boolean;
    88     aiming: boolean;
    89     aimingUp, aimingDown: boolean; 
    89     aimingUp, aimingDown: boolean; 
    90     crosshairCommand: ShortString;
       
    91     targetAngle: LongInt;
    90     targetAngle: LongInt;
    92     stopFiring: boolean;
    91     stopFiring: boolean;
    93 
    92 
    94     //moving
    93     //moving
    95     stopLeft, stopRight, walkingLeft, walkingRight :  boolean;
    94     stopLeft, stopRight, walkingLeft, walkingRight :  boolean;
   101 begin
   100 begin
   102 finger := addFinger(x,y,pointerId);
   101 finger := addFinger(x,y,pointerId);
   103 case pointerCount of
   102 case pointerCount of
   104         1:
   103         1:
   105         begin
   104         begin
   106             moveCursor:= not bShowAmmoMenu;
   105             moveCursor:= false;
   107 
   106 
   108             if isOnCrosshair(finger^) then
   107             if isOnCrosshair(finger^) then
   109             begin
   108             begin
   110                 aiming:= true;
   109                 aiming:= true;
       
   110                 aim(finger^);
   111                 exit;
   111                 exit;
   112             end;
   112             end;
   113 
   113 
   114             if isOnRect(fireButtonX, fireButtonY, fireButtonW, fireButtonH, finger^) then
   114             if isOnRect(fireButtonX, fireButtonY, fireButtonW, fireButtonH, finger^) then
   115             begin
   115             begin
   150             if isOnRect(forwardjumpX, forwardjumpY, forwardjumpW, forwardjumpH, finger^) then
   150             if isOnRect(forwardjumpX, forwardjumpY, forwardjumpW, forwardjumpH, finger^) then
   151             begin
   151             begin
   152                 ParseCommand('ljump', true);
   152                 ParseCommand('ljump', true);
   153                 exit;
   153                 exit;
   154             end;
   154             end;
       
   155             moveCursor:= not bShowAmmoMenu;
   155         end;
   156         end;
   156         2:
   157         2:
   157         begin
   158         begin
   158             aiming:= false;
   159             aiming:= false;
   159             stopFiring:= true;
   160             stopFiring:= true;
   184             CursorPoint.Y := CursorPoint.Y - finger^.dy;
   185             CursorPoint.Y := CursorPoint.Y - finger^.dy;
   185         end;
   186         end;
   186         exit //todo change into switch rather than ugly ifs
   187         exit //todo change into switch rather than ugly ifs
   187     end;
   188     end;
   188     
   189     
   189     if aiming then 
   190 if aiming then 
   190     begin
   191     begin
   191         aim(finger^);
   192         aim(finger^);
   192         exit
   193         exit
   193     end;
   194     end;
   194     if pointerCount = 2 then
   195 
       
   196 if pointerCount = 2 then
   195     begin
   197     begin
   196        secondFinger := getSecondFinger(finger^);
   198        secondFinger := getSecondFinger(finger^);
   197        currentPinchDelta := calculateDelta(finger^, secondFinger^) - pinchSize;
   199        currentPinchDelta := calculateDelta(finger^, secondFinger^) - pinchSize;
   198        zoom := currentPinchDelta/cScreenWidth;
   200        zoom := currentPinchDelta/cScreenWidth;
   199        ZoomValue := baseZoomValue - ((hwFloat2Float(zoom) * cMinMaxZoomLevelDelta));
   201        ZoomValue := baseZoomValue - ((hwFloat2Float(zoom) * cMinMaxZoomLevelDelta));
   200        if ZoomValue < cMaxZoomLevel then
   202        if ZoomValue < cMaxZoomLevel then
   201            ZoomValue := cMaxZoomLevel;
   203            ZoomValue := cMaxZoomLevel;
   202        if ZoomValue > cMinZoomLevel then
   204        if ZoomValue > cMinZoomLevel then
   203            ZoomValue := cMinZoomLevel;
   205            ZoomValue := cMinZoomLevel;
   204     end;
   206     end;
       
   207 
   205 end;
   208 end;
   206 
   209 
   207 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
   210 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
   208 var
   211 var
   209     finger: PTouch_Finger;
   212     finger: PTouch_Finger;
   219 
   222 
   220 deleteFinger(pointerId);
   223 deleteFinger(pointerId);
   221 
   224 
   222 if walkingLeft then
   225 if walkingLeft then
   223     begin
   226     begin
   224         ParseCommand('-left', true);
   227     ParseCommand('-left', true);
   225         walkingLeft := false;
   228     walkingLeft := false;
   226     end;
   229     end;
   227 
   230 
   228 if walkingRight then
   231 if walkingRight then
   229     begin
   232     begin
   230         ParseCommand('-right', true);
   233     ParseCommand('-right', true);
   231         walkingRight := false;
   234     walkingRight := false;
   232     end;
   235     end;
   233 
   236 
   234 if aimingUp then
   237 if aimingUp then
   235     begin
   238     begin
   236     ParseCommand('-up', true);
   239     ParseCommand('-up', true);
   239 if aimingDown then
   242 if aimingDown then
   240     begin
   243     begin
   241     ParseCommand('-down', true);
   244     ParseCommand('-down', true);
   242     aimingDown:= false;
   245     aimingDown:= false;
   243     end;
   246     end;
   244 
       
   245 
       
   246 end;
   247 end;
   247 
   248 
   248 procedure onTouchDoubleClick(finger: Touch_Finger);
   249 procedure onTouchDoubleClick(finger: Touch_Finger);
   249 begin
   250 begin
   250 finger := finger;//avoid compiler hint
   251 finger := finger;//avoid compiler hint
   365 
   366 
   366 procedure ProcessTouch;
   367 procedure ProcessTouch;
   367 var
   368 var
   368     deltaAngle: LongInt;
   369     deltaAngle: LongInt;
   369 begin
   370 begin
   370     invertCursor := not(bShowAmmoMenu);
   371 invertCursor := not(bShowAmmoMenu);
   371     if aiming then
   372 if aiming then
   372     begin
   373     if CurrentHedgehog^.Gear <> nil then
   373         if CurrentHedgehog^.Gear <> nil then
   374         begin
   374             begin
   375         deltaAngle:= CurrentHedgehog^.Gear^.Angle - targetAngle;
   375             deltaAngle:= CurrentHedgehog^.Gear^.Angle - targetAngle;
   376         if (deltaAngle = 0) then 
   376             if (deltaAngle <> 0) and not(movingCrosshair) then 
   377             begin
       
   378             if aimingUp then
   377                 begin
   379                 begin
   378                 ParseCommand('+' + crosshairCommand, true);
   380                 ParseCommand('-up', true);
   379                 movingCrosshair := true;
   381                 aimingUp:= false;
       
   382                 end;
       
   383             if aimingDown then
       
   384                 begin
       
   385                 ParseCommand('-down', true);
       
   386                 aimingDown:= false;
   380                 end
   387                 end
   381             else 
   388             end
   382                 if movingCrosshair then 
   389         else
       
   390             begin
       
   391             if (deltaAngle < 0) then
       
   392                 begin
       
   393                 if aimingUp then
   383                     begin
   394                     begin
   384                     ParseCommand('-' + crosshairCommand, true);
   395                     ParseCommand('-up', true);
   385                     movingCrosshair:= false;
   396                     aimingUp:= false;
   386                    end;
   397                     end;
   387             end;
   398                 ParseCommand('+down', true);
   388     end
   399                 aimingDown:= true;
   389     else if movingCrosshair then 
   400                 end
   390         begin
   401             else
   391         ParseCommand('-' + crosshairCommand, true);
   402                 begin
   392         movingCrosshair := false;
   403                 if aimingDown then
       
   404                     begin
       
   405                     ParseCommand('-down', true);
       
   406                     aimingDown:= false;
       
   407                     end;
       
   408                 ParseCommand('+up', true);
       
   409                 aimingUp:= true;
       
   410                 end; 
       
   411             end;
       
   412         end
       
   413     else  
       
   414         begin
       
   415         if aimingUp then
       
   416             begin
       
   417             ParseCommand('-up', true);
       
   418             aimingUp:= false;
       
   419             end;
       
   420         if aimingDown then
       
   421             begin
       
   422             ParseCommand('-down', true);
       
   423             aimingDown:= false;
       
   424             end;
   393         end;
   425         end;
   394 
   426        
   395     if stopFiring then 
   427 if stopFiring then 
   396         begin
   428     begin
   397         ParseCommand('-attack', true);
   429     ParseCommand('-attack', true);
   398         stopFiring:= false;
   430     stopFiring:= false;
   399         end;
   431     end;
   400     
   432 
   401     if stopRight then
   433 if stopRight then
   402         begin
   434     begin
   403         stopRight := false;
   435     stopRight := false;
   404         ParseCommand('-right', true);
   436     ParseCommand('-right', true);
   405         end;
   437     end;
   406  
   438  
   407     if stopLeft then
   439 if stopLeft then
   408         begin
   440     begin
   409         stopLeft := false;
   441     stopLeft := false;
   410         ParseCommand('-left', true);
   442     ParseCommand('-left', true);
   411         end;
   443     end;
   412     
   444     
   413 end;
   445 end;
   414 
   446 
   415 function findFinger(id: SDL_FingerId): PTouch_Finger;
   447 function findFinger(id: SDL_FingerId): PTouch_Finger;
   416 var
   448 var
   425 end;
   457 end;
   426 
   458 
   427 procedure aim(finger: Touch_Finger);
   459 procedure aim(finger: Touch_Finger);
   428 var 
   460 var 
   429     hogX, hogY, touchX, touchY, deltaX, deltaY, tmpAngle: hwFloat;
   461     hogX, hogY, touchX, touchY, deltaX, deltaY, tmpAngle: hwFloat;
   430     tmp: ShortString;
       
   431 begin
   462 begin
   432     if CurrentHedgehog^.Gear <> nil then
   463     if CurrentHedgehog^.Gear <> nil then
   433     begin
   464         begin
   434         touchX := _0;//avoid compiler hint
   465         touchX := _0;//avoid compiler hint
   435         touchY := _0;
   466         touchY := _0;
   436         hogX := CurrentHedgehog^.Gear^.X;
   467         hogX := CurrentHedgehog^.Gear^.X;
   437         hogY := CurrentHedgehog^.Gear^.Y;
   468         hogY := CurrentHedgehog^.Gear^.Y;
   438 
   469 
   440         deltaX := hwAbs(TouchX-HogX);
   471         deltaX := hwAbs(TouchX-HogX);
   441         deltaY := (TouchY-HogY);
   472         deltaY := (TouchY-HogY);
   442         
   473         
   443         tmpAngle:= DeltaY / Distance(deltaX, deltaY) *_2048;
   474         tmpAngle:= DeltaY / Distance(deltaX, deltaY) *_2048;
   444         targetAngle:= (hwRound(tmpAngle) + 2048) div 2;
   475         targetAngle:= (hwRound(tmpAngle) + 2048) div 2;
   445 
   476         end; //if CurrentHedgehog^.Gear <> nil
   446         tmp := crosshairCommand;
       
   447         if CurrentHedgehog^.Gear^.Angle - targetAngle < 0 then
       
   448             crosshairCommand := 'up'
       
   449         else
       
   450             crosshairCommand:= 'down';
       
   451         if movingCrosshair and (tmp <> crosshairCommand) then 
       
   452             begin
       
   453             ParseCommand('-' + tmp, true);
       
   454             movingCrosshair := false;
       
   455             end;
       
   456 
       
   457     end; //if CurrentHedgehog^.Gear <> nil
       
   458 end;
   477 end;
   459 
   478 
   460 //These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system
   479 //These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system
   461 // 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.
   480 // 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.
   462 // the CursorPoint coordinate system goes from -cScreenWidth/2 to cScreenWidth/2 on the x axis 
   481 // the CursorPoint coordinate system goes from -cScreenWidth/2 to cScreenWidth/2 on the x axis 
   508 end;
   527 end;
   509 
   528 
   510 procedure convertToWorldCoord(var x,y: hwFloat; finger: Touch_Finger);
   529 procedure convertToWorldCoord(var x,y: hwFloat; finger: Touch_Finger);
   511 begin
   530 begin
   512 //if x <> nil then 
   531 //if x <> nil then 
   513     x := int2hwFloat((finger.x-WorldDx) - (cScreenWidth div 2));
   532     x := int2hwFloat((finger.x-WorldDx));
   514 //if y <> nil then 
   533 //if y <> nil then 
   515     y := int2hwFloat(finger.y-WorldDy);
   534     y := int2hwFloat((cScreenHeight - finger.y)-WorldDy);
   516 end;
   535 end;
   517 
   536 
   518 //Method to calculate the distance this finger has moved since the downEvent
   537 //Method to calculate the distance this finger has moved since the downEvent
   519 function fingerHasMoved(finger: Touch_Finger): boolean;
   538 function fingerHasMoved(finger: Touch_Finger): boolean;
   520 begin
   539 begin
   536         getSecondFinger := @fingers[0];
   555         getSecondFinger := @fingers[0];
   537 end;
   556 end;
   538 
   557 
   539 function isOnRect(x,y,w,h: LongInt; finger: Touch_Finger): boolean;
   558 function isOnRect(x,y,w,h: LongInt; finger: Touch_Finger): boolean;
   540 begin
   559 begin
   541 WriteToConsole(Format('(%d,%d) (%d, %d) %d',[finger.x, finger.y, x,y, w]));
       
   542 isOnRect:= (finger.x > x)   and
   560 isOnRect:= (finger.x > x)   and
   543            (finger.x < x+w) and
   561            (finger.x < x+w) and
   544            (cScreenHeight - finger.y > y)   and
   562            (cScreenHeight - finger.y > y)   and
   545            (cScreenHeight - finger.y < (y+w));
   563            (cScreenHeight - finger.y < (y+h));
   546 end;
   564 end;
   547 
   565 
   548 procedure printFinger(finger: Touch_Finger);
   566 procedure printFinger(finger: Touch_Finger);
   549 begin
   567 begin
   550     WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));
   568     WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));
   553 procedure initModule;
   571 procedure initModule;
   554 var
   572 var
   555     index: Longword;
   573     index: Longword;
   556     //uRenderCoordScaleX, uRenderCoordScaleY: Longword;
   574     //uRenderCoordScaleX, uRenderCoordScaleY: Longword;
   557 begin
   575 begin
   558     movingCrosshair := false;
       
   559     stopFiring:= false;
   576     stopFiring:= false;
   560     walkingLeft := false;
   577     walkingLeft := false;
   561     walkingRight := false;
   578     walkingRight := false;
   562 
   579 
   563     setLength(fingers, 4);
   580     setLength(fingers, 4);