game/user_interface/button_selection.gd

87 lines
2.3 KiB
GDScript3
Raw Normal View History

2023-01-19 01:49:44 +01:00
@tool
class_name ItemSelection extends HFlowContainer
##
## An item has been selected via GUI selection or [method select_item].
##
signal item_selected(index: int)
var _button_group := ButtonGroup.new()
##
## Number of items in the selection.
##
var item_count: int:
get:
return _button_group.get_buttons().size()
func _get_configuration_warnings() -> PackedStringArray:
2023-01-21 16:03:12 +01:00
var warnings := PackedStringArray()
var children := get_children()
2023-01-21 16:03:12 +01:00
if not(children.is_empty()):
var self_class_name := get_class()
for child in get_children():
warnings.append(
"{0} can only have Button children, but {1} is of type {2}".format(
[self_class_name, child.name, child.get_class()]))
2023-01-21 16:03:12 +01:00
return warnings
2023-01-19 01:49:44 +01:00
##
## Adds a new item with no text and only [code]icon[/code] as the icon to the selection.
##
func add_icon_item(icon: Texture2D) -> void:
var child_count := get_child_count()
var button := Button.new()
button.icon = icon
button.icon_alignment = HORIZONTAL_ALIGNMENT_CENTER
button.toggle_mode = true
button.button_group = _button_group
button.button_pressed = child_count == 0
button.pressed.connect(func () -> void: select_item(child_count))
add_child(button)
##
## Returns the icon used by the item at [code]index[/code].
##
## An assertion is raised if [code]index[/code] is out of bounds from the item count.
##
func get_item_icon(index: int) -> Texture2D:
var buttons := _button_group.get_buttons()
assert(index < buttons.size(), "index out of range")
var button := buttons[index] as Button
return null if (button == null) else button.icon
##
## Returns the currently selected item index or [code]-1[/code] if no item is selected.
##
func get_selected_item() -> int:
var pressed_button := _button_group.get_pressed_button()
return -1 if (pressed_button == null) else pressed_button.get_index()
##
## Selects the item at [code]index[/code], emitting [signal item_selected] at the end.
##
## An assertion is raised if [code]index[/code] is out of bounds from the item count.
##
func select_item(index: int) -> void:
var buttons := _button_group.get_buttons()
assert(index < buttons.size(), "index out of range")
var pressed_button := _button_group.get_pressed_button()
if pressed_button != null:
pressed_button.set_pressed_no_signal(false)
buttons[index].set_pressed_no_signal(true)
item_selected.emit(index)