spaceship/ui/VesselPriceStrip.gd

80 lines
2.6 KiB
GDScript3
Raw Normal View History

extends Control
# #111: what a vessel costs, in glyphs.
#
# The craft menu used to spell prices out - "15 ore · 5 gas" - which is three
# words of chrome per button on a screen with six of them, and it left the
# vessel's own name fighting for the same two lines. Every resource already
# has a glyph the resource bar and the transfer list draw; this reuses it, so
# a price reads as a number beside a mark rather than as a sentence.
#
# Built in code from the costs it is given, so a vessel whose price changes -
# or a resource added to the catalog - needs nothing here.
const GLYPH_SIZE: float = 26.0
const GAP: float = 4.0
const PAIR_GAP: float = 14.0
const FONT_SIZE: int = 24
var _pairs: Array = []
func _ready() -> void:
mouse_filter = Control.MOUSE_FILTER_IGNORE
func set_costs(costs: Dictionary) -> void:
for pair in _pairs:
pair.glyph.queue_free()
pair.label.queue_free()
_pairs.clear()
for resource_type in GameState.resource_types():
var amount: int = int(costs.get(resource_type, 0))
if amount <= 0:
continue
var glyph := ResourceGlyph.new()
glyph.resource_type = String(resource_type)
glyph.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(glyph)
var label := Label.new()
label.text = str(amount)
label.mouse_filter = Control.MOUSE_FILTER_IGNORE
label.add_theme_font_size_override("font_size", FONT_SIZE)
label.add_theme_color_override("font_color", GameState.resource_color(resource_type))
label.add_theme_color_override("font_outline_color", Color(0.01, 0.01, 0.035, 0.95))
label.add_theme_constant_override("outline_size", 2)
label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
add_child(label)
_pairs.append({"glyph": glyph, "label": label, "width": _text_width(label)})
_layout()
func _text_width(label: Label) -> float:
var font: Font = label.get_theme_font("font")
if font == null:
return 20.0
return font.get_string_size(label.text, HORIZONTAL_ALIGNMENT_LEFT, -1.0,
FONT_SIZE).x
func _layout() -> void:
# Centred as a block: a one-resource price and a two-resource price
# should both sit under the middle of the button they belong to.
var total: float = 0.0
for pair in _pairs:
total += GLYPH_SIZE + GAP + float(pair.width) + PAIR_GAP
total = maxf(total - PAIR_GAP, 0.0)
var x: float = (size.x - total) * 0.5
var mid: float = (size.y - GLYPH_SIZE) * 0.5
for pair in _pairs:
pair.glyph.position = Vector2(x, mid)
pair.glyph.size = Vector2(GLYPH_SIZE, GLYPH_SIZE)
x += GLYPH_SIZE + GAP
pair.label.position = Vector2(x, 0.0)
pair.label.size = Vector2(float(pair.width), size.y)
x += float(pair.width) + PAIR_GAP
func is_free() -> bool:
return _pairs.is_empty()