-
Notifications
You must be signed in to change notification settings - Fork 1
/
TouchScroll.cs
281 lines (213 loc) · 5.84 KB
/
TouchScroll.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Camera))]
public class TouchScroll : MonoBehaviour {
//enums
private enum InputState { Down, Up}
public enum Axis { XAndY, XOnly, YOnly }
//private variables
private Vector3 m_velocity;
private Camera r_camera;
private bool m_isEnabled;
// Inspector Options
[Range(0.001f,1f)]
public float drag = 0.06f;
public Axis axis = Axis.XAndY;
public bool autoEnable = true;
//limits
public bool doLimitHorizontal;
public float limitLeft;
public float limitRight;
public bool doLimitVertical;
public float limitTop;
public float limitBottom;
[Range(0f, 1f)]
public float bouncePower = 0.16f;
[Range(0f, .3f)]
public float offLimitFriction = 0.18f;
//passed values from touch detection
private Vector2 m_oldPos;
private float m_oldTime;
// Returns if mouse or touch is down/up, depending on platform support for touchscreen
private InputState m_inputState
{
get
{
#if (UNITY_ANDROID || UNITY_IOS || UNITY_WP8 || UNITY_WP8_1 || UNITY_BLACKBERRY) && ! UNITY_EDITOR
if(Input.touchCount > 0)
return InputState.Down;
else
return InputState.Up;
#else
if(Input.GetMouseButton(0))
return InputState.Down;
else
return InputState.Up;
#endif
}
}
// Returns touch or mouses position on world space
private Vector2 m_cursorWorldPosition
{
get
{
#if (UNITY_ANDROID || UNITY_IOS || UNITY_WP8 || UNITY_WP8_1 || UNITY_BLACKBERRY) && ! UNITY_EDITOR
if (Input.touchCount > 0)
return r_camera.ScreenToWorldPoint(Input.GetTouch(0).position);
else
return r_camera.ScreenToWorldPoint(Vector2.zero);
#else
return r_camera.ScreenToWorldPoint(Input.mousePosition);
#endif
}
}
// Initting on awake
void Awake()
{
r_camera = GetComponent<Camera>();
if(! r_camera.orthographic)
Debug.LogError("Camera attached to game object (" + gameObject.name + ") should be orthographic. Scrolling will not work properly");
m_isEnabled = false;
m_velocity = Vector3.zero;
}
void Start()
{
if(autoEnable)
Enable();
}
// Main coroutine loop.
private IEnumerator c_Scroll()
{
while (true)
{
yield return StartCoroutine("c_DetectTouch");
yield return StartCoroutine("c_CalculateVelocity");
yield return StartCoroutine("c_ApplyVelocity");
}
}
// Detects touch start.
private IEnumerator c_DetectTouch()
{
while (true)
{
if(m_inputState == InputState.Down)
break;
yield return null;
}
m_oldPos = m_cursorWorldPosition;
m_oldTime = Time.time;
}
// While touching, detects velocity, moves camera with touch.move and limits camera movement.
private IEnumerator c_CalculateVelocity()
{
Vector2 oldPos = m_oldPos;
Vector2 calculatedVelocity = Vector2.zero;
Vector2 oldCalculatedVelocity = Vector2.zero;
float oldTime = m_oldTime;
while (m_inputState == InputState.Down)
{
float now = Time.time;
float elapsed = now - oldTime;
Vector2 pos = m_cursorWorldPosition;
Vector2 deltaPos = pos - oldPos;
if (axis == Axis.XOnly)
deltaPos.y = 0f;
else if(axis == Axis.YOnly)
deltaPos.x = 0f;
Vector2 curVelocity = deltaPos / (1f+elapsed);
calculatedVelocity = 0.8f*curVelocity + 0.2f*calculatedVelocity;
if(calculatedVelocity.magnitude < oldCalculatedVelocity.magnitude)
calculatedVelocity = Vector2.Lerp(oldCalculatedVelocity, calculatedVelocity, .5f);
Vector3 deltaPos3 = new Vector3(deltaPos.x, deltaPos.y);
LimitBoundsWhileDragging(ref deltaPos3);
transform.position -= deltaPos3;
oldTime = now;
oldPos = m_cursorWorldPosition;
oldCalculatedVelocity = calculatedVelocity;
yield return null;
}
m_velocity = calculatedVelocity;
}
// After touch ended, continues to move camera with momentum.
private IEnumerator c_ApplyVelocity()
{
float oldTime = Time.time;
while (m_inputState == InputState.Up)
{
float now = Time.time;
float elapsed = now - oldTime;
float dragMultiplier = Mathf.Exp(-elapsed / 1f-drag);
m_velocity *= dragMultiplier;
transform.position -= m_velocity;
oldTime = now;
if(doLimitHorizontal || doLimitVertical)
LimitBoundsWhileDrifting(ref m_velocity, dragMultiplier);
yield return new WaitForFixedUpdate();
}
}
// Enables scrolling
public void Enable()
{
if (m_isEnabled)
return;
m_isEnabled = true;
StartCoroutine("c_Scroll");
}
//Disables scrolling
public void Disable()
{
if (! m_isEnabled)
return;
m_isEnabled = false;
StopAllCoroutines();
}
//Limit bounds after touch ended
private void LimitBoundsWhileDrifting(ref Vector3 velocity, float dragMultiplier)
{
bool didChanged = false;
Vector3 pos = transform.position;
if(doLimitVertical)
{
if(pos.y > limitTop) {
didChanged = true;
pos.y = limitTop;
}
if(pos.y < limitBottom) {
didChanged = true;
pos.y = limitBottom;
}
}
if(doLimitHorizontal)
{
if(pos.x > limitRight) {
didChanged = true;
pos.x = limitRight;
}
if(pos.x < limitLeft) {
didChanged = true;
pos.x = limitLeft;
}
}
if(didChanged)
{
velocity *= dragMultiplier;
transform.position = Vector3.Lerp(transform.position, pos, bouncePower);
}
}
//makes harder to scroll away from bounds while dragging
private void LimitBoundsWhileDragging(ref Vector3 deltaPos)
{
Vector3 nextPos = transform.position - deltaPos;
if(nextPos.y > limitTop)
deltaPos.y = Mathf.Lerp(deltaPos.y, 0f, (nextPos.y-limitTop)*offLimitFriction);
if(nextPos.y < limitBottom)
deltaPos.y = Mathf.Lerp(deltaPos.y, 0f, (limitBottom-nextPos.y)*offLimitFriction);
if(nextPos.x > limitRight)
deltaPos.x = Mathf.Lerp(deltaPos.x, 0f, (nextPos.x-limitRight)*offLimitFriction);
if(nextPos.x < limitLeft)
deltaPos.x = Mathf.Lerp(deltaPos.x, 0f, (limitLeft-nextPos.x)*offLimitFriction);
}
//TODO movement events
//TODO multitouch bug
//TODO parameter documentation
}