hedgewars/uTouch.pas
branchhedgeroid
changeset 5579 3176ee8a9d94
child 5589 b95d10c82f7f
equal deleted inserted replaced
5577:272c82f82cee 5579:3176ee8a9d94
       
     1 {$INCLUDE "options.inc"}
       
     2 
       
     3 unit uTouch;
       
     4 
       
     5 interface
       
     6 
       
     7 uses sysutils, math, uConsole, uVariables, SDLh, uTypes, uFloat, uConsts, GLUnit;
       
     8 
       
     9 procedure initModule;
       
    10 
       
    11 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId);
       
    12 procedure onTouchMotion(x,y: Longword; dx,dy: LongInt; pointerId: SDL_FingerId);
       
    13 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
       
    14 function convertToCursor(scale: LongInt; xy: LongInt): LongInt;
       
    15 procedure addFinger(id: SDL_FingerId);
       
    16 procedure deleteFinger(id: SDL_FingerId);
       
    17 procedure onTouchClick(x,y: Longword; pointerId: SDL_FingerId);
       
    18 
       
    19 function calculateDelta(id1, id2: SDL_FingerId): hwFloat;
       
    20 function getSecondPointer(id: SDL_FingerId): SDL_FingerId;
       
    21 implementation
       
    22 
       
    23 const
       
    24     clicktime = 200;
       
    25 
       
    26 var
       
    27     pointerCount : Longword;
       
    28     xyCoord : array of LongInt;
       
    29     pointerIds : array of SDL_FingerId;
       
    30     timeSinceDown: array of Longword;
       
    31      
       
    32     //Pinch to zoom 
       
    33     pinchSize : hwFloat;
       
    34     baseZoomValue: GLFloat;
       
    35 
       
    36 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId);
       
    37 begin
       
    38     WriteToConsole('down'); 
       
    39     addFinger(pointerId);
       
    40     xyCoord[pointerId*2] := convertToCursor(cScreenWidth,x);
       
    41     xyCoord[pointerId*2+1] := convertToCursor(cScreenHeight,y);
       
    42    
       
    43     case pointerCount of
       
    44         2:
       
    45         begin
       
    46             pinchSize := calculateDelta(pointerId, getSecondPointer(pointerId));
       
    47             baseZoomValue := ZoomValue
       
    48         end;
       
    49     end;//end case pointerCount of
       
    50 end;
       
    51 
       
    52 procedure onTouchMotion(x,y: Longword;dx,dy: LongInt; pointerId: SDL_FingerId);
       
    53 var
       
    54     secondId : SDL_FingerId;
       
    55     currentPinchDelta, zoom : hwFloat;
       
    56 begin
       
    57     xyCoord[pointerId*2] := convertToCursor(cScreenWidth, x);
       
    58     xyCoord[pointerId*2+1] := convertToCursor(cScreenHeight, y);
       
    59     
       
    60     case pointerCount of
       
    61        1:
       
    62            begin
       
    63                CursorPoint.X := CursorPoint.X - convertToCursor(cScreenWidth,dx);
       
    64                CursorPoint.Y := CursorPoint.Y + convertToCursor(cScreenWidth,dy);
       
    65            end;
       
    66        2:
       
    67            begin
       
    68                secondId := getSecondPointer(pointerId);
       
    69                currentPinchDelta := calculateDelta(pointerId, secondId) - pinchSize;
       
    70                zoom := currentPinchDelta/cScreenWidth;
       
    71                ZoomValue := baseZoomValue - ((hwFloat2Float(zoom) * cMinMaxZoomLevelDelta));
       
    72                //WriteToConsole(Format('Zoom in/out. ZoomValue = %f', [ZoomValue]));
       
    73 //              if ZoomValue > cMaxZoomLevel then ZoomValue := cMaxZoomLevel;
       
    74 //               if ZoomValue < cMinZoomLevel then ZoomValue := cMinZoomLevel;
       
    75             end;
       
    76     end; //end case pointerCount of
       
    77 end;
       
    78 
       
    79 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
       
    80 begin
       
    81     pointerCount := pointerCount-1;
       
    82     deleteFinger(pointerId);
       
    83 end;
       
    84 
       
    85 procedure onTouchClick(x,y: Longword; pointerId: SDL_FingerId);
       
    86 begin
       
    87     WriteToConsole(Format('clicker %d', [SDL_GetTicks]));
       
    88     bShowAmmoMenu := not(bShowAmmoMenu);
       
    89 end;
       
    90 
       
    91 function convertToCursor(scale: LongInt; xy: LongInt): LongInt;
       
    92 begin
       
    93     convertToCursor := round(xy/32768*scale)
       
    94 end;
       
    95 
       
    96 procedure addFinger(id: SDL_FingerId);
       
    97 var 
       
    98     index, tmp: Longword;
       
    99 begin
       
   100     pointerCount := pointerCount + 1;
       
   101 
       
   102     //Check array sizes
       
   103     if length(pointerIds) < pointerCount then setLength(pointerIds, length(pointerIds)*2);
       
   104     if length(xyCoord) < pointerCount*2+1 then setLength(xyCoord, length(xyCoord)*2);
       
   105     if length(timeSinceDown) < id then setLength(timeSinceDown, id); 
       
   106 
       
   107     for index := 0 to pointerCount do //place the pointer ids as far back to the left as possible
       
   108     begin
       
   109         if pointerIds[index] = -1 then 
       
   110            begin
       
   111                pointerIds[index] := id;
       
   112                break;
       
   113            end;
       
   114     end;
       
   115     //set timestamp
       
   116     timeSinceDown[id] := SDL_GetTicks
       
   117 end;
       
   118 
       
   119 procedure deleteFinger(id: SDL_FingerId);
       
   120 var
       
   121     index, i : Longint;
       
   122 begin
       
   123     index := 0;
       
   124     for index := 0 to pointerCount do
       
   125     begin
       
   126          if pointerIds[index] = id then
       
   127          begin
       
   128              pointerIds[index] := -1;
       
   129              break;
       
   130          end;
       
   131     end;
       
   132     //put the last pointerId into the stop of the id to be removed, so that all pointerIds are to the far left
       
   133     for i := pointerCount downto index do
       
   134     begin
       
   135         if pointerIds[i] <> -1 then
       
   136         begin
       
   137             pointerIds[index] := pointerIds[i];
       
   138             break;
       
   139         end;
       
   140     end;
       
   141     if (SDL_GetTicks - timeSinceDown[id]) < clickTime then onTouchClick(xyCoord[id*2], xyCoord[id*2+1], id);
       
   142 end;
       
   143 
       
   144 function calculateDelta(id1, id2: SDL_FingerId): hwFloat;
       
   145 begin
       
   146 //    calculateDelta := Distance(xyCoord[id2*2] - xyCoord[id1*2], xyCoord[id2*2+1] - xyCoord[id1*2+1]);
       
   147     calculateDelta := int2hwFloat(trunc(sqrt(Power(xyCoord[id2*2]-xyCoord[id1*2],2) + Power(xyCoord[id2*2+1]-xyCoord[id1*2+1], 2))));
       
   148 end;
       
   149 
       
   150 // Under the premise that all pointer ids in pointerIds:SDL_FingerId are pack to the far left.
       
   151 // If the pointer to be ignored is not pointerIds[0] the second must be there
       
   152 function getSecondPointer(id: SDL_FingerId): SDL_FingerId;
       
   153 begin
       
   154     if pointerIds[0] = id then getSecondPointer := pointerIds[1]
       
   155     else getSecondPointer := pointerIds[0];
       
   156 end;
       
   157 
       
   158 procedure initModule;
       
   159 var
       
   160     index: Longword;
       
   161 begin
       
   162     setLength(xyCoord, 10);
       
   163     setLength(pointerIds, 5);
       
   164     setLength(timeSinceDown, 5);    
       
   165     for index := Low(xyCoord) to High(xyCoord) do xyCoord[index] := -1;
       
   166     for index := Low(pointerIds) to High(pointerIds) do pointerIds[index] := -1;
       
   167 
       
   168 end;
       
   169 
       
   170 begin
       
   171 end.