Randoloot/scripts/ui/crafting_recipe_button.gd
2026-10-26 19:33:45 +01:00

114 lines
4.8 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

extends Button
const RECIPES := preload("res://scripts/items/crafting_recipes.gd")
var recipe_id := ""
var inventory: ShipInventory
var _stock: Label
var _availability: Label
var _picture: TextureRect
var _output: Label
func _ready() -> void:
custom_minimum_size = Vector2(120, 166)
size_flags_horizontal = Control.SIZE_EXPAND_FILL
focus_mode = Control.FOCUS_NONE
mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
tooltip_text = "Receptura"
for state in ["normal", "hover", "pressed", "disabled"]:
var style := StyleBoxFlat.new()
style.bg_color = Color("172b3b") if state == "hover" else Color("0c1925")
style.bg_color.a = minf(style.bg_color.a, 0.64)
style.border_color = Color("e7bb73") if state == "hover" else Color("314b60")
style.set_border_width_all(1)
style.set_corner_radius_all(7)
style.set_content_margin_all(8)
add_theme_stylebox_override(state, style)
var contents := VBoxContainer.new()
contents.mouse_filter = Control.MOUSE_FILTER_IGNORE
contents.add_theme_constant_override("separation", 4)
add_child(contents)
contents.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
contents.offset_left = 8
contents.offset_right = -8
contents.offset_top = 8
contents.offset_bottom = -8
var artwork := Control.new()
artwork.custom_minimum_size.y = 60
artwork.mouse_filter = Control.MOUSE_FILTER_IGNORE
contents.add_child(artwork)
_picture = TextureRect.new()
_picture.texture = RECIPES.icon(recipe_id)
_picture.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
_picture.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
_picture.mouse_filter = Control.MOUSE_FILTER_IGNORE
artwork.add_child(_picture)
_picture.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
_output = _label("", 12, Color("efc681"))
_output.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
artwork.add_child(_output)
_output.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
var title := _label(RECIPES.DATA[recipe_id]["name"], 11, Color("e5edf4"))
title.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
title.custom_minimum_size.y = 36
title.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
contents.add_child(title)
_stock = _label("", 10, Color("8ea9bc"))
contents.add_child(_stock)
_availability = _label("", 10, Color.WHITE)
contents.add_child(_availability)
refresh()
func _label(caption: String, font_size: int, color: Color) -> Label:
var label := Label.new()
label.text = caption
label.mouse_filter = Control.MOUSE_FILTER_IGNORE
label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
label.add_theme_font_size_override("font_size", font_size)
label.add_theme_color_override("font_color", color)
return label
func refresh() -> void:
if _stock == null:
return
var available := inventory.can_craft(recipe_id)
_output.text = "+%d" % RECIPES.output_amount(recipe_id, GameState.captain)
_stock.text = "Zapas: %d" % inventory.get_crafted_stock(recipe_id)
_availability.text = "WYTWÓRZ" if available else "BRAK SUROWCÓW"
_availability.add_theme_color_override("font_color", Color("85deb1") if available else Color("d08c8e"))
_picture.modulate = Color.WHITE if available else Color("8b939f")
# Keep the card hoverable so unavailable recipes still show their costs.
mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND if available else Control.CURSOR_HELP
func _make_custom_tooltip(_text: String) -> Object:
var panel := PanelContainer.new()
var style := StyleBoxFlat.new()
style.bg_color = Color("0a1522b8")
style.bg_color.a = minf(style.bg_color.a, 0.64)
style.border_color = Color("bc975f")
style.set_border_width_all(1)
style.set_corner_radius_all(7)
style.set_content_margin_all(14)
panel.add_theme_stylebox_override("panel", style)
var contents := VBoxContainer.new()
contents.add_theme_constant_override("separation", 8)
panel.add_child(contents)
contents.add_child(_label("%s ×%d" % [RECIPES.DATA[recipe_id]["name"], RECIPES.output_amount(recipe_id, GameState.captain)], 13, Color("efc681")))
contents.add_child(HSeparator.new())
for mineral in RECIPES.DATA[recipe_id]["cost"]:
var needed := int(RECIPES.DATA[recipe_id]["cost"][mineral])
var owned := int(GameState.captain.minerals.get(mineral, 0))
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 10)
contents.add_child(row)
var icon := TextureRect.new()
icon.texture = RECIPES.icon(mineral)
icon.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
icon.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
icon.custom_minimum_size = Vector2(28, 28)
row.add_child(icon)
var line := _label("%s %d / %d" % [RECIPES.NAMES[mineral], owned, needed], 12, Color("85deb1") if owned >= needed else Color("f08087"))
line.horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT
row.add_child(line)
contents.add_child(_label("Kliknij, aby wytworzyć" if inventory.can_craft(recipe_id) else "Zdobądź brakujące surowce z asteroid", 10, Color("8ea9bc")))
return panel