hedgewars/uTouch.pas
branchwebgl
changeset 8330 aaefa587e277
parent 8204 9a6030d96273
child 9521 8054d9d775fd
equal deleted inserted replaced
8116:d24257910f8d 8330:aaefa587e277
    20 
    20 
    21 unit uTouch;
    21 unit uTouch;
    22 
    22 
    23 interface
    23 interface
    24 
    24 
    25 uses SysUtils, uConsole, uVariables, SDLh, uFloat, uConsts, uCommands, GLUnit, uTypes, uCaptions, uAmmos, uWorld, uMobile;
    25 uses SysUtils, uConsole, uVariables, SDLh, uFloat, uConsts, uCommands, GLUnit, uTypes, uCaptions, uAmmos, uWorld;
    26 
    26 
    27 
    27 
    28 procedure initModule;
    28 procedure initModule;
    29 procedure freeModule;
    29 procedure freeModule;
    30 
    30 
    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: Longword; pointerId: TSDL_FingerId);
    92 var 
    92 var
    93     finger: PTouch_Data;
    93     finger: PTouch_Data;
    94 begin
    94 begin
    95 {$IFDEF USE_TOUCH_INTERFACE}
    95 {$IFDEF USE_TOUCH_INTERFACE}
    96 finger := addFinger(x,y,pointerId);
    96 finger := addFinger(x,y,pointerId);
    97 
    97 
   159         begin
   159         begin
   160         if Ammoz[CurrentHedgehog^.CurAmmoType].Ammo.Propz and ammoprop_Timerable <> 0 then
   160         if Ammoz[CurrentHedgehog^.CurAmmoType].Ammo.Propz and ammoprop_Timerable <> 0 then
   161             ParseTeamCommand('/timer ' + inttostr((GetCurAmmoEntry(CurrentHedgeHog^)^.Timer div 1000) mod 5 + 1));
   161             ParseTeamCommand('/timer ' + inttostr((GetCurAmmoEntry(CurrentHedgeHog^)^.Timer div 1000) mod 5 + 1));
   162         end;
   162         end;
   163     exit;
   163     exit;
   164     end; 
   164     end;
   165 dec(buttonsDown);//no buttonsDown, undo the inc() above
   165 dec(buttonsDown);//no buttonsDown, undo the inc() above
   166 if buttonsDown = 0 then
   166 if buttonsDown = 0 then
   167     begin
   167     begin
   168     moveCursor:= true;
   168     moveCursor:= true;
   169     case pointerCount of
   169     case pointerCount of
   199             CursorPoint.X := CursorPoint.X + finger^.dx;
   199             CursorPoint.X := CursorPoint.X + finger^.dx;
   200             CursorPoint.Y := CursorPoint.Y - finger^.dy;
   200             CursorPoint.Y := CursorPoint.Y - finger^.dy;
   201         end;
   201         end;
   202         exit //todo change into switch rather than ugly ifs
   202         exit //todo change into switch rather than ugly ifs
   203     end;
   203     end;
   204     
   204 
   205 if aimingCrosshair then 
   205 if aimingCrosshair then
   206     begin
   206     begin
   207         aim(finger^);
   207         aim(finger^);
   208         exit
   208         exit
   209     end;
   209     end;
   210 
   210 
   250 
   250 
   251 widget:= finger^.pressedWidget;
   251 widget:= finger^.pressedWidget;
   252 if (buttonsDown > 0) and (widget <> nil) then
   252 if (buttonsDown > 0) and (widget <> nil) then
   253     begin
   253     begin
   254     dec(buttonsDown);
   254     dec(buttonsDown);
   255     
   255 
   256     if widget = @arrowLeft then
   256     if widget = @arrowLeft then
   257         ParseTeamCommand('-left');
   257         ParseTeamCommand('-left');
   258     
   258 
   259     if widget = @arrowRight then
   259     if widget = @arrowRight then
   260         ParseTeamCommand('-right');
   260         ParseTeamCommand('-right');
   261 
   261 
   262     if widget = @arrowUp then
   262     if widget = @arrowUp then
   263         ParseTeamCommand('-up');
   263         ParseTeamCommand('-up');
   265     if widget = @arrowDown then
   265     if widget = @arrowDown then
   266         ParseTeamCommand('-down');
   266         ParseTeamCommand('-down');
   267 
   267 
   268     if widget = @fireButton then
   268     if widget = @fireButton then
   269         ParseTeamCommand('-attack');
   269         ParseTeamCommand('-attack');
   270     
   270 
   271     if widget = @utilityWidget then
   271     if widget = @utilityWidget then
   272         if (CurrentHedgehog <> nil)then
   272         if (CurrentHedgehog <> nil)then
   273             if(Ammoz[CurrentHedgehog^.CurAmmoType].Ammo.Propz and ammoprop_NeedTarget <> 0)then
   273             if(Ammoz[CurrentHedgehog^.CurAmmoType].Ammo.Propz and ammoprop_NeedTarget <> 0)then
   274                 begin
   274                 begin
   275                 ParseTeamCommand('put');
   275                 ParseTeamCommand('put');
   277                 end
   277                 end
   278             else if (CurAmmoGear <> nil) and (CurAmmoGear^.AmmoType = amSwitch) then
   278             else if (CurAmmoGear <> nil) and (CurAmmoGear^.AmmoType = amSwitch) then
   279                 ParseTeamCommand('switch')
   279                 ParseTeamCommand('switch')
   280             else WriteLnToConsole(inttostr(ord(Ammoz[CurrentHedgehog^.CurAmmoType].NameId)) + ' ' + inttostr(ord(sidSwitch)));
   280             else WriteLnToConsole(inttostr(ord(Ammoz[CurrentHedgehog^.CurAmmoType].NameId)) + ' ' + inttostr(ord(sidSwitch)));
   281     end;
   281     end;
   282         
   282 
   283 if targetting then
   283 if targetting then
   284     AddCaption('Press the target button to mark the target', cWhiteColor, capgrpAmmoInfo);
   284     AddCaption('Press the target button to mark the target', cWhiteColor, capgrpAmmoInfo);
   285  
   285 
   286 deleteFinger(pointerId);
   286 deleteFinger(pointerId);
   287 {$ENDIF}
   287 {$ENDIF}
   288 end;
   288 end;
   289 
   289 
   290 procedure onTouchDoubleClick(finger: TTouch_Data);
   290 procedure onTouchDoubleClick(finger: TTouch_Data);
   307 procedure onTouchClick(finger: TTouch_Data);
   307 procedure onTouchClick(finger: TTouch_Data);
   308 begin
   308 begin
   309 //if (RealTicks - timeSinceClick < 300) and (sqrt(sqr(finger.X-xTouchClick) + sqr(finger.Y-yTouchClick)) < 30) then
   309 //if (RealTicks - timeSinceClick < 300) and (sqrt(sqr(finger.X-xTouchClick) + sqr(finger.Y-yTouchClick)) < 30) then
   310 //    begin
   310 //    begin
   311 //    onTouchDoubleClick(finger);
   311 //    onTouchDoubleClick(finger);
   312 //    timeSinceClick:= 0;//we make an assumption there won't be an 'click' in the first 300 ticks(milliseconds) 
   312 //    timeSinceClick:= 0;//we make an assumption there won't be an 'click' in the first 300 ticks(milliseconds)
   313 //    exit; 
   313 //    exit;
   314 //    end;
   314 //    end;
   315 
   315 
   316 xTouchClick:= finger.x;
   316 xTouchClick:= finger.x;
   317 yTouchClick:= finger.y;
   317 yTouchClick:= finger.y;
   318 timeSinceClick:= RealTicks;
   318 timeSinceClick:= RealTicks;
   319 
   319 
   320 if bShowAmmoMenu then
   320 if bShowAmmoMenu then
   321     begin 
   321     begin
   322     if isOnRect(AmmoRect, finger) then
   322     if isOnRect(AmmoRect, finger) then
   323         begin
   323         begin
   324         CursorPoint.X:= finger.x;
   324         CursorPoint.X:= finger.x;
   325         CursorPoint.Y:= finger.y;
   325         CursorPoint.Y:= finger.y;
   326         ParseTeamCommand('put'); 
   326         ParseTeamCommand('put');
   327         end
   327         end
   328     else
   328     else
   329         bShowAmmoMenu:= false;
   329         bShowAmmoMenu:= false;
   330     exit;
   330     exit;
   331     end;
   331     end;
   337     exit;
   337     exit;
   338     end;
   338     end;
   339 
   339 
   340 if isOnWidget(jumpWidget, finger) then
   340 if isOnWidget(jumpWidget, finger) then
   341     begin
   341     begin
   342     ParseTeamCommand('hjump');    
   342     ParseTeamCommand('hjump');
   343     exit;
   343     exit;
   344     end;
   344     end;
   345 {$ENDIF}
   345 {$ENDIF}
   346 end;
   346 end;
   347 
   347 
   348 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data;
   348 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data;
   349 var 
   349 var
   350     xCursor, yCursor, index : LongInt;
   350     xCursor, yCursor, index : LongInt;
   351 begin
   351 begin
   352     //Check array sizes
   352     //Check array sizes
   353     if length(fingers) < Integer(pointerCount) then 
   353     if length(fingers) < Integer(pointerCount) then
   354     begin
   354     begin
   355         setLength(fingers, length(fingers)*2);
   355         setLength(fingers, length(fingers)*2);
   356         for index := length(fingers) div 2 to length(fingers) do
   356         for index := length(fingers) div 2 to length(fingers) do
   357             fingers[index].id := nilFingerId;
   357             fingers[index].id := nilFingerId;
   358     end;
   358     end;
   359     
   359 
   360     
   360 
   361     xCursor := convertToCursorX(x);
   361     xCursor := convertToCursorX(x);
   362     yCursor := convertToCursorY(y);
   362     yCursor := convertToCursorY(y);
   363     
   363 
   364     //on removing fingers, all fingers are moved to the left
   364     //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
   365     //with dynamic arrays being zero based, the new position of the finger is the old pointerCount
   366     fingers[pointerCount].id := id;
   366     fingers[pointerCount].id := id;
   367     fingers[pointerCount].historicalX := xCursor;
   367     fingers[pointerCount].historicalX := xCursor;
   368     fingers[pointerCount].historicalY := yCursor;
   368     fingers[pointerCount].historicalY := yCursor;
   370     fingers[pointerCount].y := yCursor;
   370     fingers[pointerCount].y := yCursor;
   371     fingers[pointerCount].dx := 0;
   371     fingers[pointerCount].dx := 0;
   372     fingers[pointerCount].dy := 0;
   372     fingers[pointerCount].dy := 0;
   373     fingers[pointerCount].timeSinceDown:= RealTicks;
   373     fingers[pointerCount].timeSinceDown:= RealTicks;
   374     fingers[pointerCount].pressedWidget:= nil;
   374     fingers[pointerCount].pressedWidget:= nil;
   375  
   375 
   376     addFinger:= @fingers[pointerCount];
   376     addFinger:= @fingers[pointerCount];
   377     inc(pointerCount);
   377     inc(pointerCount);
   378 end;
   378 end;
   379 
   379 
   380 function updateFinger(x,y,dx,dy: Longword; id: TSDL_FingerId): PTouch_Data;
   380 function updateFinger(x,y,dx,dy: Longword; id: TSDL_FingerId): PTouch_Data;
   389 
   389 
   390 procedure deleteFinger(id: TSDL_FingerId);
   390 procedure deleteFinger(id: TSDL_FingerId);
   391 var
   391 var
   392     index : Longword;
   392     index : Longword;
   393 begin
   393 begin
   394     
   394 
   395     dec(pointerCount);
   395     dec(pointerCount);
   396     for index := 0 to pointerCount do
   396     for index := 0 to pointerCount do
   397     begin
   397     begin
   398         if fingers[index].id = id then
   398         if fingers[index].id = id then
   399         begin
   399         begin
   400  
   400 
   401             //put the last finger into the spot of the finger to be removed, 
   401             //put the last finger into the spot of the finger to be removed,
   402             //so that all fingers are packed to the far left
   402             //so that all fingers are packed to the far left
   403             if  pointerCount <> index then
   403             if  pointerCount <> index then
   404                 begin
   404                 begin
   405                 fingers[index].id := fingers[pointerCount].id;    
   405                 fingers[index].id := fingers[pointerCount].id;
   406                 fingers[index].x := fingers[pointerCount].x;    
   406                 fingers[index].x := fingers[pointerCount].x;
   407                 fingers[index].y := fingers[pointerCount].y;    
   407                 fingers[index].y := fingers[pointerCount].y;
   408                 fingers[index].historicalX := fingers[pointerCount].historicalX;    
   408                 fingers[index].historicalX := fingers[pointerCount].historicalX;
   409                 fingers[index].historicalY := fingers[pointerCount].historicalY;    
   409                 fingers[index].historicalY := fingers[pointerCount].historicalY;
   410                 fingers[index].timeSinceDown := fingers[pointerCount].timeSinceDown;
   410                 fingers[index].timeSinceDown := fingers[pointerCount].timeSinceDown;
   411 
   411 
   412                 fingers[pointerCount].id := nilFingerId;
   412                 fingers[pointerCount].id := nilFingerId;
   413             end
   413             end
   414         else fingers[index].id := nilFingerId;
   414         else fingers[index].id := nilFingerId;
   428 
   428 
   429 procedure ProcessTouch;
   429 procedure ProcessTouch;
   430 var
   430 var
   431     deltaAngle: LongInt;
   431     deltaAngle: LongInt;
   432 begin
   432 begin
   433 invertCursor := not(bShowAmmoMenu or targetting); 
   433 invertCursor := not(bShowAmmoMenu or targetting);
   434 if aimingCrosshair then
   434 if aimingCrosshair then
   435     if CurrentHedgehog^.Gear <> nil then
   435     if CurrentHedgehog^.Gear <> nil then
   436         begin
   436         begin
   437         deltaAngle:= CurrentHedgehog^.Gear^.Angle - targetAngle;
   437         deltaAngle:= CurrentHedgehog^.Gear^.Angle - targetAngle;
   438         if (deltaAngle > -5) and (deltaAngle < 5) then 
   438         if (deltaAngle > -5) and (deltaAngle < 5) then
   439             begin
   439             begin
   440                 if(aimingUp)then
   440                 if(aimingUp)then
   441                     begin
   441                     begin
   442                     aimingUp:= false;
   442                     aimingUp:= false;
   443                     ParseTeamCommand('-up');
   443                     ParseTeamCommand('-up');
   473                 if aimingUp then
   473                 if aimingUp then
   474                     begin
   474                     begin
   475                     aimingUp:= true;
   475                     aimingUp:= true;
   476                     ParseTeamCommand('+up');
   476                     ParseTeamCommand('+up');
   477                     end;
   477                     end;
   478                 end; 
   478                 end;
   479             end;
   479             end;
   480         end
   480         end
   481     else  
   481     else
   482         begin
   482         begin
   483         if aimingUp then
   483         if aimingUp then
   484             begin
   484             begin
   485             ParseTeamCommand('-up');
   485             ParseTeamCommand('-up');
   486             aimingUp:= false;
   486             aimingUp:= false;
   496 function findFinger(id: TSDL_FingerId): PTouch_Data;
   496 function findFinger(id: TSDL_FingerId): PTouch_Data;
   497 var
   497 var
   498     index: LongWord;
   498     index: LongWord;
   499 begin
   499 begin
   500     for index := 0 to High(fingers) do
   500     for index := 0 to High(fingers) do
   501         if fingers[index].id = id then 
   501         if fingers[index].id = id then
   502             begin
   502             begin
   503             findFinger := @fingers[index];
   503             findFinger := @fingers[index];
   504             break;
   504             break;
   505             end;
   505             end;
   506 end;
   506 end;
   507 
   507 
   508 procedure aim(finger: TTouch_Data);
   508 procedure aim(finger: TTouch_Data);
   509 var 
   509 var
   510     hogX, hogY, touchX, touchY, deltaX, deltaY: LongInt;
   510     hogX, hogY, touchX, touchY, deltaX, deltaY: LongInt;
   511 begin
   511 begin
   512     if CurrentHedgehog^.Gear <> nil then
   512     if CurrentHedgehog^.Gear <> nil then
   513         begin
   513         begin
   514         touchX := 0;//avoid compiler hint
   514         touchX := 0;//avoid compiler hint
   517         hogY := hwRound(CurrentHedgehog^.Gear^.Y);
   517         hogY := hwRound(CurrentHedgehog^.Gear^.Y);
   518 
   518 
   519         convertToWorldCoord(touchX, touchY, finger);
   519         convertToWorldCoord(touchX, touchY, finger);
   520         deltaX := abs(TouchX-HogX);
   520         deltaX := abs(TouchX-HogX);
   521         deltaY := TouchY-HogY;
   521         deltaY := TouchY-HogY;
   522         
   522 
   523         targetAngle:= (Round(DeltaY / sqrt(sqr(deltaX) + sqr(deltaY)) * 2048) + 2048) div 2;
   523         targetAngle:= (Round(DeltaY / sqrt(sqr(deltaX) + sqr(deltaY)) * 2048) + 2048) div 2;
   524         end; //if CurrentHedgehog^.Gear <> nil
   524         end; //if CurrentHedgehog^.Gear <> nil
   525 end;
   525 end;
   526 
   526 
   527 // These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system:
   527 // These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system:
   556     y := 0;
   556     y := 0;
   557     convertToFingerCoord(x, y, CrosshairX, CrosshairY);
   557     convertToFingerCoord(x, y, CrosshairX, CrosshairY);
   558     isOnCrosshair:= isOnRect((x-HalfRectSize), (y-HalfRectSize), RectSize, RectSize, finger);
   558     isOnCrosshair:= isOnRect((x-HalfRectSize), (y-HalfRectSize), RectSize, RectSize, finger);
   559     printFinger(finger);
   559     printFinger(finger);
   560     WriteLnToConsole(inttostr(finger.x) + '   ' + inttostr(x));
   560     WriteLnToConsole(inttostr(finger.x) + '   ' + inttostr(x));
   561     WriteLnToConsole(inttostr(x) + '  ' + inttostr(y) + '   ' + inttostr(round(uMobile.getScreenDPI * 10)));
   561     WriteLnToConsole(inttostr(x) + '  ' + inttostr(y) + '   ' + inttostr(round(mobileRecord.getScreenDPI() * 10)));
   562 end;
   562 end;
   563 
   563 
   564 function isOnCurrentHog(finger: TTouch_Data): boolean;
   564 function isOnCurrentHog(finger: TTouch_Data): boolean;
   565 var
   565 var
   566     x,y : LongInt;
   566     x,y : LongInt;
   577     y := cScreenHeight - (oldY + WorldDy);
   577     y := cScreenHeight - (oldY + WorldDy);
   578 end;
   578 end;
   579 
   579 
   580 procedure convertToWorldCoord(var x,y: LongInt; finger: TTouch_Data);
   580 procedure convertToWorldCoord(var x,y: LongInt; finger: TTouch_Data);
   581 begin
   581 begin
   582 //if x <> nil then 
   582 //if x <> nil then
   583     x := finger.x-WorldDx;
   583     x := finger.x-WorldDx;
   584 //if y <> nil then 
   584 //if y <> nil then
   585     y := (cScreenHeight - finger.y)-WorldDy;
   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;
   635     //uRenderCoordScaleX, uRenderCoordScaleY: Longword;
   635     //uRenderCoordScaleX, uRenderCoordScaleY: Longword;
   636 begin
   636 begin
   637     buttonsDown:= 0;
   637     buttonsDown:= 0;
   638 
   638 
   639     setLength(fingers, 4);
   639     setLength(fingers, 4);
   640     for index := 0 to High(fingers) do 
   640     for index := 0 to High(fingers) do
   641         fingers[index].id := nilFingerId;
   641         fingers[index].id := nilFingerId;
   642 
   642 
   643     rectSize:= round(baseRectSize * uMobile.getScreenDPI);
   643     rectSize:= round(baseRectSize * mobileRecord.getScreenDPI());
   644     halfRectSize:= rectSize shl 1;
   644     halfRectSize:= rectSize shl 1;
   645 end;
   645 end;
   646 
   646 
   647 procedure freeModule;
   647 procedure freeModule;
   648 begin
   648 begin