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

403 lines
14 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() )
$SelectType.connect("cancel_define_input", show_windows.bind() )
$DefineInput.connect("refresh_control_define_input", define_mouse_camera_move.bind() )
$DefineInput.connect("cancel_define_input", show_windows.bind())
func refresh():
configure_control()
show_windows()
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 update_mouse_button_move_camera():
$Window/v/Tab/MOUSE/v/CameraPlayer/h1/SelectButton.set_text(Common.get_input_plus("INPUT_VIEW_CAMERA_MOVE_PLAYER_FOLLOW_MOUSE"))
$Window/v/Tab/MOUSE/v/CameraOnly/h1/SelectButton.set_text(Common.get_input_plus("INPUT_VIEW_CAMERA_MOVE_ONLY_MOUSE"))
$Window/v/Tab/MOUSE/v/CameraPlayerWalk/h1/SelectButton.set_text(Common.get_input_plus("INPUT_ACTION_CAMERA_MOVE_PLAYER_MOUSE"))
func configure_control():
if $Window/v/Tab/CONTROL/Menu/ShowMethod.get_selected() == 0:
$Window/v/Tab/CONTROL/Input.visible = false
$Window/v/Tab/CONTROL/Tree.visible = true
configure_control_by_group()
elif $Window/v/Tab/CONTROL/Menu/ShowMethod.get_selected() == 1:
$Window/v/Tab/CONTROL/Tree.visible = false
$Window/v/Tab/CONTROL/Input.visible = true
configure_control_sort_by_categories()
else:
$Window/v/Tab/CONTROL/Tree.visible = false
$Window/v/Tab/CONTROL/Input.visible = true
configure_control_sort_by_input()
update_mouse_button_move_camera()
func configure_control_by_group():
$Window/v/Tab/CONTROL/Tree.clear()
var root = $Window/v/Tab/CONTROL/Tree.create_item()
$Window/v/Tab/CONTROL/Tree.hide_root = true
var child_action = $Window/v/Tab/CONTROL/Tree.create_item(root)
child_action.set_text(0, tr("CONTROL_INPUT_ACTION"))
var child_view = $Window/v/Tab/CONTROL/Tree.create_item(root)
child_view.set_text(0, tr("CONTROL_INPUT_VIEW"))
var child_other = $Window/v/Tab/CONTROL/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.ends_with("_PLUS"):
pass
elif text.find("INPUT_ACTION") == 0:
var subchild:TreeItem = $Window/v/Tab/CONTROL/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 = tr(Common.get_string_input(z)) + Common.get_input_plus_event(action ,z)
var subchild2:TreeItem = $Window/v/Tab/CONTROL/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/v/Tab/CONTROL/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 = tr(Common.get_string_input(z)) + Common.get_input_plus_event(action ,z)
var subchild2:TreeItem = $Window/v/Tab/CONTROL/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/v/Tab/CONTROL/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 = tr(Common.get_string_input(z)) + Common.get_input_plus_event(action ,z)
var subchild2:TreeItem = $Window/v/Tab/CONTROL/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/v/Tab/CONTROL/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:String
var extra = ""
var eventplus = null
if String(action).ends_with("_MOUSE"):
eventplus = Common.get_event_plus_ref(action, z)
if eventplus != null:
id = str(get_hash_inputevent(z) + "_" + get_hash_inputevent(eventplus)) + "__" + str(action) + "_" + str(eventplus)
else:
id = str(get_hash_inputevent(z)) + "__" + str(action)
else:
id = str(get_hash_inputevent(z)) + "__" + str(action)
def[id] = { 'event': z, 'action': action, 'plus': eventplus }
var defsorted = def.keys()
defsorted.sort()
var lastevent = null
for id in defsorted:
var z = def[id]['event']
var eventplus = def[id]['plus']
var action = def[id]['action']
var text:String = action
if text.ends_with("_PLUS"):
continue
var zhash # = get_dict_inputevent(z).hash()
if String(action).ends_with("_MOUSE"):
#thash = get_dict_inputevent(z).hash() + "_" + get_hash_inputevent(eventplus))
if eventplus != null:
zhash = get_dict_inputevent(z).hash() + get_dict_inputevent(eventplus).hash()
else:
zhash = get_dict_inputevent(z).hash()
else:
zhash = get_dict_inputevent(z).hash()
#var zhash = thash.hash()
if zhash != lastevent:
var separator = HSeparator.new()
$Window/v/Tab/CONTROL/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/v/Tab/CONTROL/Input/Control.add_child( item )
elif z is InputEventMouseButton:
var item = preload( "res://scenes/controls/control_input.tscn" ).instantiate()
var res:String
if eventplus == null:
res = tr(Common.get_string_input_mousse_button(z))
else:
res = tr(Common.get_string_input_mousse_button(z)) + " & " + tr(Common.get_string_input_mousse_button(eventplus))
item.set_param(action, "Mouse Button: " + res, action, z)
item.connect( "del_pressed", _on_input_box_del_pressed.bind() )
$Window/v/Tab/CONTROL/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/v/Tab/CONTROL/Input/Control.add_child( item )
for action in InputMap.get_actions():
var separator = HSeparator.new()
$Window/v/Tab/CONTROL/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/v/Tab/CONTROL/Input/Control.add_child( control_box )
func configure_control_sort_by_categories():
for child in $Window/v/Tab/CONTROL/Input/Control.get_children():
child.queue_free()
for action in InputMap.get_actions():
var text:String = action
if text.ends_with("_PLUS"):
continue
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/v/Tab/CONTROL/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/v/Tab/CONTROL/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: " + tr(Common.get_string_input_mousse_button(z)) + Common.get_input_plus_event(action ,z) , "", z)
item.connect( "del_pressed", _on_input_box_del_pressed.bind() )
$Window/v/Tab/CONTROL/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/v/Tab/CONTROL/Input/Control.add_child( item )
var separator = HSeparator.new()
$Window/v/Tab/CONTROL/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):
$Window.hide()
$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:
$Window.hide()
$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()
func show_windows():
Common.msg_debug("show_windows")
$Window.visible = true
func define_mouse_camera_move():
configure_control()
show_windows()
func _on_select_button_pressed():
$Window.visible = false
$DefineInput.set_param(2, "INPUT_VIEW_CAMERA_MOVE_PLAYER_FOLLOW_MOUSE")
$DefineInput/Window.popup_centered()
$DefineInput/Window.visible = true
func _on_camear_only_button_pressed():
$Window.visible = false
$DefineInput.set_param(2, "INPUT_VIEW_CAMERA_MOVE_ONLY_MOUSE")
$DefineInput/Window.popup_centered()
$DefineInput/Window.visible = true
func _on_camera_player_walk_button_pressed():
$Window.visible = false
$DefineInput.set_param(2, "INPUT_VIEW_CAMERA_PLAYER_WALK_MOUSE")
$DefineInput/Window.popup_centered()
$DefineInput/Window.visible = true