hedgewars/uTouch.pas
changeset 9379 1bcfcaf0926e
parent 9377 48ab6dea8d2f
child 9380 563ab624522c
equal deleted inserted replaced
9378:2be457289e60 9379:1bcfcaf0926e
    29 procedure freeModule;
    29 procedure freeModule;
    30 
    30 
    31 procedure ProcessTouch;
    31 procedure ProcessTouch;
    32 procedure NewTurnBeginning;
    32 procedure NewTurnBeginning;
    33 
    33 
    34 procedure onTouchDown(x,y: Longword; pointerId: TSDL_FingerId);
    34 procedure onTouchDown(x, y: Single; pointerId: TSDL_FingerId);
    35 procedure onTouchMotion(x,y: Longword; dx,dy: LongInt; pointerId: TSDL_FingerId);
    35 procedure onTouchMotion(x, y, dx, dy: Single; pointerId: TSDL_FingerId);
    36 procedure onTouchUp(x,y: Longword; pointerId: TSDL_FingerId);
    36 procedure onTouchUp(x, y: Single; pointerId: TSDL_FingerId);
       
    37 
    37 function convertToCursorX(x: LongInt): LongInt;
    38 function convertToCursorX(x: LongInt): LongInt;
    38 function convertToCursorY(y: LongInt): LongInt;
    39 function convertToCursorY(y: LongInt): LongInt;
    39 function convertToCursorDeltaX(x: LongInt): LongInt;
    40 
    40 function convertToCursorDeltaY(y: LongInt): LongInt;
       
    41 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data;
    41 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data;
    42 function updateFinger(x,y,dx,dy: Longword; id: TSDL_FingerId): PTouch_Data;
    42 function updateFinger(x,y,dx,dy: Longword; id: TSDL_FingerId): PTouch_Data;
    43 procedure deleteFinger(id: TSDL_FingerId);
    43 procedure deleteFinger(id: TSDL_FingerId);
       
    44 
    44 procedure onTouchClick(finger: TTouch_Data);
    45 procedure onTouchClick(finger: TTouch_Data);
    45 procedure onTouchDoubleClick(finger: TTouch_Data);
    46 procedure onTouchDoubleClick(finger: TTouch_Data);
    46 procedure onTouchLongClick(finger: TTouch_Data);
    47 procedure onTouchLongClick(finger: TTouch_Data);
    47 
    48 
    48 function findFinger(id: TSDL_FingerId): PTouch_Data;
    49 function findFinger(id: TSDL_FingerId): PTouch_Data;
    60 procedure printFinger(finger: TTouch_Data);
    61 procedure printFinger(finger: TTouch_Data);
    61 implementation
    62 implementation
    62 
    63 
    63 const
    64 const
    64     clickTime = 200;
    65     clickTime = 200;
    65     nilFingerId = High(TSDL_FingerId);
       
    66     baseRectSize = 96;
    66     baseRectSize = 96;
    67 
    67 
    68 var
    68 var
    69     rectSize, halfRectSize: LongInt;
    69     rectSize, halfRectSize: LongInt;
    70 
    70 
    74     invertCursor : boolean;
    74     invertCursor : boolean;
    75 
    75 
    76     xTouchClick,yTouchClick : LongInt;
    76     xTouchClick,yTouchClick : LongInt;
    77     timeSinceClick : Longword;
    77     timeSinceClick : Longword;
    78 
    78 
    79     //Pinch to zoom 
    79     //Pinch to zoom
    80     pinchSize : LongInt;
    80     pinchSize : LongInt;
    81     baseZoomValue: GLFloat;
    81     baseZoomValue: GLFloat;
    82 
    82 
    83     //aiming
    83     //aiming
    84     aimingCrosshair: boolean;
    84     aimingCrosshair: boolean;
    85     aimingUp, aimingDown: boolean; 
    85     aimingUp, aimingDown: boolean;
    86     targetAngle: LongInt;
    86     targetAngle: LongInt;
    87 
    87 
    88     buttonsDown: Longword;
    88     buttonsDown: Longword;
    89     targetting, targetted: boolean; //true when targetting an airstrike or the like
    89     targetting, targetted: boolean; //true when targetting an airstrike or the like
    90 
    90 
    91 procedure onTouchDown(x,y: Longword; pointerId: TSDL_FingerId);
    91 procedure onTouchDown(x, y: Single; pointerId: TSDL_FingerId);
    92 var 
    92 var
    93     finger: PTouch_Data;
    93     finger: PTouch_Data;
       
    94     xr, yr: LongWord;
    94 begin
    95 begin
    95 {$IFDEF USE_TOUCH_INTERFACE}
    96 {$IFDEF USE_TOUCH_INTERFACE}
    96 finger := addFinger(x,y,pointerId);
    97 xr:= round(x * cScreenWidth);
       
    98 yr:= round(y * cScreenHeight);
       
    99 
       
   100 finger:= addFinger(xr, yr, pointerId);
    97 
   101 
    98 inc(buttonsDown);//inc buttonsDown, if we don't see a button down we'll dec it
   102 inc(buttonsDown);//inc buttonsDown, if we don't see a button down we'll dec it
    99 
   103 
   100 if isOnCrosshair(finger^) then
   104 if isOnCrosshair(finger^) then
   101 begin
   105 begin
   159         begin
   163         begin
   160         if Ammoz[CurrentHedgehog^.CurAmmoType].Ammo.Propz and ammoprop_Timerable <> 0 then
   164         if Ammoz[CurrentHedgehog^.CurAmmoType].Ammo.Propz and ammoprop_Timerable <> 0 then
   161             ParseTeamCommand('/timer ' + inttostr((GetCurAmmoEntry(CurrentHedgeHog^)^.Timer div 1000) mod 5 + 1));
   165             ParseTeamCommand('/timer ' + inttostr((GetCurAmmoEntry(CurrentHedgeHog^)^.Timer div 1000) mod 5 + 1));
   162         end;
   166         end;
   163     exit;
   167     exit;
   164     end; 
   168     end;
   165 dec(buttonsDown);//no buttonsDown, undo the inc() above
   169 dec(buttonsDown);//no buttonsDown, undo the inc() above
   166 if buttonsDown = 0 then
   170 if buttonsDown = 0 then
   167     begin
   171     begin
   168     moveCursor:= true;
   172     moveCursor:= true;
   169     case pointerCount of
   173     case pointerCount of
   178         end;
   182         end;
   179     end;
   183     end;
   180 {$ENDIF}
   184 {$ENDIF}
   181 end;
   185 end;
   182 
   186 
   183 procedure onTouchMotion(x,y: Longword;dx,dy: LongInt; pointerId: TSDL_FingerId);
   187 procedure onTouchMotion(x, y, dx, dy: Single; pointerId: TSDL_FingerId);
   184 var
   188 var
   185     finger, secondFinger: PTouch_Data;
   189     finger, secondFinger: PTouch_Data;
   186     currentPinchDelta, zoom : single;
   190     currentPinchDelta, zoom : Single;
   187 begin
   191     xr, yr, dxr, dyr: LongWord;
   188 finger:= updateFinger(x,y,dx,dy,pointerId);
   192 begin
       
   193 xr:= round(x * cScreenWidth);
       
   194 yr:= round(y * cScreenHeight);
       
   195 dxr:= round(dx * cScreenWidth);
       
   196 dyr:= round(dy * cScreenHeight);
       
   197 
       
   198 finger:= updateFinger(xr, yr, dxr, dyr, pointerId);
       
   199 if finger = nil then
       
   200     exit;
   189 
   201 
   190 if moveCursor then
   202 if moveCursor then
   191     begin
   203     begin
   192         if invertCursor then
   204         if invertCursor then
   193         begin
   205         begin
   199             CursorPoint.X := CursorPoint.X + finger^.dx;
   211             CursorPoint.X := CursorPoint.X + finger^.dx;
   200             CursorPoint.Y := CursorPoint.Y - finger^.dy;
   212             CursorPoint.Y := CursorPoint.Y - finger^.dy;
   201         end;
   213         end;
   202         exit //todo change into switch rather than ugly ifs
   214         exit //todo change into switch rather than ugly ifs
   203     end;
   215     end;
   204     
   216 
   205 if aimingCrosshair then 
   217 if aimingCrosshair then
   206     begin
   218     begin
   207         aim(finger^);
   219         aim(finger^);
   208         exit
   220         exit
   209     end;
   221     end;
   210 
   222 
   220            ZoomValue := cMinZoomLevel;
   232            ZoomValue := cMinZoomLevel;
   221     end;
   233     end;
   222 
   234 
   223 end;
   235 end;
   224 
   236 
   225 procedure onTouchUp(x,y: Longword; pointerId: TSDL_FingerId);
   237 procedure onTouchUp(x,y: Single; pointerId: TSDL_FingerId);
   226 var
   238 var
   227     finger: PTouch_Data;
   239     finger: PTouch_Data;
   228     widget: POnScreenWidget;
   240     widget: POnScreenWidget;
       
   241     xr, yr: LongWord;
   229 begin
   242 begin
   230 {$IFDEF USE_TOUCH_INTERFACE}
   243 {$IFDEF USE_TOUCH_INTERFACE}
   231 x := x;
   244 xr:= round(x * cScreenWidth);
   232 y := y;
   245 yr:= round(y * cScreenHeight);
   233 finger:= updateFinger(x,y,0,0,pointerId);
   246 
       
   247 finger:= updateFinger(xr, yr, 0, 0, pointerId);
       
   248 if finger = nil then
       
   249     exit;
       
   250 
   234 //Check for onTouchClick event
   251 //Check for onTouchClick event
   235 if not(fingerHasMoved(finger^)) then
   252 if not(fingerHasMoved(finger^)) then
   236     begin
   253     begin
   237     if (RealTicks - finger^.timeSinceDown) < clickTime then
   254     if (RealTicks - finger^.timeSinceDown) < clickTime then
   238         onTouchClick(finger^)
   255         onTouchClick(finger^)
   239     else
   256     else
   240             onTouchLongClick(finger^);
   257         onTouchLongClick(finger^);
   241     end;
   258     end;
   242 
   259 
   243 if aimingCrosshair then
   260 if aimingCrosshair then
   244     begin
   261     begin
   245     aimingCrosshair:= false;
   262     aimingCrosshair:= false;
   250 
   267 
   251 widget:= finger^.pressedWidget;
   268 widget:= finger^.pressedWidget;
   252 if (buttonsDown > 0) and (widget <> nil) then
   269 if (buttonsDown > 0) and (widget <> nil) then
   253     begin
   270     begin
   254     dec(buttonsDown);
   271     dec(buttonsDown);
   255     
   272 
   256     if widget = @arrowLeft then
   273     if widget = @arrowLeft then
   257         ParseTeamCommand('-left');
   274         ParseTeamCommand('-left');
   258     
   275 
   259     if widget = @arrowRight then
   276     if widget = @arrowRight then
   260         ParseTeamCommand('-right');
   277         ParseTeamCommand('-right');
   261 
   278 
   262     if widget = @arrowUp then
   279     if widget = @arrowUp then
   263         ParseTeamCommand('-up');
   280         ParseTeamCommand('-up');
   265     if widget = @arrowDown then
   282     if widget = @arrowDown then
   266         ParseTeamCommand('-down');
   283         ParseTeamCommand('-down');
   267 
   284 
   268     if widget = @fireButton then
   285     if widget = @fireButton then
   269         ParseTeamCommand('-attack');
   286         ParseTeamCommand('-attack');
   270     
   287 
   271     if widget = @utilityWidget then
   288     if widget = @utilityWidget then
   272         if (CurrentHedgehog <> nil)then
   289         if (CurrentHedgehog <> nil)then
   273             if(Ammoz[CurrentHedgehog^.CurAmmoType].Ammo.Propz and ammoprop_NeedTarget <> 0)then
   290             if(Ammoz[CurrentHedgehog^.CurAmmoType].Ammo.Propz and ammoprop_NeedTarget <> 0)then
   274                 begin
   291                 begin
   275                 ParseTeamCommand('put');
   292                 ParseTeamCommand('put');
   277                 end
   294                 end
   278             else if (CurAmmoGear <> nil) and (CurAmmoGear^.AmmoType = amSwitch) then
   295             else if (CurAmmoGear <> nil) and (CurAmmoGear^.AmmoType = amSwitch) then
   279                 ParseTeamCommand('switch')
   296                 ParseTeamCommand('switch')
   280             else WriteLnToConsole(inttostr(ord(Ammoz[CurrentHedgehog^.CurAmmoType].NameId)) + ' ' + inttostr(ord(sidSwitch)));
   297             else WriteLnToConsole(inttostr(ord(Ammoz[CurrentHedgehog^.CurAmmoType].NameId)) + ' ' + inttostr(ord(sidSwitch)));
   281     end;
   298     end;
   282         
   299 
   283 if targetting then
   300 if targetting then
   284     AddCaption('Press the target button to mark the target', cWhiteColor, capgrpAmmoInfo);
   301     AddCaption('Press the target button to mark the target', cWhiteColor, capgrpAmmoInfo);
   285  
   302 
   286 deleteFinger(pointerId);
   303 deleteFinger(pointerId);
   287 {$ENDIF}
   304 {$ENDIF}
   288 end;
   305 end;
   289 
   306 
   290 procedure onTouchDoubleClick(finger: TTouch_Data);
   307 procedure onTouchDoubleClick(finger: TTouch_Data);
   307 procedure onTouchClick(finger: TTouch_Data);
   324 procedure onTouchClick(finger: TTouch_Data);
   308 begin
   325 begin
   309 //if (RealTicks - timeSinceClick < 300) and (sqrt(sqr(finger.X-xTouchClick) + sqr(finger.Y-yTouchClick)) < 30) then
   326 //if (RealTicks - timeSinceClick < 300) and (sqrt(sqr(finger.X-xTouchClick) + sqr(finger.Y-yTouchClick)) < 30) then
   310 //    begin
   327 //    begin
   311 //    onTouchDoubleClick(finger);
   328 //    onTouchDoubleClick(finger);
   312 //    timeSinceClick:= 0;//we make an assumption there won't be an 'click' in the first 300 ticks(milliseconds) 
   329 //    timeSinceClick:= 0;//we make an assumption there won't be an 'click' in the first 300 ticks(milliseconds)
   313 //    exit; 
   330 //    exit;
   314 //    end;
   331 //    end;
   315 
   332 
   316 xTouchClick:= finger.x;
   333 xTouchClick:= finger.x;
   317 yTouchClick:= finger.y;
   334 yTouchClick:= finger.y;
   318 timeSinceClick:= RealTicks;
   335 timeSinceClick:= RealTicks;
   319 
   336 
   320 if bShowAmmoMenu then
   337 if bShowAmmoMenu then
   321     begin 
   338     begin
   322     if isOnRect(AmmoRect, finger) then
   339     if isOnRect(AmmoRect, finger) then
   323         begin
   340         begin
   324         CursorPoint.X:= finger.x;
   341         CursorPoint.X:= finger.x;
   325         CursorPoint.Y:= finger.y;
   342         CursorPoint.Y:= finger.y;
   326         ParseTeamCommand('put'); 
   343         ParseTeamCommand('put');
   327         end
   344         end
   328     else
   345     else
   329         bShowAmmoMenu:= false;
   346         bShowAmmoMenu:= false;
   330     exit;
   347     exit;
   331     end;
   348     end;
   337     exit;
   354     exit;
   338     end;
   355     end;
   339 
   356 
   340 if isOnWidget(jumpWidget, finger) then
   357 if isOnWidget(jumpWidget, finger) then
   341     begin
   358     begin
   342     ParseTeamCommand('hjump');    
   359     ParseTeamCommand('hjump');
   343     exit;
   360     exit;
   344     end;
   361     end;
   345 {$ENDIF}
   362 {$ENDIF}
   346 end;
   363 end;
   347 
   364 
   348 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data;
   365 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data;
   349 var 
   366 var
   350     xCursor, yCursor, index : LongInt;
   367     xCursor, yCursor, index : LongInt;
   351 begin
   368 begin
   352     //Check array sizes
   369     //Check array sizes
   353     if length(fingers) < Integer(pointerCount) then 
   370     if length(fingers) < pointerCount then
   354     begin
   371     begin
   355         setLength(fingers, length(fingers)*2);
   372         setLength(fingers, pointerCount * 2);
   356         for index := length(fingers) div 2 to length(fingers) do
   373         WriteLnToConsole('allocated ' + inttostr(length(fingers)) + ' finger elements');
   357             fingers[index].id := nilFingerId;
   374     end;
   358     end;
   375 
   359     
       
   360     
       
   361     xCursor := convertToCursorX(x);
   376     xCursor := convertToCursorX(x);
   362     yCursor := convertToCursorY(y);
   377     yCursor := convertToCursorY(y);
   363     
   378 
   364     //on removing fingers, all fingers are moved to the left
   379     //on removing fingers, all fingers are moved to the left
   365     //with dynamic arrays being zero based, the new position of the finger is the old pointerCount
   380     //with dynamic arrays being zero based, the new position of the finger is the old pointerCount
   366     fingers[pointerCount].id := id;
   381     fingers[pointerCount].id := id;
   367     fingers[pointerCount].historicalX := xCursor;
   382     fingers[pointerCount].historicalX := xCursor;
   368     fingers[pointerCount].historicalY := yCursor;
   383     fingers[pointerCount].historicalY := yCursor;
   370     fingers[pointerCount].y := yCursor;
   385     fingers[pointerCount].y := yCursor;
   371     fingers[pointerCount].dx := 0;
   386     fingers[pointerCount].dx := 0;
   372     fingers[pointerCount].dy := 0;
   387     fingers[pointerCount].dy := 0;
   373     fingers[pointerCount].timeSinceDown:= RealTicks;
   388     fingers[pointerCount].timeSinceDown:= RealTicks;
   374     fingers[pointerCount].pressedWidget:= nil;
   389     fingers[pointerCount].pressedWidget:= nil;
   375  
   390 
   376     addFinger:= @fingers[pointerCount];
   391     addFinger:= @fingers[pointerCount];
   377     inc(pointerCount);
   392     inc(pointerCount);
   378 end;
   393 end;
   379 
   394 
   380 function updateFinger(x,y,dx,dy: Longword; id: TSDL_FingerId): PTouch_Data;
   395 function updateFinger(x, y, dx, dy: Longword; id: TSDL_FingerId): PTouch_Data;
   381 begin
   396 var finger : PTouch_Data;
   382    updateFinger:= findFinger(id);
   397 begin
   383 
   398     finger:= findFinger(id);
   384    updateFinger^.x:= convertToCursorX(x);
   399 
   385    updateFinger^.y:= convertToCursorY(y);
   400     if finger <> nil then
   386    updateFinger^.dx:= convertToCursorDeltaX(dx);
   401         begin
   387    updateFinger^.dy:= convertToCursorDeltaY(dy);
   402         finger^.x:= convertToCursorX(x);
       
   403         finger^.y:= convertToCursorY(y);
       
   404         finger^.dx:= dx;
       
   405         finger^.dy:= dy;
       
   406         end
       
   407     else
       
   408         WriteLnToConsole('finger ' + inttostr(id) + ' not found');
       
   409     updateFinger:= finger
   388 end;
   410 end;
   389 
   411 
   390 procedure deleteFinger(id: TSDL_FingerId);
   412 procedure deleteFinger(id: TSDL_FingerId);
   391 var
   413 var
   392     index : Longword;
   414     index : Longword;
   393 begin
   415 begin
   394     
   416 
   395     dec(pointerCount);
   417     dec(pointerCount);
   396     for index := 0 to pointerCount do
   418     for index := 0 to pointerCount do
   397     begin
   419     begin
   398         if fingers[index].id = id then
   420         if fingers[index].id = id then
   399         begin
   421         begin
   400  
   422 
   401             //put the last finger into the spot of the finger to be removed, 
   423             //put the last finger into the spot of the finger to be removed,
   402             //so that all fingers are packed to the far left
   424             //so that all fingers are packed to the far left
   403             if  pointerCount <> index then
   425             if  pointerCount <> index then
   404                 begin
   426                 begin
   405                 fingers[index].id := fingers[pointerCount].id;    
   427                 fingers[index].id := fingers[pointerCount].id;
   406                 fingers[index].x := fingers[pointerCount].x;    
   428                 fingers[index].x := fingers[pointerCount].x;
   407                 fingers[index].y := fingers[pointerCount].y;    
   429                 fingers[index].y := fingers[pointerCount].y;
   408                 fingers[index].historicalX := fingers[pointerCount].historicalX;    
   430                 fingers[index].historicalX := fingers[pointerCount].historicalX;
   409                 fingers[index].historicalY := fingers[pointerCount].historicalY;    
   431                 fingers[index].historicalY := fingers[pointerCount].historicalY;
   410                 fingers[index].timeSinceDown := fingers[pointerCount].timeSinceDown;
   432                 fingers[index].timeSinceDown := fingers[pointerCount].timeSinceDown;
   411 
   433 
   412                 fingers[pointerCount].id := nilFingerId;
   434                 fingers[pointerCount].id := 0;
   413             end
   435             end
   414         else fingers[index].id := nilFingerId;
   436         else fingers[index].id := 0;
   415             break;
   437             break;
   416         end;
   438         end;
   417     end;
   439     end;
   418 
   440 
   419 end;
   441 end;
   428 
   450 
   429 procedure ProcessTouch;
   451 procedure ProcessTouch;
   430 var
   452 var
   431     deltaAngle: LongInt;
   453     deltaAngle: LongInt;
   432 begin
   454 begin
   433 invertCursor := not(bShowAmmoMenu or targetting); 
   455 invertCursor := not(bShowAmmoMenu or targetting);
   434 if aimingCrosshair then
   456 if aimingCrosshair then
   435     if CurrentHedgehog^.Gear <> nil then
   457     if CurrentHedgehog^.Gear <> nil then
   436         begin
   458         begin
   437         deltaAngle:= CurrentHedgehog^.Gear^.Angle - targetAngle;
   459         deltaAngle:= CurrentHedgehog^.Gear^.Angle - targetAngle;
   438         if (deltaAngle > -5) and (deltaAngle < 5) then 
   460         if (deltaAngle > -5) and (deltaAngle < 5) then
   439             begin
   461             begin
   440                 if(aimingUp)then
   462                 if(aimingUp)then
   441                     begin
   463                     begin
   442                     aimingUp:= false;
   464                     aimingUp:= false;
   443                     ParseTeamCommand('-up');
   465                     ParseTeamCommand('-up');
   473                 if aimingUp then
   495                 if aimingUp then
   474                     begin
   496                     begin
   475                     aimingUp:= true;
   497                     aimingUp:= true;
   476                     ParseTeamCommand('+up');
   498                     ParseTeamCommand('+up');
   477                     end;
   499                     end;
   478                 end; 
   500                 end;
   479             end;
   501             end;
   480         end
   502         end
   481     else  
   503     else
   482         begin
   504         begin
   483         if aimingUp then
   505         if aimingUp then
   484             begin
   506             begin
   485             ParseTeamCommand('-up');
   507             ParseTeamCommand('-up');
   486             aimingUp:= false;
   508             aimingUp:= false;
   495 
   517 
   496 function findFinger(id: TSDL_FingerId): PTouch_Data;
   518 function findFinger(id: TSDL_FingerId): PTouch_Data;
   497 var
   519 var
   498     index: LongWord;
   520     index: LongWord;
   499 begin
   521 begin
   500     for index := 0 to High(fingers) do
   522     for index:= 0 to length(fingers) do
   501         if fingers[index].id = id then 
   523         if fingers[index].id = id then
   502             begin
   524             begin
   503             findFinger := @fingers[index];
   525             findFinger:= @fingers[index];
   504             break;
   526             exit;
   505             end;
   527             end;
       
   528     findFinger:= nil;
   506 end;
   529 end;
   507 
   530 
   508 procedure aim(finger: TTouch_Data);
   531 procedure aim(finger: TTouch_Data);
   509 var 
   532 var
   510     hogX, hogY, touchX, touchY, deltaX, deltaY: LongInt;
   533     hogX, hogY, touchX, touchY, deltaX, deltaY: LongInt;
   511 begin
   534 begin
   512     if CurrentHedgehog^.Gear <> nil then
   535     if CurrentHedgehog^.Gear <> nil then
   513         begin
   536         begin
   514         touchX := 0;//avoid compiler hint
   537         touchX := 0;//avoid compiler hint
   517         hogY := hwRound(CurrentHedgehog^.Gear^.Y);
   540         hogY := hwRound(CurrentHedgehog^.Gear^.Y);
   518 
   541 
   519         convertToWorldCoord(touchX, touchY, finger);
   542         convertToWorldCoord(touchX, touchY, finger);
   520         deltaX := abs(TouchX-HogX);
   543         deltaX := abs(TouchX-HogX);
   521         deltaY := TouchY-HogY;
   544         deltaY := TouchY-HogY;
   522         
   545 
   523         targetAngle:= (Round(DeltaY / sqrt(sqr(deltaX) + sqr(deltaY)) * 2048) + 2048) div 2;
   546         targetAngle:= (Round(DeltaY / sqrt(sqr(deltaX) + sqr(deltaY)) * 2048) + 2048) div 2;
   524         end; //if CurrentHedgehog^.Gear <> nil
   547         end; //if CurrentHedgehog^.Gear <> nil
   525 end;
   548 end;
   526 
   549 
   527 // These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system:
   550 // These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system:
   528 // - 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;
   551 // - the SDL coordinate system is proportional to the screen and values are normalized in the onTouch* functions
   529 // - the CursorPoint coordinate system goes from -cScreenWidth/2 to cScreenWidth/2 on the x axis
   552 // - the CursorPoint coordinate system goes from -cScreenWidth/2 to cScreenWidth/2 on the x axis
   530 //   and 0 to cScreenHeight on the x axis, (-cScreenWidth, cScreenHeight) being top left.
   553 //   and 0 to cScreenHeight on the x axis, (-cScreenWidth, cScreenHeight) being top left.
   531 function convertToCursorX(x: LongInt): LongInt;
   554 function convertToCursorX(x: LongInt): LongInt;
   532 begin
   555 begin
   533     convertToCursorX := round((x/32768)*cScreenWidth) - (cScreenWidth shr 1);
   556     convertToCursorX:= x - cScreenWidth shr 1;
   534 end;
   557 end;
   535 
   558 
   536 function convertToCursorY(y: LongInt): LongInt;
   559 function convertToCursorY(y: LongInt): LongInt;
   537 begin
   560 begin
   538     convertToCursorY := cScreenHeight - round((y/32768)*cScreenHeight)
   561     convertToCursorY:= cScreenHeight - y;
   539 end;
       
   540 
       
   541 function convertToCursorDeltaX(x: LongInt): LongInt;
       
   542 begin
       
   543     convertToCursorDeltaX := round(x/32768*cScreenWidth)
       
   544 end;
       
   545 
       
   546 function convertToCursorDeltaY(y: LongInt): LongInt;
       
   547 begin
       
   548     convertToCursorDeltaY := round(y/32768*cScreenHeight)
       
   549 end;
   562 end;
   550 
   563 
   551 function isOnCrosshair(finger: TTouch_Data): boolean;
   564 function isOnCrosshair(finger: TTouch_Data): boolean;
   552 var
   565 var
   553     x,y : LongInt;
   566     x, y: LongInt;
   554 begin
   567 begin
   555     x := 0;//avoid compiler hint
   568     x:= 0;
   556     y := 0;
   569     y:= 0;
   557     convertToFingerCoord(x, y, CrosshairX, CrosshairY);
   570     convertToFingerCoord(x, y, CrosshairX, CrosshairY);
   558     isOnCrosshair:= isOnRect((x-HalfRectSize), (y-HalfRectSize), RectSize, RectSize, finger);
   571     isOnCrosshair:= isOnRect(x - HalfRectSize, y - HalfRectSize, RectSize, RectSize, finger);
   559     printFinger(finger);
       
   560     WriteLnToConsole(inttostr(finger.x) + '   ' + inttostr(x));
       
   561     WriteLnToConsole(inttostr(x) + '  ' + inttostr(y) + '   ' + inttostr(10));
       
   562 end;
   572 end;
   563 
   573 
   564 function isOnCurrentHog(finger: TTouch_Data): boolean;
   574 function isOnCurrentHog(finger: TTouch_Data): boolean;
   565 var
   575 var
   566     x,y : LongInt;
   576     x, y: LongInt;
   567 begin
   577 begin
   568     x := 0;
   578     x:= 0;
   569     y := 0;
   579     y:= 0;
   570     convertToFingerCoord(x,y, hwRound(CurrentHedgehog^.Gear^.X), hwRound(CurrentHedgehog^.Gear^.Y));
   580     convertToFingerCoord(x, y, hwRound(CurrentHedgehog^.Gear^.X), hwRound(CurrentHedgehog^.Gear^.Y));
   571     isOnCurrentHog:= isOnRect((x-HalfRectSize), (y-HalfRectSize), RectSize, RectSize, finger);
   581     isOnCurrentHog:= isOnRect(x - HalfRectSize, y - HalfRectSize, RectSize, RectSize, finger);
   572 end;
   582 end;
   573 
   583 
   574 procedure convertToFingerCoord(var x,y : LongInt; oldX, oldY: LongInt);
   584 procedure convertToFingerCoord(var x, y : LongInt; oldX, oldY: LongInt);
   575 begin
   585 begin
   576     x := oldX + WorldDx;
   586     x := oldX + WorldDx;
   577     y := cScreenHeight - (oldY + WorldDy);
   587     y := cScreenHeight - oldY - WorldDy;
   578 end;
   588 end;
   579 
   589 
   580 procedure convertToWorldCoord(var x,y: LongInt; finger: TTouch_Data);
   590 procedure convertToWorldCoord(var x,y: LongInt; finger: TTouch_Data);
   581 begin
   591 begin
   582 //if x <> nil then 
   592     x := finger.x - WorldDx;
   583     x := finger.x-WorldDx;
   593     y := cScreenHeight - finger.y - WorldDy;
   584 //if y <> nil then 
       
   585     y := (cScreenHeight - finger.y)-WorldDy;
       
   586 end;
   594 end;
   587 
   595 
   588 //Method to calculate the distance this finger has moved since the downEvent
   596 //Method to calculate the distance this finger has moved since the downEvent
   589 function fingerHasMoved(finger: TTouch_Data): boolean;
   597 function fingerHasMoved(finger: TTouch_Data): boolean;
   590 begin
   598 begin
   624     isOnWidget:= widget.show and isOnRect(widget.active, finger);
   632     isOnWidget:= widget.show and isOnRect(widget.active, finger);
   625 end;
   633 end;
   626 
   634 
   627 procedure printFinger(finger: TTouch_Data);
   635 procedure printFinger(finger: TTouch_Data);
   628 begin
   636 begin
   629     WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));
   637     WriteLnToConsole(Format('id: %d, x: %d y: %d (rel x: %d rel y: %d), time: %d',
       
   638                             [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));
   630 end;
   639 end;
   631 
   640 
   632 procedure initModule;
   641 procedure initModule;
   633 var
   642 var
   634     index: Longword;
   643     index: Longword;
   635     //uRenderCoordScaleX, uRenderCoordScaleY: Longword;
   644     //uRenderCoordScaleX, uRenderCoordScaleY: Longword;
   636 begin
   645 begin
   637     buttonsDown:= 0;
   646     buttonsDown:= 0;
       
   647     pointerCount:= 0;
   638 
   648 
   639     setLength(fingers, 4);
   649     setLength(fingers, 4);
   640     for index := 0 to High(fingers) do 
   650     for index := 0 to length(fingers) do
   641         fingers[index].id := nilFingerId;
   651         fingers[index].id := 0;
   642 
   652 
   643     rectSize:= baseRectSize;
   653     rectSize:= baseRectSize;
   644     halfRectSize:= baseRectSize shl 1;
   654     halfRectSize:= baseRectSize shr 1;
   645 end;
   655 end;
   646 
   656 
   647 procedure freeModule;
   657 procedure freeModule;
   648 begin
   658 begin
   649 end;
   659 end;