extends Control const DICE_SELECTION_ITEM_SCENE := preload("res://stage/dice_selection/dice_selection_item.tscn") signal dice_selected(dice: Array[DiceConfiguration]) @export var dice_count: int func _ready() -> void: for _i in 6: var dice := DICE_SELECTION_ITEM_SCENE.instantiate() #dice.dice_configuration = dice.pressed.connect(_on_deck_dice_selected.bind(dice)) dice.focus_entered.connect(_on_dice_focus_entered.bind(dice)) dice.focus_exited.connect(_on_dice_focus_exited) dice.size_flags_vertical = Control.SIZE_EXPAND_FILL %DeckContainer.add_child(dice) for _i in dice_count: pass # clear button press buffer await get_tree().process_frame %DeckContainer.get_child(0).grab_focus() func _input(event: InputEvent) -> void: if event.is_action_pressed("right_click"): dice_selected.emit([] as Array[DiceConfiguration]) if event.is_action_pressed("menu"): %GoButton.grab_focus() func _on_deck_dice_selected(dice: Control) -> void: if %SelectedContainer.get_child_count() >= dice_count: return var slot_dice: TextureButton = dice.duplicate() slot_dice.pressed.connect(_on_slot_dice_selected.bind(slot_dice)) slot_dice.focus_entered.connect(_on_dice_focus_entered.bind(slot_dice)) slot_dice.focus_exited.connect(_on_dice_focus_exited) %SelectedContainer.add_child(slot_dice) func _on_slot_dice_selected(dice: Control) -> void: if %SelectedContainer.get_child_count() > 1: if dice.get_index() == %SelectedContainer.get_child_count() - 1: %SelectedContainer.get_child(dice.get_index() - 1).grab_focus() else: %SelectedContainer.get_child(dice.get_index() + 1).grab_focus() else: # TODO: focus last child focused in DeckContainer %DeckContainer.get_child(0).grab_focus() %SelectedContainer.remove_child(dice) func _on_dice_focus_entered(dice: Control) -> void: var dice_configuration := dice.dice_configuration as DiceConfiguration var faces := [ dice_configuration.front_face, dice_configuration.back_face, dice_configuration.left_face, dice_configuration.right_face, dice_configuration.top_face, dice_configuration.bottom_face, ] %Label.text = "" for face in faces: %Label.text += "%s: %s " % [face.type, face.value] func _on_dice_focus_exited() -> void: # is null while freeing and focus exit at same time if has_node("%Label"): %Label.text = "" func _on_go_button_pressed() -> void: if %SelectedContainer.get_child_count() < dice_count: return var selected_dice_configurations: Array[DiceConfiguration] for node in %SelectedContainer.get_children(): selected_dice_configurations.append(node.dice_configuration) dice_selected.emit(selected_dice_configurations)