hedgewars/uTouch.pas
changeset 6712 c582869fbe7f
parent 6707 07c8704b6b08
child 6714 f2f93017132c
equal deleted inserted replaced
6710:42504695122d 6712:c582869fbe7f
    20 
    20 
    21 unit uTouch;
    21 unit uTouch;
    22 
    22 
    23 interface
    23 interface
    24 
    24 
    25 uses sysutils, uConsole, uVariables, SDLh, uFloat, uConsts, uIO, GLUnit, uTypes;
    25 uses sysutils, uConsole, uVariables, SDLh, uFloat, uConsts, uCommands, uIO, GLUnit, uTypes;
    26 
    26 
    27 
    27 
    28 procedure initModule;
    28 procedure initModule;
    29 
    29 
    30 procedure ProcessTouch;
    30 procedure ProcessTouch;
    38 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data;
    38 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data;
    39 function updateFinger(x,y,dx,dy: Longword; id: TSDL_FingerId): PTouch_Data;
    39 function updateFinger(x,y,dx,dy: Longword; id: TSDL_FingerId): PTouch_Data;
    40 procedure deleteFinger(id: TSDL_FingerId);
    40 procedure deleteFinger(id: TSDL_FingerId);
    41 procedure onTouchClick(finger: TTouch_Data);
    41 procedure onTouchClick(finger: TTouch_Data);
    42 procedure onTouchDoubleClick(finger: TTouch_Data);
    42 procedure onTouchDoubleClick(finger: TTouch_Data);
       
    43 procedure onTouchLongClick(finger: TTouch_Data);
    43 
    44 
    44 function findFinger(id: TSDL_FingerId): PTouch_Data;
    45 function findFinger(id: TSDL_FingerId): PTouch_Data;
    45 procedure aim(finger: TTouch_Data);
    46 procedure aim(finger: TTouch_Data);
    46 function isOnCrosshair(finger: TTouch_Data): boolean;
    47 function isOnCrosshair(finger: TTouch_Data): boolean;
    47 function isOnCurrentHog(finger: TTouch_Data): boolean;
    48 function isOnCurrentHog(finger: TTouch_Data): boolean;
    54 function isOnRect(x,y,w,h: LongInt; finger: TTouch_Data): boolean;
    55 function isOnRect(x,y,w,h: LongInt; finger: TTouch_Data): boolean;
    55 procedure printFinger(finger: TTouch_Data);
    56 procedure printFinger(finger: TTouch_Data);
    56 implementation
    57 implementation
    57 
    58 
    58 const
    59 const
    59     clicktime = 200;
    60     clickTime = 200;
       
    61     longClickTime = 400;
    60     nilFingerId = High(TSDL_FingerId);
    62     nilFingerId = High(TSDL_FingerId);
    61 
    63 
    62 var
    64 var
    63     pointerCount : Longword;
    65     pointerCount : Longword;
    64     fingers: array of TTouch_Data;
    66     fingers: array of TTouch_Data;
   130      moveCursor:= false;
   132      moveCursor:= false;
   131      finger^.pressedWidget:= @arrowDown;
   133      finger^.pressedWidget:= @arrowDown;
   132      exit;
   134      exit;
   133      end;
   135      end;
   134 
   136 
   135 if isOnRect(backjump.active, finger^) then
       
   136      begin
       
   137      enterKey:= true;
       
   138      moveCursor:= false;
       
   139      finger^.pressedWidget:= @backjump;
       
   140      exit;
       
   141      end;
       
   142 if isOnRect(forwardjump.active, finger^) then
       
   143      begin
       
   144      backspaceKey:= true;
       
   145      moveCursor:= false;
       
   146      finger^.pressedWidget:= @forwardjump;
       
   147      exit;
       
   148      end;
       
   149 if isOnRect(pauseButton.active, finger^) then
   137 if isOnRect(pauseButton.active, finger^) then
   150      begin
   138      begin
   151      isPaused:= not isPaused;
   139      isPaused:= not isPaused;
   152      moveCursor:= false;
   140      moveCursor:= false;
   153      finger^.pressedWidget:= @pauseButton;
   141      finger^.pressedWidget:= @pauseButton;
   218 {$IFDEF USE_TOUCH_INTERFACE}
   206 {$IFDEF USE_TOUCH_INTERFACE}
   219 x := x;
   207 x := x;
   220 y := y;
   208 y := y;
   221 finger:= updateFinger(x,y,0,0,pointerId);
   209 finger:= updateFinger(x,y,0,0,pointerId);
   222 //Check for onTouchClick event
   210 //Check for onTouchClick event
   223 if ((RealTicks - finger^.timeSinceDown) < clickTime) AND not(fingerHasMoved(finger^)) then
   211 if not(fingerHasMoved(finger^)) then
   224     onTouchClick(finger^);
   212     if (RealTicks - finger^.timeSinceDown) < clickTime then
   225 WriteToConsole(Format('%d', [buttonsDown]));
   213         onTouchClick(finger^)
       
   214     else
       
   215         onTouchLongClick(finger^);
   226 
   216 
   227 if aimingCrosshair then
   217 if aimingCrosshair then
   228     begin
   218     begin
   229     aimingCrosshair:= false;
   219     aimingCrosshair:= false;
   230     upKey:= false;
   220     upKey:= false;
   249     if widget = @arrowDown then
   239     if widget = @arrowDown then
   250         downKey:= false;
   240         downKey:= false;
   251 
   241 
   252     if widget = @fireButton then
   242     if widget = @fireButton then
   253         spaceKey:= false;
   243         spaceKey:= false;
   254 
       
   255     if widget = @backjump then
       
   256         enterKey:= false;
       
   257 
       
   258     if widget = @forwardjump then
       
   259         backspaceKey:= false;
       
   260     end;
   244     end;
   261  
   245  
   262 deleteFinger(pointerId);
   246 deleteFinger(pointerId);
   263 {$ENDIF}
   247 {$ENDIF}
   264 end;
   248 end;
   266 procedure onTouchDoubleClick(finger: TTouch_Data);
   250 procedure onTouchDoubleClick(finger: TTouch_Data);
   267 begin
   251 begin
   268 finger := finger;//avoid compiler hint
   252 finger := finger;//avoid compiler hint
   269 end;
   253 end;
   270 
   254 
       
   255 procedure onTouchLongClick(finger: TTouch_Data);
       
   256 begin
       
   257 if isOnRect(backjump.active, finger) then
       
   258     begin
       
   259     ParseCommand('ljump', (CurrentTeam <> nil) and not(CurrentTeam^.ExtDriven) and (CurrentHedgehog^.BotLevel=0));
       
   260     if (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) and (ReadyTimeLeft > 1) then
       
   261         ParseCommand('gencmd R', true);
       
   262     exit;
       
   263     end;
       
   264 end;
       
   265 
   271 procedure onTouchClick(finger: TTouch_Data);
   266 procedure onTouchClick(finger: TTouch_Data);
   272 begin
   267 begin
   273 if (RealTicks - timeSinceClick < 300) and (sqrt(sqr(finger.X-xTouchClick) + sqr(finger.Y-yTouchClick)) < 30) then
   268 //if (RealTicks - timeSinceClick < 300) and (sqrt(sqr(finger.X-xTouchClick) + sqr(finger.Y-yTouchClick)) < 30) then
   274     begin
   269 //    begin
   275     onTouchDoubleClick(finger);
   270 //    onTouchDoubleClick(finger);
   276     timeSinceClick:= 0;//we make an assumption there won't be an 'click' in the first 300 ticks(milliseconds) 
   271 //    timeSinceClick:= 0;//we make an assumption there won't be an 'click' in the first 300 ticks(milliseconds) 
   277     exit; 
   272 //    exit; 
   278     end;
   273 //    end;
   279 
   274 
   280 xTouchClick:= finger.x;
   275 xTouchClick:= finger.x;
   281 yTouchClick:= finger.y;
   276 yTouchClick:= finger.y;
   282 timeSinceClick:= RealTicks;
   277 timeSinceClick:= RealTicks;
   283 
   278 
   296 
   291 
   297 
   292 
   298 if isOnCurrentHog(finger) then
   293 if isOnCurrentHog(finger) then
   299     begin
   294     begin
   300     bShowAmmoMenu := true;
   295     bShowAmmoMenu := true;
       
   296     exit;
       
   297     end;
       
   298 
       
   299 if isOnRect(backjump.active, finger) then
       
   300     begin
       
   301     ParseCommand('hjump', (CurrentTeam <> nil) and not(CurrentTeam^.ExtDriven) and (CurrentHedgehog^.BotLevel=0));
       
   302     if (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) and (ReadyTimeLeft > 1) then
       
   303         ParseCommand('gencmd R', true);
   301     exit;
   304     exit;
   302     end;
   305     end;
   303 end;
   306 end;
   304 
   307 
   305 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data;
   308 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data;