input_prompt.gd 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2022-2023 John Pennycook
  2. # SPDX-License-Identifier: MIT
  3. @tool
  4. @icon("res://addons/input_prompts/icon.svg")
  5. class_name InputPrompt
  6. extends TextureRect
  7. ## Base class for input prompts.
  8. ##
  9. ## Base class for input prompts.
  10. ## Emitted when one of the [InputEvent]s associated with this prompt is pressed.
  11. signal pressed
  12. enum Icons {
  13. AUTOMATIC,
  14. XBOX,
  15. SONY,
  16. NINTENDO,
  17. KEYBOARD,
  18. }
  19. ## The set of [InputEvent]s that should satisfy this input prompt.
  20. @export var events: Array[InputEvent] = []
  21. func _init():
  22. texture = null
  23. stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
  24. func _update_icon():
  25. pass
  26. func _refresh():
  27. _update_icon()
  28. func _input(event: InputEvent):
  29. if not events.any(func(e): return event.is_match(e)):
  30. return
  31. if not event.is_pressed():
  32. return
  33. if event.is_echo():
  34. return
  35. emit_signal("pressed")
  36. func _enter_tree():
  37. PromptManager.icons_changed.connect(_update_icon)
  38. add_to_group("_input_prompts")
  39. func _exit_tree():
  40. remove_from_group("_input_prompts")
  41. PromptManager.icons_changed.disconnect(_update_icon)
  42. ## Force this [InputPrompt] node to refresh its icons and events.
  43. ## Must be called if the [InputMap] is changed.
  44. ## [br][br]
  45. ## [b]Note[/b]: Use [InputPromptManager] to refresh all nodes at once.
  46. func refresh():
  47. _refresh()