joypad_motion_prompt.gd 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (C) 2022-2023 John Pennycook
  2. # SPDX-License-Identifier: MIT
  3. @tool
  4. @icon("res://addons/input_prompts/joypad_motion_prompt/icon.svg")
  5. class_name JoypadMotionPrompt
  6. extends "res://addons/input_prompts/input_prompt.gd"
  7. ## Displays a prompt based on a joypad axis and value.
  8. ##
  9. ## Displays a prompt based on a joypad axis and value.
  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 [JoypadMotionPrompt] will never show keyboard or mouse
  15. ## prompts. To automatically reflect the most recent input device, use
  16. ## [ActionPrompt] instead.
  17. ## A joypad axis index, such as [constant @GlobalScope.JOY_AXIS_LEFT_X].
  18. var axis := 0:
  19. set = _set_axis
  20. ## A joypad axis value (positive or negative).
  21. var axis_value := -1:
  22. set = _set_axis_value
  23. ## The icon preference for this prompt:
  24. ## Automatic (0), Xbox (1), Sony (2), Nintendo (3).
  25. ## When set to "Automatic", the prompt automatically adjusts to match the most
  26. ## recent joypad device.
  27. var icon: int = Icons.AUTOMATIC:
  28. set = _set_icon
  29. func _ready():
  30. _update_icon()
  31. func _set_axis(new_axis: int):
  32. axis = new_axis
  33. var event := InputEventJoypadMotion.new()
  34. event.axis = axis
  35. event.axis_value = axis_value
  36. events = [event]
  37. _update_icon()
  38. func _set_axis_value(new_value: int):
  39. axis_value = new_value
  40. var event := InputEventJoypadMotion.new()
  41. event.axis = axis
  42. event.axis_value = axis_value
  43. events = [event]
  44. _update_icon()
  45. func _set_icon(new_icon):
  46. icon = new_icon
  47. _update_icon()
  48. func _update_icon():
  49. var textures := PromptManager.get_joypad_motion_textures(icon)
  50. texture = textures.get_texture(events[0])
  51. queue_redraw()
  52. func _get_property_list():
  53. var properties = []
  54. properties.append(
  55. {
  56. name = "JoypadMotionPrompt",
  57. type = TYPE_NIL,
  58. usage = PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_SCRIPT_VARIABLE
  59. }
  60. )
  61. const AXIS_HINT := (
  62. "Left Horizontal:0,"
  63. + "Left Vertical:1,"
  64. + "Right Horizontal:2,"
  65. + "Right Vertical:3,"
  66. + "Left Trigger:4,"
  67. + "Right Trigger:5"
  68. )
  69. properties.append(
  70. {name = "axis", type = TYPE_INT, hint = PROPERTY_HINT_ENUM, hint_string = AXIS_HINT}
  71. )
  72. properties.append(
  73. {
  74. name = "axis_value",
  75. type = TYPE_INT,
  76. hint = PROPERTY_HINT_ENUM,
  77. hint_string = "Negative:-1,Positive:1"
  78. }
  79. )
  80. properties.append(
  81. {
  82. name = "icon",
  83. type = TYPE_INT,
  84. hint = PROPERTY_HINT_ENUM,
  85. hint_string = "Automatic,Xbox,Sony,Nintendo"
  86. }
  87. )
  88. return properties