Public repository for MUR pre alpha
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.0 KiB

  1. tool
  2. extends Control
  3. onready var grid = find_node("grid")
  4. onready var key_dialog = get_node("key_dialog")
  5. const INPUT_TYPE_KEY = 0
  6. const INPUT_TYPE_JOYPAD = 1
  7. var last_action:String
  8. var last_event:InputEvent
  9. var new_inputs = {}
  10. func _ready():
  11. init_mapping()
  12. func init_mapping():
  13. new_inputs = {}
  14. for action in InputMap.get_actions():
  15. if action.begins_with("controls_"):
  16. var action_label = Label.new()
  17. action_label.set_name(action + "_label")
  18. action_label.set_text(action)
  19. grid.add_child(action_label)
  20. var key_action_button = Button.new()
  21. key_action_button.set_h_size_flags(SIZE_SHRINK_CENTER)
  22. key_action_button.set_flat(true)
  23. key_action_button.set_meta("action",action)
  24. key_action_button.set_button_icon(get_node("icons/key").get_button_icon())
  25. var joypad_action_button = Button.new()
  26. joypad_action_button.set_h_size_flags(SIZE_SHRINK_CENTER)
  27. joypad_action_button.set_flat(true)
  28. joypad_action_button.set_meta("action",action)
  29. for mapping in InputMap.get_action_list(action):
  30. if mapping is InputEventKey:
  31. key_action_button.set_text(mapping.as_text())
  32. key_action_button.set_tooltip(mapping.as_text())
  33. elif mapping is InputEventJoypadButton:
  34. joypad_action_button.set_tooltip(str(mapping.button_index))
  35. if has_node("icons/joypad/" + str(mapping.button_index)):
  36. joypad_action_button.set_button_icon(get_node("icons/joypad/" + str(mapping.button_index)).get_button_icon())
  37. else:
  38. joypad_action_button.set_button_icon(get_node("icons/joypad/other").get_button_icon())
  39. joypad_action_button.set_text(str(mapping.button_index))
  40. grid.add_child(key_action_button)
  41. key_action_button.connect("pressed",self,"_on_button_pressed",[action, INPUT_TYPE_KEY])
  42. grid.add_child(joypad_action_button)
  43. joypad_action_button.connect("pressed",self,"_on_button_pressed",[action, INPUT_TYPE_JOYPAD])
  44. func _unhandled_input(event):
  45. if key_dialog.is_visible_in_tree():
  46. if key_dialog.get_meta("type") == INPUT_TYPE_KEY && event is InputEventKey:
  47. last_event = event
  48. key_dialog.set_text(tr(last_event.as_text()))
  49. key_dialog.get_ok().set_disabled(false)
  50. elif key_dialog.get_meta("type") == INPUT_TYPE_JOYPAD && event is InputEventJoypadButton:
  51. last_event = event
  52. key_dialog.set_text(last_event.as_text())
  53. key_dialog.get_ok().set_disabled(false)
  54. func _on_button_pressed(action, type):
  55. last_action = action
  56. last_event = null
  57. key_dialog.set_text(tr("PRESS_KEY"))
  58. key_dialog.set_meta("type", type)
  59. key_dialog.connect("confirmed",self,"_on_dialog_confirmed")
  60. key_dialog.get_ok().set_disabled(true)
  61. key_dialog.popup_centered()
  62. func _on_dialog_confirmed(type):
  63. if not new_inputs.has(last_action):
  64. new_inputs[last_action] = {}
  65. match key_dialog.get_meta("type"):
  66. INPUT_TYPE_KEY:
  67. if last_event is InputEventKey:
  68. new_inputs[last_action]["key"] = OS.get_scancode_string(last_event.scancode)
  69. INPUT_TYPE_JOYPAD:
  70. if last_event is InputEventJoypadButton:
  71. new_inputs[last_action]["joypad"] = last_event.button_index