#136, #137, #134: one build grid that cannot stack, one way to show a price

#136 - the gas burner drawn on top of the transport hub. The build menu's
rows were a written-down list of tops read with `tops[mini(row, size - 1)]`,
and with every structure researched the menu wanted a fourth row where the
list had three, so the last entry was placed on the third row's centre slot.
A list that silently stacks whatever will not fit in it is not a layout.
Rows are computed now, from a first top and a pitch that tightens to fit and
is floored at the row's own height - so two rows cannot occupy the same
space however far the catalog grows - and the column count widens rather
than adding a row the band cannot hold. Ten structures now sit in four
columns of three rows. The test unlocks the whole tree and checks every pair
of entries for overlap, which is the shape of the bug rather than a
coordinate that happens to be right today.

#137 - the game had three answers to "what does this cost". The build menu
drew a NeonNumber under each option, the craft menu drew glyph strips, and
the ship lab spelled prices out inside the caption. PriceTag is that answer
once: the resource's glyph, then what you HOLD over what it COSTS. It
replaces all three, plus the info popup and the skill panel, and
VesselPriceStrip - which was the same idea for one menu - goes with them.
The build menu also gains the resource bar the other two already had, in the
same place and at the same size, and the craft menu's - the only one not
running the full width of its panel - now matches. The bar drops its LOCAL
column where there is no building to report on, for the same reason #95
dropped it where it only repeated BANKED.

#134 - the skill panel's explanation and its price were both centred in the
same band, so a long description landed on the number. The text is the left
of the band now and the tag is the right. The shortfall it used to spell out
in a sentence - one per case, ore or research points - is the red on the tag
instead, and research points ride along as an entry of their own, so the tag
says WHICH thing is missing without a line of prose per possibility.

One trap worth recording: PriceTag and ResourceBar read the GameState
autoload, and an `extends SceneTree` test script that so much as names such
a class - or preloads the file - fails to compile, because GameState is not
an identifier in that context. GDScript then caches the failure, so the
scene the test instantiates gets a Control with no script where its resource
bar should be. The tests compare script PATHS, as strings.
This commit is contained in:
Loïc Wisniewski 2026-08-28 15:05:20 +02:00
commit ea25318201
12 changed files with 661 additions and 199 deletions

198
ui/PriceTag.gd Normal file
View file

@ -0,0 +1,198 @@
class_name PriceTag
extends Control
# Forgejo #137 (and #134): one way of saying what something costs.
#
# The game had three. The build menu drew a NeonNumber under each option; the
# craft menu drew glyph-and-number strips (#111); the ship lab spelled prices
# out inside the option's caption - "25 ore · 10 gas · 2 RP" - and the skill
# tree put the number in the middle of the panel with a sentence underneath
# saying whether you could afford it. Four menus, four answers to the same
# question, and a player learning one of them learned nothing about the next.
#
# This is that answer, once: for each thing a purchase costs, its glyph, then
# what you HAVE over what it COSTS.
#
# [ore] 12/25 [gas] 40/10
#
# #134 asks for the two numbers to be told apart by more than position, so
# the amount you have is drawn smaller than the price - a price is a fact
# about the thing, what you hold is a fact about you - and it turns red when
# it is not enough. That replaces the "Not enough rocks - you have 12" line
# the skill panel used to carry: the shortfall is now shown where the price
# is rather than described in a sentence somewhere else on the panel.
#
# Research points ride along as an entry with a written mark rather than a
# glyph, because they are not a resource - they have no inventory, no colour
# in the catalog, and nothing carries them.
enum Align { LEFT, CENTER, RIGHT }
const GLYPH_SIZE: float = 26.0
const GAP: float = 5.0
const PAIR_GAP: float = 18.0
const COST_FONT_SIZE: int = 24
# Deliberately smaller than the price. See the note above.
const HAVE_FONT_SIZE: int = 17
const SHORT_COLOR: Color = Color(1.0, 0.36, 0.32)
const HAVE_COLOR: Color = Color(0.60, 0.73, 0.82)
const POINT_COLOR: Color = Color(0.72, 0.62, 1.0)
const POINT_MARK: String = "RP"
@export var alignment: int = Align.CENTER
# Off for a price with nothing to compare against - a shop with no wallet in
# the same breath. Every menu in the game wants it on; it exists so a caller
# that only has a number can still use this widget rather than inventing a
# fifth way to draw one.
@export var show_available: bool = true
var _entries: Array = []
func _ready() -> void:
mouse_filter = Control.MOUSE_FILTER_IGNORE
resized.connect(_layout)
# costs: {resource_type: amount}. points: research points, 0 for none.
func show_price(costs: Dictionary, points: int = 0) -> void:
_clear()
for resource_type in GameState.resource_types():
var amount: int = int(costs.get(resource_type, 0))
if amount <= 0:
continue
_add_entry(String(resource_type), "", amount,
GameState.available_resource(String(resource_type)),
GameState.resource_color(resource_type))
if points > 0:
_add_entry("", POINT_MARK, points, GameState.research_points, POINT_COLOR)
_layout()
# The old name, kept because the craft menu speaks in costs alone and reads
# better for it.
func set_costs(costs: Dictionary) -> void:
show_price(costs)
func is_free() -> bool:
return _entries.is_empty()
# Whether anything on this tag is out of reach. The one question every caller
# used to answer for itself, in its own words.
func is_affordable() -> bool:
for entry in _entries:
if int(entry.have) < int(entry.cost):
return false
return true
# What this tag says about one resource. Accessors rather than a public
# dictionary so a caller - or a test - reads the same numbers the player
# does, off the thing that drew them.
func priced(resource_type: String) -> int:
return int(_entry_for(resource_type).get("cost", 0))
func held(resource_type: String) -> int:
return int(_entry_for(resource_type).get("have", 0))
func is_short(resource_type: String) -> bool:
return bool(_entry_for(resource_type).get("short", false))
func _entry_for(resource_type: String) -> Dictionary:
for entry in _entries:
if String(entry.resource) == resource_type:
return entry
return {}
func _clear() -> void:
for entry in _entries:
for node in [entry.mark, entry.have_label, entry.cost_label]:
if node != null:
node.queue_free()
_entries.clear()
func _add_entry(resource_type: String, mark_text: String, cost: int, have: int,
colour: Color) -> void:
var mark: Control
if resource_type != "":
var glyph := ResourceGlyph.new()
glyph.resource_type = resource_type
glyph.mouse_filter = Control.MOUSE_FILTER_IGNORE
mark = glyph
else:
mark = _make_label(mark_text, HAVE_FONT_SIZE, colour)
add_child(mark)
var short: bool = have < cost
var have_label: Label = null
if show_available:
have_label = _make_label("%d/" % have, HAVE_FONT_SIZE,
SHORT_COLOR if short else HAVE_COLOR)
add_child(have_label)
var cost_label: Label = _make_label(str(cost), COST_FONT_SIZE, colour)
add_child(cost_label)
_entries.append({
"resource": resource_type,
"mark": mark, "have_label": have_label, "cost_label": cost_label,
"cost": cost, "have": have, "short": short,
"have_width": 0.0 if have_label == null else _text_width(have_label, HAVE_FONT_SIZE),
"cost_width": _text_width(cost_label, COST_FONT_SIZE),
})
func _make_label(text: String, font_size: int, colour: Color) -> Label:
var label := Label.new()
label.text = text
label.mouse_filter = Control.MOUSE_FILTER_IGNORE
label.add_theme_font_size_override("font_size", font_size)
label.add_theme_color_override("font_color", colour)
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
return label
func _text_width(label: Label, font_size: int) -> float:
var font: Font = label.get_theme_font("font")
if font == null:
return float(label.text.length()) * float(font_size) * 0.6
return font.get_string_size(label.text, HORIZONTAL_ALIGNMENT_LEFT, -1.0, font_size).x
func _entry_width(entry: Dictionary) -> float:
return GLYPH_SIZE + GAP + float(entry.have_width) + float(entry.cost_width)
func _layout() -> void:
var total: float = 0.0
for entry in _entries:
total += _entry_width(entry) + PAIR_GAP
total = maxf(total - PAIR_GAP, 0.0)
var x: float = 0.0
match alignment:
Align.CENTER:
x = (size.x - total) * 0.5
Align.RIGHT:
x = size.x - total
var mid: float = (size.y - GLYPH_SIZE) * 0.5
for entry in _entries:
entry.mark.position = Vector2(x, mid)
entry.mark.size = Vector2(GLYPH_SIZE, GLYPH_SIZE)
x += GLYPH_SIZE + GAP
if entry.have_label != null:
entry.have_label.position = Vector2(x, 0.0)
entry.have_label.size = Vector2(float(entry.have_width), size.y)
x += float(entry.have_width)
entry.cost_label.position = Vector2(x, 0.0)
entry.cost_label.size = Vector2(float(entry.cost_width), size.y)
x += float(entry.cost_width) + PAIR_GAP

1
ui/PriceTag.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://4qp8g8ogbigj

View file

@ -26,7 +26,7 @@ var _header_labels: Array[Label] = []
var _rows: Dictionary = {}
# The value columns, in the order they are drawn. LOCAL is dropped from this
# list at a building that IS the bank - see _local_is_banked().
# list where it would only repeat another column - see _local_is_redundant().
const VALUE_KEYS: Array = ["bank", "fleet", "local"]
var _local_shown: bool = true
@ -79,15 +79,22 @@ func _make_value_label(colour: Color) -> Label:
return label
# Whether a LOCAL column would say anything a reader could act on. Two ways
# it would not:
#
# #95: at a building that IS the bank - the mothership carries both the "bank"
# and the "research" capability, so the research menu is always opened from one
# - building_inventory() redirects to bank_inventory itself, and LOCAL is not
# merely equal to BANKED but the very same store read twice. Printing the
# number again teaches nothing and costs a third of the table, so the column
# goes away there and the remaining two share the width.
func _local_is_banked() -> bool:
# number again teaches nothing and costs a third of the table.
#
# #137: and at no building at all, which is how the build menu opens it - it
# is reached from the fleet's own button rather than from a structure. There
# is no "here" to report on, and a column of em-dashes is worth less than the
# width it takes from the two columns that do say something.
func _local_is_redundant() -> bool:
if building_id == 0:
return false
return true
return GameState.building_inventory(building_id) == GameState.bank_inventory
@ -142,7 +149,7 @@ func _layout() -> void:
func _process(_delta: float) -> void:
# building_id is assigned by whichever panel opens, so the column count can
# change between frames; re-lay out only when it actually flips.
var local_shown: bool = not _local_is_banked()
var local_shown: bool = not _local_is_redundant()
if local_shown != _local_shown:
_local_shown = local_shown
_layout()

View file

@ -1,80 +0,0 @@
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()

View file

@ -1 +0,0 @@
uid://c1xt1uhl8f023