< Summary

Class:Builder.DCLBuilderInput
Assembly:Builder
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Builder/Scripts/DCLBuilderInput.cs
Covered lines:22
Uncovered lines:32
Coverable lines:54
Total lines:159
Line coverage:40.7% (22 of 54)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DCLBuilderInput()0%110100%
Update()0%330100%
HasMouseButtonInput(...)0%24.58036.36%
OnMouseWheelInput(...)0%12300%
SetMouseWheelDelta(...)0%6200%
UpdateMouseWheelInput()0%2.062075%
EditorKeyDownEvent()0%17.449052.94%
SendMessageToBridge(...)0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Builder/Scripts/DCLBuilderInput.cs

#LineLine coverage
 1using UnityEngine;
 2
 3namespace Builder
 4{
 5    public class DCLBuilderInput : MonoBehaviour
 6    {
 7        const string MouseXAxis = "Mouse X";
 8        const string MouseYAxis = "Mouse Y";
 9
 19910        [SerializeField] float mouseWheelThrottle = 0.1f;
 11
 12        public delegate void MouseClickDelegate(int buttonId, Vector3 mousePosition);
 13
 14        public delegate void MouseDragDelegate(int buttonId, Vector3 mousePosition, float axisX, float axisY);
 15
 16        public delegate void MouseRawDragDelegate(int buttonId, Vector3 mousePosition, float axisX, float axisY);
 17
 18        public delegate void MouseWheelDelegate(float axisValue);
 19
 20        public static event MouseClickDelegate OnMouseDown;
 21        public static event MouseClickDelegate OnMouseUp;
 22        public static event MouseDragDelegate OnMouseDrag;
 23        public static event MouseRawDragDelegate OnMouseRawDrag;
 24        public static event MouseWheelDelegate OnMouseWheel;
 25
 26        private int lastMouseWheelAxisDirection = 0;
 27        private float lastMouseWheelTime = 0;
 28
 29        private void Update()
 30        {
 8831            for (int i = 0; i <= 2; i++)
 32            {
 3333                if (HasMouseButtonInput(i))
 34                    break;
 35            }
 36
 1137            UpdateMouseWheelInput();
 38
 39#if UNITY_EDITOR
 1140            EditorKeyDownEvent();
 41#endif
 1142        }
 43
 44        private bool HasMouseButtonInput(int button)
 45        {
 3346            if (Input.GetMouseButtonDown(button))
 47            {
 048                OnMouseDown?.Invoke(button, Input.mousePosition);
 049                return true;
 50            }
 3351            else if (Input.GetMouseButton(button))
 52            {
 053                OnMouseDrag?.Invoke(button, Input.mousePosition, Input.GetAxis(MouseXAxis), Input.GetAxis(MouseYAxis));
 054                OnMouseRawDrag?.Invoke(button, Input.mousePosition, Input.GetAxisRaw(MouseXAxis), Input.GetAxisRaw(Mouse
 055                return true;
 56            }
 3357            else if (Input.GetMouseButtonUp(button))
 58            {
 059                OnMouseUp?.Invoke(button, Input.mousePosition);
 060                return true;
 61            }
 62
 3363            return false;
 64        }
 65
 66        private void OnMouseWheelInput(float axisValue)
 67        {
 068            int axisDirection = (int)Mathf.Sign(axisValue);
 069            if (lastMouseWheelAxisDirection == axisDirection)
 70            {
 071                if (Time.unscaledTime - lastMouseWheelTime >= mouseWheelThrottle)
 72                {
 073                    SetMouseWheelDelta(axisValue, axisDirection);
 74                }
 075            }
 76            else
 77            {
 078                SetMouseWheelDelta(axisValue, axisDirection);
 79            }
 080        }
 81
 82        private void SetMouseWheelDelta(float axisValue, int axisDirection)
 83        {
 084            OnMouseWheel?.Invoke(axisValue);
 085            lastMouseWheelTime = Time.unscaledTime;
 086            lastMouseWheelAxisDirection = axisDirection;
 087        }
 88
 89        private void UpdateMouseWheelInput()
 90        {
 1191            float axisValue = Input.GetAxis("Mouse ScrollWheel");
 1192            if (axisValue != 0)
 93            {
 094                OnMouseWheelInput(axisValue);
 95            }
 1196        }
 97
 98#if UNITY_EDITOR
 99        GameObject bridgeGameObject;
 100
 101        private void EditorKeyDownEvent()
 102        {
 11103            if (Input.GetKey(KeyCode.UpArrow))
 104            {
 0105                SendMessageToBridge("OnBuilderKeyDown", "UpArrow");
 106            }
 107
 11108            if (Input.GetKey(KeyCode.DownArrow))
 109            {
 0110                SendMessageToBridge("OnBuilderKeyDown", "DownArrow");
 111            }
 112
 11113            if (Input.GetKey(KeyCode.LeftArrow))
 114            {
 0115                SendMessageToBridge("OnBuilderKeyDown", "LeftArrow");
 116            }
 117
 11118            if (Input.GetKey(KeyCode.RightArrow))
 119            {
 0120                SendMessageToBridge("OnBuilderKeyDown", "RightArrow");
 121            }
 122
 11123            if (Input.GetKey(KeyCode.LeftShift))
 124            {
 0125                SendMessageToBridge("OnBuilderKeyDown", "LeftShift");
 126            }
 127
 128
 11129            if (Input.GetKeyDown(KeyCode.W))
 130            {
 0131                SendMessageToBridge("SelectGizmo", DCL.Components.DCLGizmos.Gizmo.MOVE);
 132            }
 133
 11134            if (Input.GetKeyDown(KeyCode.R))
 135            {
 0136                SendMessageToBridge("SelectGizmo", DCL.Components.DCLGizmos.Gizmo.ROTATE);
 137            }
 138
 11139            if (Input.GetKeyDown(KeyCode.S))
 140            {
 0141                SendMessageToBridge("SelectGizmo", DCL.Components.DCLGizmos.Gizmo.SCALE);
 142            }
 11143        }
 144
 145        private void SendMessageToBridge(string method, string arg)
 146        {
 0147            if (bridgeGameObject == null)
 148            {
 0149                bridgeGameObject = GameObject.Find("BuilderController");
 150            }
 151
 0152            if (bridgeGameObject != null)
 153            {
 0154                bridgeGameObject.SendMessage(method, arg);
 155            }
 0156        }
 157#endif
 158    }
 159}