summaryrefslogtreecommitdiff
path: root/Scenes/UI/Menu.gd
blob: fd5d9dc355226fd44aa6a12be5b0efa75def7452 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
extends TabContainer


@onready var audio_bus = AudioServer.get_bus_index("Master")

@onready var focused_tab = 0
@onready var focused_tab_elements: Dictionary = {
	0: $Bombs/Panel/HBoxContainer/VBoxContainer/ButtonBombNormal,
	1: $Character/Panel/HBoxContainer/Collaborations/TextureButton,
	2: $System/Panel/VBoxContainer/ButtonResume,
}
@onready var exclude_from_focus = [
	%BombComponents,
]

var current_slot_idx = 0

var character_empty_slot: TextureButton


func _ready():
	hide()
	
	self._ready_bombs()
	self._ready_character()
	self._ready_system()
	
	var tabs = get_children()
	for idx in tabs.size():
		for child in Utilities.get_all_children(tabs[idx]):
			if child is Control and not exclude_from_focus.has(child):
				child.focus_entered.connect(func():
					focused_tab_elements[idx] = child
				)


func _input(event: InputEvent):
	if event.is_action_pressed("ui_menu"):
		if get_tree().paused: # game is paused
			self.close_menu()
		else: # game is running
			self.open_menu()
	
	if self.visible:
		if event.is_action_pressed("ui_menu_left"):
			if get_current_tab() > 0:
				set_current_tab(get_current_tab() - 1)
		if event.is_action_pressed("ui_menu_right"):
			if get_current_tab() < get_tab_count() - 1:
				set_current_tab(get_current_tab() + 1)
	
	tab_changed.connect(func(tab_idx):
		focused_tab = tab_idx
		(func(): focused_tab_elements[tab_idx].grab_focus()).call_deferred()
	)
	
	_input_bombs(event)


func open_menu():
	get_tree().paused = true
	tab_changed.emit(focused_tab)
	show()


func close_menu():
	%BombComponents.hide()
	
	hide()
	get_tree().paused = false


func get_player():
	return Global.player


### Bombs ###


func _ready_bombs():
	for component in Bomb.COMPONENT_TYPE:
		var idx = Bomb.COMPONENT_TYPE[component]
		$Bombs/Panel/HBoxContainer/VBoxContainer3/BombComponents.set_item_text(idx, component.capitalize())
	
	for idx in Global.player.bomb_components.size():
		var component = Bomb.COMPONENT_TYPE.find_key(Global.player.bomb_components[idx])
		if component:
			$Bombs/Panel/HBoxContainer/VBoxContainer3/VBoxContainer.get_node("Slot" + str(idx+1)).text = component.capitalize()
	
	%BombComponents.item_activated.connect(Callable(self, "_on_bomb_components_item_selected"))


func _input_bombs(event):
	if %BombComponents.visible and event.is_action_pressed("ui_cancel"):
		%BombComponents.hide()
		focused_tab_elements[focused_tab].grab_focus()


func _on_button_bomb_normal_pressed():
	self.get_player().BombScene = preload("res://Scenes/Entities/Bombs/Bomb__Normal.tscn")


func _on_button_bomb_breakables_pressed():
	self.get_player().BombScene = preload("res://Scenes/Entities/Bombs/Bomb__Breakables.tscn")


func _on_bomb_power_item_selected(index):
	var power = int($Bombs/Panel/HBoxContainer/VBoxContainer2/BombPower.get_item_text(index))
	self.get_player().bomb_power = power


func _on_bomb_components_multi_selected(_index, _selected):
	pass
	#Global.player.bomb_components = {}
	#for idx in $Bombs/Panel/HBoxContainer/VBoxContainer3/BombComponents.get_selected_items():
		#Global.player.bomb_components.append(idx) # same as enum idx


func _on_slot_pressed(slot_idx):
	%BombComponents.clear()
	
	for component in Bomb.COMPONENT_TYPE:
		var idx = Bomb.COMPONENT_TYPE[component]
		%BombComponents.add_item(
			component.capitalize(),
			null,
			Global.player.bomb_components.find_key(idx) == null
		)
		# todo position swap
		# make all selectable
		# but when already in bomb_components, swap position
		# nonetheless, hightlight already selected item differently
	
	%BombComponents.show()
	%BombComponents.force_update_list_size()
	%BombComponents.grab_focus()
	
	current_slot_idx = slot_idx


func _on_bomb_components_item_selected(idx):
	Global.player.bomb_components[current_slot_idx] = idx
	%BombComponents.hide()
	$Bombs/Panel/HBoxContainer/VBoxContainer3/VBoxContainer.get_node("Slot" + str(current_slot_idx+1)).grab_focus()
	$Bombs/Panel/HBoxContainer/VBoxContainer3/VBoxContainer.get_node("Slot" + str(current_slot_idx+1)).text = Bomb.COMPONENT_TYPE.find_key(idx).capitalize()


### Character ###


func _ready_character():
	## Set $Main/$Main to current character
	## TODO
	
	
	## Add Slots to $Collaborations
	character_empty_slot = $Character/Panel/HBoxContainer/Collaborations/TextureButton.duplicate()
	$Character/Panel/HBoxContainer/Collaborations.remove_child($Character/Panel/HBoxContainer/Collaborations/TextureButton)
	
	var base_slot = character_empty_slot.duplicate()
	for idx in range(Global.collaboration_slots):
		var slot: TextureButton = base_slot.duplicate()
		slot.name = "Slot" + str(idx)
		
		if Global.selected_collaborations.has(idx):
			slot.modulate = Color(1, 1, 1, 1)
			slot.texture_normal = PlaceholderTexture2D.new()
			slot.texture_normal = load("res://.godot/imported/UsadaPekora_Full.png-de55b1ee87c19101262c28a2200b6697.ctex")
		
		$Character/Panel/HBoxContainer/Collaborations.add_child(slot)
	
	focused_tab_elements[1] = $Character/Panel/HBoxContainer/Collaborations.get_child(0)
	
	
	## Set $Preview/$Collaboration to empty? to all? to first slot?
	## TODO


### System ###


func _ready_system():
	AudioServer.set_bus_volume_db(self.audio_bus, linear_to_db(0))
	$System/Panel/VBoxContainer2/SliderVolume.value = db_to_linear(AudioServer.get_bus_volume_db(self.audio_bus))


func _on_slider_volume_value_changed(value):
	AudioServer.set_bus_volume_db(self.audio_bus, linear_to_db(value))


func quit_game():
	get_tree().quit()


func _on_button_resume_pressed():
	self.close_menu()


func _on_button_quit_pressed():
	self.quit_game()