void UpdateControlState(ControlState controlState, InputDeviceSlot slot) { var wasHeld = controlState.held; controlState.held = false; controlState.value = 0f; controlState.valuePrefersDeltaUse = true; foreach (var input in inputs) { var v = input.AxisCheck(slot); //update axis-as-button and button state (When checking axis we also check for button state) switch (input.inputType) { case InputDeviceType.GamepadAxis: controlState.held |= v > input.axisButtoncompareVal; break; case InputDeviceType.Mouse: controlState.held |= Math.Abs(v) > 0.5f; break; case InputDeviceType.Keyboard: case InputDeviceType.GamepadButton: controlState.held |= v == 1; break; case InputDeviceType.Virtual: // Meh. Would be better to unify GetVirtualButton and GetVirtualAxis controlState.held |= VirtualInputs.GetVirtualButton(input.virtualInputID); break; case InputDeviceType.XR: // TO DO break; } if (Math.Abs(v) > Math.Abs(controlState.value)) { //this is the value we're going with controlState.value = v; //now find out if what set this value was something we shouldn't multiply by deltaTime controlState.valuePrefersDeltaUse = input.inputType != InputDeviceType.Mouse || input.mouseInputType <MouseInputType.MouseMoveLeft || input.mouseInputType> MouseInputType.MouseScroll; } } UpdateButtonStates(controlState, wasHeld); }
//public float virtualAxisValue; public bool ButtonCheck(ButtonAction bAction, InputDeviceSlot slot) { //keyboard key checks if (inputType == InputDeviceType.Keyboard) { if (slot == InputDeviceSlot.any || slot == InputDeviceSlot.keyboard || slot == InputDeviceSlot.keyboardAndMouse) { if (bAction == ButtonAction.HELD) { return(Input.GetKey(keyboardKeyCode)); } if (bAction == ButtonAction.DOWN) { return(Input.GetKeyDown(keyboardKeyCode)); } if (bAction == ButtonAction.UP) { return(Input.GetKeyUp(keyboardKeyCode)); } } return(false); } //gamepad button checks if (inputType == InputDeviceType.GamepadButton || inputType == InputDeviceType.GamepadAxis) { if (slot == InputDeviceSlot.keyboard || slot == InputDeviceSlot.mouse || slot == InputDeviceSlot.keyboardAndMouse) { return(false); } //if checking any slot, call this function for each possible slot if (slot == InputDeviceSlot.any) { return(ButtonCheck(bAction, InputDeviceSlot.gamepad1) || ButtonCheck(bAction, InputDeviceSlot.gamepad2) || ButtonCheck(bAction, InputDeviceSlot.gamepad3) || ButtonCheck(bAction, InputDeviceSlot.gamepad4) || ButtonCheck(bAction, InputDeviceSlot.gamepad5) || ButtonCheck(bAction, InputDeviceSlot.gamepad6) || ButtonCheck(bAction, InputDeviceSlot.gamepad7) || ButtonCheck(bAction, InputDeviceSlot.gamepad7) || ButtonCheck(bAction, InputDeviceSlot.gamepad9) || ButtonCheck(bAction, InputDeviceSlot.gamepad10) || ButtonCheck(bAction, InputDeviceSlot.gamepad11) || ButtonCheck(bAction, InputDeviceSlot.gamepad12) || ButtonCheck(bAction, InputDeviceSlot.gamepad13) || ButtonCheck(bAction, InputDeviceSlot.gamepad14) || ButtonCheck(bAction, InputDeviceSlot.gamepad15) || ButtonCheck(bAction, InputDeviceSlot.gamepad16)); } int slotIndex = ((int)slot) - 1; //don't check slots without a connected gamepad if (Sinput.gamepads.Length <= slotIndex) { return(false); } //make sure the gamepad in this slot is one this input is allowed to check (eg don't check PS4 pad bindings for an XBOX pad) bool allowInputFromThisPad = false; for (int i = 0; i < allowedSlots.Length; i++) { if (slotIndex == allowedSlots[i]) { allowInputFromThisPad = true; } } if (!allowInputFromThisPad) { return(false); } //gamepad button check if (inputType == InputDeviceType.GamepadButton) { //get the keycode for the gamepad's slot/button string buttonString = string.Format("Joystick{0}Button{1}", (slotIndex + 1), gamepadButtonNumber); if (string.IsNullOrEmpty(buttonString)) { return(false); } UnityGamepadKeyCode keyCode = (UnityGamepadKeyCode)System.Enum.Parse(typeof(UnityGamepadKeyCode), buttonString); //button check now if (bAction == ButtonAction.HELD) { return(Input.GetKey((KeyCode)(int)keyCode)); } if (bAction == ButtonAction.DOWN) { return(Input.GetKeyDown((KeyCode)(int)keyCode)); } if (bAction == ButtonAction.UP) { return(Input.GetKeyUp((KeyCode)(int)keyCode)); } } //gamepad axis as a button check if (inputType == InputDeviceType.GamepadAxis) { if (bAction == axisButtonState[slotIndex + 1]) { return(true); } if (bAction == ButtonAction.HELD && axisButtonState[slotIndex + 1] == ButtonAction.DOWN) { return(true); } return(false); } return(false); } //virtual device input checks if (inputType == InputDeviceType.Virtual) { if (slot == InputDeviceSlot.any || slot == InputDeviceSlot.virtual1) { virtualInputState = VirtualInputs.GetVirtualButton(virtualInputID); if (bAction == virtualInputState) { return(true); } if (bAction == ButtonAction.HELD && virtualInputState == ButtonAction.DOWN) { return(true); } } } //mouseaxis button checks (these don't happen) if (inputType == InputDeviceType.Mouse) { if (slot != InputDeviceSlot.any && slot != InputDeviceSlot.mouse && slot != InputDeviceSlot.keyboardAndMouse) { return(false); } KeyCode mouseKeyCode = KeyCode.None; if (mouseInputType == MouseInputType.Mouse0) { mouseKeyCode = KeyCode.Mouse0; } if (mouseInputType == MouseInputType.Mouse1) { mouseKeyCode = KeyCode.Mouse1; } if (mouseInputType == MouseInputType.Mouse2) { mouseKeyCode = KeyCode.Mouse2; } if (mouseInputType == MouseInputType.Mouse3) { mouseKeyCode = KeyCode.Mouse3; } if (mouseInputType == MouseInputType.Mouse4) { mouseKeyCode = KeyCode.Mouse4; } if (mouseInputType == MouseInputType.Mouse5) { mouseKeyCode = KeyCode.Mouse5; } if (mouseInputType == MouseInputType.Mouse6) { mouseKeyCode = KeyCode.Mouse6; } if (mouseKeyCode != KeyCode.None) { //clicky mouse input if (bAction == ButtonAction.HELD) { return(Input.GetKey(mouseKeyCode)); } if (bAction == ButtonAction.DOWN) { return(Input.GetKeyDown(mouseKeyCode)); } if (bAction == ButtonAction.UP) { return(Input.GetKeyUp(mouseKeyCode)); } } else { //mouse axis as button input if (bAction == axisButtonState[0]) { return(true); } if (bAction == ButtonAction.HELD && axisButtonState[0] == ButtonAction.DOWN) { return(true); } } return(false); } return(false); }