hedgewars/uTouch.pas
branchhedgeroid
changeset 5605 31bd6e30df02
parent 5599 2e4b90f33a83
child 5609 9d66611e4d0a
equal deleted inserted replaced
5603:4e4a579a60af 5605:31bd6e30df02
    18 procedure deleteFinger(id: SDL_FingerId);
    18 procedure deleteFinger(id: SDL_FingerId);
    19 procedure onTouchClick(x,y: Longword; pointerId: SDL_FingerId);
    19 procedure onTouchClick(x,y: Longword; pointerId: SDL_FingerId);
    20 
    20 
    21 procedure aim(id: SDL_FingerId);
    21 procedure aim(id: SDL_FingerId);
    22 function isOnCurrentHog(id: SDL_FingerId): boolean;
    22 function isOnCurrentHog(id: SDL_FingerId): boolean;
       
    23 function isOnFireButton(id: SDL_FingerId): boolean;
    23 procedure convertToWorldCoord(var x,y: hwFloat; id: SDL_FingerId);
    24 procedure convertToWorldCoord(var x,y: hwFloat; id: SDL_FingerId);
    24 function fingerHasMoved(id: SDL_FingerId): boolean;
    25 function fingerHasMoved(id: SDL_FingerId): boolean;
    25 function calculateDelta(id1, id2: SDL_FingerId): hwFloat;
    26 function calculateDelta(id1, id2: SDL_FingerId): hwFloat;
    26 function getSecondPointer(id: SDL_FingerId): SDL_FingerId;
    27 function getSecondPointer(id: SDL_FingerId): SDL_FingerId;
    27 implementation
    28 implementation
    28 
    29 
    29 const
    30 const
    30     clicktime = 200;
    31     clicktime = 200;
    31 
    32 var
    32 var
    33     leftButtonBoundary  : LongInt;
       
    34     rightButtonBoundary : LongInt;
       
    35     topButtonBoundary   : LongInt;
       
    36     
    33     pointerCount : Longword;
    37     pointerCount : Longword;
    34     xyCoord : array of LongInt;
    38     xyCoord : array of LongInt;
    35     pointerIds : array of SDL_FingerId;
    39     pointerIds : array of SDL_FingerId;
    36     timeSinceDown: array of Longword;
    40     timeSinceDown: array of Longword;
    37     historicalXY : array of LongInt;
    41     historicalXY : array of LongInt;
       
    42 
       
    43     moveCursor : boolean;
       
    44 
    38     //Pinch to zoom 
    45     //Pinch to zoom 
    39     pinchSize : hwFloat;
    46     pinchSize : hwFloat;
    40     baseZoomValue: GLFloat;
    47     baseZoomValue: GLFloat;
    41 
    48 
    42     invertCursor : boolean;
    49     invertCursor : boolean;
    43 
    50 
    44     //aiming
    51     //aiming
    45     aiming, movingCrosshair: boolean; 
    52     aiming, movingCrosshair: boolean; 
    46     crosshairCommand: ShortString;
    53     crosshairCommand: ShortString;
    47     aimingPointerId: SDL_FingerId;
    54     aimingPointerId: SDL_FingerId;
    48     targetAngle: LongInt;    
    55     targetAngle: LongInt;
       
    56 
       
    57     stopFiring: boolean;
       
    58 
       
    59     //moving
       
    60     stopLeft, stopRight, walkingLeft, walkingRight :  boolean;
    49 
    61 
    50 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId);
    62 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId);
    51 begin
    63 begin
    52     addFinger(x,y,pointerId);
    64     addFinger(x,y,pointerId);
    53     xyCoord[pointerId*2] := convertToCursor(cScreenWidth,x);
    65     xyCoord[pointerId*2] := convertToCursor(cScreenWidth,x);
    54     xyCoord[pointerId*2+1] := convertToCursor(cScreenHeight,y);
    66     xyCoord[pointerId*2+1] := convertToCursor(cScreenHeight,y);
    55     
    67     
    56    
    68    
    57     case pointerCount of
    69     case pointerCount of
    58         1:
    70         1:
    59             if isOnCurrentHog(pointerId) then aiming:= true;
    71         begin
       
    72             moveCursor:= false;
       
    73             if bShowAmmoMenu then
       
    74             begin
       
    75                 moveCursor := true;
       
    76                 exit;
       
    77             end;
       
    78 
       
    79             if isOnCurrentHog(pointerId) then
       
    80             begin
       
    81                 aiming:= true;
       
    82                 exit;
       
    83             end;
       
    84 
       
    85             if isOnFireButton(pointerId) then
       
    86             begin
       
    87                 stopFiring:= false;
       
    88                 ParseCommand('+attack', true);
       
    89                 exit;
       
    90             end;
       
    91             if xyCoord[pointerId*2] < leftButtonBoundary then
       
    92             begin
       
    93                 ParseCommand('+left', true);
       
    94                 walkingLeft := true;
       
    95                 exit;
       
    96             end;
       
    97             if xyCoord[pointerId*2] > rightButtonBoundary then
       
    98             begin
       
    99                 ParseCommand('+right', true);
       
   100                 walkingRight:= true;
       
   101                 exit;
       
   102             end;
       
   103             WriteToConsole(Format('%d, %d', [xyCoord[pointerId*2+1], topButtonBoundary]));    
       
   104             if xyCoord[pointerId*2+1] < topButtonBoundary then
       
   105             begin
       
   106                 ParseCommand('hjump', true);
       
   107                 exit;
       
   108             end;
       
   109             moveCursor:= true; 
       
   110         end;
    60         2:
   111         2:
    61         begin
   112         begin
    62             aiming:= false;
   113             aiming:= false;
       
   114             stopFiring:= true;
    63             
   115             
    64             pinchSize := calculateDelta(pointerId, getSecondPointer(pointerId));
   116             pinchSize := calculateDelta(pointerId, getSecondPointer(pointerId));
    65             baseZoomValue := ZoomValue
   117             baseZoomValue := ZoomValue
    66         end;
   118         end;
    67     end;//end case pointerCount of
   119     end;//end case pointerCount of
    81                if aiming then 
   133                if aiming then 
    82                begin
   134                begin
    83                    aim(pointerId);
   135                    aim(pointerId);
    84                    exit
   136                    exit
    85                end;
   137                end;
    86                if invertCursor then
   138                if moveCursor then
    87                begin
   139                    if invertCursor then
    88                    CursorPoint.X := CursorPoint.X - convertToCursor(cScreenWidth,dx);
   140                    begin
    89                    CursorPoint.Y := CursorPoint.Y + convertToCursor(cScreenWidth,dy);
   141                        CursorPoint.X := CursorPoint.X - convertToCursor(cScreenWidth,dx);
    90                end
   142                        CursorPoint.Y := CursorPoint.Y + convertToCursor(cScreenWidth,dy);
    91                else
   143                    end
    92                begin
   144                    else
    93                    CursorPoint.X := CursorPoint.X + convertToCursor(cScreenWidth,dx);
   145                    begin
    94                    CursorPoint.Y := CursorPoint.Y - convertToCursor(cScreenWidth,dy);
   146                        CursorPoint.X := CursorPoint.X + convertToCursor(cScreenWidth,dx);
    95                end;
   147                        CursorPoint.Y := CursorPoint.Y - convertToCursor(cScreenWidth,dy);
       
   148                    end;
    96            end;
   149            end;
    97        2:
   150        2:
    98            begin
   151            begin
    99                secondId := getSecondPointer(pointerId);
   152                secondId := getSecondPointer(pointerId);
   100                currentPinchDelta := calculateDelta(pointerId, secondId) - pinchSize;
   153                currentPinchDelta := calculateDelta(pointerId, secondId) - pinchSize;
   101                zoom := currentPinchDelta/cScreenWidth;
   154                zoom := currentPinchDelta/cScreenWidth;
   102                ZoomValue := baseZoomValue - ((hwFloat2Float(zoom) * cMinMaxZoomLevelDelta));
   155                ZoomValue := baseZoomValue - ((hwFloat2Float(zoom) * cMinMaxZoomLevelDelta));
   103                WriteToConsole(Format('Zoom in/out. ZoomValue = %f, %f', [ZoomValue, cMaxZoomLevel]));
   156                if ZoomValue < cMaxZoomLevel then ZoomValue := cMaxZoomLevel;
   104                if ZoomValue > cMaxZoomLevel then ZoomValue := cMaxZoomLevel;
   157                if ZoomValue > cMinZoomLevel then ZoomValue := cMinZoomLevel;
   105 //               if ZoomValue < cMinZoomLevel then ZoomValue := cMinZoomLevel;
       
   106             end;
   158             end;
   107     end; //end case pointerCount of
   159     end; //end case pointerCount of
   108 end;
   160 end;
   109 
   161 
   110 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
   162 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
   111 begin
   163 begin
   112     aiming:= false;
   164     aiming:= false;
   113     pointerCount := pointerCount-1;
   165     pointerCount := pointerCount-1;
       
   166     stopFiring:= true;
   114     deleteFinger(pointerId);
   167     deleteFinger(pointerId);
       
   168 
       
   169     if walkingLeft then
       
   170     begin
       
   171         ParseCommand('-left', true);
       
   172         walkingLeft := false;
       
   173     end;
       
   174 
       
   175     if walkingRight then
       
   176     begin
       
   177         ParseCommand('-right', true);
       
   178         walkingRight := false;
       
   179     end;
   115 end;
   180 end;
   116 
   181 
   117 procedure onTouchClick(x,y: Longword; pointerId: SDL_FingerId);
   182 procedure onTouchClick(x,y: Longword; pointerId: SDL_FingerId);
   118 begin
   183 begin
   119     if bShowAmmoMenu then 
   184     if bShowAmmoMenu then 
   122         exit
   187         exit
   123     end;
   188     end;
   124 
   189 
   125     if isOnCurrentHog(pointerId) then
   190     if isOnCurrentHog(pointerId) then
   126     begin
   191     begin
   127     bShowAmmoMenu := true;
   192         bShowAmmoMenu := true;
   128     end;
   193         exit;
   129     //WriteToConsole(Format('%s, %s : %d, %d', [cstr(CurrentHedgehog^.Gear^.X), cstr(CurrentHedgehog^.Gear^.Y), x-WorldDX, y-WorldDY]));
   194     end;
       
   195 
       
   196     if xyCoord[pointerId*2+1] < topButtonBoundary then
       
   197     begin
       
   198         ParseCommand('hjump', true);
       
   199         exit;
       
   200     end;
   130 end;
   201 end;
   131 
   202 
   132 procedure addFinger(x,y: Longword; id: SDL_FingerId);
   203 procedure addFinger(x,y: Longword; id: SDL_FingerId);
   133 var 
   204 var 
   134     index, tmp: Longword;
   205     index, tmp: Longword;
   208     else if movingCrosshair then 
   279     else if movingCrosshair then 
   209     begin
   280     begin
   210         ParseCommand('-' + crosshairCommand, true);
   281         ParseCommand('-' + crosshairCommand, true);
   211         movingCrosshair := false;
   282         movingCrosshair := false;
   212     end;
   283     end;
       
   284 
       
   285     if stopFiring then 
       
   286     begin
       
   287         ParseCommand('-attack', true);
       
   288         stopFiring:= false;
       
   289     end;
       
   290     
       
   291     if stopRight then
       
   292     begin
       
   293         stopRight := false;
       
   294         ParseCommand('-right', true);
       
   295     end;
       
   296  
       
   297     if stopLeft then
       
   298     begin
       
   299         stopLeft := false;
       
   300         ParseCommand('-left', true);
       
   301     end;
       
   302     
   213 end;
   303 end;
   214 
   304 
   215 procedure aim(id: SDL_FingerId);
   305 procedure aim(id: SDL_FingerId);
   216 var 
   306 var 
   217     hogX, hogY, touchX, touchY, deltaX, deltaY, tmpAngle: hwFloat;
   307     hogX, hogY, touchX, touchY, deltaX, deltaY, tmpAngle: hwFloat;
   244 function convertToCursor(scale: LongInt; xy: LongInt): LongInt;
   334 function convertToCursor(scale: LongInt; xy: LongInt): LongInt;
   245 begin
   335 begin
   246     convertToCursor := round(xy/32768*scale)
   336     convertToCursor := round(xy/32768*scale)
   247 end;
   337 end;
   248 
   338 
       
   339 function isOnFireButton(id: SDL_FingerId): boolean;
       
   340 begin
       
   341     isOnFireButton:= (xyCoord[id*2] < 150) and (xyCoord[id*2+1] > 390);
       
   342 end;
       
   343 
   249 function isOnCurrentHog(id: SDL_FingerId): boolean;
   344 function isOnCurrentHog(id: SDL_FingerId): boolean;
   250 var
   345 var
   251     x,y, fingerX, fingerY : hwFloat;
   346     x,y, fingerX, fingerY : hwFloat;
   252 begin
   347 begin
   253     x := CurrentHedgehog^.Gear^.X;
   348     x := CurrentHedgehog^.Gear^.X;
   295     setLength(timeSinceDown, 5);
   390     setLength(timeSinceDown, 5);
   296     setLength(historicalXY, 10);    
   391     setLength(historicalXY, 10);    
   297     for index := Low(xyCoord) to High(xyCoord) do xyCoord[index] := -1;
   392     for index := Low(xyCoord) to High(xyCoord) do xyCoord[index] := -1;
   298     for index := Low(pointerIds) to High(pointerIds) do pointerIds[index] := -1;
   393     for index := Low(pointerIds) to High(pointerIds) do pointerIds[index] := -1;
   299     movingCrosshair := false;
   394     movingCrosshair := false;
       
   395     stopFiring:= false;
       
   396     walkingLeft := false;
       
   397     walkingRight := false;
       
   398 
       
   399     leftButtonBoundary := cScreenWidth div 4;
       
   400     rightButtonBoundary := cScreenWidth div 4*3;
       
   401     topButtonBoundary := cScreenHeight div 6;
       
   402    
   300 end;
   403 end;
   301 
   404 
   302 begin
   405 begin
   303 end.
   406 end.