hedgewars/uTouch.pas
changeset 6702 37c56511df0c
parent 6701 58a43c2064ad
child 6703 abe2abfe9de2
equal deleted inserted replaced
6701:58a43c2064ad 6702:37c56511df0c
    43 
    43 
    44 function findFinger(id: TSDL_FingerId): PTouch_Data;
    44 function findFinger(id: TSDL_FingerId): PTouch_Data;
    45 procedure aim(finger: TTouch_Data);
    45 procedure aim(finger: TTouch_Data);
    46 function isOnCrosshair(finger: TTouch_Data): boolean;
    46 function isOnCrosshair(finger: TTouch_Data): boolean;
    47 function isOnCurrentHog(finger: TTouch_Data): boolean;
    47 function isOnCurrentHog(finger: TTouch_Data): boolean;
    48 procedure convertToWorldCoord(var x,y: hwFloat; finger: TTouch_Data);
    48 procedure convertToWorldCoord(var x,y: LongInt; finger: TTouch_Data);
    49 procedure convertToFingerCoord(var x,y: hwFloat; oldX, oldY: hwFloat);
    49 procedure convertToFingerCoord(var x,y: LongInt; oldX, oldY: LongInt);
    50 function fingerHasMoved(finger: TTouch_Data): boolean;
    50 function fingerHasMoved(finger: TTouch_Data): boolean;
    51 function calculateDelta(finger1, finger2: TTouch_Data): hwFloat;
    51 function calculateDelta(finger1, finger2: TTouch_Data): LongInt;
    52 function getSecondFinger(finger: TTouch_Data): PTouch_Data;
    52 function getSecondFinger(finger: TTouch_Data): PTouch_Data;
    53 function isOnRect(rect: TSDL_Rect; finger: TTouch_Data): boolean;
    53 function isOnRect(rect: TSDL_Rect; finger: TTouch_Data): boolean;
    54 procedure printFinger(finger: TTouch_Data);
    54 procedure printFinger(finger: TTouch_Data);
    55 implementation
    55 implementation
    56 
    56 
    66 
    66 
    67     xTouchClick,yTouchClick : LongInt;
    67     xTouchClick,yTouchClick : LongInt;
    68     timeSinceClick : Longword;
    68     timeSinceClick : Longword;
    69 
    69 
    70     //Pinch to zoom 
    70     //Pinch to zoom 
    71     pinchSize : hwFloat;
    71     pinchSize : LongInt;
    72     baseZoomValue: GLFloat;
    72     baseZoomValue: GLFloat;
    73 
    73 
    74     //aiming
    74     //aiming
    75     aiming: boolean;
    75     aiming: boolean;
    76     aimingUp, aimingDown: boolean; 
    76     aimingUp, aimingDown: boolean; 
   168 end;
   168 end;
   169 
   169 
   170 procedure onTouchMotion(x,y: Longword;dx,dy: LongInt; pointerId: TSDL_FingerId);
   170 procedure onTouchMotion(x,y: Longword;dx,dy: LongInt; pointerId: TSDL_FingerId);
   171 var
   171 var
   172     finger, secondFinger: PTouch_Data;
   172     finger, secondFinger: PTouch_Data;
   173     currentPinchDelta, zoom : hwFloat;
   173     currentPinchDelta, zoom : single;
   174 begin
   174 begin
   175 finger:= updateFinger(x,y,dx,dy,pointerId);
   175 finger:= updateFinger(x,y,dx,dy,pointerId);
   176 
   176 
   177 if moveCursor then
   177 if moveCursor then
   178     begin
   178     begin
   198 if (buttonsDown = 0) and (pointerCount = 2) then
   198 if (buttonsDown = 0) and (pointerCount = 2) then
   199     begin
   199     begin
   200        secondFinger := getSecondFinger(finger^);
   200        secondFinger := getSecondFinger(finger^);
   201        currentPinchDelta := calculateDelta(finger^, secondFinger^) - pinchSize;
   201        currentPinchDelta := calculateDelta(finger^, secondFinger^) - pinchSize;
   202        zoom := currentPinchDelta/cScreenWidth;
   202        zoom := currentPinchDelta/cScreenWidth;
   203        ZoomValue := baseZoomValue - ((hwFloat2Float(zoom) * cMinMaxZoomLevelDelta));
   203        ZoomValue := baseZoomValue - (zoom * cMinMaxZoomLevelDelta);
   204        if ZoomValue < cMaxZoomLevel then
   204        if ZoomValue < cMaxZoomLevel then
   205            ZoomValue := cMaxZoomLevel;
   205            ZoomValue := cMaxZoomLevel;
   206        if ZoomValue > cMinZoomLevel then
   206        if ZoomValue > cMinZoomLevel then
   207            ZoomValue := cMinZoomLevel;
   207            ZoomValue := cMinZoomLevel;
   208     end;
   208     end;
   258 finger := finger;//avoid compiler hint
   258 finger := finger;//avoid compiler hint
   259 end;
   259 end;
   260 
   260 
   261 procedure onTouchClick(finger: TTouch_Data);
   261 procedure onTouchClick(finger: TTouch_Data);
   262 begin
   262 begin
   263 if (RealTicks - timeSinceClick < 300) and (DistanceI(finger.X-xTouchClick, finger.Y-yTouchClick) < _30) then
   263 if (RealTicks - timeSinceClick < 300) and (sqrt(sqr(finger.X-xTouchClick) + sqr(finger.Y-yTouchClick)) < 30) then
   264     begin
   264     begin
   265     onTouchDoubleClick(finger);
   265     onTouchDoubleClick(finger);
   266     timeSinceClick:= 0;//we make an assumption there won't be an 'click' in the first 300 ticks(milliseconds) 
   266     timeSinceClick:= 0;//we make an assumption there won't be an 'click' in the first 300 ticks(milliseconds) 
   267     exit; 
   267     exit; 
   268     end;
   268     end;
   438             end;
   438             end;
   439 end;
   439 end;
   440 
   440 
   441 procedure aim(finger: TTouch_Data);
   441 procedure aim(finger: TTouch_Data);
   442 var 
   442 var 
   443     hogX, hogY, touchX, touchY, deltaX, deltaY, tmpAngle: hwFloat;
   443     hogX, hogY, touchX, touchY, deltaX, deltaY: LongInt;
   444 begin
   444 begin
   445     if CurrentHedgehog^.Gear <> nil then
   445     if CurrentHedgehog^.Gear <> nil then
   446         begin
   446         begin
   447         touchX := _0;//avoid compiler hint
   447         touchX := 0;//avoid compiler hint
   448         touchY := _0;
   448         touchY := 0;
   449         hogX := CurrentHedgehog^.Gear^.X;
   449         hogX := hwRound(CurrentHedgehog^.Gear^.X);
   450         hogY := CurrentHedgehog^.Gear^.Y;
   450         hogY := hwRound(CurrentHedgehog^.Gear^.Y);
   451 
   451 
   452         convertToWorldCoord(touchX, touchY, finger);
   452         convertToWorldCoord(touchX, touchY, finger);
   453         deltaX := hwAbs(TouchX-HogX);
   453         deltaX := abs(TouchX-HogX);
   454         deltaY := (TouchY-HogY);
   454         deltaY := TouchY-HogY;
   455         
   455         
   456         tmpAngle:= DeltaY / Distance(deltaX, deltaY) *_2048;
   456         targetAngle:= (Round(DeltaY / sqrt(sqr(deltaX) + sqr(deltaY))) + 2048) div 2;
   457         targetAngle:= (hwRound(tmpAngle) + 2048) div 2;
       
   458         end; //if CurrentHedgehog^.Gear <> nil
   457         end; //if CurrentHedgehog^.Gear <> nil
   459 end;
   458 end;
   460 
   459 
   461 // These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system:
   460 // These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system:
   462 // - 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;
   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;
   482     convertToCursorDeltaY := round(y/32768*cScreenHeight)
   481     convertToCursorDeltaY := round(y/32768*cScreenHeight)
   483 end;
   482 end;
   484 
   483 
   485 function isOnCrosshair(finger: TTouch_Data): boolean;
   484 function isOnCrosshair(finger: TTouch_Data): boolean;
   486 var
   485 var
   487     x,y : hwFloat;
   486     x,y : LongInt;
   488 begin
   487 begin
   489     x := _0;//avoid compiler hint
   488     x := 0;//avoid compiler hint
   490     y := _0;
   489     y := 0;
   491     convertToFingerCoord(x, y, int2hwFloat(CrosshairX), int2hwFloat(CrosshairY));
   490     convertToFingerCoord(x, y, CrosshairX, CrosshairY);
   492     isOnCrosshair:= Distance(int2hwFloat(finger.x)-x, int2hwFloat(finger.y)-y) < _50;
   491     isOnCrosshair:= sqrt(sqr(finger.x-x) + sqr(finger.y-y)) < 50;
   493 end;
   492 end;
   494 
   493 
   495 function isOnCurrentHog(finger: TTouch_Data): boolean;
   494 function isOnCurrentHog(finger: TTouch_Data): boolean;
   496 var
   495 var
   497     x,y : hwFloat;
   496     x,y : LongInt;
   498 begin
   497 begin
   499     x := _0;
   498     x := 0;
   500     y := _0;
   499     y := 0;
   501     convertToFingerCoord(x,y, CurrentHedgehog^.Gear^.X, CurrentHedgehog^.Gear^.Y);
   500     convertToFingerCoord(x,y, hwRound(CurrentHedgehog^.Gear^.X), hwRound(CurrentHedgehog^.Gear^.Y));
   502     isOnCurrentHog := Distance(int2hwFloat(finger.X)-x, int2hwFloat(finger.Y)-y) < _50;
   501     isOnCurrentHog := sqrt(sqr(finger.X-x) + sqr(finger.Y-y)) < 50;
   503 end;
   502 end;
   504 
   503 
   505 procedure convertToFingerCoord(var x,y : hwFloat; oldX, oldY: hwFloat);
   504 procedure convertToFingerCoord(var x,y : LongInt; oldX, oldY: LongInt);
   506 begin
   505 begin
   507     x := oldX + int2hwFloat(WorldDx);
   506     x := oldX + WorldDx;
   508     y := int2hwFloat(cScreenHeight) - (oldY + int2hwFloat(WorldDy));
   507     y := cScreenHeight - (oldY + WorldDy);
   509 end;
   508 end;
   510 
   509 
   511 procedure convertToWorldCoord(var x,y: hwFloat; finger: TTouch_Data);
   510 procedure convertToWorldCoord(var x,y: LongInt; finger: TTouch_Data);
   512 begin
   511 begin
   513 //if x <> nil then 
   512 //if x <> nil then 
   514     x := int2hwFloat((finger.x-WorldDx));
   513     x := finger.x-WorldDx;
   515 //if y <> nil then 
   514 //if y <> nil then 
   516     y := int2hwFloat((cScreenHeight - finger.y)-WorldDy);
   515     y := (cScreenHeight - finger.y)-WorldDy;
   517 end;
   516 end;
   518 
   517 
   519 //Method to calculate the distance this finger has moved since the downEvent
   518 //Method to calculate the distance this finger has moved since the downEvent
   520 function fingerHasMoved(finger: TTouch_Data): boolean;
   519 function fingerHasMoved(finger: TTouch_Data): boolean;
   521 begin
   520 begin
   522     fingerHasMoved := trunc(sqrt(Power(finger.X-finger.historicalX,2) + Power(finger.y-finger.historicalY, 2))) > 330;
   521     fingerHasMoved := trunc(sqrt(Power(finger.X-finger.historicalX,2) + Power(finger.y-finger.historicalY, 2))) > 330;
   523 end;
   522 end;
   524 
   523 
   525 function calculateDelta(finger1, finger2: TTouch_Data): hwFloat; inline;
   524 function calculateDelta(finger1, finger2: TTouch_Data): LongInt; inline;
   526 begin
   525 begin
   527     calculateDelta := DistanceI(finger2.x-finger1.x, finger2.y-finger1.y);
   526     calculateDelta := Round(sqrt(sqr(finger2.x-finger1.x) + sqr(finger2.y-finger1.y)));
   528 end;
   527 end;
   529 
   528 
   530 // Under the premise that all pointer ids in pointerIds:TSDL_FingerId are packed to the far left.
   529 // Under the premise that all pointer ids in pointerIds:TSDL_FingerId are packed to the far left.
   531 // If the pointer to be ignored is not pointerIds[0] the second must be there
   530 // If the pointer to be ignored is not pointerIds[0] the second must be there
   532 function getSecondFinger(finger: TTouch_Data): PTouch_Data;
   531 function getSecondFinger(finger: TTouch_Data): PTouch_Data;