// Copyright (c) 2012-2024 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" #include "Engine/Content/JsonAssetReference.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; /// /// Event fired when virtual input axis is changed. Called before scripts update. See to edit configuration. /// /// API_EVENT() static Delegate AxisValueChanged; /// /// 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); /// /// Sets and overwrites the Action and Axis mappings with the values from a new InputSettings. /// /// The input settings. API_FUNCTION() static void SetInputMappingFromSettings(const JsonAssetReference& settings); /// /// Sets and overwrites the Action and Axis mappings with the values from the InputSettings in GameSettings. /// API_FUNCTION() static void SetInputMappingToDefaultSettings(); /// /// Gets the first action configuration by name. /// /// The name of the action config. /// The first Action configuration with the name. Empty configuration if not found. API_FUNCTION() static ActionConfig GetActionConfigByName(const StringView& name); /// /// Gets all the action configurations by name. /// /// The name of the action config. /// The Action configurations with the name. API_FUNCTION() static Array GetAllActionConfigsByName(const StringView& name); /// /// Sets the action configuration keyboard key by name. /// /// The name of the action config. /// The key to set. /// Whether to set only the first config found or all of them. API_FUNCTION() static void SetActionConfigByName(const StringView& name, const KeyboardKeys key, bool all = false); /// /// Sets the action configuration mouse button by name. /// /// The name of the action config. /// The mouse button to set. /// Whether to set only the first config found or all of them. API_FUNCTION() static void SetActionConfigByName(const StringView& name, const MouseButton mouseButton, bool all = false); /// /// Sets the action configuration gamepad button by name and index. /// /// The name of the action config. /// The gamepad button to set. /// The gamepad index used to find the correct config. /// Whether to set only the first config found or all of them. API_FUNCTION() static void SetActionConfigByName(const StringView& name, const GamepadButton gamepadButton, InputGamepadIndex gamepadIndex, bool all = false); /// /// Sets the action configuration by name. /// /// The name of the action config. /// The action configuration to set. Leave the config name empty to use set name. /// Whether to set only the first config found or all of them. API_FUNCTION() static void SetActionConfigByName(const StringView& name, ActionConfig& config, bool all = false); /// /// Gets the first axis configurations by name. /// /// The name of the axis config. /// The first Axis configuration with the name. Empty configuration if not found. API_FUNCTION() static AxisConfig GetAxisConfigByName(const StringView& name); /// /// Gets all the axis configurations by name. /// /// The name of the axis config. /// The axis configurations with the name. API_FUNCTION() static Array GetAllAxisConfigsByName(const StringView& name); /// /// Sets the axis configuration keyboard key by name and type. /// /// The name of the action config. /// The configuration to set. Leave the config name empty to use set name. /// Whether to set only the first config found or all of them. API_FUNCTION() static void SetAxisConfigByName(const StringView& name, AxisConfig& config, bool all = false); /// /// Sets the axis configuration keyboard key buttons by name and type. /// /// The name of the action config. /// The type to sort by. /// The positive key button. /// The negative key button. /// Whether to set only the first config found or all of them. API_FUNCTION() static void SetAxisConfigByName(const StringView& name, InputAxisType inputType, const KeyboardKeys positiveButton, const KeyboardKeys negativeButton, bool all = false); /// /// Sets the axis configuration gamepad buttons by name, type, and index. /// /// The name of the action config. /// The type to sort by. /// The positive gamepad button. /// The negative gamepad button. /// The gamepad index to sort by. /// Whether to set only the first config found or all of them. API_FUNCTION() static void SetAxisConfigByName(const StringView& name, InputAxisType inputType, const GamepadButton positiveButton, const GamepadButton negativeButton, InputGamepadIndex gamepadIndex, bool all = false); /// /// Sets the axis configuration accessories by name, and type. /// /// The name of the action config. /// The type to sort by. /// The gravity to set. /// The dead zone to set. /// The sensitivity to set. /// The scale to set. /// The snap to set. /// Whether to set only the first config found or all of them. API_FUNCTION() static void SetAxisConfigByName(const StringView& name, InputAxisType inputType, const float gravity, const float deadZone, const float sensitivity, const float scale, const bool snap, bool all = false); };