controls.gd 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. extends Control
  2. # Note for the reader:
  3. #
  4. # This demo conveniently uses the same names for actions and for the container nodes
  5. # that hold each remapping button. This allow to get back to the button based simply
  6. # on the name of the corresponding action, but it might not be so simple in your project.
  7. #
  8. # A better approach for large-scale input remapping might be to do the connections between
  9. # buttons and wait_for_input through the code, passing as arguments both the name of the
  10. # action and the node, e.g.:
  11. # button.connect("pressed", self, "wait_for_input", [ button, action ])
  12. # Constants
  13. const INPUT_ACTIONS = [ "move_up", "move_down", "move_left", "move_right", "jump" ]
  14. const CONFIG_FILE = "user://input.cfg"
  15. # Member variables
  16. var action # To register the action the UI is currently handling
  17. var button # Button node corresponding to the above action
  18. # Load/save input mapping to a config file
  19. # Changes done while testing the demo will be persistent, saved to CONFIG_FILE
  20. func load_config():
  21. var config = ConfigFile.new()
  22. var err = config.load(CONFIG_FILE)
  23. if err: # Assuming that file is missing, generate default config
  24. for action_name in INPUT_ACTIONS:
  25. var action_list = InputMap.get_action_list(action_name)
  26. # There could be multiple actions in the list, but we save the first one by default
  27. var scancode = OS.get_scancode_string(action_list[0].scancode)
  28. config.set_value("input", action_name, scancode)
  29. config.save(CONFIG_FILE)
  30. else: # ConfigFile was properly loaded, initialize InputMap
  31. for action_name in config.get_section_keys("input"):
  32. # Get the key scancode corresponding to the saved human-readable string
  33. var scancode = OS.find_scancode_from_string(config.get_value("input", action_name))
  34. # Create a new event object based on the saved scancode
  35. var event = InputEvent()
  36. event.type = InputEvent.KEY
  37. event.scancode = scancode
  38. # Replace old action (key) events by the new one
  39. for old_event in InputMap.get_action_list(action_name):
  40. if old_event.type == InputEvent.KEY:
  41. InputMap.action_erase_event(action_name, old_event)
  42. InputMap.action_add_event(action_name, event)
  43. func save_to_config(section, key, value):
  44. """Helper function to redefine a parameter in the settings file"""
  45. var config = ConfigFile.new()
  46. var err = config.load(CONFIG_FILE)
  47. if err:
  48. print("Error code when loading config file: ", err)
  49. else:
  50. config.set_value(section, key, value)
  51. config.save(CONFIG_FILE)
  52. # Input management
  53. func wait_for_input(action_bind):
  54. action = action_bind
  55. # See note at the beginning of the script
  56. button = get_node("bindings").get_node(action).get_node("Button")
  57. get_node("contextual_help").set_text("Press a key to assign to the '" + action + "' action.")
  58. set_process_input(true)
  59. func _input(event):
  60. # Handle the first pressed key
  61. if event.type == InputEvent.KEY:
  62. # Register the event as handled and stop polling
  63. get_tree().set_input_as_handled()
  64. set_process_input(false)
  65. # Reinitialise the contextual help label
  66. get_node("contextual_help").set_text("Click a key binding to reassign it, or press the Cancel action.")
  67. if not event.is_action("ui_cancel"):
  68. # Display the string corresponding to the pressed key
  69. var scancode = OS.get_scancode_string(event.scancode)
  70. button.set_text(scancode)
  71. # Start by removing previously key binding(s)
  72. for old_event in InputMap.get_action_list(action):
  73. InputMap.action_erase_event(action, old_event)
  74. # Add the new key binding
  75. InputMap.action_add_event(action, event)
  76. save_to_config("input", action, scancode)
  77. func _ready():
  78. # Load config if existing, if not it will be generated with default values
  79. load_config()
  80. # Initialise each button with the default key binding from InputMap
  81. for action in INPUT_ACTIONS:
  82. # We assume that the key binding that we want is the first one (0), if there are several
  83. var input_event = InputMap.get_action_list(action)[0]
  84. # See note at the beginning of the script
  85. var button = get_node("bindings").get_node(action).get_node("Button")
  86. button.set_text(OS.get_scancode_string(input_event.scancode))
  87. button.connect("pressed", self, "wait_for_input", [action])