Randoloot/scripts/ui/resource_bar.gd
2026-10-20 20:32:50 +02:00

118 lines
4.2 KiB
GDScript

extends Control
const HEIGHT := 32.0
const ENTRIES := [
["credits","Kredyty","svg",Color("efd48e")],
["parts","Części","png",Color("dfded2")],
["deuter","Deuter","svg",Color("9cdeef")],
["time_crystals","Kryształy czasu","png",Color("d4b6ff")],
["iron","Żelazo","png",Color("d3dce0")],
["titanium","Tytan","png",Color("d3dce0")],
["aluminium","Aluminium","png",Color("d3dce0")],
["uranium","Uran","png",Color("d3dce0")],
["sulfur","Siarka","png",Color("d3dce0")],
["silicon","Krzem","svg",Color("d3dce0")],
["copper","Miedź","svg",Color("d3dce0")],
["nickel","Nikiel","svg",Color("d3dce0")],
["tungsten","Wolfram","svg",Color("d3dce0")],
["mercury","Rtęć","svg",Color("d3dce0")]
]
var _row: HBoxContainer
var _labels: Dictionary = {}
var _cells: Dictionary = {}
var _style: StyleBoxFlat
var _elapsed := 0.0
func _ready() -> void:
name = "ResourceBar"
process_mode = Node.PROCESS_MODE_ALWAYS
set_anchors_and_offsets_preset(Control.PRESET_TOP_WIDE)
offset_bottom = HEIGHT
mouse_filter = Control.MOUSE_FILTER_STOP
_style = StyleBoxFlat.new()
_style.bg_color = Color("0a171ff0")
_style.border_color = Color("46666a")
_style.border_width_bottom = 1
_row = HBoxContainer.new()
_row.add_theme_constant_override("separation",8)
_row.mouse_filter = Control.MOUSE_FILTER_PASS
add_child(_row)
for index in ENTRIES.size():
var entry: Array = ENTRIES[index]
var key := str(entry[0])
if index in [4,9]:
var separator := VSeparator.new()
separator.mouse_filter = Control.MOUSE_FILTER_IGNORE
separator.modulate = Color("48636a")
_row.add_child(separator)
var cell := HBoxContainer.new()
cell.name = key.to_pascal_case()
cell.size_flags_horizontal = Control.SIZE_EXPAND_FILL
cell.add_theme_constant_override("separation",4)
cell.mouse_filter = Control.MOUSE_FILTER_PASS
_row.add_child(cell)
var icon := TextureRect.new()
icon.texture = load("res://assets/icons/resources/%s.%s" % [key,entry[2]])
icon.custom_minimum_size = Vector2(18,18)
icon.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
icon.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
icon.texture_filter = CanvasItem.TEXTURE_FILTER_LINEAR
icon.mouse_filter = Control.MOUSE_FILTER_IGNORE
cell.add_child(icon)
var label := Label.new()
label.custom_minimum_size.x = 36
label.add_theme_font_override("font",ThemeDB.fallback_font)
label.add_theme_font_size_override("font_size",11)
label.add_theme_color_override("font_color",entry[3])
label.mouse_filter = Control.MOUSE_FILTER_IGNORE
label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
cell.add_child(label)
_labels[key] = label
_cells[key] = cell
GameState.captain.changed.connect(refresh)
GameState.captain.expedition.changed.connect(refresh)
resized.connect(_layout)
_row.minimum_size_changed.connect(_layout.call_deferred)
refresh()
_layout.call_deferred()
static func compact(value: float) -> String:
if value >= 1000000: return ("%.1fM" % (floorf(value/100000.0)/10.0)).replace(".0M","M")
if value >= 1000: return ("%.1fK" % (floorf(value/100.0)/10.0)).replace(".0K","K")
return str(floori(value))
func refresh() -> void:
if _labels.is_empty(): return
var captain: Resource = GameState.captain
var progress: Resource = captain.expedition
for entry in ENTRIES:
var key := str(entry[0])
var amount := float(captain.minerals.get(key,0))
match key:
"credits": amount = captain.credits
"parts": amount = captain.parts
"deuter": amount = progress.deuter
"time_crystals": amount = progress.time_crystals
var label: Label = _labels[key]
label.text = compact(amount)
var exact := "%.1f" % amount if key == "deuter" else str(floori(amount))
_cells[key].tooltip_text = "%s: %s" % [entry[1],exact]
func _process(delta: float) -> void:
_elapsed += delta
if _elapsed >= 0.25:
_elapsed = 0
refresh()
func _layout() -> void:
if not is_instance_valid(_row): return
var minimum := _row.get_combined_minimum_size()
var available := maxf(1,size.x-24)
var factor := minf(1.0,available/maxf(1,minimum.x))
_row.scale = Vector2.ONE*factor
_row.size = Vector2(available/factor,minimum.y)
_row.position = Vector2(12,(HEIGHT-minimum.y*factor)*0.5)
queue_redraw()
func _draw() -> void:
if _style != null: draw_style_box(_style,Rect2(Vector2.ZERO,size))