extends Control #signal reload_control var control_system_conf = {} func _init(): load_control_system() func _ready(): $SelectType.connect("refresh_select_type_control", refresh.bind() ) func refresh(): configure_control() func reload_control_system(): # Adding new Action for key in control_system_conf: if not InputMap.has_action(key): InputMap.add_action(key) # Adding new event for newevent in control_system_conf[key]: var ele = generate_inputevent(newevent) if not InputMap.action_has_event(key, ele): InputMap.action_add_event(key, ele) # Remove action/event not used for keyInput in InputMap.get_actions(): var foundAction:bool = false for key in control_system_conf: if key == keyInput: foundAction = true break if not foundAction: InputMap.erase_action(keyInput) continue var listInput = InputMap.action_get_events(keyInput) for eventInput in listInput: var foundEvent:bool = false var vis = get_dict_inputevent(eventInput) for v in control_system_conf[keyInput]: if compare_dict_inputevent(v, vis): foundEvent = true break if not foundEvent: InputMap.action_erase_event(keyInput, eventInput) configure_control() func compare_dict_inputevent(ref, cmp): if ref.size() != cmp.size(): return false for key in ref: if ref[key] != cmp[key]: return false return true func get_dict_inputevent(event): if event is InputEventKey: return { 'type' : 'InputEventKey', 'keycode' : event.get_keycode(), 'physical_keycode' : event.get_physical_keycode(), 'unicode' :event.get_unicode(), 'echo' : event.is_echo(), 'alt' : event.is_alt_pressed(), 'command' : event.is_command_pressed(), 'control' : event.is_ctrl_pressed(), 'meta' : event.is_meta_pressed(), 'shift' : event.is_shift_pressed(), 'storing_command': event.is_storing_command() } elif event is InputEventMouseButton: return { 'type' : 'InputEventMouseButton', 'value' : event.get_button_index() } elif event is InputEventJoypadButton: return { 'type' : 'InputEventJoypadButton', 'value' : event.get_button_index() } return { 'type' : 'Unknown'} func generate_inputevent(param): if param['type'] == 'InputEventKey': var ele:InputEventKey = InputEventKey.new() ele.set_keycode(param['keycode']) ele.set_physical_keycode(param['physical_keycode']) ele.set_unicode(param['unicode']) ele.set_echo(param['echo']) ele.set_alt_pressed(param['alt']) ele.set_command_pressed(param['command']) ele.set_ctrl_pressed(param['control']) ele.set_meta_pressed(param['meta']) ele.set_shift_pressed(param['shift']) ele.set_store_command(param['storing_command']) return ele elif param['type'] == 'InputEventMouseButton': var ele:InputEventMouseButton = InputEventMouseButton.new() ele.set_button_index(param['value']) return ele elif param['type'] == 'InputEventJoypadButton': var ele:InputEventJoypadButton = InputEventJoypadButton.new() ele.set_button_index(param['value']) return ele return null func get_hash_inputevent(event): var head:String if event is InputEventKey: head = 'a' elif event is InputEventMouseButton: head = 'm' elif event is InputEventJoypadButton: head = 'j' else: head ='z' return head + str(Common.get_string_input(event)) func load_current_control(): var conf = {} for key in InputMap.get_actions(): var a = InputMap.action_get_events(key) var beta = [] for z in a: beta.append( get_dict_inputevent(z) ) conf[key] = beta return conf func configure_control(): if $Window/VBox/Menu/SortByInput.is_pressed(): configure_control_sort_by_input() else: configure_control_sort_by_categories() func configure_control_sort_by_input(): for child in $Window/VBox/Input/Control.get_children(): child.queue_free() var def = {} for action in InputMap.get_actions(): for z in InputMap.action_get_events(action): var id = str(get_hash_inputevent(z)) + "_" + str(action) def[id] = { 'event': z, 'action': action } var defsorted = def.keys() defsorted.sort() var lastevent = null for id in defsorted: var z = def[id]['event'] var action = def[id]['action'] var zhash = get_dict_inputevent(z).hash() if zhash != lastevent: var separator = HSeparator.new() $Window/VBox/Input/Control.add_child( separator ) lastevent = zhash if z is InputEventKey: var item = preload( "res://scenes/controls/control_input.tscn" ).instantiate() #.instance() item.set_param(action, "Key: " + Common.get_string_input_keyboard(z), action, z) item.connect( "del_pressed", _on_input_box_del_pressed.bind() ) $Window/VBox/Input/Control.add_child( item ) elif z is InputEventMouseButton: var item = preload( "res://scenes/controls/control_input.tscn" ).instantiate() item.set_param(action, "Mouse Button: " + Common.get_string_input_mousse_button(z), action, z) item.connect( "del_pressed", _on_input_box_del_pressed.bind() ) $Window/VBox/Input/Control.add_child( item ) elif z is InputEventJoypadButton: var item = preload( "res://scenes/controls/control_input.tscn" ).instantiate() item.set_param(action, "Joypad Button: " + Common.get_string_input_joypad_button(z), action, z) item.connect( "del_pressed", _on_input_box_del_pressed.bind() ) $Window/VBox/Input/Control.add_child( item ) for action in InputMap.get_actions(): var separator = HSeparator.new() $Window/VBox/Input/Control.add_child( separator ) var control_box = preload( "res://scenes/controls/control_function.tscn" ).instantiate() control_box.set_param(action, action, true) control_box.connect( "add_pressed", _on_control_box_add_pressed.bind() ) $Window/VBox/Input/Control.add_child( control_box ) func configure_control_sort_by_categories(): for child in $Window/VBox/Input/Control.get_children(): child.queue_free() for action in InputMap.get_actions(): var control_box = preload( "res://scenes/controls/control_function.tscn" ).instantiate() control_box.set_param(action, action, false) control_box.connect( "add_pressed", _on_control_box_add_pressed.bind() ) $Window/VBox/Input/Control.add_child( control_box ) var a = InputMap.action_get_events(action) for z in a: if z is InputEventKey: var item = preload( "res://scenes/controls/control_input.tscn" ).instantiate() item.set_param(action, "Key: " + Common.get_string_input_keyboard(z), "", z) item.connect( "del_pressed", _on_input_box_del_pressed.bind() ) $Window/VBox/Input/Control.add_child( item ) elif z is InputEventMouseButton: var item = preload( "res://scenes/controls/control_input.tscn" ).instantiate() item.set_param(action, "Mouse Button: " + Common.get_string_input_mousse_button(z), "", z) item.connect( "del_pressed", _on_input_box_del_pressed.bind() ) $Window/VBox/Input/Control.add_child( item ) elif z is InputEventJoypadButton: var item = preload( "res://scenes/controls/control_input.tscn" ).instantiate() item.set_param(action, "Joypad Button: " + Common.get_string_input_joypad_button(z), "", z) item.connect( "del_pressed", _on_input_box_del_pressed.bind() ) $Window/VBox/Input/Control.add_child( item ) var separator = HSeparator.new() $Window/VBox/Input/Control.add_child( separator ) func load_control_system(): control_system_conf = load_current_control() func _on_quit_pressed(): $Window.visible = false func _on_sort_by_input_pressed(): configure_control() func _on_input_box_del_pressed(command, control, inputevent): InputMap.action_erase_event(command, inputevent) configure_control() func _on_control_box_add_pressed(action, command): $SelectType.set_param(action, "signal_refresh" ) $SelectType/Window.popup_centered() $SelectType/Window.visible = true