hedgewars/uTouch.pas
changeset 6703 abe2abfe9de2
parent 6702 37c56511df0c
child 6704 77c93096d055
equal deleted inserted replaced
6702:37c56511df0c 6703:abe2abfe9de2
    49 procedure convertToFingerCoord(var x,y: LongInt; oldX, oldY: LongInt);
    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): LongInt;
    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 function isOnRect(x,y,w,h: LongInt; finger: TTouch_Data): boolean;
    54 procedure printFinger(finger: TTouch_Data);
    55 procedure printFinger(finger: TTouch_Data);
    55 implementation
    56 implementation
    56 
    57 
    57 const
    58 const
    58     clicktime = 200;
    59     clicktime = 200;
    70     //Pinch to zoom 
    71     //Pinch to zoom 
    71     pinchSize : LongInt;
    72     pinchSize : LongInt;
    72     baseZoomValue: GLFloat;
    73     baseZoomValue: GLFloat;
    73 
    74 
    74     //aiming
    75     //aiming
    75     aiming: boolean;
    76     aimingCrosshair: boolean;
    76     aimingUp, aimingDown: boolean; 
    77     aimingUp, aimingDown: boolean; 
    77     targetAngle: LongInt;
    78     targetAngle: LongInt;
    78 
    79 
    79     buttonsDown: Longword;
    80     buttonsDown: Longword;
    80 
    81 
    82 var 
    83 var 
    83     finger: PTouch_Data;
    84     finger: PTouch_Data;
    84 begin
    85 begin
    85 {$IFDEF USE_TOUCH_INTERFACE}
    86 {$IFDEF USE_TOUCH_INTERFACE}
    86 finger := addFinger(x,y,pointerId);
    87 finger := addFinger(x,y,pointerId);
       
    88 
       
    89 inc(buttonsDown);//inc buttonsDown, if we don't see a button down we'll dec it
       
    90 
    87 if isOnCrosshair(finger^) then
    91 if isOnCrosshair(finger^) then
    88 begin
    92 begin
    89     aiming:= true;
    93     aimingCrosshair:= true;
    90     aim(finger^);
    94     aim(finger^);
       
    95     moveCursor:= false;
    91     exit;
    96     exit;
    92 end;
    97 end;
    93 
    98 
    94 inc(buttonsDown);//inc buttonsDown, if we don't see a button down we'll dec it
       
    95 if isOnRect(fireButton.active, finger^) then
    99 if isOnRect(fireButton.active, finger^) then
    96     begin
   100     begin
    97     spaceKey:= true;
   101     spaceKey:= true;
    98     moveCursor:= false;
   102     moveCursor:= false;
    99     finger^.pressedWidget:= @fireButton;
   103     finger^.pressedWidget:= @fireButton;
   156 if buttonsDown = 0 then
   160 if buttonsDown = 0 then
   157     begin
   161     begin
   158     moveCursor:= true;
   162     moveCursor:= true;
   159     if pointerCount = 2 then
   163     if pointerCount = 2 then
   160         begin
   164         begin
   161         aiming:= false;
       
   162         moveCursor:= false;
   165         moveCursor:= false;
   163         pinchSize := calculateDelta(finger^, getSecondFinger(finger^)^);
   166         pinchSize := calculateDelta(finger^, getSecondFinger(finger^)^);
   164         baseZoomValue := ZoomValue
   167         baseZoomValue := ZoomValue
   165     end;
   168     end;
   166 end;
   169 end;
   187             CursorPoint.Y := CursorPoint.Y - finger^.dy;
   190             CursorPoint.Y := CursorPoint.Y - finger^.dy;
   188         end;
   191         end;
   189         exit //todo change into switch rather than ugly ifs
   192         exit //todo change into switch rather than ugly ifs
   190     end;
   193     end;
   191     
   194     
   192 if aiming then 
   195 if aimingCrosshair then 
   193     begin
   196     begin
   194         aim(finger^);
   197         aim(finger^);
   195         exit
   198         exit
   196     end;
   199     end;
   197 
   200 
   214     finger: PTouch_Data;
   217     finger: PTouch_Data;
   215     widget: POnScreenWidget;
   218     widget: POnScreenWidget;
   216 begin
   219 begin
   217 x := x;
   220 x := x;
   218 y := y;
   221 y := y;
   219 aiming:= false;
       
   220 finger:= updateFinger(x,y,0,0,pointerId);
   222 finger:= updateFinger(x,y,0,0,pointerId);
   221 //Check for onTouchClick event
   223 //Check for onTouchClick event
   222 if ((RealTicks - finger^.timeSinceDown) < clickTime) AND not(fingerHasMoved(finger^)) then
   224 if ((RealTicks - finger^.timeSinceDown) < clickTime) AND not(fingerHasMoved(finger^)) then
   223     onTouchClick(finger^);
   225     onTouchClick(finger^);
   224 WriteToConsole(Format('%d', [buttonsDown]));
   226 WriteToConsole(Format('%d', [buttonsDown]));
       
   227 
       
   228 if aimingCrosshair then
       
   229     begin
       
   230     aimingCrosshair:= false;
       
   231     dec(buttonsDown);
       
   232     end;
   225 
   233 
   226 widget:= finger^.pressedWidget;
   234 widget:= finger^.pressedWidget;
   227 if (buttonsDown > 0) and (widget <> nil) then
   235 if (buttonsDown > 0) and (widget <> nil) then
   228     begin
   236     begin
   229     dec(buttonsDown);
   237     dec(buttonsDown);
   368 procedure ProcessTouch;
   376 procedure ProcessTouch;
   369 var
   377 var
   370     deltaAngle: LongInt;
   378     deltaAngle: LongInt;
   371 begin
   379 begin
   372 invertCursor := not(bShowAmmoMenu);
   380 invertCursor := not(bShowAmmoMenu);
   373 if aiming then
   381 if aimingCrosshair or aimingUp or aimingDown then
   374     if CurrentHedgehog^.Gear <> nil then
   382     if CurrentHedgehog^.Gear <> nil then
   375         begin
   383         begin
   376         deltaAngle:= CurrentHedgehog^.Gear^.Angle - targetAngle;
   384         deltaAngle:= CurrentHedgehog^.Gear^.Angle - targetAngle;
   377         if (deltaAngle = 0) then 
   385         if (deltaAngle = 0) then 
   378             begin
   386             begin
   451 
   459 
   452         convertToWorldCoord(touchX, touchY, finger);
   460         convertToWorldCoord(touchX, touchY, finger);
   453         deltaX := abs(TouchX-HogX);
   461         deltaX := abs(TouchX-HogX);
   454         deltaY := TouchY-HogY;
   462         deltaY := TouchY-HogY;
   455         
   463         
   456         targetAngle:= (Round(DeltaY / sqrt(sqr(deltaX) + sqr(deltaY))) + 2048) div 2;
   464         targetAngle:= (Round(DeltaY / sqrt(sqr(deltaX) + sqr(deltaY)) * 2048) + 2048) div 2;
   457         end; //if CurrentHedgehog^.Gear <> nil
   465         end; //if CurrentHedgehog^.Gear <> nil
   458 end;
   466 end;
   459 
   467 
   460 // These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system:
   468 // 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;
   469 // - 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;
   486     x,y : LongInt;
   494     x,y : LongInt;
   487 begin
   495 begin
   488     x := 0;//avoid compiler hint
   496     x := 0;//avoid compiler hint
   489     y := 0;
   497     y := 0;
   490     convertToFingerCoord(x, y, CrosshairX, CrosshairY);
   498     convertToFingerCoord(x, y, CrosshairX, CrosshairY);
   491     isOnCrosshair:= sqrt(sqr(finger.x-x) + sqr(finger.y-y)) < 50;
   499   isOnCrosshair:= sqrt(sqr(finger.x-x) + sqr(finger.y-y)) < 50;
       
   500 //    isOnCrosshair:= isOnRect(x-24, y-24, 48, 48, finger);
   492 end;
   501 end;
   493 
   502 
   494 function isOnCurrentHog(finger: TTouch_Data): boolean;
   503 function isOnCurrentHog(finger: TTouch_Data): boolean;
   495 var
   504 var
   496     x,y : LongInt;
   505     x,y : LongInt;
   536         getSecondFinger := @fingers[0];
   545         getSecondFinger := @fingers[0];
   537 end;
   546 end;
   538 
   547 
   539 function isOnRect(rect: TSDL_Rect; finger: TTouch_Data): boolean;
   548 function isOnRect(rect: TSDL_Rect; finger: TTouch_Data): boolean;
   540 begin
   549 begin
   541     isOnRect:= (finger.x > rect.x)   and
   550     isOnRect:= isOnRect(rect.x, rect.y, rect.w, rect.h, finger);
   542                (finger.x < rect.x + rect.w) and
   551 end;
   543                (cScreenHeight - finger.y > rect.y) and
   552 
   544                (cScreenHeight - finger.y < rect.y + rect.h);
   553 function isOnRect(x,y,w,h: LongInt; finger: TTouch_Data): boolean;
       
   554 begin
       
   555     isOnRect:= (finger.x > x)   and
       
   556                (finger.x < x + w) and
       
   557                (cScreenHeight - finger.y > y) and
       
   558                (cScreenHeight - finger.y < y + h);
   545 end;
   559 end;
   546 
   560 
   547 procedure printFinger(finger: TTouch_Data);
   561 procedure printFinger(finger: TTouch_Data);
   548 begin
   562 begin
   549     WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));
   563     WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));