62 lines
1.8 KiB
GDScript
62 lines
1.8 KiB
GDScript
@tool
|
|
extends HBoxContainer
|
|
class_name InputGroup
|
|
|
|
const MAX_INPUT_NAME = 30
|
|
|
|
var event : InputEvent = null
|
|
var is_remapping := false
|
|
|
|
@export var action_name : String :
|
|
set(v):
|
|
action_name = v
|
|
var events : Array[InputEvent] = InputMap.action_get_events(action_name)
|
|
var saved_remapped_input = get_remapped_input()
|
|
if saved_remapped_input :
|
|
event = saved_remapped_input
|
|
else :
|
|
event = null if events.size() == 0 else events[0]
|
|
update()
|
|
|
|
func update():
|
|
%Text.text = action_name.to_upper()
|
|
var input_name = "UNASSIGNED" if event == null else event.as_text()
|
|
if input_name.length() > MAX_INPUT_NAME:
|
|
input_name = input_name.substr(0,MAX_INPUT_NAME) + "..."
|
|
%Button.text = input_name
|
|
# (%Button as Button).add_theme_color_override("font_color", Color.RED if )
|
|
|
|
func _input(new_event):
|
|
if is_remapping:
|
|
if (
|
|
new_event is InputEventKey or (new_event is InputEventMouseButton and new_event.is_pressed())
|
|
):
|
|
is_remapping = false
|
|
event = new_event
|
|
save_remapped_input(event)
|
|
GameInfo.update_inputs()
|
|
update()
|
|
|
|
func get_remapped_input() -> InputEvent:
|
|
var existing_remapped_id : int = GameInfo.settings_data.action_remapped.find(action_name)
|
|
|
|
if existing_remapped_id != -1:
|
|
return GameInfo.settings_data.input_remapped[existing_remapped_id]
|
|
|
|
return null
|
|
|
|
func save_remapped_input(e : InputEvent = event):
|
|
var existing_remapped_id : int = GameInfo.settings_data.action_remapped.find(action_name)
|
|
|
|
if existing_remapped_id != -1:
|
|
GameInfo.settings_data.action_remapped.remove_at(existing_remapped_id)
|
|
GameInfo.settings_data.input_remapped.remove_at(existing_remapped_id)
|
|
|
|
GameInfo.settings_data.action_remapped.push_back(action_name)
|
|
GameInfo.settings_data.input_remapped.push_back(e)
|
|
|
|
func _on_button_button_down():
|
|
if !is_remapping:
|
|
is_remapping = true
|
|
%Button.text = "PRESS_KEY"
|