brackeys-12/ui/building_menu.gd
2024-09-13 21:02:26 -04:00

66 lines
1.4 KiB
GDScript

extends Control
@onready var tabs = [
$HBoxContainer/BuildingTab, $HBoxContainer/BuildingTab2, $HBoxContainer/BuildingTab3
]
@onready var building_data = Data.data.buildings
@onready var buildings = building_data.keys()
@onready var v_box_container: HBoxContainer = $Buildings/VBoxContainer
@onready var icons = [
$Buildings/VBoxContainer/BuildingIcon,
$Buildings/VBoxContainer/BuildingIcon2,
$Buildings/VBoxContainer/BuildingIcon3,
$Buildings/VBoxContainer/BuildingIcon4
]
@onready var spacers = [
$Buildings/VBoxContainer/Spacer,
$Buildings/VBoxContainer/Spacer2,
$Buildings/VBoxContainer/Spacer3
]
func _ready() -> void:
swap_to_tab(0)
func swap_to_tab(index):
for i in range(0, tabs.size()):
if i == index:
tabs[i].select()
else:
tabs[i].unselect()
var filtered_buildings = buildings.filter(func(building): return building_data[building].type == tabs[index].key)
for i in range(0, icons.size()):
if i < filtered_buildings.size():
icons[i].visible = true
icons[i].key = filtered_buildings[i]
icons[i].texture_rect.texture = Data.data.images[filtered_buildings[i]]
if i > 0:
spacers[i - 1].visible = true
else:
icons[i].visible = false
if i > 0:
spacers[i - 1].visible = false
print(icons.map(func(icon): return icon.key))
func _on_building_tab_clicked() -> void:
swap_to_tab(0)
func _on_building_tab_2_clicked() -> void:
swap_to_tab(1)
func _on_building_tab_3_clicked() -> void:
swap_to_tab(2)