< Summary

Class:DCL.InputController_Legacy
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/InputController_Legacy.cs
Covered lines:57
Uncovered lines:8
Coverable lines:65
Total lines:165
Line coverage:87.6% (57 of 65)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
InputController_Legacy()0%110100%
AddListener(...)0%330100%
RemoveListener(...)0%440100%
RaiseEvent(...)0%3.013088.89%
Update()0%12.8911075%
IsPressed(...)0%13.127050%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/InputController_Legacy.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using DCL.Configuration;
 3using DCL.Interface;
 4using UnityEngine;
 5
 6namespace DCL
 7{
 8    public class InputController_Legacy
 9    {
 10        public delegate void ButtonListenerCallback(WebInterface.ACTION_BUTTON buttonId, EVENT eventType, bool useRaycas
 11
 678012        private static bool renderingEnabled => CommonScriptableObjects.rendererState.Get();
 13        private static InputController_Legacy instance = null;
 14
 15        public static InputController_Legacy i
 16        {
 17            get
 18            {
 2441619                if (instance == null)
 120                    instance = new InputController_Legacy();
 21
 2441622                return instance;
 23            }
 24        }
 25
 26        public enum EVENT
 27        {
 28            BUTTON_DOWN,
 29            BUTTON_UP
 30        }
 31
 32        private enum BUTTON_TYPE
 33        {
 34            MOUSE,
 35            KEYBOARD
 36        }
 37
 38        private struct BUTTON_MAP
 39        {
 40            public BUTTON_TYPE type;
 41            public int buttonNum;
 42            public WebInterface.ACTION_BUTTON buttonId;
 43            public bool useRaycast;
 44            public bool enablePointerEvent;
 45        }
 46
 147        private Dictionary<WebInterface.ACTION_BUTTON, List<ButtonListenerCallback>> listeners = new Dictionary<WebInter
 148        private List<BUTTON_MAP> buttonsMap = new List<BUTTON_MAP>();
 49
 150        private InputController_Legacy()
 51        {
 152            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.MOUSE, buttonNum = 0, buttonId = WebInterface.ACTION_BU
 153            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.PrimaryButton
 154            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.SecondaryButt
 55
 156            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.ForwardButton
 157            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.ForwardButton
 58
 159            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.BackwardButto
 160            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.BackwardButto
 61
 162            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.RightButtonKe
 163            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.RightButtonKe
 64
 165            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.LeftButtonKey
 166            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.LeftButtonKey
 67
 168            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.WalkButtonKey
 169            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.JumpButtonKey
 70
 171            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.ActionButton3
 172            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.ActionButton4
 173            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.ActionButton5
 174            buttonsMap.Add(new BUTTON_MAP() { type = BUTTON_TYPE.KEYBOARD, buttonNum = (int) InputSettings.ActionButton6
 175        }
 76
 77        public void AddListener(WebInterface.ACTION_BUTTON buttonId, ButtonListenerCallback callback)
 78        {
 867179            if (!listeners.ContainsKey(buttonId))
 867180                listeners.Add(buttonId, new List<ButtonListenerCallback>());
 81
 867182            if (!listeners[buttonId].Contains(callback))
 867183                listeners[buttonId].Add(callback);
 867184        }
 85
 86        public void RemoveListener(WebInterface.ACTION_BUTTON buttonId, ButtonListenerCallback callback)
 87        {
 867188            if (listeners.ContainsKey(buttonId))
 89            {
 867190                if (listeners[buttonId].Contains(callback))
 91                {
 867192                    listeners[buttonId].Remove(callback);
 93
 867194                    if (listeners[buttonId].Count == 0)
 867195                        listeners.Remove(buttonId);
 96                }
 97            }
 867198        }
 99
 100        // Note (Zak): it is public for testing purposes only
 101        public void RaiseEvent(WebInterface.ACTION_BUTTON buttonId, EVENT evt, bool useRaycast, bool enablePointerEvent)
 102        {
 14103            if (!listeners.ContainsKey(buttonId))
 0104                return;
 105
 14106            List<ButtonListenerCallback> callbacks = listeners[buttonId];
 14107            int count = callbacks.Count;
 108
 56109            for (int i = 0; i < count; i++)
 110            {
 14111                callbacks[i].Invoke(buttonId, evt, useRaycast, enablePointerEvent);
 112            }
 14113        }
 114
 115        public void Update()
 116        {
 6780117            if (!renderingEnabled)
 1118                return;
 119
 6779120            int count = buttonsMap.Count;
 121
 244044122            for (int i = 0; i < count; i++)
 123            {
 115243124                BUTTON_MAP btnMap = buttonsMap[i];
 125
 115243126                switch (btnMap.type)
 127                {
 128                    case BUTTON_TYPE.MOUSE:
 6779129                        if (CommonScriptableObjects.allUIHidden.Get())
 130                            break;
 6651131                        if (Input.GetMouseButtonDown(btnMap.buttonNum))
 0132                            RaiseEvent(btnMap.buttonId, EVENT.BUTTON_DOWN, btnMap.useRaycast, btnMap.enablePointerEvent)
 6651133                        else if (Input.GetMouseButtonUp(btnMap.buttonNum))
 0134                            RaiseEvent(btnMap.buttonId, EVENT.BUTTON_UP, btnMap.useRaycast, btnMap.enablePointerEvent);
 0135                        break;
 136                    case BUTTON_TYPE.KEYBOARD:
 108464137                        if (CommonScriptableObjects.allUIHidden.Get())
 138                            break;
 106416139                        if (Input.GetKeyDown((KeyCode) btnMap.buttonNum))
 0140                            RaiseEvent(btnMap.buttonId, EVENT.BUTTON_DOWN, btnMap.useRaycast, btnMap.enablePointerEvent)
 106416141                        else if (Input.GetKeyUp((KeyCode) btnMap.buttonNum))
 0142                            RaiseEvent(btnMap.buttonId, EVENT.BUTTON_UP, btnMap.useRaycast, btnMap.enablePointerEvent);
 143                        break;
 144                }
 145            }
 6779146        }
 147
 148        public bool IsPressed(WebInterface.ACTION_BUTTON button)
 149        {
 150            switch (button)
 151            {
 152                case WebInterface.ACTION_BUTTON.POINTER:
 0153                    return Input.GetMouseButton(0);
 154                case WebInterface.ACTION_BUTTON.PRIMARY:
 1155                    return Input.GetKey(InputSettings.PrimaryButtonKeyCode);
 156                case WebInterface.ACTION_BUTTON.SECONDARY:
 0157                    return Input.GetKey(InputSettings.SecondaryButtonKeyCode);
 158                default: // ANY
 279159                    return Input.GetMouseButton(0) ||
 160                           Input.GetKey(InputSettings.PrimaryButtonKeyCode) ||
 161                           Input.GetKey(InputSettings.SecondaryButtonKeyCode);
 162            }
 163        }
 164    }
 165}