// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. #pragma once #include "Engine/Core/Delegate.h" #include "Engine/Core/Types/StringView.h" #include "Engine/Core/Collections/Array.h" #include "Engine/Scripting/ScriptingType.h" #include "KeyboardKeys.h" #include "Enums.h" #include "VirtualInput.h" class Mouse; class Keyboard; class Gamepad; class InputDevice; /// /// The user input handling service. /// API_CLASS(Static) class FLAXENGINE_API Input { DECLARE_SCRIPTING_TYPE_NO_SPAWN(Input); /// /// Gets the mouse (null if platform does not support mouse or it is not connected). /// API_FIELD(ReadOnly) static Mouse* Mouse; /// /// Gets the keyboard (null if platform does not support keyboard or it is not connected). /// API_FIELD(ReadOnly) static Keyboard* Keyboard; /// /// Gets the gamepads. /// API_FIELD(ReadOnly) static Array> Gamepads; /// /// Gets the gamepads count. /// /// The amount of active gamepads devices. API_PROPERTY() static int32 GetGamepadsCount(); /// /// Gets the gamepads count. /// /// The gamepad index. /// The gamepad device or null if index is invalid. API_FUNCTION() static Gamepad* GetGamepad(int32 index); /// /// Action called when gamepads collection gets changed (during input update). /// API_EVENT() static Action GamepadsChanged; /// /// Called when gamepads collection gets changed. /// static void OnGamepadsChanged(); /// /// Gets or sets the custom input devices. /// static Array> CustomDevices; public: /// /// Event fired on character input. /// API_EVENT() static Delegate CharInput; /// /// Event fired on key pressed. /// API_EVENT() static Delegate KeyDown; /// /// Event fired on key released. /// API_EVENT() static Delegate KeyUp; /// /// Event fired when mouse button goes down. /// API_EVENT() static Delegate MouseDown; /// /// Event fired when mouse button goes up. /// API_EVENT() static Delegate MouseUp; /// /// Event fired when mouse button double clicks. /// API_EVENT() static Delegate MouseDoubleClick; /// /// Event fired when mouse wheel is scrolling (wheel delta is normalized). /// API_EVENT() static Delegate MouseWheel; /// /// Event fired when mouse moves. /// API_EVENT() static Delegate MouseMove; /// /// Event fired when mouse leaves window. /// API_EVENT() static Action MouseLeave; /// /// Event fired when touch action begins. /// API_EVENT() static Delegate TouchDown; /// /// Event fired when touch action moves. /// API_EVENT() static Delegate TouchMove; /// /// Event fired when touch action ends. /// API_EVENT() static Delegate TouchUp; public: /// /// Gets the text entered during the current frame (Unicode). /// /// The input text (Unicode). API_PROPERTY() static StringView GetInputText(); /// /// Gets the key state (true if key is being pressed during this frame). /// /// Key ID to check /// True while the user holds down the key identified by id API_FUNCTION() static bool GetKey(KeyboardKeys key); /// /// Gets the key 'down' state (true if key was pressed in this frame). /// /// Key ID to check /// True during the frame the user starts pressing down the key API_FUNCTION() static bool GetKeyDown(KeyboardKeys key); /// /// Gets the key 'up' state (true if key was released in this frame). /// /// Key ID to check /// True during the frame the user releases the key API_FUNCTION() static bool GetKeyUp(KeyboardKeys key); public: /// /// Gets the mouse position in game window coordinates. /// /// Mouse cursor coordinates API_PROPERTY() static Float2 GetMousePosition(); /// /// Sets the mouse position in game window coordinates. /// /// Mouse position to set on API_PROPERTY() static void SetMousePosition(const Float2& position); /// /// Gets the mouse position in screen-space coordinates. /// /// Mouse cursor coordinates API_PROPERTY() static Float2 GetMouseScreenPosition(); /// /// Sets the mouse position in screen-space coordinates. /// /// Mouse position to set on API_PROPERTY() static void SetMouseScreenPosition(const Float2& position); /// /// Gets the mouse position change during the last frame. /// /// Mouse cursor position delta API_PROPERTY() static Float2 GetMousePositionDelta(); /// /// Gets the mouse wheel change during the last frame. /// /// Mouse wheel value delta API_PROPERTY() static float GetMouseScrollDelta(); /// /// Gets the mouse button state. /// /// Mouse button to check /// True while the user holds down the button API_FUNCTION() static bool GetMouseButton(MouseButton button); /// /// Gets the mouse button down state. /// /// Mouse button to check /// True during the frame the user starts pressing down the button API_FUNCTION() static bool GetMouseButtonDown(MouseButton button); /// /// Gets the mouse button up state. /// /// Mouse button to check /// True during the frame the user releases the button API_FUNCTION() static bool GetMouseButtonUp(MouseButton button); public: /// /// Gets the gamepad axis value. /// /// The gamepad index /// Gamepad axis to check /// Axis value. API_FUNCTION() static float GetGamepadAxis(int32 gamepadIndex, GamepadAxis axis); /// /// Gets the gamepad button state (true if being pressed during the current frame). /// /// The gamepad index /// Gamepad button to check /// True if user holds down the button, otherwise false. API_FUNCTION() static bool GetGamepadButton(int32 gamepadIndex, GamepadButton button); /// /// Gets the gamepad button down state (true if was pressed during the current frame). /// /// The gamepad index /// Gamepad button to check /// True if user starts pressing down the button, otherwise false. API_FUNCTION() static bool GetGamepadButtonDown(int32 gamepadIndex, GamepadButton button); /// /// Gets the gamepad button up state (true if was released during the current frame). /// /// The gamepad index /// Gamepad button to check /// True if user releases the button, otherwise false. API_FUNCTION() static bool GetGamepadButtonUp(int32 gamepadIndex, GamepadButton button); /// /// Gets the gamepad axis value. /// /// The gamepad /// Gamepad axis to check /// Axis value. API_FUNCTION() static float GetGamepadAxis(InputGamepadIndex gamepad, GamepadAxis axis); /// /// Gets the gamepad button state (true if being pressed during the current frame). /// /// The gamepad /// Gamepad button to check /// True if user holds down the button, otherwise false. API_FUNCTION() static bool GetGamepadButton(InputGamepadIndex gamepad, GamepadButton button); /// /// Gets the gamepad button down state (true if was pressed during the current frame). /// /// The gamepad /// Gamepad button to check /// True if user starts pressing down the button, otherwise false. API_FUNCTION() static bool GetGamepadButtonDown(InputGamepadIndex gamepad, GamepadButton button); /// /// Gets the gamepad button up state (true if was released during the current frame). /// /// The gamepad /// Gamepad button to check /// True if user releases the button, otherwise false. API_FUNCTION() static bool GetGamepadButtonUp(InputGamepadIndex gamepad, GamepadButton button); public: /// /// Maps a discrete button or key press events to a "friendly name" that will later be bound to event-driven behavior. The end effect is that pressing (and/or releasing) a key, mouse button, or keypad button. /// API_FIELD() static Array ActionMappings; /// /// Maps keyboard, controller, or mouse inputs to a "friendly name" that will later be bound to continuous game behavior, such as movement. The inputs mapped in AxisMappings are continuously polled, even if they are just reporting that their input value. /// API_FIELD() static Array AxisMappings; /// /// Event fired when virtual input action is triggered. Called before scripts update. See to edit configuration. /// /// API_EVENT() static Delegate ActionTriggered; /// /// Gets the value of the virtual action identified by name. Use to get the current config. /// /// The action name. /// True if action has been triggered in the current frame (e.g. button pressed), otherwise false. /// API_FUNCTION() static bool GetAction(const StringView& name); /// /// Gets the value of the virtual action identified by name. Use to get the current config. /// /// The action name. /// A InputActionPhase determining the current phase of the Action (e.g If it was just pressed, is being held or just released). /// API_FUNCTION() static InputActionState GetActionState(const StringView& name); /// /// Gets the value of the virtual axis identified by name. Use to get the current config. /// /// The action name. /// The current axis value (e.g for gamepads it's in the range -1..1). Value is smoothed to reduce artifacts. /// API_FUNCTION() static float GetAxis(const StringView& name); /// /// Gets the raw value of the virtual axis identified by name with no smoothing filtering applied. Use to get the current config. /// /// The action name. /// The current axis value (e.g for gamepads it's in the range -1..1). No smoothing applied. /// API_FUNCTION() static float GetAxisRaw(const StringView& name); };