wangguan
2020-10-29 8afc9ce1c752c24decad77e0989b367b8f2c4179
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
using UnityEngine;
using UnityInput = UnityEngine.Input;
 
namespace Core.Input
{
    /// <summary>
    /// Base control scheme for desktop devices, which performs CameraRig motion
    /// </summary>
    public class KeyboardMouseInput : CameraInputScheme
    {
        /// <summary>
        /// Pan threshold (how near to the edge before we pan. Also the denominator for RMB pan)
        /// </summary>
        public float screenPanThreshold = 40f;
 
        /// <summary>
        /// Pan speed for edge panning
        /// </summary>
        public float mouseEdgePanSpeed = 30f;
 
        /// <summary>
        /// Pan speed for RMB panning
        /// </summary>
        public float mouseRmbPanSpeed = 15f;
        
        
        /// <summary>
        /// Gets whether the scheme should be activated or not
        /// </summary>
        public override bool shouldActivate
        {
            get
            {
                if (UnityInput.touchCount > 0)
                {
                    return false;
                }
                bool anyKey = UnityInput.anyKey;
                bool buttonPressedThisFrame = InputController.instance.mouseButtonPressedThisFrame;
                bool movedMouseThisFrame = InputController.instance.mouseMovedOnThisFrame;
 
                return (anyKey || buttonPressedThisFrame || movedMouseThisFrame);
            }
        }
 
        /// <summary>
        /// This is the default scheme on desktop devices
        /// </summary>
        public override bool isDefault
        {
            get
            {
#if UNITY_STANDALONE || UNITY_EDITOR
                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;
            }
 
            InputController controller = InputController.instance;
 
            controller.spunWheel += OnWheel;
            controller.dragged += OnDrag;
            controller.pressed += OnPress;
        }
 
        /// <summary>
        /// Deregister input events
        /// </summary>
        protected virtual void OnDisable()
        {
            if (!InputController.instanceExists)
            {
                return;
            }
 
            InputController controller = InputController.instance;
 
            controller.pressed -= OnPress;
            controller.dragged -= OnDrag;
            controller.spunWheel -= OnWheel;
        }
        
        /// <summary>
        /// Handle camera panning behaviour
        /// </summary>
        protected virtual void Update()
        {
            if (cameraRig != null)
            {
                DoScreenEdgePan();
                DoKeyboardPan();
                DecayZoom();
            }
        }
 
        /// <summary>
        /// Called when we drag
        /// </summary>
        protected virtual void OnDrag(PointerActionInfo pointer)
        {
            if (cameraRig != null)
            {
                DoRightMouseDragPan(pointer);
            }
        }
 
        /// <summary>
        /// Called on mouse wheel input
        /// </summary>
        protected virtual void OnWheel(WheelInfo wheel)
        {
            if (cameraRig != null)
            {
                DoWheelZoom(wheel);
            }
        }
 
        /// <summary>
        /// Called on input press, for MMB panning
        /// </summary>
        protected virtual void OnPress(PointerActionInfo pointer)
        {
            if (cameraRig != null)
            {
                DoMiddleMousePan(pointer);
            }
        }
 
        /// <summary>
        /// Perform mouse screen-edge panning
        /// </summary>
        protected void DoScreenEdgePan()
        {
            Vector2 mousePos = UnityInput.mousePosition;
 
            bool mouseInside = (mousePos.x >= 0) &&
                               (mousePos.x < Screen.width) &&
                               (mousePos.y >= 0) &&
                               (mousePos.y < Screen.height);
 
            // Mouse can be outside of our window
            if (mouseInside)
            {
                PanWithScreenCoordinates(mousePos, screenPanThreshold, mouseEdgePanSpeed);
            }
        }
 
        /// <summary>
        /// Perform keyboard panning
        /// </summary>
        protected void DoKeyboardPan()
        {
            // Calculate zoom ratio
            float zoomRatio = GetPanSpeedForZoomLevel();
            
            // Left
            if (UnityInput.GetKey(KeyCode.LeftArrow) || UnityInput.GetKey(KeyCode.A))
            {
                cameraRig.PanCamera(Vector3.left * Time.deltaTime * mouseEdgePanSpeed * zoomRatio);
 
                cameraRig.StopTracking();
            }
 
            // Right
            if (UnityInput.GetKey(KeyCode.RightArrow) || UnityInput.GetKey(KeyCode.D))
            {
                cameraRig.PanCamera(Vector3.right * Time.deltaTime * mouseEdgePanSpeed * zoomRatio);
 
                cameraRig.StopTracking();
            }
 
            // Down
            if (UnityInput.GetKey(KeyCode.DownArrow) || UnityInput.GetKey(KeyCode.S))
            {
                cameraRig.PanCamera(Vector3.back * Time.deltaTime * mouseEdgePanSpeed * zoomRatio);
 
                cameraRig.StopTracking();
            }
 
            // Up
            if (UnityInput.GetKey(KeyCode.UpArrow) || UnityInput.GetKey(KeyCode.W))
            {
                cameraRig.PanCamera(Vector3.forward * Time.deltaTime * mouseEdgePanSpeed * zoomRatio);
 
                cameraRig.StopTracking();
            }
        }
 
        /// <summary>
        /// Decay the zoom if it's springy
        /// </summary>
        protected void DecayZoom()
        {
            cameraRig.ZoomDecay();
        }
 
        /// <summary>
        /// Pan with right mouse
        /// </summary>
        /// <param name="pointer">The drag pointer event</param>
        protected void DoRightMouseDragPan(PointerActionInfo pointer)
        {
            var mouseInfo = pointer as MouseButtonInfo;
            if ((mouseInfo != null) &&
                (mouseInfo.mouseButtonId == 1))
            {
                // Calculate zoom ratio
                float zoomRatio = GetPanSpeedForZoomLevel();
 
                Vector2 panVector = mouseInfo.currentPosition - mouseInfo.startPosition;
                panVector = (panVector * Time.deltaTime * mouseRmbPanSpeed * zoomRatio) / screenPanThreshold;
 
                var camVector = new Vector3(panVector.x, 0, panVector.y);
                cameraRig.PanCamera(camVector);
 
                cameraRig.StopTracking();
            }
        }
 
        /// <summary>
        /// Perform mouse wheel zooming
        /// </summary>
        protected void DoWheelZoom(WheelInfo wheel)
        {
            float prevZoomDist = cameraRig.zoomDist;
            cameraRig.ZoomCameraRelative(wheel.zoomAmount * -1);
 
            // Calculate actual zoom change after clamping
            float zoomChange = cameraRig.zoomDist / prevZoomDist;
 
            // First get floor position of cursor
            Ray ray = cameraRig.cachedCamera.ScreenPointToRay(UnityInput.mousePosition);
 
            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
            cameraRig.PanCamera(offsetValue * (1 - zoomChange));
        }
 
        /// <summary>
        /// Pan with middle mouse
        /// </summary>
        /// <param name="pointer">Pointer with press event</param>
        protected void DoMiddleMousePan(PointerActionInfo pointer)
        {
            var mouseInfo = pointer as MouseButtonInfo;
 
            // Pan to mouse position on MMB
            if ((mouseInfo != null) &&
                (mouseInfo.mouseButtonId == 2))
            {
                // First get floor position of cursor
                Ray ray = cameraRig.cachedCamera.ScreenPointToRay(UnityInput.mousePosition);
 
                float dist;
 
                if (cameraRig.floorPlane.Raycast(ray, out dist))
                {
                    Vector3 worldPos = ray.GetPoint(dist);
                    cameraRig.PanTo(worldPos);
                }
 
                cameraRig.StopTracking();
            }
        }
    }
}