2018-07-25 07:36:19 +00:00
|
|
|
tool
|
|
|
|
extends HBoxContainer
|
|
|
|
|
2018-08-03 10:02:46 +00:00
|
|
|
#export(String) var action_name = ""
|
2018-07-25 07:36:19 +00:00
|
|
|
export(String) var description = "Lorem ipsum."
|
|
|
|
export(String) var config_file = "user://input.cfg"
|
|
|
|
|
2018-08-03 10:02:46 +00:00
|
|
|
export(String) var default_keyboard
|
|
|
|
export(String) var default_keyboard_alt
|
|
|
|
export(String) var default_joypad
|
|
|
|
|
|
|
|
var action_name
|
|
|
|
var current_keyboard
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
action_name = self.name
|
|
|
|
current_keyboard = $keyboard
|
|
|
|
load_from_config()
|
|
|
|
|
|
|
|
var key_found = false
|
|
|
|
var key_alt_found = false
|
|
|
|
for event in InputMap.get_action_list( action_name ):
|
|
|
|
if event is InputEventKey:
|
|
|
|
pass
|
|
|
|
if not key_found:
|
|
|
|
$keyboard.text = OS.get_scancode_string( event.get_scancode_with_modifiers() )
|
|
|
|
key_found = true
|
|
|
|
elif not key_alt_found:
|
|
|
|
$keyboard_alt.text = OS.get_scancode_string( event.get_scancode_with_modifiers() )
|
|
|
|
key_alt_found = true
|
|
|
|
elif event is InputEventJoypadButton:
|
|
|
|
$joypad.text = Input.get_joy_button_string( event.button_index )
|
|
|
|
|
|
|
|
$keyboard.connect( "pressed", self, "wait_for_input" )
|
|
|
|
$keyboard_alt.connect( "pressed", self, "wait_for_input_alt" )
|
|
|
|
$joypad.connect( "pressed", self, "wait_for_input" )
|
|
|
|
|
|
|
|
set_process_input(false)
|
2018-07-25 07:36:19 +00:00
|
|
|
func _enter_tree():
|
|
|
|
|
|
|
|
if not self.has_node( "description" ):
|
|
|
|
var new_label = Label.new()
|
|
|
|
new_label.name = "description"
|
|
|
|
new_label.text = description
|
|
|
|
new_label.align = HALIGN_RIGHT
|
|
|
|
new_label.size_flags_horizontal = SIZE_EXPAND_FILL
|
|
|
|
new_label.size_flags_vertical = SIZE_EXPAND
|
|
|
|
self.add_child( new_label )
|
|
|
|
new_label.set_owner( self )
|
|
|
|
|
|
|
|
if not self.has_node( "keyboard" ):
|
|
|
|
var new_button = Button.new()
|
|
|
|
new_button.name = "keyboard"
|
|
|
|
new_button.align = HALIGN_LEFT
|
|
|
|
new_button.size_flags_horizontal = SIZE_EXPAND_FILL
|
|
|
|
new_button.size_flags_vertical = SIZE_EXPAND
|
|
|
|
self.add_child( new_button )
|
|
|
|
new_button.set_owner( self )
|
2018-08-03 10:02:46 +00:00
|
|
|
|
|
|
|
if not self.has_node( "keyboard_alt" ):
|
|
|
|
var new_button = Button.new()
|
|
|
|
new_button.name = "keyboard_alt"
|
|
|
|
new_button.align = HALIGN_LEFT
|
|
|
|
new_button.size_flags_horizontal = SIZE_EXPAND_FILL
|
|
|
|
new_button.size_flags_vertical = SIZE_EXPAND
|
|
|
|
self.add_child( new_button )
|
|
|
|
new_button.set_owner( self )
|
|
|
|
#
|
2018-07-25 07:36:19 +00:00
|
|
|
#
|
|
|
|
if not self.has_node( "joypad" ):
|
|
|
|
var new_button_joypad = Button.new()
|
|
|
|
new_button_joypad.name = "joypad"
|
|
|
|
new_button_joypad.align = HALIGN_LEFT
|
|
|
|
new_button_joypad.size_flags_horizontal = SIZE_EXPAND_FILL
|
|
|
|
new_button_joypad.size_flags_vertical = SIZE_EXPAND
|
|
|
|
self.add_child( new_button_joypad )
|
|
|
|
new_button_joypad.set_owner( self )
|
|
|
|
|
|
|
|
func get_event_with_modifier_from_string( scancode, type ):
|
|
|
|
if type == "keyboard":
|
|
|
|
var new_event = InputEventKey.new()
|
|
|
|
|
|
|
|
if not scancode.find( "Meta" ) == -1:
|
|
|
|
new_event.meta = true
|
|
|
|
if not scancode.find( "Control" ) == -1:
|
|
|
|
new_event.control = true
|
|
|
|
if not scancode.find( "Alt" ) == -1:
|
|
|
|
new_event.alt = true
|
|
|
|
if not scancode.find( "Shift" ) == -1:
|
|
|
|
new_event.shift = true
|
|
|
|
|
|
|
|
if not scancode.findn( "+" ) == -1:
|
|
|
|
scancode = scancode.right(scancode.find_last( "+" )+1)
|
|
|
|
|
|
|
|
new_event.scancode = OS.find_scancode_from_string( scancode )
|
|
|
|
return new_event
|
|
|
|
elif type == "joypad":
|
|
|
|
var new_event = InputEventJoypadButton.new()
|
|
|
|
|
|
|
|
new_event.button_index = Input.get_joy_button_index_from_string( scancode )
|
|
|
|
return new_event
|
|
|
|
return null
|
2018-08-03 10:02:46 +00:00
|
|
|
|
|
|
|
|
2018-07-25 07:36:19 +00:00
|
|
|
func load_from_config():
|
|
|
|
var config = ConfigFile.new()
|
|
|
|
var err = config.load( config_file )
|
|
|
|
if err:# Assuming that file is missing, generate default config
|
2018-08-03 10:02:46 +00:00
|
|
|
#
|
|
|
|
# for event in InputMap.get_action_list( action_name ):
|
|
|
|
# if event is InputEventKey:
|
|
|
|
# var scancode = OS.get_scancode_string( event.get_scancode_with_modifiers() )
|
|
|
|
# config.set_value(current_keyboard.name, action_name, scancode)
|
|
|
|
## if current_keyboard.name == "keyboard_alt":
|
|
|
|
## break
|
|
|
|
# current_keyboard = $keyboard_alt
|
|
|
|
#
|
|
|
|
# elif event is InputEventJoypadButton:
|
|
|
|
# var joy_button_name = Input.get_joy_button_string( event.button_index )
|
|
|
|
# config.set_value("joypad", action_name, joy_button_name)
|
2018-07-25 07:36:19 +00:00
|
|
|
config.save( config_file )
|
2018-08-03 10:02:46 +00:00
|
|
|
load_from_config()
|
2018-07-25 07:36:19 +00:00
|
|
|
else:
|
2018-08-03 10:02:46 +00:00
|
|
|
|
|
|
|
# on initialise l'entrée, si elle n'existe pas déjà, par la valeur par defaut du projet.
|
|
|
|
if not config.has_section_key("keyboard", action_name):
|
2018-07-25 07:36:19 +00:00
|
|
|
for event in InputMap.get_action_list( action_name ):
|
|
|
|
if event is InputEventKey:
|
|
|
|
var scancode = OS.get_scancode_string( event.get_scancode_with_modifiers() )
|
|
|
|
config.set_value("keyboard", action_name, scancode)
|
2018-08-03 10:02:46 +00:00
|
|
|
break
|
|
|
|
# on initialise l'entrée alternative, si elle n'existe pas déjà, par la valeur par defaut du projet.
|
|
|
|
var is_first = true
|
|
|
|
if not config.has_section_key("keyboard_alt", action_name):
|
2018-07-25 07:36:19 +00:00
|
|
|
for event in InputMap.get_action_list( action_name ):
|
2018-08-03 10:02:46 +00:00
|
|
|
if not is_first and event is InputEventKey:
|
|
|
|
var scancode = OS.get_scancode_string( event.get_scancode_with_modifiers() )
|
|
|
|
config.set_value("keyboard_alt", action_name, scancode)
|
|
|
|
break
|
|
|
|
is_first = false
|
2018-07-25 07:36:19 +00:00
|
|
|
|
2018-08-03 10:02:46 +00:00
|
|
|
|
|
|
|
# On efface toutes les touches de clavier de l'InputMap correspondants à l'action en cours.
|
|
|
|
for old_event in InputMap.get_action_list( action_name ):
|
|
|
|
if old_event is InputEventKey:
|
|
|
|
InputMap.action_erase_event(action_name, old_event)
|
|
|
|
|
|
|
|
# on recupere les touches du fichier de config et on les ajoutes à l'input map.
|
|
|
|
if config.has_section("keyboard"):
|
|
|
|
var action_found = false
|
|
|
|
for action in config.get_section_keys("keyboard"):
|
|
|
|
if action == action_name:
|
|
|
|
action_found = true
|
|
|
|
var event = get_event_with_modifier_from_string( config.get_value("keyboard", action_name), "keyboard")
|
|
|
|
InputMap.action_add_event(action_name, event)
|
|
|
|
break
|
|
|
|
# if not action_found:
|
|
|
|
# for event in InputMap.get_action_list( action_name ):
|
|
|
|
# if event is InputEventKey:
|
|
|
|
# var scancode = OS.get_scancode_string( event.get_scancode_with_modifiers() )
|
|
|
|
# config.set_value("keyboard", action_name, scancode)
|
|
|
|
|
|
|
|
if config.has_section("keyboard_alt"):
|
|
|
|
var action_found = false
|
|
|
|
for action in config.get_section_keys("keyboard_alt"):
|
|
|
|
if action == action_name:
|
|
|
|
action_found = true
|
|
|
|
var event = get_event_with_modifier_from_string( config.get_value("keyboard_alt", action_name), "keyboard")
|
|
|
|
InputMap.action_add_event(action_name, event)
|
|
|
|
break
|
|
|
|
# if not action_found:
|
|
|
|
# for event in InputMap.get_action_list( action_name ):
|
|
|
|
# if event is InputEventKey:
|
|
|
|
# var scancode = OS.get_scancode_string( event.get_scancode_with_modifiers() )
|
|
|
|
# config.set_value("keyboard_alt", action_name, scancode)
|
|
|
|
|
|
|
|
|
|
|
|
if config.has_section("joypad"):
|
|
|
|
var action_found = false
|
|
|
|
for action in config.get_section_keys("joypad"):
|
|
|
|
if action == action_name:
|
|
|
|
action_found = true
|
|
|
|
var event = get_event_with_modifier_from_string( config.get_value("joypad", action_name), "joypad")
|
|
|
|
for old_event in InputMap.get_action_list( action_name ):
|
|
|
|
if old_event is InputEventJoypadButton:
|
|
|
|
InputMap.action_erase_event(action_name, old_event)
|
|
|
|
InputMap.action_add_event(action_name, event)
|
|
|
|
|
|
|
|
if not action_found:
|
|
|
|
for event in InputMap.get_action_list( action_name ):
|
|
|
|
if event is InputEventJoypadButton:
|
|
|
|
var joy_button_name = Input.get_joy_button_string( event.button_index )
|
|
|
|
config.set_value("joypad", action_name, joy_button_name)
|
|
|
|
|
2018-07-25 07:36:19 +00:00
|
|
|
config.save( config_file )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func save_to_config(section, key, value):
|
|
|
|
var config = ConfigFile.new()
|
|
|
|
var err = config.load( config_file )
|
|
|
|
if err:
|
|
|
|
print("Error code when loading config file: ", err)
|
|
|
|
else:
|
|
|
|
config.set_value(section, key, value)
|
|
|
|
config.save( config_file )
|
|
|
|
|
|
|
|
|
|
|
|
# Input management
|
|
|
|
func wait_for_input():
|
2018-08-03 10:02:46 +00:00
|
|
|
current_keyboard = $keyboard
|
2018-07-25 07:36:19 +00:00
|
|
|
set_process_input(true)
|
|
|
|
|
2018-08-03 10:02:46 +00:00
|
|
|
func wait_for_input_alt():
|
|
|
|
current_keyboard = $keyboard_alt
|
|
|
|
set_process_input(true)
|
2018-07-25 07:36:19 +00:00
|
|
|
# Input management
|
|
|
|
|
|
|
|
func _input(event):
|
|
|
|
if event is InputEventKey and not event.pressed and not event.is_echo():
|
|
|
|
var input_string = OS.get_scancode_string( event.scancode )
|
|
|
|
|
|
|
|
if not event.is_action("ui_cancel") \
|
|
|
|
and not input_string == "Meta" \
|
|
|
|
and not input_string == "Control" \
|
|
|
|
and not input_string == "Alt" \
|
|
|
|
and not input_string == "Shift" \
|
|
|
|
:
|
|
|
|
get_tree().set_input_as_handled()
|
|
|
|
set_process_input( false )
|
|
|
|
|
2018-08-03 10:02:46 +00:00
|
|
|
current_keyboard.text = ""
|
2018-07-25 07:36:19 +00:00
|
|
|
if event.meta:
|
2018-08-03 10:02:46 +00:00
|
|
|
current_keyboard.text += "Meta+"
|
2018-07-25 07:36:19 +00:00
|
|
|
if event.control:
|
2018-08-03 10:02:46 +00:00
|
|
|
current_keyboard.text += "Control+"
|
2018-07-25 07:36:19 +00:00
|
|
|
if event.alt:
|
2018-08-03 10:02:46 +00:00
|
|
|
current_keyboard.text += "Alt+"
|
2018-07-25 07:36:19 +00:00
|
|
|
if event.shift:
|
2018-08-03 10:02:46 +00:00
|
|
|
current_keyboard.text += "Shift+"
|
|
|
|
current_keyboard.text += input_string
|
|
|
|
|
|
|
|
|
|
|
|
var key_to_change = 0
|
|
|
|
if current_keyboard.name == "keyboard_alt":
|
|
|
|
key_to_change = 1
|
|
|
|
var current_key = 0
|
2018-07-25 07:36:19 +00:00
|
|
|
|
|
|
|
for old_event in InputMap.get_action_list( action_name ):
|
2018-08-03 10:02:46 +00:00
|
|
|
if old_event is InputEventKey and key_to_change == current_key:
|
2018-07-25 07:36:19 +00:00
|
|
|
InputMap.action_erase_event(action_name, old_event)
|
2018-08-03 10:02:46 +00:00
|
|
|
current_key +=1
|
2018-07-25 07:36:19 +00:00
|
|
|
|
|
|
|
InputMap.action_add_event(action_name, event)
|
2018-08-03 10:02:46 +00:00
|
|
|
save_to_config(current_keyboard.name, action_name, current_keyboard.text)
|
2018-07-25 07:36:19 +00:00
|
|
|
|
|
|
|
elif event is InputEventJoypadButton and not event.pressed and not event.is_echo():
|
|
|
|
var input_string = Input.get_joy_button_string( event.button_index )
|
|
|
|
|
|
|
|
get_tree().set_input_as_handled()
|
|
|
|
set_process_input( false )
|
|
|
|
|
|
|
|
$joypad.text = input_string
|
|
|
|
for old_event in InputMap.get_action_list( action_name ):
|
|
|
|
if old_event is InputEventJoypadButton:
|
|
|
|
InputMap.action_erase_event(action_name, old_event)
|
|
|
|
InputMap.action_add_event(action_name, event)
|
|
|
|
save_to_config("joypad", action_name, $joypad.text)
|
|
|
|
|