godot-third-person-basic-scene/scenes/controls/controls.gd

328 lines
11 KiB
GDScript

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/ShowMethod.get_selected() == 0:
$Window/VBox/Input.visible = false
$Window/VBox/Tree.visible = true
configure_control_by_group()
elif $Window/VBox/Menu/ShowMethod.get_selected() == 1:
$Window/VBox/Tree.visible = false
$Window/VBox/Input.visible = true
configure_control_sort_by_categories()
else:
$Window/VBox/Tree.visible = false
$Window/VBox/Input.visible = true
configure_control_sort_by_input()
func configure_control_by_group():
$Window/VBox/Tree.clear()
var root = $Window/VBox/Tree.create_item()
$Window/VBox/Tree.hide_root = true
var child_action = $Window/VBox/Tree.create_item(root)
child_action.set_text(0, tr("CONTROL_INPUT_ACTION"))
var child_view = $Window/VBox/Tree.create_item(root)
child_view.set_text(0, tr("CONTROL_INPUT_VIEW"))
var child_other = $Window/VBox/Tree.create_item(root)
child_other.set_text(0, tr("CONTROL_INPUT_OTHER"))
for action in InputMap.get_actions():
var text:String = action
if text.find("INPUT_ACTION") == 0:
var subchild:TreeItem = $Window/VBox/Tree.create_item(child_action)
subchild.set_text(0, tr(text))
var buttontxt : Texture2D = preload("res://scenes/controls/add.png")
subchild.add_button(0, buttontxt, 0, false, "CONTROL_ADD_INPUT")
subchild.set_meta("action", 0)
subchild.set_meta("group", action)
var a = InputMap.action_get_events(action)
for z in a:
var text2:String = Common.get_string_input(z)
var subchild2:TreeItem = $Window/VBox/Tree.create_item(subchild)
subchild2.set_text(0, tr(text2))
var button2txt : Texture2D = preload("res://scenes/controls/trash.png")
subchild2.add_button(0, button2txt, 0, false, "CONTROL_DEL_INPUT")
subchild2.set_meta("action", 1)
subchild2.set_meta("group", action)
subchild2.set_meta("control", z)
elif text.find("INPUT_VIEW") == 0:
var subchild:TreeItem = $Window/VBox/Tree.create_item(child_view)
subchild.set_text(0, tr(text))
var buttontxt : Texture2D = preload("res://scenes/controls/add.png")
subchild.add_button(0, buttontxt, 0, false, "CONTROL_ADD_INPUT")
subchild.set_meta("action", 0)
subchild.set_meta("group", action)
var a = InputMap.action_get_events(action)
for z in a:
var text2:String = Common.get_string_input(z)
var subchild2:TreeItem = $Window/VBox/Tree.create_item(subchild)
subchild2.set_text(0, tr(text2))
var button2txt : Texture2D = preload("res://scenes/controls/trash.png")
subchild2.add_button(0, button2txt, 0, false, "CONTROL_DEL_INPUT")
subchild2.set_meta("action", 1)
subchild2.set_meta("group", action)
subchild2.set_meta("control", z)
else:
var subchild:TreeItem = $Window/VBox/Tree.create_item(child_other)
subchild.set_text(0, tr(text))
var buttontxt : Texture2D = preload("res://scenes/controls/add.png")
subchild.add_button(0, buttontxt, 0, false, "CONTROL_ADD_INPUT")
subchild.set_meta("action", 0)
subchild.set_meta("group", action)
var a = InputMap.action_get_events(action)
for z in a:
var text2:String = Common.get_string_input(z)
var subchild2:TreeItem = $Window/VBox/Tree.create_item(subchild)
subchild2.set_text(0, tr(text2))
var button2txt : Texture2D = preload("res://scenes/controls/trash.png")
subchild2.add_button(0, button2txt, 0, false, "CONTROL_DEL_INPUT")
subchild2.set_meta("action", 1)
subchild2.set_meta("group", action)
subchild2.set_meta("control", z)
func __button_pressed():
print("__button_pressed")
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
func _on_option_button_item_selected(index):
configure_control()
func _on_tree_button_pressed(item:TreeItem, column, id):
if item.has_meta("action") and item.has_meta("group"):
var action = item.get_meta("group")
if item.get_meta("action") == 0:
$SelectType.set_param(action, "signal_refresh" )
$SelectType/Window.popup_centered()
$SelectType/Window.visible = true
elif item.has_meta("control"):
var control = item.get_meta("control")
InputMap.action_erase_event(action, control)
configure_control()