This repository has been archived on 2024-07-02. You can view files and clone it, but cannot push or open issues or pull requests.
HoppyEaster/Scripts/UI/Remaping/RemapController.gd

69 lines
1.7 KiB
GDScript3
Raw Normal View History

extends Control
2023-04-11 08:33:19 +00:00
@export var min_time_between_remaps := 0.4
var remap_button : Button
var remap_action : String
var time_since_remap := 0.0
func _unhandled_key_input(event):
if remap_button != null and event.pressed:
remap_key(event)
pass
func _input(event):
if remap_button != null:
if event is InputEventMouseButton and event.pressed:
remap_key(event)
if event is InputEventJoypadButton and event.pressed:
print(event)
remap_key(event)
if event is InputEventJoypadMotion:
remap_key(event)
pass
func _ready():
set_process_unhandled_key_input(false)
remap_button = null
pass
func _process(delta):
if remap_button == null:
time_since_remap += delta
pass
func start_remap(button : Button, action : String):
if(time_since_remap < min_time_between_remaps): return
if(remap_button != null):
remap_button.display_key()
remap_button = button
remap_action = action
remap_button.text = "..."
remap_button.disabled = true
set_process_unhandled_key_input(true)
2023-04-10 22:27:26 +00:00
button.release_focus()
pass
func remap_key(event):
2023-04-10 22:27:26 +00:00
set_process_unhandled_key_input(false)
for key in InputMap.action_get_events(remap_action):
2023-04-10 22:27:26 +00:00
InputMap.action_erase_event(remap_action, key)
InputMap.action_add_event(remap_action, event)
match str(event.get_class()):
"InputEventJoypadButton":
Save.game_data["%s" % [remap_action]] = "b" + str(event.button_index)
"InputEventJoypadMotion":
Save.game_data["%s" % [remap_action]] = "j" + str(event.axis)
"InputEventMouseButton":
Save.game_data["%s" % [remap_action]] = "m" + str(event.button_index)
"InputEventKey":
Save.game_data["%s" % [remap_action]] = event.keycode
remap_button.display_key()
2023-04-10 22:27:26 +00:00
remap_button.grab_focus()
remap_button.disabled = false
time_since_remap = 0
remap_button = null
pass