33 lines
942 B
GDScript
33 lines
942 B
GDScript
extends HBoxContainer
|
|
|
|
signal selection_changed(selection)
|
|
|
|
export (ButtonGroup) var group
|
|
|
|
var _selection = 0
|
|
|
|
func _ready():
|
|
focus_mode = FOCUS_ALL
|
|
connect("focus_entered", self, "_focused")
|
|
for child in get_children():
|
|
if child is BaseButton:
|
|
child.focus_neighbour_top = "../%s" % focus_neighbour_top
|
|
child.focus_neighbour_bottom = "../%s" % focus_neighbour_bottom
|
|
child.connect("focus_entered", child, "set_pressed", [true])
|
|
child.connect("focus_entered", self, "_button_selected", [], CONNECT_DEFERRED)
|
|
|
|
func _focused():
|
|
_selection = get_selection()
|
|
group.get_pressed_button().grab_focus()
|
|
|
|
func _button_selected():
|
|
var new_selection = get_selection()
|
|
if _selection != new_selection:
|
|
_selection = new_selection
|
|
emit_signal("selection_changed", _selection)
|
|
|
|
func get_selection():
|
|
return group.get_buttons().find(group.get_pressed_button())
|
|
|
|
func select(selection):
|
|
group.get_buttons()[selection].pressed = true
|