< Summary

Class:BIWInputWrapper
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Controllers/BuilderInWorldInputWrapper.cs
Covered lines:26
Uncovered lines:63
Coverable lines:89
Total lines:205
Line coverage:29.2% (26 of 89)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWInputWrapper()0%2100%
Update()0%12300%
Dispose()0%110100%
HasMouseButtonInput(...)0%20400%
OnMouseWheelInput(...)0%3.713057.14%
SetMouseWheelDelta(...)0%110100%
UpdateMouseWheelInput()0%6200%
StopInput()0%2100%
ResumeInput()0%2100%
MouseUp(...)0%26.836016.67%
MouseUpInvoke(...)0%6.975057.14%
MouseUpOnGUI(...)0%220100%
MouseDown(...)0%17.85020%
MouseWheel(...)0%14.115028.57%
MouseDrag(...)0%8.744033.33%
MouseRawDrag(...)0%8.744033.33%
CanDrag()0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Controllers/BuilderInWorldInputWrapper.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL.Controllers;
 5using UnityEngine;
 6using UnityEngine.EventSystems;
 7
 8public class BIWInputWrapper : BIWController, IBIWInputWrapper
 9{
 10    private const float MS_CLICK_THRESHOLD = 500;
 11    private const float MOVEMENT_CLICK_THRESHOLD = 50;
 12    private const float MOUSE_WHEEL_THROTTLE = 0.1f;
 13
 14    private const string MOUSE_X_AXIS = "Mouse X";
 15    private const string MOUSE_Y_AXIS = "Mouse Y";
 16    private const string MOUSE_SCROLLWHEEL = "Mouse ScrollWheel";
 17
 18    public static event Action<int, Vector3> OnMouseClick;
 19    public static event Action<int, Vector3> OnMouseClickOnUI;
 20    public static event Action<int, Vector3> OnMouseDown;
 21    public static event Action<int, Vector3> OnMouseUp;
 22    public static event Action<int, Vector3> OnMouseUpOnUI;
 23
 24    public static event Action<float> OnMouseWheel;
 25
 26    public static event OnMouseDragDelegate OnMouseDrag;
 27    public static event OnMouseDragDelegateRaw OnMouseDragRaw;
 28
 29    public delegate void OnMouseDragDelegate(int buttonId, Vector3 position, float axisX, float axisY);
 30
 31    public delegate void OnMouseDragDelegateRaw(int buttonId, Vector3 position, float axisX, float axisY);
 32
 33    internal float lastTimeMouseDown = 0;
 34    internal Vector3 lastMousePosition;
 035    internal bool canInputBeMade = true;
 36    internal bool currentClickIsOnUi = false;
 37    internal int lastMouseWheelAxisDirection = 0;
 38    internal float lastMouseWheelTime = 0;
 39
 40    public override void Update()
 41    {
 042        base.Update();
 043        for (int i = 0; i <= 2; i++)
 44        {
 045            if (HasMouseButtonInput(i))
 46                break;
 47        }
 48
 049        UpdateMouseWheelInput();
 050    }
 51
 52    public override void Dispose()
 53    {
 1854        base.Dispose();
 55
 1856        currentClickIsOnUi = false;
 1857    }
 58
 59    internal bool HasMouseButtonInput(int button)
 60    {
 061        if (Input.GetMouseButtonDown(button))
 62        {
 063            MouseDown(button, Input.mousePosition);
 064            return true;
 65        }
 66
 067        if (Input.GetMouseButton(button))
 68        {
 069            MouseDrag(button, Input.mousePosition, Input.GetAxis(MOUSE_X_AXIS), Input.GetAxis(MOUSE_Y_AXIS));
 070            MouseRawDrag(button, Input.mousePosition, Input.GetAxis(MOUSE_X_AXIS), Input.GetAxis(MOUSE_Y_AXIS));
 071            return true;
 72        }
 73
 074        if (Input.GetMouseButtonUp(button))
 75        {
 076            MouseUp(button, Input.mousePosition);
 077            return true;
 78        }
 79
 080        return false;
 81    }
 82
 83    internal void OnMouseWheelInput(float axisValue)
 84    {
 185        int axisDirection = (int)Mathf.Sign(axisValue);
 186        if (lastMouseWheelAxisDirection == axisDirection)
 87        {
 088            if (Time.unscaledTime - lastMouseWheelTime >= MOUSE_WHEEL_THROTTLE)
 089                SetMouseWheelDelta(axisValue, axisDirection);
 090        }
 91        else
 92        {
 193            SetMouseWheelDelta(axisValue, axisDirection);
 94        }
 195    }
 96
 97    internal void SetMouseWheelDelta(float axisValue, int axisDirection)
 98    {
 199        MouseWheel(axisValue);
 1100        lastMouseWheelTime = Time.unscaledTime;
 1101        lastMouseWheelAxisDirection = axisDirection;
 1102    }
 103
 104    internal void UpdateMouseWheelInput()
 105    {
 0106        float axisValue = Input.GetAxis(MOUSE_SCROLLWHEEL);
 0107        if (axisValue != 0)
 0108            OnMouseWheelInput(axisValue);
 0109    }
 110
 0111    public void StopInput() { canInputBeMade = false; }
 112
 0113    public void ResumeInput() { canInputBeMade = true; }
 114
 115    internal void MouseUp(int buttonId, Vector3 mousePosition)
 116    {
 1117        if (!isEditModeActive)
 1118            return;
 119
 0120        if (currentClickIsOnUi)
 121        {
 0122            OnMouseClickOnUI?.Invoke(buttonId, mousePosition);
 0123            currentClickIsOnUi = false;
 0124            return;
 125        }
 126
 0127        if (!canInputBeMade)
 0128            return;
 129
 0130        if (!BIWUtils.IsPointerOverUIElement())
 0131            MouseUpInvoke(buttonId, mousePosition);
 132        else
 0133            MouseUpOnGUI(buttonId, mousePosition);
 0134    }
 135
 136    internal void MouseUpInvoke(int buttonId, Vector3 mousePosition)
 137    {
 1138        OnMouseUp?.Invoke(buttonId, mousePosition);
 1139        if (Vector3.Distance(mousePosition, lastMousePosition) >= MOVEMENT_CLICK_THRESHOLD)
 0140            return;
 1141        if (Time.unscaledTime >= lastTimeMouseDown + MS_CLICK_THRESHOLD / 1000)
 1142            return;
 0143        OnMouseClick?.Invoke(buttonId, mousePosition);
 0144    }
 145
 2146    internal void MouseUpOnGUI(int buttonId, Vector3 mousePosition) { OnMouseUpOnUI?.Invoke(buttonId, mousePosition); }
 147
 148    internal void MouseDown(int buttonId, Vector3 mousePosition)
 149    {
 1150        if (!isEditModeActive)
 1151            return;
 152
 0153        lastTimeMouseDown = Time.unscaledTime;
 0154        lastMousePosition = mousePosition;
 0155        currentClickIsOnUi = BIWUtils.IsPointerOverUIElement();
 156
 0157        if (!canInputBeMade)
 0158            return;
 0159        if (!currentClickIsOnUi)
 0160            OnMouseDown?.Invoke(buttonId, mousePosition);
 0161    }
 162
 163    internal void MouseWheel(float axisValue)
 164    {
 1165        if (!isEditModeActive)
 1166            return;
 167
 0168        if (!canInputBeMade)
 0169            return;
 0170        if (!BIWUtils.IsPointerOverUIElement())
 0171            OnMouseWheel?.Invoke(axisValue);
 0172    }
 173
 174    internal void MouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY)
 175    {
 1176        if (!isEditModeActive)
 1177            return;
 178
 0179        if (!CanDrag())
 0180            return;
 181
 0182        OnMouseDrag?.Invoke(buttonId, mousePosition, axisX, axisY);
 0183    }
 184
 185    internal void MouseRawDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY)
 186    {
 1187        if (!isEditModeActive)
 1188            return;
 189
 0190        if (!CanDrag())
 0191            return;
 192
 0193        OnMouseDragRaw?.Invoke(buttonId, mousePosition, axisX, axisY);
 0194    }
 195
 196    private bool CanDrag()
 197    {
 0198        if (!canInputBeMade ||
 199            currentClickIsOnUi ||
 200            BIWUtils.IsPointerOverUIElement())
 0201            return false;
 202
 0203        return true;
 204    }
 205}