Tools for handling multiple input methods

This commit is contained in:
Rob Kelly 2024-12-17 12:04:41 -07:00
parent 47383f60b6
commit ec1b73d7b3
3 changed files with 69 additions and 9 deletions

View File

@ -11,6 +11,16 @@ const UNKNOWN_LABEL_SYM := "[unknown]"
action = value action = value
_update() _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: func _ready() -> void:
_update() _update()
@ -19,15 +29,20 @@ func _ready() -> void:
func _update() -> void: func _update() -> void:
var input_symbol: String var input_symbol: String
var actions := InputMap.action_get_events(action) var actions := InputMap.action_get_events(action)
if actions: if actions and event_index < len(actions):
var primary := actions[0] var primary := actions[event_index]
input_symbol = PromptMap.from_event(primary) input_symbol = PromptMap.from_event(primary)
var loc_action := tr(ControlBinding.ACTION_KEY_FMT.format([action])) var loc_action := tr(ControlBinding.ACTION_KEY_FMT.format([action]))
text = PROMPT_FORMAT.format( if not input_symbol:
[ input_symbol = str(PromptMap.UNKNOWN_INPUT_SYMBOL)
input_symbol if input_symbol else str(PromptMap.UNKNOWN_INPUT_SYMBOL), if show_name:
loc_action if loc_action else str(action) 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

View File

@ -9,5 +9,5 @@ anchor_bottom = 1.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
theme_type_variation = &"InputPrompt" theme_type_variation = &"InputPrompt"
text = "❓ - [unknown]" text = "❓ - ACTION_"
script = ExtResource("1_qq6w5") script = ExtResource("1_qq6w5")

45
src/util/input_method.gd Normal file
View File

@ -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