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.

127 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 apply():
  12. config.set_value("controls","mapping",new_inputs)
  13. func init_mapping():
  14. new_inputs = {}
  15. for action in config.CONTROL_ACTIONS:
  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 event in InputMap.get_action_list(action):
  30. if event is InputEventKey:
  31. key_action_button.set_text(event.as_text())
  32. key_action_button.set_tooltip(event.as_text())
  33. elif event is InputEventJoypadButton:
  34. joypad_action_button.set_tooltip(str(event.button_index))
  35. if has_node("icons/joypad/" + str(event.button_index)):
  36. joypad_action_button.set_button_icon(get_node("icons/joypad/" + str(event.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(event.button_index))
  40. grid.add_child(key_action_button)
  41. key_action_button.connect("pressed",self,"_on_button_pressed",[key_action_button, INPUT_TYPE_KEY])
  42. grid.add_child(joypad_action_button)
  43. joypad_action_button.connect("pressed",self,"_on_button_pressed",[joypad_action_button, INPUT_TYPE_JOYPAD])
  44. func _unhandled_input(event):
  45. if key_dialog.is_visible_in_tree():
  46. var action_button = key_dialog.find_node("action_button")
  47. var check_usage = false
  48. if key_dialog.get_meta("type") == INPUT_TYPE_KEY && event is InputEventKey:
  49. check_usage = true
  50. last_event = event
  51. action_button.set_button_icon(get_node("icons/key").get_button_icon())
  52. action_button.set_text(event.as_text())
  53. action_button.set_tooltip(event.as_text())
  54. elif key_dialog.get_meta("type") == INPUT_TYPE_JOYPAD && event is InputEventJoypadButton:
  55. check_usage = true
  56. last_event = event
  57. action_button.set_tooltip(str(event.button_index))
  58. if has_node("icons/joypad/" + str(event.button_index)):
  59. action_button.set_text("")
  60. action_button.set_button_icon(get_node("icons/joypad/" + str(event.button_index)).get_button_icon())
  61. else:
  62. action_button.set_button_icon(get_node("icons/joypad/other").get_button_icon())
  63. action_button.set_text(str(event.button_index))
  64. if check_usage:
  65. var has_event = ""
  66. for action in config.CONTROL_ACTIONS:
  67. if InputMap.event_is_action(event, action):
  68. has_event = action
  69. if not has_event == "" && not has_event == last_action:
  70. key_dialog.set_text(tr("KEY_ALREADY_TAKEN") + '\n\'' + tr(has_event) + '\'')
  71. key_dialog.get_ok().set_disabled(true)
  72. else:
  73. key_dialog.set_text(tr("PRESS_KEY"))
  74. key_dialog.get_ok().set_disabled(false)
  75. func _on_button_pressed(button, type):
  76. last_action = button.get_meta("action")
  77. last_event = null
  78. var action_button = key_dialog.find_node("action_button")
  79. action_button.set_text("")
  80. action_button.set_button_icon(null)
  81. key_dialog.set_text(tr("PRESS_KEY"))
  82. key_dialog.set_meta("type", type)
  83. key_dialog.connect("confirmed",self,"_on_dialog_confirmed",[button])
  84. key_dialog.get_ok().set_disabled(true)
  85. key_dialog.get_label().set_align(HALIGN_CENTER)
  86. key_dialog.popup_centered()
  87. func _on_dialog_confirmed(button):
  88. if not new_inputs.has(last_action):
  89. new_inputs[last_action] = {}
  90. match key_dialog.get_meta("type"):
  91. INPUT_TYPE_KEY:
  92. if last_event is InputEventKey:
  93. new_inputs[last_action]["key"] = OS.get_scancode_string(last_event.scancode)
  94. button.set_text(last_event.as_text())
  95. button.set_tooltip(last_event.as_text())
  96. INPUT_TYPE_JOYPAD:
  97. if last_event is InputEventJoypadButton:
  98. new_inputs[last_action]["joypad"] = last_event.button_index
  99. button.set_tooltip(str(last_event.button_index))
  100. if has_node("icons/joypad/" + str(last_event.button_index)):
  101. button.set_button_icon(get_node("icons/joypad/" + str(last_event.button_index)).get_button_icon())
  102. else:
  103. button.set_button_icon(get_node("icons/joypad/other").get_button_icon())
  104. button.set_text(str(last_event.button_index))