using Beyond; using UnityEngine; using UnityEngine.EventSystems; //using UnityEngine.InputSystem; //#if MOBILE_INPUT using UnityStandardAssets.CrossPlatformInput; //#endif namespace Invector.vCharacterController { public class vInput : MonoBehaviour { public delegate void OnChangeInputType(InputDevice type); public event OnChangeInputType onChangeInputType; //public GameControls inputActions; private static vInput _instance; public static vInput instance { get { if (_instance == null) { _instance = GameObject.FindObjectOfType(); if (_instance == null) { _instance = new GameObject("vInputType").AddComponent(); return _instance; } } return _instance; } } public vHUDController hud; private void Awake() { if(_instance != null && _instance != this) { Destroy(this); } transform.SetParent(null); DontDestroyOnLoad(gameObject); //inputActions = new GameControls(); } void Start() { if (hud == null) hud = vHUDController.instance; } private void OnEnable() { //inputActions.Enable(); } private void OnDisable() { //inputActions.Disable(); } private InputDevice _inputType = InputDevice.MouseKeyboard; [HideInInspector] public InputDevice inputDevice { get { return _inputType; } set { _inputType = value; OnChangeInput(); } } void OnGUI() { switch (inputDevice) { case InputDevice.MouseKeyboard: if (isJoystickInput()) { inputDevice = InputDevice.Joystick; if (hud != null) { hud.controllerInput = true; hud.ShowText("Control scheme changed to Controller", 2f, 0.5f); } } else if (isMobileInput()) { inputDevice = InputDevice.Mobile; if (hud != null) { hud.controllerInput = true; hud.ShowText("Control scheme changed to Mobile", 2f, 0.5f); } } break; case InputDevice.Joystick: if (isMouseKeyboard()) { inputDevice = InputDevice.MouseKeyboard; if (hud != null) { hud.controllerInput = false; hud.ShowText("Control scheme changed to Keyboard/Mouse", 2f, 0.5f); } } else if (isMobileInput()) { inputDevice = InputDevice.Mobile; if (hud != null) { hud.controllerInput = true; hud.ShowText("Control scheme changed to Mobile", 2f, 0.5f); } } break; case InputDevice.Mobile: if (isMouseKeyboard()) { inputDevice = InputDevice.MouseKeyboard; if (hud != null) { hud.controllerInput = false; hud.ShowText("Control scheme changed to Keyboard/Mouse", 2f, 0.5f); } } else if (isJoystickInput()) { inputDevice = InputDevice.Joystick; if (hud != null) { hud.controllerInput = true; hud.ShowText("Control scheme changed to Controller", 2f, 0.5f); } } break; } } private bool isMobileInput() { #if UNITY_EDITOR && UNITY_MOBILE if (EventSystem.current.IsPointerOverGameObject() && Input.GetMouseButtonDown(0)) { return true; } #elif MOBILE_INPUT if (EventSystem.current.IsPointerOverGameObject() || (Input.touches.Length > 0)) return true; #endif return false; } private bool isMouseKeyboard() { #if MOBILE_INPUT return false; #else // mouse & keyboard buttons if (Event.current.isKey || Event.current.isMouse) return true; // mouse movement if (Input.GetAxis("Mouse X") != 0.0f || Input.GetAxis("Mouse Y") != 0.0f) return true; return false; #endif } private bool isJoystickInput() { // joystick buttons if (Input.GetKey(KeyCode.Joystick1Button0) || Input.GetKey(KeyCode.Joystick1Button1) || Input.GetKey(KeyCode.Joystick1Button2) || Input.GetKey(KeyCode.Joystick1Button3) || Input.GetKey(KeyCode.Joystick1Button4) || Input.GetKey(KeyCode.Joystick1Button5) || Input.GetKey(KeyCode.Joystick1Button6) || Input.GetKey(KeyCode.Joystick1Button7) || Input.GetKey(KeyCode.Joystick1Button8) || Input.GetKey(KeyCode.Joystick1Button9) || Input.GetKey(KeyCode.Joystick1Button10) || Input.GetKey(KeyCode.Joystick1Button11) || Input.GetKey(KeyCode.Joystick1Button12) || Input.GetKey(KeyCode.Joystick1Button13) || Input.GetKey(KeyCode.Joystick1Button14) || Input.GetKey(KeyCode.Joystick1Button15) || Input.GetKey(KeyCode.Joystick1Button16) || Input.GetKey(KeyCode.Joystick1Button17) || Input.GetKey(KeyCode.Joystick1Button18) || Input.GetKey(KeyCode.Joystick1Button19)) { return true; } // joystick axis if (Input.GetAxis("LeftAnalogHorizontal") != 0.0f || Input.GetAxis("LeftAnalogVertical") != 0.0f || Input.GetAxis("RightAnalogHorizontal") != 0.0f || Input.GetAxis("RightAnalogVertical") != 0.0f || Input.GetAxis("LT") != 0.0f || Input.GetAxis("RT") != 0.0f || Input.GetAxis("D-Pad Horizontal") != 0.0f || Input.GetAxis("D-Pad Vertical") != 0.0f) { return true; } return false; } void OnChangeInput() { if (onChangeInputType != null) { onChangeInputType(inputDevice); } } } /// /// INPUT TYPE - check in real time if you are using a joystick, mobile or mouse/keyboard /// [HideInInspector] public enum InputDevice { MouseKeyboard, Joystick, Mobile }; [System.Serializable] public class GenericInput { //new input public enum Axis { None, Horizontal, Vertical } //private InputAction action; private bool wasReleasedThisFrame; private int wasReleasedFrameCount; private bool m_isPressed; private Axis m_Axis; private bool useNewInput = false; private bool binded; private string newInput; /* public bool UseNewInput { get { BindButtons(); return useNewInput; } set => useNewInput = value; } */ //public GenericInput(InputAction action) //{ // this.action = action; // m_Axis = Axis.None; // UseNewInput = true; // AssignToEvents(action); //} //public GenericInput(InputAction action, Axis axis) //{ // this.action = action; // m_Axis = axis; // UseNewInput = true; // AssignToEvents(action); //} /* private void AssignToEvents(InputAction action) { action.performed += ctx => { m_isPressed = true; }; action.canceled += ctx => { wasReleasedFrameCount = Time.frameCount; wasReleasedThisFrame = true; m_isPressed = false; }; } */ protected InputDevice inputDevice { get { return vInput.instance.inputDevice; } } public bool useInput = true; [SerializeField] private bool isAxisInUse; [SerializeField] private bool isUnityInput; [SerializeField] public string keyboard; [SerializeField] public bool keyboardAxis; [SerializeField] public string joystick; [SerializeField] public bool joystickAxis; [SerializeField] public string mobile; [SerializeField] public bool mobileAxis; [SerializeField] public bool joystickAxisInvert; [SerializeField] public bool keyboardAxisInvert; [SerializeField] public bool mobileAxisInvert; public float timeButtonWasPressed; public float lastTimeTheButtonWasPressed; public bool inButtomTimer; private float multTapTimer; private int multTapCounter; public bool isAxis { get { bool value = false; switch (inputDevice) { case InputDevice.Joystick: value = joystickAxis; break; case InputDevice.MouseKeyboard: value = keyboardAxis; break; case InputDevice.Mobile: value = mobileAxis; break; } return value; } } public bool isAxisInvert { get { bool value = false; switch (inputDevice) { case InputDevice.Joystick: value = joystickAxisInvert; break; case InputDevice.MouseKeyboard: value = keyboardAxisInvert; break; case InputDevice.Mobile: value = mobileAxisInvert; break; } return value; } } /// /// Initialise a new GenericInput /// /// /// /// public GenericInput(string keyboard, string joystick, string mobile, string additionalTag = "") { this.keyboard = keyboard; this.joystick = joystick; this.mobile = mobile; this.newInput = keyboard + additionalTag; } /* private void BindButtons() { if (binded) { return; } binded = true; bool binding = true; m_Axis = Axis.None; switch (newInput) { case "L": action = vInput.instance.inputActions.Player.ActionTrigger; break; case "Vertical": action = vInput.instance.inputActions.Player.Move; m_Axis = Axis.Vertical; break; case "E": action = vInput.instance.inputActions.Player.Enter; break; case "Space": action = vInput.instance.inputActions.Player.Exit; break; case "LeftShift": action = vInput.instance.inputActions.Player.FastClimb; break; case "Q": action = vInput.instance.inputActions.Player.SlideDown; break; case "E1": action = vInput.instance.inputActions.Player.ActionTrigger; break; case "Horizontal": action = vInput.instance.inputActions.Player.Move; m_Axis = Axis.Horizontal; break; case "Space1": action = vInput.instance.inputActions.Player.Jump; m_Axis = Axis.Horizontal; break; case "Q1": action = vInput.instance.inputActions.Player.Roll; break; case "Tab": action = vInput.instance.inputActions.Player.Strafe; break; case "LeftShift1": action = vInput.instance.inputActions.Player.Sprint; break; case "C": action = vInput.instance.inputActions.Player.Crouch; break; case "Mouse X": action = vInput.instance.inputActions.Player.CameraRotate; m_Axis = Axis.Horizontal; break; case "Mouse Y": action = vInput.instance.inputActions.Player.CameraRotate; m_Axis = Axis.Vertical; break; case "Mouse ScrollWheel": action = vInput.instance.inputActions.Player.CameraZoom; m_Axis = Axis.Vertical; break; case "H": action = vInput.instance.inputActions.Player.HideAndDrawWeapons; break; case "Escape": action = vInput.instance.inputActions.Player.CancelInput; break; case "Alpha 1": //[Tooltip("Input to equip on press")] action = vInput.instance.inputActions.Player.EquipOnPress; break; case "I": action = vInput.instance.inputActions.Player.OpenInventory; break; case "Mouse1": action = vInput.instance.inputActions.Player.RemoveEquipment; break; case "Horizontal1": action = vInput.instance.inputActions.Player.MoveInventory; m_Axis = Axis.Horizontal; break; case "Vertical1": action = vInput.instance.inputActions.Player.MoveInventory; m_Axis = Axis.Vertical; break; case "Return": action = vInput.instance.inputActions.Player.SubmitInventory; break; case "Backspace": action = vInput.instance.inputActions.Player.CancelInventory; break; case "U": action = vInput.instance.inputActions.Player.UseItem; break; case "LeftArrow": action = vInput.instance.inputActions.Player.PreviousItem; break; case "RightArrow": action = vInput.instance.inputActions.Player.NextItem; break; case "Escape1": // [Tooltip("Input to press")] // class vSimpleInput // [Tooltip("This Gameobject will turn off after the input is pressed")] // public bool disableThisObjectAfterInput = true; action = vInput.instance.inputActions.Player.SimpleInput; break; case "Mouse0": action = vInput.instance.inputActions.Player.AttackWeak; break; case "Alpha1": action = vInput.instance.inputActions.Player.AttackStrong; break; case "Mouse11": action = vInput.instance.inputActions.Player.Defense; break; case "Tab1": action = vInput.instance.inputActions.Player.LockOnTarget; break; case "X": action = vInput.instance.inputActions.Player.NextTarget; break; case "Z": action = vInput.instance.inputActions.Player.PreviousTarget; break; case "L1": action = vInput.instance.inputActions.Player.ActionAnimation; break; case "Q2": action = vInput.instance.inputActions.Player.selectLeftInputInventory; break; case "E2": action = vInput.instance.inputActions.Player.selectRightInputInventory; break; default: Debug.Log(" Button binding not find for " + keyboard); binding = false; break; } if (binding) { Debug.Log("Button binded for " + keyboard); AssignToEvents(action); } } */ /// /// Initialise a new GenericInput /// /// /// /// public GenericInput(string keyboard, bool keyboardAxis, string joystick, bool joystickAxis, string mobile, bool mobileAxis) { this.keyboard = keyboard; this.keyboardAxis = keyboardAxis; this.joystick = joystick; this.joystickAxis = joystickAxis; this.mobile = mobile; this.mobileAxis = mobileAxis; this.newInput = keyboard; } /// /// Initialise a new GenericInput /// /// /// /// public GenericInput(string keyboard, bool keyboardAxis, bool keyboardInvert, string joystick, bool joystickAxis, bool joystickInvert, string mobile, bool mobileAxis, bool mobileInvert) { this.keyboard = keyboard; this.keyboardAxis = keyboardAxis; this.keyboardAxisInvert = keyboardInvert; this.joystick = joystick; this.joystickAxis = joystickAxis; this.joystickAxisInvert = joystickInvert; this.mobile = mobile; this.mobileAxis = mobileAxis; this.mobileAxisInvert = mobileInvert; this.newInput = keyboard; } /// /// Button Name /// public string buttonName { get { if (vInput.instance != null) { if (vInput.instance.inputDevice == InputDevice.MouseKeyboard) return keyboard.ToString(); else if (vInput.instance.inputDevice == InputDevice.Joystick) return joystick; else return mobile; } return string.Empty; } } /// /// Check if button is a Key /// public bool isKey { get { if (vInput.instance != null && !isUnityInput) { if (System.Enum.IsDefined(typeof(KeyCode), buttonName)) return true; } return false; } } /// /// Get value /// public KeyCode key { get { return (KeyCode)System.Enum.Parse(typeof(KeyCode), buttonName); } } /// /// Get Button /// /// public bool GetButton() { /* if (UseNewInput) { if (action != null) { return m_isPressed; } else { return false; } } */ if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false; if (isAxis) return GetAxisButton(); // mobile if (inputDevice == InputDevice.Mobile) { #if MOBILE_INPUT if (CrossPlatformInputManager.GetButton(this.mobile)) return true; /* if (isKey) { if (Input.GetKey(key)) return true; } else { if (Input.GetButton(this.buttonName)) return true; } */ #endif } // keyboard/mouse else if (inputDevice == InputDevice.MouseKeyboard) { if (CrossPlatformInputManager.ButtonExists(this.mobile) && CrossPlatformInputManager.GetButton(this.mobile)) return true; if (isKey) { if (Input.GetKey(key)) return true; } else { if (Input.GetButton(this.buttonName)) return true; } } // joystick else if (inputDevice == InputDevice.Joystick) { if (Input.GetButton(this.buttonName)) return true; } return false; } /// /// Get ButtonDown /// /// public bool GetButtonDown() { /* if (UseNewInput) { if (action != null) { return action.triggered; } else { return false; } } */ if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false; if (isAxis) return GetAxisButtonDown(); // mobile if (inputDevice == InputDevice.Mobile) { #if MOBILE_INPUT if (CrossPlatformInputManager.GetButtonDown(this.buttonName)) return true; #endif } // keyboard/mouse else if (inputDevice == InputDevice.MouseKeyboard) { if (CrossPlatformInputManager.ButtonExists(this.mobile) && CrossPlatformInputManager.GetButtonDown(this.mobile)) return true; // if (isKey) { if (Input.GetKeyDown(key)) return true; } else { if (Input.GetButtonDown(this.buttonName)) return true; } } // joystick else if (inputDevice == InputDevice.Joystick) { if (Input.GetButtonDown(this.buttonName)) return true; } return false; } /// /// Get Button Up /// /// public bool GetButtonUp() { /* if (UseNewInput) { if (action != null) { if (wasReleasedThisFrame) { if (wasReleasedFrameCount < Time.frameCount) { wasReleasedThisFrame = false; } } return wasReleasedThisFrame; } else { return false; } } */ if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false; if (isAxis) return GetAxisButtonUp(); // mobile if (inputDevice == InputDevice.Mobile) { #if MOBILE_INPUT if (CrossPlatformInputManager.GetButtonUp(this.buttonName)) return true; #endif } // keyboard/mouse else if (inputDevice == InputDevice.MouseKeyboard) { if (CrossPlatformInputManager.ButtonExists(this.mobile) && CrossPlatformInputManager.GetButtonUp(this.mobile)) return true; if (isKey) { if (Input.GetKeyUp(key)) return true; } else { if (Input.GetButtonUp(this.buttonName)) return true; } } // joystick else if (inputDevice == InputDevice.Joystick) { if (Input.GetButtonUp(this.buttonName)) return true; } return false; } /// /// Get Axis /// /// public float GetAxis() { /* if (UseNewInput) { if (action != null) { if (m_Axis == Axis.Horizontal) { // Debug.Log("action.ReadValue().x: "+ action.ReadValue().x); return action.ReadValue().x; } else if (m_Axis == Axis.Vertical) { //Debug.Log("action.ReadValue().y: " + action.ReadValue().y); return action.ReadValue().y; } else { return 0f; } } else { return 0f; } } */ if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName) || isKey) return 0; // mobile if (inputDevice == InputDevice.Mobile) { #if MOBILE_INPUT /* var val = CrossPlatformInputManager.GetAxis(this.buttonName); var tmp = Input.GetAxis(this.buttonName); return Mathf.Abs(val) > Mathf.Abs(tmp) ? val : tmp; */ return CrossPlatformInputManager.GetAxis(this.buttonName); #endif } // keyboard/mouse else if (inputDevice == InputDevice.MouseKeyboard) { return Input.GetAxis(this.buttonName); } // joystick else if (inputDevice == InputDevice.Joystick) { return Input.GetAxis(this.buttonName); } return 0; } /// /// Get Axis Raw /// /// public float GetAxisRaw() { /* if (UseNewInput) { return GetAxis(); } */ if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName) || isKey) return 0; // mobile if (inputDevice == InputDevice.Mobile) { #if MOBILE_INPUT return CrossPlatformInputManager.GetAxisRaw(this.buttonName); #endif } // keyboard/mouse else if (inputDevice == InputDevice.MouseKeyboard) { return Input.GetAxisRaw(this.buttonName); } // joystick else if (inputDevice == InputDevice.Joystick) { return Input.GetAxisRaw(this.buttonName); } return 0; } /// /// Get Double Button Down Check if button is pressed Within the defined time /// /// /// public bool GetDoubleButtonDown(float inputTime = 1) { if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false; if (multTapCounter == 0 && GetButtonDown()) { multTapTimer = Time.time; multTapCounter = 1; return false; } else if (multTapCounter == 1 && GetButtonDown()) { var time = multTapTimer + inputTime; var valid = (Time.time < time); multTapTimer = 0; multTapCounter = 0; return valid; } else if(multTapCounter == 1 && multTapTimer + inputTime /// Get Buttom Timer Check if a button is pressed for defined time /// /// time to check button press /// public bool GetButtonTimer(float inputTime = 2) { if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false; if (GetButtonDown() && !inButtomTimer) { lastTimeTheButtonWasPressed = Time.time + 0.1f; timeButtonWasPressed = Time.time; inButtomTimer = true; } if (inButtomTimer) { var time = timeButtonWasPressed + inputTime; var valid = (time - Time.time <= 0); if (!GetButton() || lastTimeTheButtonWasPressed < Time.time) { inButtomTimer = false; return false; } else { lastTimeTheButtonWasPressed = Time.time + 0.1f; } if (valid) { inButtomTimer = false; } return valid; } return false; } /// /// Get Buttom Timer Check if a button is pressed for defined time /// /// time to check button press /// public bool GetButtonTimer(ref float currentTimer, float inputTime = 2) { if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false; if (GetButtonDown() && !inButtomTimer) { lastTimeTheButtonWasPressed = Time.time + 0.1f; timeButtonWasPressed = Time.time; inButtomTimer = true; } if (inButtomTimer) { var time = timeButtonWasPressed + inputTime; currentTimer = time - Time.time; var valid = (time - Time.time <= 0); if (!GetButton() || lastTimeTheButtonWasPressed < Time.time) { inButtomTimer = false; return false; } else { lastTimeTheButtonWasPressed = Time.time + 0.1f; } if (valid) { inButtomTimer = false; } return valid; } return false; } /// /// Get Buttom Timer Check if a button is pressed for defined time /// /// time to check button press /// public bool GetButtonTimer(ref float currentTimer, ref bool upAfterPressed, float inputTime = 2) { if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false; if (GetButtonDown()) { lastTimeTheButtonWasPressed = Time.time + 0.1f; timeButtonWasPressed = Time.time; inButtomTimer = true; } if (inButtomTimer) { var time = timeButtonWasPressed + inputTime; currentTimer = (inputTime - (time - Time.time)) / inputTime; var valid = (time - Time.time <= 0); if (!GetButton() || lastTimeTheButtonWasPressed < Time.time) { inButtomTimer = false; upAfterPressed = true; return false; } else { upAfterPressed = false; lastTimeTheButtonWasPressed = Time.time + 0.1f; } if (valid) { inButtomTimer = false; } return valid; } return false; } /// /// Get Axis like a button /// /// Value to check need to be diferent 0 /// public bool GetAxisButton(float value = 0.5f) { if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false; if (isAxisInvert) value *= -1f; if (value > 0) { return GetAxisRaw() >= value; } else if (value < 0) { return GetAxisRaw() <= value; } return false; } /// /// Get Axis like a buttonDown /// /// Value to check need to be diferent 0 /// public bool GetAxisButtonDown(float value = 0.5f) { if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false; if (isAxisInvert) value *= -1f; if (value > 0) { if (!isAxisInUse && GetAxisRaw() >= value) { isAxisInUse = true; return true; } else if (isAxisInUse && GetAxisRaw() == 0) { isAxisInUse = false; } } else if (value < 0) { if (!isAxisInUse && GetAxisRaw() <= value) { isAxisInUse = true; return true; } else if (isAxisInUse && GetAxisRaw() == 0) { isAxisInUse = false; } } return false; } /// /// Get Axis like a buttonUp /// Check if Axis is zero after press /// public bool GetAxisButtonUp() { if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false; if (isAxisInUse && GetAxisRaw() == 0) { isAxisInUse = false; return true; } else if (!isAxisInUse && GetAxisRaw() != 0) { isAxisInUse = true; } return false; } bool IsButtonAvailable(string btnName) { /* if (UseNewInput) { if (action != null && action.enabled) { return true; } else { return false; } } */ if (!useInput) return false; try { if (isKey) return true; Input.GetButton(buttonName); return true; } catch (System.Exception exc) { Debug.LogWarning(" Failure to try access button :" + buttonName + "\n" + exc.Message); return false; } } } }