| 1 |
{ |
| 2 |
Copyright 2001-2024 Michalis Kamburelis, Jan Adamec. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Recognize touch gestures. } |
| 17 |
unit CastleInternalCameraGestures; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses Classes, |
| 24 |
CastleKeysMouse, CastleVectors; |
| 25 |
|
| 26 |
type |
| 27 |
TCastleGestureType = (gtNone, gtPinch, gtPan); |
| 28 |
|
| 29 |
{ This gesture recognizer detects pan and pinch gesture, as both use two fingers, |
| 30 |
but cannot be used at the same time. |
| 31 |
|
| 32 |
Pass the input events using Press, Motion and Release functions, listen |
| 33 |
to recognized gestures in @link(OnGestureChanged) event. } |
| 34 |
TCastlePinchPanGestureRecognizer = class |
| 35 |
strict private |
| 36 |
FGesture: TCastleGestureType; |
| 37 |
FPanMove: TVector2; |
| 38 |
FPinchScaleFactor: Single; |
| 39 |
//FPinchCenter: TVector2; |
| 40 |
|
| 41 |
FOnGestureChanged: TNotifyEvent; |
| 42 |
|
| 43 |
FFinger0Pressed, FFinger1Pressed: Boolean; |
| 44 |
// stored position of the fingers, as we get only one of them in Motion event |
| 45 |
FFinger0Pos, FFinger1Pos: TVector2; |
| 46 |
// gesture start finger positions |
| 47 |
FFinger0StartPos, FFinger1StartPos: TVector2; |
| 48 |
public |
| 49 |
constructor Create; |
| 50 |
|
| 51 |
{ Functions to pass the input to the recognizer from some @link(TCastleUserInterface). |
| 52 |
@groupBegin } |
| 53 |
function Press(const Event: TInputPressRelease): Boolean; |
| 54 |
function Release(const Event: TInputPressRelease): Boolean; |
| 55 |
function Motion(const Event: TInputMotion; const Dpi: Single): Boolean; |
| 56 |
{ @groupEnd } |
| 57 |
|
| 58 |
{ Gesture type once it's recognized. Check it inside OnGestureChanged event. } |
| 59 |
property Gesture: TCastleGestureType read FGesture; |
| 60 |
|
| 61 |
{ Movement of the current pan gesture. } |
| 62 |
property PanMove: TVector2 read FPanMove; |
| 63 |
|
| 64 |
{ Scale factor of the pinch gesture. |
| 65 |
- < 1.0 means zoom out |
| 66 |
- 1.0 means no change |
| 67 |
- > 1.0 means zoom in. } |
| 68 |
property PinchScaleFactor: Single read FPinchScaleFactor; |
| 69 |
|
| 70 |
{ Coordinates of the pinch gesture center. } |
| 71 |
// unused |
| 72 |
//property PinchCenter: TVector2 read FPinchCenter; |
| 73 |
|
| 74 |
{ Listen to this event to receive updates on recognized gestures. } |
| 75 |
property OnGestureChanged: TNotifyEvent |
| 76 |
read FOnGestureChanged write FOnGestureChanged; |
| 77 |
end; |
| 78 |
|
| 79 |
implementation |
| 80 |
|
| 81 |
uses Math, |
| 82 |
CastleUtils; |
| 83 |
|
| 84 |
{ TCastlePinchPanGestureRecognizer ------------------------------------------- } |
| 85 |
|
| 86 |
constructor TCastlePinchPanGestureRecognizer.Create; |
| 87 |
begin |
| 88 |
inherited; |
| 89 |
FGesture := gtNone; |
| 90 |
end; |
| 91 |
|
| 92 |
function TCastlePinchPanGestureRecognizer.Press(const Event: TInputPressRelease): Boolean; |
| 93 |
begin |
| 94 |
if Event.FingerIndex = 0 then |
| 95 |
begin |
| 96 |
FFinger0StartPos := Event.Position; |
| 97 |
FFinger0Pos := Event.Position; |
| 98 |
FFinger0Pressed := true; |
| 99 |
end else |
| 100 |
if Event.FingerIndex = 1 then |
| 101 |
begin |
| 102 |
FFinger1StartPos := Event.Position; |
| 103 |
FFinger1Pos := Event.Position; |
| 104 |
FFinger1Pressed := true; |
| 105 |
end; |
| 106 |
Result := FFinger0Pressed and FFinger1Pressed; |
| 107 |
end; |
| 108 |
|
| 109 |
function TCastlePinchPanGestureRecognizer.Release(const Event: TInputPressRelease): Boolean; |
| 110 |
var |
| 111 |
OldBothPressed, NewBothPressed: Boolean; |
| 112 |
begin |
| 113 |
Result := false; |
| 114 |
|
| 115 |
OldBothPressed := FFinger0Pressed and FFinger1Pressed; |
| 116 |
|
| 117 |
if Event.FingerIndex = 0 then |
| 118 |
FFinger0Pressed := false |
| 119 |
else |
| 120 |
if Event.FingerIndex = 1 then |
| 121 |
FFinger1Pressed := false; |
| 122 |
|
| 123 |
NewBothPressed := FFinger0Pressed and FFinger1Pressed; |
| 124 |
|
| 125 |
// finger release stopped the gesture, mark as handled |
| 126 |
if OldBothPressed and not NewBothPressed then |
| 127 |
Exit(true); |
| 128 |
end; |
| 129 |
|
| 130 |
function TCastlePinchPanGestureRecognizer.Motion(const Event: TInputMotion; |
| 131 |
const Dpi: Single): Boolean; |
| 132 |
|
| 133 |
{ CosAngleBetweenVectors and AngleRadBetweenVectors are mostly copied from |
| 134 |
CastleVectors unit and adjusted for 2D vectors (and not make exception |
| 135 |
even on zero vectors, which may happen here). } |
| 136 |
|
| 137 |
function CosAngleBetweenVectors(const V1, V2: TVector2): Single; |
| 138 |
var |
| 139 |
LensSquared: Single; |
| 140 |
begin |
| 141 |
LensSquared := v1.LengthSqr * v2.LengthSqr; |
| 142 |
if IsZero(LensSquared) then |
| 143 |
Result := 1 |
| 144 |
else |
| 145 |
Result := Clamped(TVector2.DotProduct(V1, V2) / Sqrt(LensSquared), -1.0, 1.0); |
| 146 |
end; |
| 147 |
|
| 148 |
function AngleRadBetweenVectors(const V1, V2: TVector2): Single; |
| 149 |
begin |
| 150 |
Result := ArcCos(CosAngleBetweenVectors(V1, V2)); |
| 151 |
end; |
| 152 |
|
| 153 |
var |
| 154 |
OldDist, NewDist: Single; |
| 155 |
// Length0, Length1: Single; |
| 156 |
ParallelFingerMovement: Boolean; |
| 157 |
begin |
| 158 |
Result := false; |
| 159 |
|
| 160 |
if Event.FingerIndex = 0 then |
| 161 |
begin |
| 162 |
FFinger0StartPos := FFinger0Pos; |
| 163 |
FFinger0Pos := Event.Position |
| 164 |
end else |
| 165 |
if Event.FingerIndex = 1 then |
| 166 |
begin |
| 167 |
FFinger1StartPos := FFinger1Pos; |
| 168 |
FFinger1Pos := Event.Position |
| 169 |
end else |
| 170 |
Exit(false); // moving with additional finger |
| 171 |
|
| 172 |
if (not FFinger0Pressed) or (not FFinger1Pressed) then |
| 173 |
Exit(false); |
| 174 |
|
| 175 |
// Length0 := PointsDistance(FFinger0Pos, FFinger0StartPos); |
| 176 |
// Length1 := PointsDistance(FFinger1Pos, FFinger1StartPos); |
| 177 |
|
| 178 |
// angle less then 60 deg |
| 179 |
ParallelFingerMovement := AngleRadBetweenVectors( |
| 180 |
FFinger0Pos - FFinger0StartPos, |
| 181 |
FFinger1Pos - FFinger1StartPos) < 1.0; |
| 182 |
|
| 183 |
{ For pinch gesture, the fingers move either closer or further apart. |
| 184 |
For pan gesture, the fingers move in the same direction. } |
| 185 |
if ParallelFingerMovement then |
| 186 |
begin |
| 187 |
FGesture := gtPan; |
| 188 |
|
| 189 |
{ Note: both fingers movement determines pan. } |
| 190 |
FPanMove := 0.5 * ( |
| 191 |
(FFinger0StartPos - FFinger0Pos) + |
| 192 |
(FFinger1StartPos - FFinger1Pos) |
| 193 |
); |
| 194 |
|
| 195 |
if Assigned(FOnGestureChanged) then |
| 196 |
FOnGestureChanged(Self); |
| 197 |
Result := true; |
| 198 |
end else |
| 199 |
begin |
| 200 |
FGesture := gtPinch; |
| 201 |
|
| 202 |
OldDist := PointsDistance(FFinger0StartPos, FFinger1StartPos); |
| 203 |
NewDist := PointsDistance(FFinger0Pos, FFinger1Pos); |
| 204 |
//FPinchCenter := (FFinger0Pos + FFinger1Pos) / 2.0; |
| 205 |
FPinchScaleFactor := NewDist / OldDist; |
| 206 |
|
| 207 |
if Assigned(FOnGestureChanged) then |
| 208 |
FOnGestureChanged(Self); |
| 209 |
Result := true; |
| 210 |
end; |
| 211 |
end; |
| 212 |
|
| 213 |
end. |