wangguan
2020-12-22 3c54c3efb141adf11146eff7678e363f1be9cad3
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using UnityEngine;
using UnityInput = UnityEngine.Input;
 
namespace Core.Input
{
    /// <summary>
    /// Base control scheme for touch devices, which performs CameraRig control
    /// </summary>
    public class TouchInput : CameraInputScheme
    {
        /// <summary>
        /// Configuration of the pan speed
        /// </summary>
        public float panSpeed = 5;
 
        /// <summary>
        /// How quickly flicks decay
        /// </summary>
        public float flickDecayFactor = 0.2f;
 
        /// <summary>
        /// Flick direction
        /// </summary>
        Vector3 m_FlickDirection;
 
        /// <summary>
        /// Gets whether the scheme should be activated or not
        /// </summary>
        public override bool shouldActivate
        {
            get { return UnityInput.touchCount > 0; }
        }
 
        /// <summary>
        /// This default scheme on IOS and Android devices
        /// </summary>
        public override bool isDefault
        {
            get
            {
#if UNITY_IOS || UNITY_ANDROID
                return true;
#else
                return false;
#endif
            }
        }
 
        /// <summary>
        /// Register input events
        /// </summary>
        protected virtual void OnEnable()
        {
            if (!InputController.instanceExists)
            {
                Debug.LogError("[UI] Keyboard and Mouse UI requires InputController");
                return;
            }
            
            // Register drag event
            InputController inputController = InputController.instance;
            inputController.pressed += OnPress;
            inputController.released += OnRelease;
            inputController.dragged += OnDrag;
            inputController.pinched += OnPinch;
        }
        
        /// <summary>
        /// Deregister input events
        /// </summary>
        protected virtual void OnDisable()
        {
            if (!InputController.instanceExists)
            {
                return;
            }
            
            if (InputController.instanceExists)
            {
                InputController inputController = InputController.instance;
                inputController.pressed -= OnPress;
                inputController.released -= OnRelease;
                inputController.dragged -= OnDrag;
                inputController.pinched -= OnPinch;
            }
        }
 
        /// <summary>
        /// Perform flick and zoom
        /// </summary>
        protected virtual void Update()
        {
            if (cameraRig != null)
            {
                UpdateFlick();
                DecayZoom();
            }
        }
 
        /// <summary>
        /// Called on input press
        /// </summary>
        protected virtual void OnPress(PointerActionInfo pointer)
        {
            if (cameraRig != null)
            {
                DoFlickCatch(pointer);
            }
        }
        
        /// <summary>
        /// Called on input release
        /// </summary>
        protected virtual void OnRelease(PointerActionInfo pointer)
        {
            if (cameraRig != null)
            {
                DoReleaseFlick(pointer);
            }
        }
 
        /// <summary>
        /// Called when we drag
        /// </summary>
        protected virtual void OnDrag(PointerActionInfo pointer)
        {
            // Drag panning for touch input
            if (cameraRig != null)
            {
                DoDragPan(pointer);
            }
        }
 
        /// <summary>
        /// Called on pinch gestures
        /// </summary>
        protected virtual void OnPinch(PinchInfo pinch)
        {
            if (cameraRig != null)
            {
                DoPinchZoom(pinch);
            }
        }
 
        /// <summary>
        /// Update current flick velocity
        /// </summary>
        protected void UpdateFlick()
        {
            // Flick?
            if (m_FlickDirection.sqrMagnitude > Mathf.Epsilon)
            {
                cameraRig.PanCamera(m_FlickDirection * Time.deltaTime);
                m_FlickDirection *= flickDecayFactor;
            }
        }
 
        /// <summary>
        /// Decay the zoom if no touches are active
        /// </summary>
        protected void DecayZoom()
        {
            if (InputController.instance.activeTouchCount == 0)
            {
                cameraRig.ZoomDecay();
            }
        }
 
        /// <summary>
        /// "Catch" flicks on press, to stop the panning momentum
        /// </summary>
        /// <param name="pointer">The press pointer event</param>
        protected void DoFlickCatch(PointerActionInfo pointer)
        {
            var touchInfo = pointer as TouchInfo;
            // Stop flicks on touch
            if (touchInfo != null)
            {
                m_FlickDirection = Vector2.zero;
                cameraRig.StopTracking();
            }
        }
        
        /// <summary>
        /// Do flicks, on release only
        /// </summary>
        /// <param name="pointer">The release pointer event</param>
        protected void DoReleaseFlick(PointerActionInfo pointer)
        {
            var touchInfo = pointer as TouchInfo;
 
            if (touchInfo != null && touchInfo.flickVelocity.sqrMagnitude > Mathf.Epsilon)
            {
                // We have a flick!
                // Work out velocity from motion
                Ray prevRay = cameraRig.cachedCamera.ScreenPointToRay(pointer.currentPosition -
                                                                        pointer.flickVelocity);
                Ray currRay = cameraRig.cachedCamera.ScreenPointToRay(pointer.currentPosition);
 
                Vector3 startPoint = Vector3.zero;
                Vector3 endPoint = Vector3.zero;
                float dist;
 
                if (cameraRig.floorPlane.Raycast(prevRay, out dist))
                {
                    startPoint = prevRay.GetPoint(dist);
                }
                if (cameraRig.floorPlane.Raycast(currRay, out dist))
                {
                    endPoint = currRay.GetPoint(dist);
                }
 
                // Work out that movement in units per second
                m_FlickDirection = (startPoint - endPoint) / Time.deltaTime;
            }
        }
 
        /// <summary>
        /// Controls the pan with a drag
        /// </summary>
        protected void DoDragPan(PointerActionInfo pointer)
        {
            var touchInfo = pointer as TouchInfo;
            if (touchInfo != null)
            {
                // Work out movement amount by raycasting onto floor plane from delta positions
                // and getting that distance
                Ray currRay = cameraRig.cachedCamera.ScreenPointToRay(touchInfo.currentPosition);
 
                Vector3 endPoint = Vector3.zero;
                float dist;
                if (cameraRig.floorPlane.Raycast(currRay, out dist))
                {
                    endPoint = currRay.GetPoint(dist);
                }
                // Pan
                Ray prevRay = cameraRig.cachedCamera.ScreenPointToRay(touchInfo.previousPosition);
                Vector3 startPoint = Vector3.zero;
 
                if (cameraRig.floorPlane.Raycast(prevRay, out dist))
                {
                    startPoint = prevRay.GetPoint(dist);
                }
                Vector3 panAmount = startPoint - endPoint;
                // If this is a touch, we divide the pan amount by the number of touches
                if (UnityInput.touchCount > 0)
                {
                    panAmount /= UnityInput.touchCount;
                }
                
                PanCamera(panAmount);
            }
        }
        
        /// <summary>
        /// Perform a zoom with the given pinch
        /// </summary>
        protected void DoPinchZoom(PinchInfo pinch)
        {
            float currentDistance = (pinch.touch1.currentPosition - pinch.touch2.currentPosition).magnitude;
            float prevDistance = (pinch.touch1.previousPosition - pinch.touch2.previousPosition).magnitude;
 
            float zoomChange = prevDistance / currentDistance;
            float prevZoomDist = cameraRig.zoomDist;
 
            cameraRig.SetZoom(zoomChange * cameraRig.rawZoomDist);
 
            // Calculate actual zoom change after clamping
            zoomChange = cameraRig.zoomDist / prevZoomDist;
 
            // First get floor position of middle of gesture
            Vector2 averageScreenPos = (pinch.touch1.currentPosition + pinch.touch2.currentPosition) * 0.5f;
            Ray ray = cameraRig.cachedCamera.ScreenPointToRay(averageScreenPos);
 
            Vector3 worldPos = Vector3.zero;
            float dist;
 
            if (cameraRig.floorPlane.Raycast(ray, out dist))
            {
                worldPos = ray.GetPoint(dist);
            }
 
            // Vector from our current look pos to this point 
            Vector3 offsetValue = worldPos - cameraRig.lookPosition;
 
            // Pan towards or away from our zoom center
            PanCamera(offsetValue * (1 - zoomChange));
        }
        
        /// <summary>
        /// Pans the camera
        /// </summary>
        /// <param name="panAmount">
        /// The vector to pan
        /// </param>
        protected void PanCamera(Vector3 panAmount)
        {
            cameraRig.StopTracking();
            cameraRig.PanCamera(panAmount);
        }
    }
}