hedgewars/uTouch.pas
branchwebgl
changeset 9521 8054d9d775fd
parent 8330 aaefa587e277
parent 9380 563ab624522c
child 10108 c68cf030eded
equal deleted inserted replaced
9282:92af50454cf2 9521:8054d9d775fd
    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 
    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
   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
   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;
   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
   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;
   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, length(fingers)*2);
   356         for index := length(fingers) div 2 to length(fingers) do
   364         for index := length(fingers) div 2 to length(fingers) do
   357             fingers[index].id := nilFingerId;
   365             fingers[index].id := nilFingerId;
   358     end;
   366     end;
   359 
       
   360 
   367 
   361     xCursor := convertToCursorX(x);
   368     xCursor := convertToCursorX(x);
   362     yCursor := convertToCursorY(y);
   369     yCursor := convertToCursorY(y);
   363 
   370 
   364     //on removing fingers, all fingers are moved to the left
   371     //on removing fingers, all fingers are moved to the left
   375 
   382 
   376     addFinger:= @fingers[pointerCount];
   383     addFinger:= @fingers[pointerCount];
   377     inc(pointerCount);
   384     inc(pointerCount);
   378 end;
   385 end;
   379 
   386 
   380 function updateFinger(x,y,dx,dy: Longword; id: TSDL_FingerId): PTouch_Data;
   387 function updateFinger(x, y, dx, dy: Longword; id: TSDL_FingerId): PTouch_Data;
   381 begin
   388 var finger : PTouch_Data;
   382    updateFinger:= findFinger(id);
   389 begin
   383 
   390     finger:= findFinger(id);
   384    updateFinger^.x:= convertToCursorX(x);
   391 
   385    updateFinger^.y:= convertToCursorY(y);
   392     if finger <> nil then
   386    updateFinger^.dx:= convertToCursorDeltaX(dx);
   393         begin
   387    updateFinger^.dy:= convertToCursorDeltaY(dy);
   394         finger^.x:= convertToCursorX(x);
       
   395         finger^.y:= convertToCursorY(y);
       
   396         finger^.dx:= dx;
       
   397         finger^.dy:= dy;
       
   398         end
       
   399     else
       
   400         WriteLnToConsole('finger ' + inttostr(id) + ' not found');
       
   401     updateFinger:= finger
   388 end;
   402 end;
   389 
   403 
   390 procedure deleteFinger(id: TSDL_FingerId);
   404 procedure deleteFinger(id: TSDL_FingerId);
   391 var
   405 var
   392     index : Longword;
   406     index : Longword;
   407                 fingers[index].y := fingers[pointerCount].y;
   421                 fingers[index].y := fingers[pointerCount].y;
   408                 fingers[index].historicalX := fingers[pointerCount].historicalX;
   422                 fingers[index].historicalX := fingers[pointerCount].historicalX;
   409                 fingers[index].historicalY := fingers[pointerCount].historicalY;
   423                 fingers[index].historicalY := fingers[pointerCount].historicalY;
   410                 fingers[index].timeSinceDown := fingers[pointerCount].timeSinceDown;
   424                 fingers[index].timeSinceDown := fingers[pointerCount].timeSinceDown;
   411 
   425 
   412                 fingers[pointerCount].id := nilFingerId;
   426                 fingers[pointerCount].id := 0;
   413             end
   427             end
   414         else fingers[index].id := nilFingerId;
   428         else fingers[index].id := 0;
   415             break;
   429             break;
   416         end;
   430         end;
   417     end;
   431     end;
   418 
   432 
   419 end;
   433 end;
   495 
   509 
   496 function findFinger(id: TSDL_FingerId): PTouch_Data;
   510 function findFinger(id: TSDL_FingerId): PTouch_Data;
   497 var
   511 var
   498     index: LongWord;
   512     index: LongWord;
   499 begin
   513 begin
   500     for index := 0 to High(fingers) do
   514     for index:= 0 to length(fingers) do
   501         if fingers[index].id = id then
   515         if fingers[index].id = id then
   502             begin
   516             begin
   503             findFinger := @fingers[index];
   517             findFinger:= @fingers[index];
   504             break;
   518             exit;
   505             end;
   519             end;
       
   520     findFinger:= nil;
   506 end;
   521 end;
   507 
   522 
   508 procedure aim(finger: TTouch_Data);
   523 procedure aim(finger: TTouch_Data);
   509 var
   524 var
   510     hogX, hogY, touchX, touchY, deltaX, deltaY: LongInt;
   525     hogX, hogY, touchX, touchY, deltaX, deltaY: LongInt;
   523         targetAngle:= (Round(DeltaY / sqrt(sqr(deltaX) + sqr(deltaY)) * 2048) + 2048) div 2;
   538         targetAngle:= (Round(DeltaY / sqrt(sqr(deltaX) + sqr(deltaY)) * 2048) + 2048) div 2;
   524         end; //if CurrentHedgehog^.Gear <> nil
   539         end; //if CurrentHedgehog^.Gear <> nil
   525 end;
   540 end;
   526 
   541 
   527 // These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system:
   542 // 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;
   543 // - 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
   544 // - 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.
   545 //   and 0 to cScreenHeight on the x axis, (-cScreenWidth, cScreenHeight) being top left.
   531 function convertToCursorX(x: LongInt): LongInt;
   546 function convertToCursorX(x: LongInt): LongInt;
   532 begin
   547 begin
   533     convertToCursorX := round((x/32768)*cScreenWidth) - (cScreenWidth shr 1);
   548     convertToCursorX:= x - cScreenWidth shr 1;
   534 end;
   549 end;
   535 
   550 
   536 function convertToCursorY(y: LongInt): LongInt;
   551 function convertToCursorY(y: LongInt): LongInt;
   537 begin
   552 begin
   538     convertToCursorY := cScreenHeight - round((y/32768)*cScreenHeight)
   553     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;
   554 end;
   550 
   555 
   551 function isOnCrosshair(finger: TTouch_Data): boolean;
   556 function isOnCrosshair(finger: TTouch_Data): boolean;
   552 var
   557 var
   553     x,y : LongInt;
   558     x, y: LongInt;
   554 begin
   559 begin
   555     x := 0;//avoid compiler hint
   560     x:= 0;
   556     y := 0;
   561     y:= 0;
   557     convertToFingerCoord(x, y, CrosshairX, CrosshairY);
   562     convertToFingerCoord(x, y, CrosshairX, CrosshairY);
   558     isOnCrosshair:= isOnRect((x-HalfRectSize), (y-HalfRectSize), RectSize, RectSize, finger);
   563     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;
   564 end;
   563 
   565 
   564 function isOnCurrentHog(finger: TTouch_Data): boolean;
   566 function isOnCurrentHog(finger: TTouch_Data): boolean;
   565 var
   567 var
   566     x,y : LongInt;
   568     x, y: LongInt;
   567 begin
   569 begin
   568     x := 0;
   570     x:= 0;
   569     y := 0;
   571     y:= 0;
   570     convertToFingerCoord(x,y, hwRound(CurrentHedgehog^.Gear^.X), hwRound(CurrentHedgehog^.Gear^.Y));
   572     convertToFingerCoord(x, y, hwRound(CurrentHedgehog^.Gear^.X), hwRound(CurrentHedgehog^.Gear^.Y));
   571     isOnCurrentHog:= isOnRect((x-HalfRectSize), (y-HalfRectSize), RectSize, RectSize, finger);
   573     isOnCurrentHog:= isOnRect(x - HalfRectSize, y - HalfRectSize, RectSize, RectSize, finger);
   572 end;
   574 end;
   573 
   575 
   574 procedure convertToFingerCoord(var x,y : LongInt; oldX, oldY: LongInt);
   576 procedure convertToFingerCoord(var x, y : LongInt; oldX, oldY: LongInt);
   575 begin
   577 begin
   576     x := oldX + WorldDx;
   578     x := oldX + WorldDx;
   577     y := cScreenHeight - (oldY + WorldDy);
   579     y := cScreenHeight - oldY - WorldDy;
   578 end;
   580 end;
   579 
   581 
   580 procedure convertToWorldCoord(var x,y: LongInt; finger: TTouch_Data);
   582 procedure convertToWorldCoord(var x,y: LongInt; finger: TTouch_Data);
   581 begin
   583 begin
   582 //if x <> nil then
   584     x := finger.x - WorldDx;
   583     x := finger.x-WorldDx;
   585     y := cScreenHeight - finger.y - WorldDy;
   584 //if y <> nil then
       
   585     y := (cScreenHeight - finger.y)-WorldDy;
       
   586 end;
   586 end;
   587 
   587 
   588 //Method to calculate the distance this finger has moved since the downEvent
   588 //Method to calculate the distance this finger has moved since the downEvent
   589 function fingerHasMoved(finger: TTouch_Data): boolean;
   589 function fingerHasMoved(finger: TTouch_Data): boolean;
   590 begin
   590 begin
   624     isOnWidget:= widget.show and isOnRect(widget.active, finger);
   624     isOnWidget:= widget.show and isOnRect(widget.active, finger);
   625 end;
   625 end;
   626 
   626 
   627 procedure printFinger(finger: TTouch_Data);
   627 procedure printFinger(finger: TTouch_Data);
   628 begin
   628 begin
   629     WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));
   629     WriteLnToConsole(Format('id: %d, x: %d y: %d (rel x: %d rel y: %d), time: %d',
       
   630                             [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));
   630 end;
   631 end;
   631 
   632 
   632 procedure initModule;
   633 procedure initModule;
   633 var
   634 var
   634     index: Longword;
   635     index: Longword;
   635     //uRenderCoordScaleX, uRenderCoordScaleY: Longword;
   636     //uRenderCoordScaleX, uRenderCoordScaleY: Longword;
   636 begin
   637 begin
   637     buttonsDown:= 0;
   638     buttonsDown:= 0;
       
   639     pointerCount:= 0;
   638 
   640 
   639     setLength(fingers, 4);
   641     setLength(fingers, 4);
   640     for index := 0 to High(fingers) do
   642     for index := 0 to length(fingers) do
   641         fingers[index].id := nilFingerId;
   643         fingers[index].id := 0;
   642 
   644 
   643     rectSize:= round(baseRectSize * mobileRecord.getScreenDPI());
   645     rectSize:= baseRectSize;
   644     halfRectSize:= rectSize shl 1;
   646     halfRectSize:= baseRectSize shr 1;
   645 end;
   647 end;
   646 
   648 
   647 procedure freeModule;
   649 procedure freeModule;
   648 begin
   650 begin
   649 end;
   651 end;