From ec1b73d7b312719bc5a678609e929abd5e92dabe Mon Sep 17 00:00:00 2001 From: Rob Kelly Date: Tue, 17 Dec 2024 12:04:41 -0700 Subject: [PATCH] Tools for handling multiple input methods --- src/ui/elements/input_prompt/input_prompt.gd | 31 +++++++++---- .../elements/input_prompt/input_prompt.tscn | 2 +- src/util/input_method.gd | 45 +++++++++++++++++++ 3 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 src/util/input_method.gd diff --git a/src/ui/elements/input_prompt/input_prompt.gd b/src/ui/elements/input_prompt/input_prompt.gd index 2579ec4..9243d03 100644 --- a/src/ui/elements/input_prompt/input_prompt.gd +++ b/src/ui/elements/input_prompt/input_prompt.gd @@ -11,6 +11,16 @@ const UNKNOWN_LABEL_SYM := "[unknown]" action = value _update() +@export var show_name := true: + set(value): + show_name = value + _update() + +@export var event_index := 0: + set(value): + event_index = value + _update() + func _ready() -> void: _update() @@ -19,15 +29,20 @@ func _ready() -> void: func _update() -> void: var input_symbol: String var actions := InputMap.action_get_events(action) - if actions: - var primary := actions[0] + if actions and event_index < len(actions): + var primary := actions[event_index] input_symbol = PromptMap.from_event(primary) var loc_action := tr(ControlBinding.ACTION_KEY_FMT.format([action])) - text = PROMPT_FORMAT.format( - [ - input_symbol if input_symbol else str(PromptMap.UNKNOWN_INPUT_SYMBOL), - loc_action if loc_action else str(action) - ] - ) + if not input_symbol: + input_symbol = str(PromptMap.UNKNOWN_INPUT_SYMBOL) + if show_name: + text = PROMPT_FORMAT.format( + [ + input_symbol if input_symbol else str(PromptMap.UNKNOWN_INPUT_SYMBOL), + loc_action if loc_action else str(action) + ] + ) + else: + text = input_symbol diff --git a/src/ui/elements/input_prompt/input_prompt.tscn b/src/ui/elements/input_prompt/input_prompt.tscn index 5113eff..4e1c191 100644 --- a/src/ui/elements/input_prompt/input_prompt.tscn +++ b/src/ui/elements/input_prompt/input_prompt.tscn @@ -9,5 +9,5 @@ anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 theme_type_variation = &"InputPrompt" -text = "❓ - [unknown]" +text = "❓ - ACTION_" script = ExtResource("1_qq6w5") diff --git a/src/util/input_method.gd b/src/util/input_method.gd new file mode 100644 index 0000000..77114d7 --- /dev/null +++ b/src/util/input_method.gd @@ -0,0 +1,45 @@ +class_name InputMethod +## Tools for disambiguating game input methods. + +enum Type { + UNKNOWN, + MOUSE_KEYBOARD, + GAMEPAD, + SONY, + XBOX, + NINTENDO, +} + + +static func _get_gamepad_type(device_id: int) -> Type: + var name := Input.get_joy_name(device_id) + if name.contains("Nintendo Switch"): + return Type.NINTENDO + if name.contains("Xbox"): + return Type.XBOX + # rpk: If you see this and think "HA! I could replace that with one simple regex!"... + # ... buy me a beer sometime and ask me about the User Agent Regex. + if ( + name.contains("PlayStation") + or name.contains("PS1") + or name.contains("PS2") + or name.contains("PS3") + or name.contains("PS4") + or name.contains("PS5") + ): + return Type.SONY + + return Type.GAMEPAD + + +static func get_method(event: InputEvent) -> Type: + if event is InputEventKey or event is InputEventMouse: + return Type.MOUSE_KEYBOARD + + if event is InputEventJoypadButton: + return _get_gamepad_type((event as InputEventJoypadButton).device) + + if event is InputEventJoypadMotion: + return _get_gamepad_type((event as InputEventJoypadMotion).device) + + return Type.UNKNOWN