static void ProcessGamepadButtonEvent(GamepadEventType state, GamepadKeyCode buttonIndex, double id, double value) { GamepadButton buttonToUpdate = GamepadButton.DpadUp; GamepadState gamepadState = GamepadsStates[id]; switch (buttonIndex) { case GamepadKeyCode.dpadUp: buttonToUpdate = GamepadButton.DpadUp; break; case GamepadKeyCode.dpadDown: buttonToUpdate = GamepadButton.DpadDown; break; case GamepadKeyCode.dpadLeft: buttonToUpdate = GamepadButton.DpadLeft; break; case GamepadKeyCode.dpadRight: buttonToUpdate = GamepadButton.DpadRight; break; case GamepadKeyCode.Button0: buttonToUpdate = GamepadButton.B; break; case GamepadKeyCode.Button1: buttonToUpdate = GamepadButton.A; break; case GamepadKeyCode.Button2: buttonToUpdate = GamepadButton.Y; break; case GamepadKeyCode.Button3: buttonToUpdate = GamepadButton.X; break; case GamepadKeyCode.Button6: buttonToUpdate = GamepadButton.LeftTrigger; gamepadState.leftTrigger = (float)value; break; case GamepadKeyCode.Button7: buttonToUpdate = GamepadButton.RightTrigger; gamepadState.rightTrigger = (float)value; break; default: UE.Debug.Log("Unmapped button code: " + buttonIndex); break; } GamepadsStates[id] = gamepadState.WithButton(buttonToUpdate, GamepadEventType.ButtonDown == state || GamepadEventType.ButtonPressed == state); }
//--------------------------------------------------------------------------------------------------------------------- public void ProcessInput(byte[] bytes) { switch ((EventType)bytes[0]) { case EventType.Keyboard: var type = (KeyboardEventType)bytes[1]; var repeat = bytes[2] == 1; var key = bytes[3]; var character = (char)bytes[4]; ProcessKeyEvent(type, repeat, key, character); break; case EventType.Mouse: var deltaX = BitConverter.ToInt16(bytes, 1); var deltaY = BitConverter.ToInt16(bytes, 3); var button = bytes[5]; ProcessMouseMoveEvent(deltaX, deltaY, button); break; case EventType.MouseWheel: var scrollX = BitConverter.ToSingle(bytes, 1); var scrollY = BitConverter.ToSingle(bytes, 5); ProcessMouseWheelEvent(scrollX, scrollY); break; case EventType.Touch: { var length = bytes[1]; var index = 2; var touches = new TouchState[length]; for (int i = 0; i < length; i++) { const int INPUTSYSTEM_ZERO_ID_GUARD = 128; //ID 0 is reserved by inputsystem int identifier = BitConverter.ToInt32(bytes, index) + INPUTSYSTEM_ZERO_ID_GUARD; index += 4; var phase = (UnityEngine.InputSystem.TouchPhase)bytes[index]; index += 1; var pageX = BitConverter.ToInt16(bytes, index); index += 2; var pageY = BitConverter.ToInt16(bytes, index); index += 2; var force = BitConverter.ToSingle(bytes, index); index += 4; touches[i] = new TouchState { touchId = identifier, phase = phase, position = new UnityEngine.Vector2Int(pageX, pageY), pressure = force }; } ProcessTouchMoveEvent(touches); if (Touch.activeTouches.Count > length) { ChangeEndStateUnusedTouches(touches); } } break; case EventType.ButtonClick: var elementId = BitConverter.ToInt16(bytes, 1); ProcessButtonClickEvent(elementId); break; case EventType.Gamepad: { GamepadEventType gamepadEventType = (GamepadEventType)bytes[1]; switch (gamepadEventType) { case GamepadEventType.ButtonDown: case GamepadEventType.ButtonUp: case GamepadEventType.ButtonPressed: { var buttonIndex = bytes[2]; var value = BitConverter.ToDouble(bytes, 3); ProcessGamepadButtonEvent(gamepadEventType, (GamepadKeyCode)buttonIndex, value); } break; case GamepadEventType.Axis: { var buttonIndex = bytes[2]; var x = BitConverter.ToDouble(bytes, 3); var y = BitConverter.ToDouble(bytes, 11); ProcessGamepadAxisEvent(x, y, (GamepadKeyCode)buttonIndex); } break; } InputSystem.QueueStateEvent(RemoteGamepad, m_gamepadState); } break; } }