hedgewars/uTouch.pas
changeset 6580 6155187bf599
parent 6344 cba81e10235c
child 6625 2d8c5815292f
equal deleted inserted replaced
6579:fc52f7c22c9b 6580:6155187bf599
   100 
   100 
   101 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId);
   101 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId);
   102 var 
   102 var 
   103     finger: PTouch_Finger;
   103     finger: PTouch_Finger;
   104 begin
   104 begin
   105     finger := addFinger(x,y,pointerId);
   105 finger := addFinger(x,y,pointerId);
   106     case pointerCount of
   106 case pointerCount of
   107         1:
   107         1:
   108         begin
   108         begin
   109             moveCursor:= false;
   109             moveCursor:= false;
   110             if bShowAmmoMenu then
   110             if bShowAmmoMenu then
   111             begin
   111             begin
   159 var
   159 var
   160     finger, secondFinger: PTouch_Finger;
   160     finger, secondFinger: PTouch_Finger;
   161     currentPinchDelta, zoom : hwFloat;
   161     currentPinchDelta, zoom : hwFloat;
   162     tmpX, tmpY: LongInt;
   162     tmpX, tmpY: LongInt;
   163 begin
   163 begin
   164     x := x;
   164 x := x;
   165     y := y;
   165 y := y;
   166     dx := dx;
   166 dx := dx;
   167     dy := dy;
   167 dy := dy;
   168     finger:= findFinger(pointerId);
   168 finger:= findFinger(pointerId);
   169     tmpX := convertToCursor(cScreenWidth, x);
   169 tmpX := convertToCursor(cScreenWidth, x);
   170     tmpY := convertToCursor(cScreenHeight, y);
   170 tmpY := convertToCursor(cScreenHeight, y);
   171 
   171 
   172     if moveCursor then
   172 if moveCursor then
   173     begin
   173     begin
   174         if invertCursor then
   174         if invertCursor then
   175         begin
   175         begin
   176             CursorPoint.X := CursorPoint.X + (finger^.x - tmpX);
   176             CursorPoint.X := CursorPoint.X + (finger^.x - tmpX);
   177             CursorPoint.Y := CursorPoint.Y - (finger^.y - tmpY);
   177             CursorPoint.Y := CursorPoint.Y - (finger^.y - tmpY);
   178         end
   178         end
   179         else
   179     else
   180         begin
   180         begin
   181             CursorPoint.X := CursorPoint.X - (finger^.x - tmpX);
   181             CursorPoint.X := CursorPoint.X - (finger^.x - tmpX);
   182             CursorPoint.Y := CursorPoint.Y + (finger^.y - tmpY);
   182             CursorPoint.Y := CursorPoint.Y + (finger^.y - tmpY);
   183         end;
   183         end;
   184         finger^.x := tmpX;
   184         finger^.x := tmpX;
   198     begin
   198     begin
   199        secondFinger := getSecondFinger(finger^);
   199        secondFinger := getSecondFinger(finger^);
   200        currentPinchDelta := calculateDelta(finger^, secondFinger^) - pinchSize;
   200        currentPinchDelta := calculateDelta(finger^, secondFinger^) - pinchSize;
   201        zoom := currentPinchDelta/cScreenWidth;
   201        zoom := currentPinchDelta/cScreenWidth;
   202        ZoomValue := baseZoomValue - ((hwFloat2Float(zoom) * cMinMaxZoomLevelDelta));
   202        ZoomValue := baseZoomValue - ((hwFloat2Float(zoom) * cMinMaxZoomLevelDelta));
   203        if ZoomValue < cMaxZoomLevel then ZoomValue := cMaxZoomLevel;
   203        if ZoomValue < cMaxZoomLevel then
   204        if ZoomValue > cMinZoomLevel then ZoomValue := cMinZoomLevel;
   204            ZoomValue := cMaxZoomLevel;
       
   205        if ZoomValue > cMinZoomLevel then
       
   206            ZoomValue := cMinZoomLevel;
   205     end;
   207     end;
   206 end;
   208 end;
   207 
   209 
   208 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
   210 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId);
   209 begin
   211 begin
   210     x := x;
   212 x := x;
   211     y := y;
   213 y := y;
   212     aiming:= false;
   214 aiming:= false;
   213     stopFiring:= true;
   215 stopFiring:= true;
   214     deleteFinger(pointerId);
   216 deleteFinger(pointerId);
   215 
   217 
   216     if walkingLeft then
   218 if walkingLeft then
   217     begin
   219     begin
   218         ParseCommand('-left', true);
   220         ParseCommand('-left', true);
   219         walkingLeft := false;
   221         walkingLeft := false;
   220     end;
   222     end;
   221 
   223 
   226     end;
   228     end;
   227 end;
   229 end;
   228 
   230 
   229 procedure onTouchDoubleClick(finger: Touch_Finger);
   231 procedure onTouchDoubleClick(finger: Touch_Finger);
   230 begin
   232 begin
   231     finger := finger;//avoid compiler hint
   233 finger := finger;//avoid compiler hint
   232     ParseCommand('ljump', true);
   234 ParseCommand('ljump', true);
   233 end;
   235 end;
   234 
   236 
   235 procedure onTouchClick(finger: Touch_Finger);
   237 procedure onTouchClick(finger: Touch_Finger);
   236 begin
   238 begin
   237     if (SDL_GetTicks - timeSinceClick < 300) and (DistanceI(finger.X-xTouchClick, finger.Y-yTouchClick) < _30) then
   239 if (SDL_GetTicks - timeSinceClick < 300) and (DistanceI(finger.X-xTouchClick, finger.Y-yTouchClick) < _30) then
   238     begin
   240     begin
   239     onTouchDoubleClick(finger);
   241     onTouchDoubleClick(finger);
   240     exit; 
   242     exit; 
   241     end
   243     end
   242     else
   244 else
   243     begin
   245     begin
   244         xTouchClick := finger.x;
   246         xTouchClick := finger.x;
   245         yTouchClick := finger.y;
   247         yTouchClick := finger.y;
   246         timeSinceClick := SDL_GetTicks;
   248         timeSinceClick := SDL_GetTicks;
   247     end;
   249     end;
   271 begin
   273 begin
   272     //Check array sizes
   274     //Check array sizes
   273     if length(fingers) < Integer(pointerCount) then 
   275     if length(fingers) < Integer(pointerCount) then 
   274     begin
   276     begin
   275         setLength(fingers, length(fingers)*2);
   277         setLength(fingers, length(fingers)*2);
   276         for index := length(fingers) div 2 to length(fingers) do fingers[index].id := nilFingerId;
   278         for index := length(fingers) div 2 to length(fingers) do
       
   279             fingers[index].id := nilFingerId;
   277     end;
   280     end;
   278     
   281     
   279     
   282     
   280     xCursor := convertToCursor(cScreenWidth, x);
   283     xCursor := convertToCursor(cScreenWidth, x);
   281     yCursor := convertToCursor(cScreenHeight, y);
   284     yCursor := convertToCursor(cScreenHeight, y);
   299 begin
   302 begin
   300     
   303     
   301     dec(pointerCount);
   304     dec(pointerCount);
   302     for index := 0 to pointerCount do
   305     for index := 0 to pointerCount do
   303     begin
   306     begin
   304          if fingers[index].id = id then
   307         if fingers[index].id = id then
   305          begin
   308         begin
   306              //Check for onTouchClick event
   309             //Check for onTouchClick event
   307              if ((SDL_GetTicks - fingers[index].timeSinceDown) < clickTime) AND  
   310             if ((SDL_GetTicks - fingers[index].timeSinceDown) < clickTime) AND not(fingerHasMoved(fingers[index])) then
   308                  not(fingerHasMoved(fingers[index])) then onTouchClick(fingers[index]);
   311                 onTouchClick(fingers[index]);
   309 
   312  
   310              //put the last finger into the spot of the finger to be removed, 
   313             //put the last finger into the spot of the finger to be removed, 
   311              //so that all fingers are packed to the far left
   314             //so that all fingers are packed to the far left
   312              if  pointerCount <> index then
   315             if  pointerCount <> index then
   313              begin
   316                 begin
   314                 fingers[index].id := fingers[pointerCount].id;    
   317                 fingers[index].id := fingers[pointerCount].id;    
   315                 fingers[index].x := fingers[pointerCount].x;    
   318                 fingers[index].x := fingers[pointerCount].x;    
   316                 fingers[index].y := fingers[pointerCount].y;    
   319                 fingers[index].y := fingers[pointerCount].y;    
   317                 fingers[index].historicalX := fingers[pointerCount].historicalX;    
   320                 fingers[index].historicalX := fingers[pointerCount].historicalX;    
   318                 fingers[index].historicalY := fingers[pointerCount].historicalY;    
   321                 fingers[index].historicalY := fingers[pointerCount].historicalY;    
   319                 fingers[index].timeSinceDown := fingers[pointerCount].timeSinceDown;
   322                 fingers[index].timeSinceDown := fingers[pointerCount].timeSinceDown;
   320 
   323 
   321                 fingers[pointerCount].id := nilFingerId;
   324                 fingers[pointerCount].id := nilFingerId;
   322              end
   325             end
   323              else fingers[index].id := nilFingerId;
   326         else fingers[index].id := nilFingerId;
   324              break;
   327             break;
   325          end;
   328         end;
   326     end;
   329     end;
   327 
   330 
   328 end;
   331 end;
   329 
   332 
   330 procedure ProcessTouch;
   333 procedure ProcessTouch;
   333 begin
   336 begin
   334     invertCursor := not(bShowAmmoMenu);
   337     invertCursor := not(bShowAmmoMenu);
   335     if aiming then
   338     if aiming then
   336     begin
   339     begin
   337         if CurrentHedgehog^.Gear <> nil then
   340         if CurrentHedgehog^.Gear <> nil then
   338         begin
   341             begin
   339             deltaAngle:= CurrentHedgehog^.Gear^.Angle - targetAngle;
   342             deltaAngle:= CurrentHedgehog^.Gear^.Angle - targetAngle;
   340             if (deltaAngle <> 0) and not(movingCrosshair) then 
   343             if (deltaAngle <> 0) and not(movingCrosshair) then 
   341             begin
   344                 begin
   342                 ParseCommand('+' + crosshairCommand, true);
   345                 ParseCommand('+' + crosshairCommand, true);
   343                 movingCrosshair := true;
   346                 movingCrosshair := true;
   344             end
   347                 end
   345             else 
   348             else 
   346                 if movingCrosshair then 
   349                 if movingCrosshair then 
   347                 begin
   350                     begin
   348                     ParseCommand('-' + crosshairCommand, true);
   351                     ParseCommand('-' + crosshairCommand, true);
   349                     movingCrosshair:= false;
   352                     movingCrosshair:= false;
   350                 end;
   353                    end;
   351         end;
   354             end;
   352     end
   355     end
   353     else if movingCrosshair then 
   356     else if movingCrosshair then 
   354     begin
   357         begin
   355         ParseCommand('-' + crosshairCommand, true);
   358         ParseCommand('-' + crosshairCommand, true);
   356         movingCrosshair := false;
   359         movingCrosshair := false;
   357     end;
   360         end;
   358 
   361 
   359     if stopFiring then 
   362     if stopFiring then 
   360     begin
   363         begin
   361         ParseCommand('-attack', true);
   364         ParseCommand('-attack', true);
   362         stopFiring:= false;
   365         stopFiring:= false;
   363     end;
   366         end;
   364     
   367     
   365     if stopRight then
   368     if stopRight then
   366     begin
   369         begin
   367         stopRight := false;
   370         stopRight := false;
   368         ParseCommand('-right', true);
   371         ParseCommand('-right', true);
   369     end;
   372         end;
   370  
   373  
   371     if stopLeft then
   374     if stopLeft then
   372     begin
   375         begin
   373         stopLeft := false;
   376         stopLeft := false;
   374         ParseCommand('-left', true);
   377         ParseCommand('-left', true);
   375     end;
   378         end;
   376     
   379     
   377 end;
   380 end;
   378 
   381 
   379 function findFinger(id: SDL_FingerId): PTouch_Finger;
   382 function findFinger(id: SDL_FingerId): PTouch_Finger;
   380 var
   383 var
   381     index: LongWord;
   384     index: LongWord;
   382 begin
   385 begin
   383    for index := 0 to High(fingers) do
   386     for index := 0 to High(fingers) do
   384        if fingers[index].id = id then 
   387         if fingers[index].id = id then 
   385        begin
   388             begin
   386            findFinger := @fingers[index];
   389             findFinger := @fingers[index];
   387            break;
   390             break;
   388        end;
   391             end;
   389 end;
   392 end;
   390 
   393 
   391 procedure aim(finger: Touch_Finger);
   394 procedure aim(finger: Touch_Finger);
   392 var 
   395 var 
   393     hogX, hogY, touchX, touchY, deltaX, deltaY, tmpAngle: hwFloat;
   396     hogX, hogY, touchX, touchY, deltaX, deltaY, tmpAngle: hwFloat;
   406         
   409         
   407         tmpAngle:= DeltaY / Distance(deltaX, deltaY) *_2048;
   410         tmpAngle:= DeltaY / Distance(deltaX, deltaY) *_2048;
   408         targetAngle:= (hwRound(tmpAngle) + 2048) div 2;
   411         targetAngle:= (hwRound(tmpAngle) + 2048) div 2;
   409 
   412 
   410         tmp := crosshairCommand;
   413         tmp := crosshairCommand;
   411         if CurrentHedgehog^.Gear^.Angle - targetAngle < 0 then crosshairCommand := 'down'
   414         if CurrentHedgehog^.Gear^.Angle - targetAngle < 0 then
   412         else crosshairCommand:= 'up';
   415             crosshairCommand := 'down'
       
   416         else
       
   417             crosshairCommand:= 'up';
   413         if movingCrosshair and (tmp <> crosshairCommand) then 
   418         if movingCrosshair and (tmp <> crosshairCommand) then 
   414         begin
   419             begin
   415             ParseCommand('-' + tmp, true);
   420             ParseCommand('-' + tmp, true);
   416             movingCrosshair := false;
   421             movingCrosshair := false;
   417         end;
   422             end;
   418 
   423 
   419     end; //if CurrentHedgehog^.Gear <> nil
   424     end; //if CurrentHedgehog^.Gear <> nil
   420 end;
   425 end;
   421 
   426 
   422 function convertToCursor(scale: LongInt; xy: LongInt): LongInt;
   427 function convertToCursor(scale: LongInt; xy: LongInt): LongInt;
   476 
   481 
   477 // Under the premise that all pointer ids in pointerIds:SDL_FingerId are packed to the far left.
   482 // Under the premise that all pointer ids in pointerIds:SDL_FingerId are packed to the far left.
   478 // If the pointer to be ignored is not pointerIds[0] the second must be there
   483 // If the pointer to be ignored is not pointerIds[0] the second must be there
   479 function getSecondFinger(finger: Touch_Finger): PTouch_Finger;
   484 function getSecondFinger(finger: Touch_Finger): PTouch_Finger;
   480 begin
   485 begin
   481     if fingers[0].id = finger.id then getSecondFinger := @fingers[1]
   486     if fingers[0].id = finger.id then
   482     else getSecondFinger := @fingers[0];
   487         getSecondFinger := @fingers[1]
       
   488     else
       
   489         getSecondFinger := @fingers[0];
   483 end;
   490 end;
   484 
   491 
   485 procedure printFinger(finger: Touch_Finger);
   492 procedure printFinger(finger: Touch_Finger);
   486 begin
   493 begin
   487     WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));
   494     WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown]));