-
Notifications
You must be signed in to change notification settings - Fork 11
/
PanAndZoom.cs
264 lines (208 loc) · 9.9 KB
/
PanAndZoom.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using UnityEngine;
using System;
using System.Collections.Generic;
using UnityEngine.EventSystems;
/// <summary> A modular and easily customisable Unity MonoBehaviour for handling swipe and pinch motions on mobile. </summary>
public class PanAndZoom : MonoBehaviour {
/// <summary> Called as soon as the player touches the screen. The argument is the screen position. </summary>
public event Action<Vector2> onStartTouch;
/// <summary> Called as soon as the player stops touching the screen. The argument is the screen position. </summary>
public event Action<Vector2> onEndTouch;
/// <summary> Called if the player completed a quick tap motion. The argument is the screen position. </summary>
public event Action<Vector2> onTap;
/// <summary> Called if the player swiped the screen. The argument is the screen movement delta. </summary>
public event Action<Vector2> onSwipe;
/// <summary> Called if the player pinched the screen. The arguments are the distance between the fingers before and after. </summary>
public event Action<float, float> onPinch;
[Header("Tap")]
[Tooltip("The maximum movement for a touch motion to be treated as a tap")]
public float maxDistanceForTap = 40;
[Tooltip("The maximum duration for a touch motion to be treated as a tap")]
public float maxDurationForTap = 0.4f;
[Header("Desktop debug")]
[Tooltip("Use the mouse on desktop?")]
public bool useMouse = true;
[Tooltip("The simulated pinch speed using the scroll wheel")]
public float mouseScrollSpeed = 2;
[Header("Camera control")]
[Tooltip("Does the script control camera movement?")]
public bool controlCamera = true;
[Tooltip("The controlled camera, ignored of controlCamera=false")]
public Camera cam;
[Header("UI")]
[Tooltip("Are touch motions listened to if they are over UI elements?")]
public bool ignoreUI = false;
[Header("Bounds")]
[Tooltip("Is the camera bound to an area?")]
public bool useBounds;
public float boundMinX = -150;
public float boundMaxX = 150;
public float boundMinY = -150;
public float boundMaxY = 150;
Vector2 touch0StartPosition;
Vector2 touch0LastPosition;
float touch0StartTime;
bool cameraControlEnabled = true;
bool canUseMouse;
/// <summary> Has the player at least one finger on the screen? </summary>
public bool isTouching { get; private set; }
/// <summary> The point of contact if it exists in Screen space. </summary>
public Vector2 touchPosition { get { return touch0LastPosition; } }
void Start() {
canUseMouse = Application.platform != RuntimePlatform.Android && Application.platform != RuntimePlatform.IPhonePlayer && Input.mousePresent;
}
void Update() {
if (useMouse && canUseMouse) {
UpdateWithMouse();
} else {
UpdateWithTouch();
}
}
void LateUpdate() {
CameraInBounds();
}
void UpdateWithMouse() {
if (Input.GetMouseButtonDown(0)) {
if (ignoreUI || !IsPointerOverUIObject()) {
touch0StartPosition = Input.mousePosition;
touch0StartTime = Time.time;
touch0LastPosition = touch0StartPosition;
isTouching = true;
if (onStartTouch != null) onStartTouch(Input.mousePosition);
}
}
if (Input.GetMouseButton(0) && isTouching) {
Vector2 move = (Vector2)Input.mousePosition - touch0LastPosition;
touch0LastPosition = Input.mousePosition;
if (move != Vector2.zero) {
OnSwipe(move);
}
}
if (Input.GetMouseButtonUp(0) && isTouching) {
if (Time.time - touch0StartTime <= maxDurationForTap
&& Vector2.Distance(Input.mousePosition, touch0StartPosition) <= maxDistanceForTap) {
OnClick(Input.mousePosition);
}
if (onEndTouch != null) onEndTouch(Input.mousePosition);
isTouching = false;
cameraControlEnabled = true;
}
if (Input.mouseScrollDelta.y != 0) {
OnPinch(Input.mousePosition, 1, Input.mouseScrollDelta.y < 0 ? (1 / mouseScrollSpeed) : mouseScrollSpeed, Vector2.right);
}
}
void UpdateWithTouch() {
int touchCount = Input.touches.Length;
if (touchCount == 1) {
Touch touch = Input.touches[0];
switch (touch.phase) {
case TouchPhase.Began: {
if (ignoreUI || !IsPointerOverUIObject()) {
touch0StartPosition = touch.position;
touch0StartTime = Time.time;
touch0LastPosition = touch0StartPosition;
isTouching = true;
if (onStartTouch != null) onStartTouch(touch0StartPosition);
}
break;
}
case TouchPhase.Moved: {
touch0LastPosition = touch.position;
if (touch.deltaPosition != Vector2.zero && isTouching) {
OnSwipe(touch.deltaPosition);
}
break;
}
case TouchPhase.Ended: {
if (Time.time - touch0StartTime <= maxDurationForTap
&& Vector2.Distance(touch.position, touch0StartPosition) <= maxDistanceForTap
&& isTouching) {
OnClick(touch.position);
}
if (onEndTouch != null) onEndTouch(touch.position);
isTouching = false;
cameraControlEnabled = true;
break;
}
case TouchPhase.Stationary:
case TouchPhase.Canceled:
break;
}
} else if (touchCount == 2) {
Touch touch0 = Input.touches[0];
Touch touch1 = Input.touches[1];
if (touch0.phase == TouchPhase.Ended || touch1.phase == TouchPhase.Ended) return;
isTouching = true;
float previousDistance = Vector2.Distance(touch0.position - touch0.deltaPosition, touch1.position - touch1.deltaPosition);
float currentDistance = Vector2.Distance(touch0.position, touch1.position);
if (previousDistance != currentDistance) {
OnPinch((touch0.position + touch1.position) / 2, previousDistance, currentDistance, (touch1.position - touch0.position).normalized);
}
} else {
if (isTouching) {
if (onEndTouch != null) onEndTouch(touch0LastPosition);
isTouching = false;
}
cameraControlEnabled = true;
}
}
void OnClick(Vector2 position) {
if (onTap != null && (ignoreUI || !IsPointerOverUIObject())) {
onTap(position);
}
}
void OnSwipe(Vector2 deltaPosition) {
if (onSwipe != null) {
onSwipe(deltaPosition);
}
if (controlCamera && cameraControlEnabled) {
if (cam == null) cam = Camera.main;
cam.transform.position -= (cam.ScreenToWorldPoint(deltaPosition) - cam.ScreenToWorldPoint(Vector2.zero));
}
}
void OnPinch(Vector2 center, float oldDistance, float newDistance, Vector2 touchDelta) {
if (onPinch != null) {
onPinch(oldDistance, newDistance);
}
if (controlCamera && cameraControlEnabled) {
if (cam == null) cam = Camera.main;
if (cam.orthographic) {
var currentPinchPosition = cam.ScreenToWorldPoint(center);
cam.orthographicSize = Mathf.Max(0.1f, cam.orthographicSize * oldDistance / newDistance);
var newPinchPosition = cam.ScreenToWorldPoint(center);
cam.transform.position -= newPinchPosition - currentPinchPosition;
} else {
cam.fieldOfView = Mathf.Clamp(cam.fieldOfView * oldDistance / newDistance, 0.1f, 179.9f);
}
}
}
/// <summary> Checks if the the current input is over canvas UI </summary>
public bool IsPointerOverUIObject() {
if (EventSystem.current == null) return false;
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
return results.Count > 0;
}
/// <summary> Cancels camera movement for the current motion. Resets to use camera at the end of the touch motion.</summary>
public void CancelCamera() {
cameraControlEnabled = false;
}
void CameraInBounds() {
if(controlCamera && useBounds && cam != null && cam.orthographic) {
cam.orthographicSize = Mathf.Min(cam.orthographicSize, ((boundMaxY - boundMinY) / 2) - 0.001f);
cam.orthographicSize = Mathf.Min(cam.orthographicSize, (Screen.height * (boundMaxX - boundMinX) / (2 * Screen.width)) - 0.001f);
Vector2 margin = cam.ScreenToWorldPoint((Vector2.up * Screen.height / 2) + (Vector2.right * Screen.width / 2)) - cam.ScreenToWorldPoint(Vector2.zero);
float marginX = margin.x;
float marginY = margin.y;
float camMaxX = boundMaxX - marginX;
float camMaxY = boundMaxY - marginY;
float camMinX = boundMinX + marginX;
float camMinY = boundMinY + marginY;
float camX = Mathf.Clamp(cam.transform.position.x, camMinX, camMaxX);
float camY = Mathf.Clamp(cam.transform.position.y, camMinY, camMaxY);
cam.transform.position = new Vector3(camX, camY, cam.transform.position.z);
}
}
}