///<summary> Returns true if the given key or controller button is pressed down</summary> public bool GetKey(string name) { if (config.controlList.GetKeybind(name).HasControllerInput() && state.IsConnected && GamepadStates.ToButtonState(config.controlList.GetKeybind(name).controllerKeyCode, state) == ButtonState.Pressed) { return(true); } return(Input.GetKey(GetKeyCode(name).ToLower())); }
///While controller button is being set: Waits for a valid input public IEnumerator WaitForController(string name, InputButton btnRef) { Text btnText = btnRef.GetComponentInChildren <Text>(); //Text that indicates when selection is occurring Text infoText; try { infoText = GameObject.Find("InfoText").GetComponent <Text>(); infoText.text = infoTextContent; infoText.enabled = true; } catch { throw new NullReferenceException("InfoText not found! Create a Text object named 'InfoText'."); } while (true) { if (currentEvent != null && (currentEvent.isKey || currentEvent.isMouse)) { if (currentEvent.keyCode == cancelKeyCode) { infoText.enabled = false; print("Controler Button Selection cancelled"); yield break; } } string currButton = GamepadStates.GetPressedButton(state); if (currButton != null) { print("Controller Button Selection successful"); btnText.text = currButton; config.controlList.GetKeybind(name).controllerKeyCode = currButton; print("Set button for " + name + " to " + currButton); infoText.enabled = false; config.axisList.RefreshList(); //Save to file config.WriteControls(config.configPath); yield break; } yield return(null); } }
///<summary> /// Returns the current value of the axis /// 1 = positive key down, -1 = negative key down. /// Joystick/Gamepad axis values will be somewhere in between. /// Joystick is checked first, if joystick returns zero or is disconnected, then defaults to keyboard value. ///</summary> public float GetAxis(string name) { float result = 0f; //gamepad connected if (state.IsConnected) { result = GamepadStates.ToAxisValue(GetAxisName(name), state); } if (result == 0f) { result = (GetKey(GetAxisNegative(name).name)) ? -1f : (GetKey(GetAxisPositive(name).name)) ? 1f : 0f; } return(result); }
///<summary> Returns true on the given key's release</summary> public bool GetKeyUp(string name) { //Controller detected if (config.controlList.GetKeybind(name).HasControllerInput() && state.IsConnected) { if (GamepadStates.ToButtonState(config.controlList.GetKeybind(name).controllerKeyCode, state) != ButtonState.Pressed) { if (!keyPress[GamepadStates.ToButtonID(config.controlList.GetKeybind(name).controllerKeyCode)]) { return(false); } keyPress[GamepadStates.ToButtonID(config.controlList.GetKeybind(name).controllerKeyCode)] = false; return(true); } else { keyPress[GamepadStates.ToButtonID(config.controlList.GetKeybind(name).controllerKeyCode)] = true; } } return(Input.GetKeyUp(GetKeyCode(name).ToLower())); }