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