remap_wizard.gd 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. extends Node
  2. const DEADZONE = 0.3
  3. var joy_guid = ""
  4. var joy_name = ""
  5. var steps = JoyMapping.BASE.keys()
  6. var cur_step = -1
  7. var cur_mapping = {}
  8. var last_mapping = ""
  9. @onready var joy_buttons = $Mapping/Margin/VBox/SubViewportContainer/SubViewport/JoypadDiagram/Buttons
  10. @onready var joy_axes = $Mapping/Margin/VBox/SubViewportContainer/SubViewport/JoypadDiagram/Axes
  11. @onready var joy_mapping_text = $Mapping/Margin/VBox/Info/Text/Value
  12. @onready var joy_mapping_full_axis = $Mapping/Margin/VBox/Info/Extra/FullAxis
  13. @onready var joy_mapping_axis_invert = $Mapping/Margin/VBox/Info/Extra/InvertAxis
  14. func _input(event):
  15. if cur_step == -1:
  16. return
  17. if event is InputEventJoypadMotion:
  18. get_viewport().set_input_as_handled()
  19. var motion = event as InputEventJoypadMotion
  20. if abs(motion.axis_value) > DEADZONE:
  21. var idx = motion.axis
  22. var map = JoyMapping.new(JoyMapping.TYPE.AXIS, idx)
  23. map.inverted = joy_mapping_axis_invert.pressed
  24. if joy_mapping_full_axis.pressed:
  25. map.axis = JoyMapping.AXIS.FULL
  26. elif motion.axis_value > 0:
  27. map.axis = JoyMapping.AXIS.HALF_PLUS
  28. else:
  29. map.axis = JoyMapping.AXIS.HALF_MINUS
  30. joy_mapping_text.text = map.to_human_string()
  31. cur_mapping[steps[cur_step]] = map
  32. elif event is InputEventJoypadButton and event.pressed:
  33. get_viewport().set_input_as_handled()
  34. var btn = event as InputEventJoypadButton
  35. var map = JoyMapping.new(JoyMapping.TYPE.BTN, btn.button_index)
  36. joy_mapping_text.text = map.to_human_string()
  37. cur_mapping[steps[cur_step]] = map
  38. func create_mapping_string(mapping):
  39. var string = "%s,%s," % [joy_guid, joy_name]
  40. for k in mapping:
  41. var m = mapping[k]
  42. if typeof(m) == TYPE_OBJECT and m.type == JoyMapping.TYPE.NONE:
  43. continue
  44. string += "%s:%s," % [k, str(m)]
  45. var platform = "Unknown"
  46. if JoyMapping.PLATFORMS.keys().has(OS.get_name()):
  47. platform = JoyMapping.PLATFORMS[OS.get_name()]
  48. return string + "platform:" + platform
  49. func start(idx):
  50. joy_guid = Input.get_joy_guid(idx)
  51. joy_name = Input.get_joy_name(idx)
  52. if joy_guid.is_empty():
  53. printerr("Unable to find controller")
  54. return
  55. if OS.get_name() == "HTML5":
  56. # Propose trying known mapping on HTML5.
  57. $Start.window_title = "%s - %s" % [joy_guid, joy_name]
  58. $Start.popup_centered()
  59. else:
  60. # Run wizard directly.
  61. _on_Wizard_pressed()
  62. func remap_and_close(mapping):
  63. last_mapping = create_mapping_string(mapping)
  64. Input.add_joy_mapping(last_mapping, true)
  65. reset()
  66. show_map()
  67. func reset():
  68. $Start.hide()
  69. $Mapping.hide()
  70. joy_guid = ""
  71. joy_name = ""
  72. cur_mapping = {}
  73. cur_step = -1
  74. func step_next():
  75. $Mapping.title = "Step: %d/%d" % [cur_step + 1, steps.size()]
  76. joy_mapping_text.text = ""
  77. if cur_step >= steps.size():
  78. remap_and_close(cur_mapping)
  79. else:
  80. _update_step()
  81. func show_map():
  82. if OS.get_name() == "Web":
  83. JavaScriptBridge.eval("window.prompt('This is the resulting remap string', '%s')" % last_mapping)
  84. else:
  85. $MapWindow/Margin/VBoxContainer/TextEdit.text = last_mapping
  86. $MapWindow.popup_centered()
  87. func _update_step():
  88. $Mapping/Margin/VBox/Info/Buttons/Next.grab_focus()
  89. for btn in joy_buttons.get_children():
  90. btn.hide()
  91. for axis in joy_axes.get_children():
  92. axis.hide()
  93. var key = steps[cur_step]
  94. var idx = JoyMapping.BASE[key]
  95. if key in ["leftx", "lefty", "rightx", "righty"]:
  96. joy_axes.get_node(str(idx) + "+").show()
  97. joy_axes.get_node(str(idx) + "-").show()
  98. else:
  99. joy_buttons.get_node(str(idx)).show()
  100. joy_mapping_full_axis.button_pressed = key in ["leftx", "lefty", "rightx", "righty", "righttrigger", "lefttrigger"]
  101. joy_mapping_axis_invert.button_pressed = false
  102. if cur_mapping.has(key):
  103. var cur = cur_mapping[steps[cur_step]]
  104. joy_mapping_text.text = cur.to_human_string()
  105. if cur.type == JoyMapping.TYPE.AXIS:
  106. joy_mapping_full_axis.pressed = cur.axis == JoyMapping.AXIS.FULL
  107. joy_mapping_axis_invert.pressed = cur.inverted
  108. func _on_Wizard_pressed():
  109. Input.remove_joy_mapping(joy_guid)
  110. $Start.hide()
  111. $Mapping.popup_centered()
  112. cur_step = 0
  113. step_next()
  114. func _on_Cancel_pressed():
  115. reset()
  116. func _on_xbox_pressed():
  117. remap_and_close(JoyMapping.XBOX)
  118. func _on_xboxosx_pressed():
  119. remap_and_close(JoyMapping.XBOX_OSX)
  120. func _on_Mapping_popup_hide():
  121. reset()
  122. func _on_Next_pressed():
  123. cur_step += 1
  124. step_next()
  125. func _on_Prev_pressed():
  126. if cur_step > 0:
  127. cur_step -= 1
  128. step_next()
  129. func _on_Skip_pressed():
  130. var key = steps[cur_step]
  131. if cur_mapping.has(key):
  132. cur_mapping.erase(key)
  133. cur_step += 1
  134. step_next()
  135. func _on_FullAxis_toggled(button_pressed):
  136. if cur_step == -1 or not button_pressed:
  137. return
  138. var key = steps[cur_step]
  139. if cur_mapping.has(key) and cur_mapping[key].type == JoyMapping.TYPE.AXIS:
  140. cur_mapping[key].axis = JoyMapping.AXIS.FULL
  141. joy_mapping_text.text = cur_mapping[key].to_human_string()
  142. func _on_InvertAxis_toggled(button_pressed):
  143. if cur_step == -1:
  144. return
  145. var key = steps[cur_step]
  146. if cur_mapping.has(key) and cur_mapping[key].type == JoyMapping.TYPE.AXIS:
  147. cur_mapping[key].inverted = button_pressed
  148. joy_mapping_text.text = cur_mapping[key].to_human_string()
  149. func _on_start_close_requested():
  150. $Start.hide()
  151. func _on_mapping_close_requested():
  152. $Mapping.hide()
  153. func _on_map_window_close_requested():
  154. $MapWindow.hide()