game/user_interface/selection_prompt.gd

70 lines
1.9 KiB
GDScript3
Raw Permalink Normal View History

@tool
class_name SelectionPrompt extends Control
##
2023-02-01 01:05:48 +01:00
## The prompt has been started.
##
signal prompted()
##
2023-02-01 01:05:48 +01:00
## The item at index [code]selected_item[/code] has been selected in the prompt.
##
signal prompt_selected(selected_item: int)
@export
var _button_group: ButtonGroup = null
##
## If set to [code]true[/code], the selection prompt dismisses itself after a selection is made.
## Otherwise, if set to [code]false[/code], the prompt remains present after a selection is made.
##
@export
var dismiss_on_selection := false
func _get_configuration_warnings() -> PackedStringArray:
var warnings := PackedStringArray()
if _button_group == null:
warnings.append("`Button Group` must point to a valid BaseButton instance")
return warnings
func _ready() -> void:
assert(_button_group != null, "button group cannot be null")
_button_group.pressed.connect(func (button: BaseButton) -> void:
var selected_item = _button_group.get_buttons().find(button)
assert(selected_item > -1, "selected item cannot be less than 0")
prompt_selected.emit(selected_item)
if dismiss_on_selection:
hide())
##
## Returns the [ButtonGroup] used by the selection prompt to handle selections.
##
func get_button_group() -> ButtonGroup:
return _button_group
##
## Displays the selection prompt, with [code]initial_selection[/code] as the initial value selected.
##
## Once a value has been manually selected in the prompt, [signal prompt_selected] is emitted.
##
## *Note* that [code]initial_selection[/code] *must* be a valid index in the range of the button
## group used by the selection prompt, which may be accessed by the calling logic via
## [member get_button_group]
##
func prompt(initial_selection: int) -> void:
LocalPlayer.override_controls(hidden)
assert(_button_group != null, "button group cannot be null")
var selected_button := _button_group.get_buttons()[initial_selection]
selected_button.button_pressed = true
show()
selected_button.grab_focus()
prompted.emit()