summaryrefslogtreecommitdiff
path: root/stage/dice_selection/dice_selection.gd
blob: b56bebe7a2509be88b142c699537db76fa3ee144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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)