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

428 lines
16 KiB
GDScript3
Raw Normal View History

2022-02-12 22:15:01 +00:00
extends Control
2022-02-25 00:28:14 +00:00
#signal reload_control
2022-02-12 22:15:01 +00:00
var control_system_conf = {}
func _init():
load_control_system()
func _ready():
2022-02-25 00:28:14 +00:00
$SelectType.connect("refresh_select_type_control", refresh.bind() )
2022-04-15 07:53:44 +00:00
$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())
2022-02-12 22:15:01 +00:00
2022-02-25 00:28:14 +00:00
func refresh():
configure_control()
2022-04-15 12:33:00 +00:00
show_windows()
2022-02-24 22:08:03 +00:00
2022-02-12 22:15:01 +00:00
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
2022-02-25 00:28:14 +00:00
var listInput = InputMap.action_get_events(keyInput)
2022-02-12 22:15:01 +00:00
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)
2022-02-25 00:28:14 +00:00
configure_control()
2022-02-12 22:15:01 +00:00
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()
2022-02-25 00:28:14 +00:00
ele.set_keycode(param['keycode'])
ele.set_physical_keycode(param['physical_keycode'])
ele.set_unicode(param['unicode'])
2022-02-12 22:15:01 +00:00
ele.set_echo(param['echo'])
2022-02-25 00:28:14 +00:00
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'])
2022-02-12 22:15:01 +00:00
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'
2022-02-25 00:28:14 +00:00
return head + str(Common.get_string_input(event))
2022-02-12 22:15:01 +00:00
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
2022-04-15 07:53:44 +00:00
func update_mouse_button_move_camera():
2022-04-15 12:33:00 +00:00
$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"))
2022-04-15 13:54:35 +00:00
$Window/v/Tab/JOYPAD/v/CameraPlayer/h1/SelectButton.set_text(Common.get_input_plus("INPUT_VIEW_CAMERA_MOVE_PLAYER_FOLLOW_JOYPAD"))
$Window/v/Tab/JOYPAD/v/CameraOnly/h1/SelectButton.set_text(Common.get_input_plus("INPUT_VIEW_CAMERA_MOVE_ONLY_JOYPAD"))
$Window/v/Tab/JOYPAD/v/CameraPlayerWalk/h1/SelectButton.set_text(Common.get_input_plus("INPUT_ACTION_CAMERA_MOVE_PLAYER_JOYPAD"))
2022-04-15 07:53:44 +00:00
2022-02-12 22:15:01 +00:00
func configure_control():
2022-04-15 12:33:00 +00:00
if $Window/v/Tab/CONTROL/Menu/ShowMethod.get_selected() == 0:
$Window/v/Tab/CONTROL/Input.visible = false
$Window/v/Tab/CONTROL/Tree.visible = true
2022-03-06 17:15:34 +00:00
configure_control_by_group()
2022-04-15 12:33:00 +00:00
elif $Window/v/Tab/CONTROL/Menu/ShowMethod.get_selected() == 1:
$Window/v/Tab/CONTROL/Tree.visible = false
$Window/v/Tab/CONTROL/Input.visible = true
2022-02-12 22:15:01 +00:00
configure_control_sort_by_categories()
2022-03-06 17:15:34 +00:00
else:
2022-04-15 12:33:00 +00:00
$Window/v/Tab/CONTROL/Tree.visible = false
$Window/v/Tab/CONTROL/Input.visible = true
2022-03-06 17:15:34 +00:00
configure_control_sort_by_input()
2022-04-15 07:53:44 +00:00
update_mouse_button_move_camera()
2022-03-06 17:15:34 +00:00
func configure_control_by_group():
2022-04-15 12:33:00 +00:00
$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)
2022-03-06 17:15:34 +00:00
child_action.set_text(0, tr("CONTROL_INPUT_ACTION"))
2022-04-15 12:33:00 +00:00
var child_view = $Window/v/Tab/CONTROL/Tree.create_item(root)
2022-03-06 17:15:34 +00:00
child_view.set_text(0, tr("CONTROL_INPUT_VIEW"))
2022-04-15 12:33:00 +00:00
var child_other = $Window/v/Tab/CONTROL/Tree.create_item(root)
2022-03-06 17:15:34 +00:00
child_other.set_text(0, tr("CONTROL_INPUT_OTHER"))
for action in InputMap.get_actions():
var text:String = action
2022-04-15 07:53:44 +00:00
if text.ends_with("_PLUS"):
pass
elif text.find("INPUT_ACTION") == 0:
2022-04-15 12:33:00 +00:00
var subchild:TreeItem = $Window/v/Tab/CONTROL/Tree.create_item(child_action)
2022-03-06 17:15:34 +00:00
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:
2022-04-15 07:53:44 +00:00
var text2:String = tr(Common.get_string_input(z)) + Common.get_input_plus_event(action ,z)
2022-04-15 12:33:00 +00:00
var subchild2:TreeItem = $Window/v/Tab/CONTROL/Tree.create_item(subchild)
2022-03-06 17:15:34 +00:00
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:
2022-04-15 12:33:00 +00:00
var subchild:TreeItem = $Window/v/Tab/CONTROL/Tree.create_item(child_view)
2022-03-06 17:15:34 +00:00
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:
2022-04-15 07:53:44 +00:00
var text2:String = tr(Common.get_string_input(z)) + Common.get_input_plus_event(action ,z)
2022-04-15 12:33:00 +00:00
var subchild2:TreeItem = $Window/v/Tab/CONTROL/Tree.create_item(subchild)
2022-03-06 17:15:34 +00:00
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:
2022-04-15 12:33:00 +00:00
var subchild:TreeItem = $Window/v/Tab/CONTROL/Tree.create_item(child_other)
2022-03-06 17:15:34 +00:00
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:
2022-04-15 07:53:44 +00:00
var text2:String = tr(Common.get_string_input(z)) + Common.get_input_plus_event(action ,z)
2022-04-15 12:33:00 +00:00
var subchild2:TreeItem = $Window/v/Tab/CONTROL/Tree.create_item(subchild)
2022-03-06 17:15:34 +00:00
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")
2022-02-12 22:15:01 +00:00
func configure_control_sort_by_input():
2022-04-15 12:33:00 +00:00
for child in $Window/v/Tab/CONTROL/Input/Control.get_children():
2022-02-24 22:08:03 +00:00
child.queue_free()
2022-02-12 22:15:01 +00:00
var def = {}
for action in InputMap.get_actions():
for z in InputMap.action_get_events(action):
2022-04-15 12:33:00 +00:00
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 }
2022-02-12 22:15:01 +00:00
var defsorted = def.keys()
defsorted.sort()
var lastevent = null
for id in defsorted:
var z = def[id]['event']
2022-04-15 12:33:00 +00:00
var eventplus = def[id]['plus']
2022-02-12 22:15:01 +00:00
var action = def[id]['action']
2022-04-15 12:33:00 +00:00
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()
2022-02-12 22:15:01 +00:00
if zhash != lastevent:
var separator = HSeparator.new()
2022-04-15 12:33:00 +00:00
$Window/v/Tab/CONTROL/Input/Control.add_child( separator )
2022-02-12 22:15:01 +00:00
lastevent = zhash
if z is InputEventKey:
2022-02-24 22:08:03 +00:00
var item = preload( "res://scenes/controls/control_input.tscn" ).instantiate() #.instance()
2022-02-25 00:28:14 +00:00
item.set_param(action, "Key: " + Common.get_string_input_keyboard(z), action, z)
2022-02-24 22:08:03 +00:00
item.connect( "del_pressed", _on_input_box_del_pressed.bind() )
2022-04-15 12:33:00 +00:00
$Window/v/Tab/CONTROL/Input/Control.add_child( item )
2022-02-12 22:15:01 +00:00
elif z is InputEventMouseButton:
var item = preload( "res://scenes/controls/control_input.tscn" ).instantiate()
2022-04-15 12:33:00 +00:00
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)
2022-02-24 22:08:03 +00:00
item.connect( "del_pressed", _on_input_box_del_pressed.bind() )
2022-04-15 12:33:00 +00:00
$Window/v/Tab/CONTROL/Input/Control.add_child( item )
2022-02-12 22:15:01 +00:00
elif z is InputEventJoypadButton:
var item = preload( "res://scenes/controls/control_input.tscn" ).instantiate()
2022-02-25 00:28:14 +00:00
item.set_param(action, "Joypad Button: " + Common.get_string_input_joypad_button(z), action, z)
2022-02-24 22:08:03 +00:00
item.connect( "del_pressed", _on_input_box_del_pressed.bind() )
2022-04-15 12:33:00 +00:00
$Window/v/Tab/CONTROL/Input/Control.add_child( item )
2022-02-12 22:15:01 +00:00
for action in InputMap.get_actions():
var separator = HSeparator.new()
2022-04-15 12:33:00 +00:00
$Window/v/Tab/CONTROL/Input/Control.add_child( separator )
2022-02-12 22:15:01 +00:00
var control_box = preload( "res://scenes/controls/control_function.tscn" ).instantiate()
control_box.set_param(action, action, true)
2022-02-24 22:08:03 +00:00
control_box.connect( "add_pressed", _on_control_box_add_pressed.bind() )
2022-04-15 12:33:00 +00:00
$Window/v/Tab/CONTROL/Input/Control.add_child( control_box )
2022-02-12 22:15:01 +00:00
func configure_control_sort_by_categories():
2022-04-15 12:33:00 +00:00
for child in $Window/v/Tab/CONTROL/Input/Control.get_children():
2022-02-24 22:08:03 +00:00
child.queue_free()
for action in InputMap.get_actions():
2022-04-15 12:33:00 +00:00
var text:String = action
if text.ends_with("_PLUS"):
continue
2022-02-24 22:08:03 +00:00
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() )
2022-04-15 12:33:00 +00:00
$Window/v/Tab/CONTROL/Input/Control.add_child( control_box )
2022-02-24 22:08:03 +00:00
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()
2022-02-25 00:28:14 +00:00
item.set_param(action, "Key: " + Common.get_string_input_keyboard(z), "", z)
2022-02-24 22:08:03 +00:00
item.connect( "del_pressed", _on_input_box_del_pressed.bind() )
2022-04-15 12:33:00 +00:00
$Window/v/Tab/CONTROL/Input/Control.add_child( item )
2022-02-24 22:08:03 +00:00
elif z is InputEventMouseButton:
var item = preload( "res://scenes/controls/control_input.tscn" ).instantiate()
2022-04-15 12:33:00 +00:00
item.set_param(action, "Mouse Button: " + tr(Common.get_string_input_mousse_button(z)) + Common.get_input_plus_event(action ,z) , "", z)
2022-02-24 22:08:03 +00:00
item.connect( "del_pressed", _on_input_box_del_pressed.bind() )
2022-04-15 12:33:00 +00:00
$Window/v/Tab/CONTROL/Input/Control.add_child( item )
2022-02-24 22:08:03 +00:00
elif z is InputEventJoypadButton:
var item = preload( "res://scenes/controls/control_input.tscn" ).instantiate()
2022-02-25 00:28:14 +00:00
item.set_param(action, "Joypad Button: " + Common.get_string_input_joypad_button(z), "", z)
2022-02-24 22:08:03 +00:00
item.connect( "del_pressed", _on_input_box_del_pressed.bind() )
2022-04-15 12:33:00 +00:00
$Window/v/Tab/CONTROL/Input/Control.add_child( item )
2022-02-24 22:08:03 +00:00
var separator = HSeparator.new()
2022-04-15 12:33:00 +00:00
$Window/v/Tab/CONTROL/Input/Control.add_child( separator )
2022-02-12 22:15:01 +00:00
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()
2022-02-24 22:08:03 +00:00
func _on_input_box_del_pressed(command, control, inputevent):
2022-02-25 00:28:14 +00:00
InputMap.action_erase_event(command, inputevent)
configure_control()
2022-02-24 22:08:03 +00:00
func _on_control_box_add_pressed(action, command):
2022-04-15 07:53:44 +00:00
$Window.hide()
2022-02-25 00:28:14 +00:00
$SelectType.set_param(action, "signal_refresh" )
$SelectType/Window.popup_centered()
$SelectType/Window.visible = true
2022-02-24 22:08:03 +00:00
2022-03-06 17:15:34 +00:00
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:
2022-04-15 07:53:44 +00:00
$Window.hide()
2022-03-06 17:15:34 +00:00
$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()
2022-04-15 07:53:44 +00:00
func show_windows():
2022-04-15 12:33:00 +00:00
Common.msg_debug("show_windows")
2022-04-15 07:53:44 +00:00
$Window.visible = true
func define_mouse_camera_move():
configure_control()
show_windows()
2022-04-15 13:54:35 +00:00
func _on_select_mouse_button_camera_player_pressed():
2022-04-15 07:53:44 +00:00
$Window.visible = false
2022-04-15 12:33:00 +00:00
$DefineInput.set_param(2, "INPUT_VIEW_CAMERA_MOVE_PLAYER_FOLLOW_MOUSE")
2022-04-15 07:53:44 +00:00
$DefineInput/Window.popup_centered()
$DefineInput/Window.visible = true
2022-04-15 13:54:35 +00:00
func _on_select_mouse_button_camera_only_pressed():
2022-04-15 07:53:44 +00:00
$Window.visible = false
2022-04-15 12:33:00 +00:00
$DefineInput.set_param(2, "INPUT_VIEW_CAMERA_MOVE_ONLY_MOUSE")
2022-04-15 07:53:44 +00:00
$DefineInput/Window.popup_centered()
$DefineInput/Window.visible = true
2022-04-15 13:54:35 +00:00
func _on_select_mouse_button_camera_player_walk_pressed():
2022-04-15 07:53:44 +00:00
$Window.visible = false
2022-04-15 12:33:00 +00:00
$DefineInput.set_param(2, "INPUT_VIEW_CAMERA_PLAYER_WALK_MOUSE")
2022-04-15 07:53:44 +00:00
$DefineInput/Window.popup_centered()
$DefineInput/Window.visible = true
2022-04-15 13:54:35 +00:00
func _on_select_joypad_button_camera_player_pressed():
$Window.visible = false
$DefineInput.set_param(4, "INPUT_VIEW_CAMERA_MOVE_PLAYER_FOLLOW_JOYPAD")
$DefineInput/Window.popup_centered()
$DefineInput/Window.visible = true
func _on_select_joypad_button_camera_only_pressed():
$Window.visible = false
$DefineInput.set_param(4, "INPUT_VIEW_CAMERA_MOVE_ONLY_JOYPAD")
$DefineInput/Window.popup_centered()
$DefineInput/Window.visible = true
func _on_joypad_camera_player_walk_button_pressed():
$Window.visible = false
$DefineInput.set_param(4, "INPUT_VIEW_CAMERA_PLAYER_WALK_JOYPAD")
$DefineInput/Window.popup_centered()
$DefineInput/Window.visible = true