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.

123 lines
4.5 KiB

  1. extends Control
  2. onready var grid = find_node("grid")
  3. onready var key_dialog = get_node("key_dialog")
  4. const INPUT_TYPE_KEY = 0
  5. const INPUT_TYPE_JOYPAD = 1
  6. var last_action:String
  7. var last_event:InputEvent
  8. var new_inputs = {}
  9. func _ready():
  10. init_mapping()
  11. func init_mapping():
  12. new_inputs = {}
  13. for action in config.CONTROL_ACTIONS:
  14. var action_label = Label.new()
  15. action_label.set_name(action + "_label")
  16. action_label.set_text(action)
  17. grid.add_child(action_label)
  18. var key_action_button = Button.new()
  19. key_action_button.set_h_size_flags(SIZE_SHRINK_CENTER)
  20. key_action_button.set_flat(true)
  21. key_action_button.set_meta("action",action)
  22. key_action_button.set_button_icon(get_node("icons/key").get_button_icon())
  23. var joypad_action_button = Button.new()
  24. joypad_action_button.set_h_size_flags(SIZE_SHRINK_CENTER)
  25. joypad_action_button.set_flat(true)
  26. joypad_action_button.set_meta("action",action)
  27. for event in InputMap.get_action_list(action):
  28. if event is InputEventKey:
  29. key_action_button.set_text(event.as_text())
  30. key_action_button.set_tooltip(event.as_text())
  31. elif event is InputEventJoypadButton:
  32. joypad_action_button.set_tooltip(str(event.button_index))
  33. if has_node("icons/joypad/" + str(event.button_index)):
  34. joypad_action_button.set_button_icon(get_node("icons/joypad/" + str(event.button_index)).get_button_icon())
  35. else:
  36. joypad_action_button.set_button_icon(get_node("icons/joypad/other").get_button_icon())
  37. joypad_action_button.set_text(str(event.button_index))
  38. grid.add_child(key_action_button)
  39. key_action_button.connect("pressed",self,"_on_button_pressed",[key_action_button, INPUT_TYPE_KEY])
  40. grid.add_child(joypad_action_button)
  41. joypad_action_button.connect("pressed",self,"_on_button_pressed",[joypad_action_button, INPUT_TYPE_JOYPAD])
  42. func _unhandled_input(event):
  43. if key_dialog.is_visible_in_tree():
  44. var action_button = key_dialog.find_node("action_button")
  45. var check_usage = false
  46. if key_dialog.get_meta("type") == INPUT_TYPE_KEY && event is InputEventKey:
  47. check_usage = true
  48. last_event = event
  49. action_button.set_button_icon(get_node("icons/key").get_button_icon())
  50. action_button.set_text(event.as_text())
  51. action_button.set_tooltip(event.as_text())
  52. elif key_dialog.get_meta("type") == INPUT_TYPE_JOYPAD && event is InputEventJoypadButton:
  53. check_usage = true
  54. last_event = event
  55. action_button.set_tooltip(str(event.button_index))
  56. if has_node("icons/joypad/" + str(event.button_index)):
  57. action_button.set_text("")
  58. action_button.set_button_icon(get_node("icons/joypad/" + str(event.button_index)).get_button_icon())
  59. else:
  60. action_button.set_button_icon(get_node("icons/joypad/other").get_button_icon())
  61. action_button.set_text(str(event.button_index))
  62. if check_usage:
  63. var has_event = ""
  64. for action in config.CONTROL_ACTIONS:
  65. if InputMap.event_is_action(event, action):
  66. has_event = action
  67. if not has_event == "" && not has_event == last_action:
  68. key_dialog.set_text(tr("KEY_ALREADY_TAKEN") + '\n\'' + tr(has_event) + '\'')
  69. key_dialog.get_ok().set_disabled(true)
  70. else:
  71. key_dialog.set_text(tr("PRESS_KEY"))
  72. key_dialog.get_ok().set_disabled(false)
  73. func _on_button_pressed(button, type):
  74. last_action = button.get_meta("action")
  75. last_event = null
  76. var action_button = key_dialog.find_node("action_button")
  77. action_button.set_text("")
  78. action_button.set_button_icon(null)
  79. key_dialog.set_text(tr("PRESS_KEY"))
  80. key_dialog.set_meta("type", type)
  81. key_dialog.connect("confirmed",self,"_on_dialog_confirmed",[button])
  82. key_dialog.get_ok().set_disabled(true)
  83. key_dialog.get_label().set_align(HALIGN_CENTER)
  84. key_dialog.popup_centered()
  85. func _on_dialog_confirmed(button):
  86. if not new_inputs.has(last_action):
  87. new_inputs[last_action] = {}
  88. match key_dialog.get_meta("type"):
  89. INPUT_TYPE_KEY:
  90. if last_event is InputEventKey:
  91. new_inputs[last_action]["key"] = OS.get_scancode_string(last_event.scancode)
  92. button.set_text(last_event.as_text())
  93. button.set_tooltip(last_event.as_text())
  94. INPUT_TYPE_JOYPAD:
  95. if last_event is InputEventJoypadButton:
  96. new_inputs[last_action]["joypad"] = last_event.button_index
  97. button.set_tooltip(str(last_event.button_index))
  98. if has_node("icons/joypad/" + str(last_event.button_index)):
  99. button.set_button_icon(get_node("icons/joypad/" + str(last_event.button_index)).get_button_icon())
  100. else:
  101. button.set_button_icon(get_node("icons/joypad/other").get_button_icon())
  102. button.set_text(str(last_event.button_index))