joypad_button_prompt.gd 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (C) 2022-2023 John Pennycook
  2. # SPDX-License-Identifier: MIT
  3. @tool
  4. @icon("res://addons/input_prompts/joypad_button_prompt/icon.svg")
  5. class_name JoypadButtonPrompt
  6. extends "res://addons/input_prompts/input_prompt.gd"
  7. ## Displays a prompt based on a joypad button index.
  8. ##
  9. ## Displays a prompt based on a joypad button index.
  10. ## The texture used for the prompt is determined by an icon preference. When
  11. ## the icon preference is set to "Automatic", the prompt automatically adjusts
  12. ## to match the most recent joypad device.
  13. ## [br][br]
  14. ## [b]Note[/b]: A [JoypadButtonPrompt] will never show keyboard or mouse
  15. ## prompts. To automatically reflect the most recent input device, use
  16. ## [ActionPrompt] instead.
  17. ## A joypad button index, such as [constant @GlobalScope.JOY_BUTTON_A].
  18. var button := 0:
  19. set = _set_button
  20. ## The icon preference for this prompt:
  21. ## Automatic (0), Xbox (1), Sony (2), Nintendo (3).
  22. ## When set to "Automatic", the prompt automatically adjusts to match the most
  23. ## recent joypad device.
  24. var icon: int = Icons.AUTOMATIC:
  25. set = _set_icon
  26. func _ready():
  27. _update_icon()
  28. func _set_button(index: int):
  29. button = index
  30. var event := InputEventJoypadButton.new()
  31. event.button_index = button
  32. events = [event]
  33. _update_icon()
  34. func _set_icon(new_icon):
  35. icon = new_icon
  36. _update_icon()
  37. func _update_icon():
  38. var textures := PromptManager.get_joypad_button_textures(icon)
  39. texture = textures.get_texture(events[0])
  40. queue_redraw()
  41. func _get_property_list():
  42. var properties = []
  43. properties.append(
  44. {
  45. name = "JoypadButtonPrompt",
  46. type = TYPE_NIL,
  47. usage = PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_SCRIPT_VARIABLE
  48. }
  49. )
  50. properties.append(
  51. {name = "button", type = TYPE_INT, hint = PROPERTY_HINT_RANGE, hint_string = "0,22"}
  52. )
  53. properties.append(
  54. {
  55. name = "icon",
  56. type = TYPE_INT,
  57. hint = PROPERTY_HINT_ENUM,
  58. hint_string = "Automatic,Xbox,Sony,Nintendo"
  59. }
  60. )
  61. return properties