TableTower/ui/ledger_card_rows.gd
RobinFiredrake edb49fce27 Ledger entries are saved on the card and editable on the spread
The entries were only saved values of the spread, so copies, exports and
imports lost them. Now a Ledger card lists its entries (name and starting
value) in the spread file; current values stay saved values. Build mode
edits them on the card too. Older spreads take in the entries made on the
card when they open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 13:36:03 -06:00

67 lines
2.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.

class_name LedgerCardRows
extends RefCounted
## A Ledger card's entries in Build mode, on the spread: a name field, a
## starting-value field and × for each entry, so the entries can be edited on
## the card as well as in the inspector. Edits go through LedgerEdits.
## Fills `box` with one editable row per entry of the card's table.
static func fill(card: TableCard, box: VBoxContainer) -> void:
var table := card.table
if table.members.is_empty():
var none := Style.make_label("No entries yet", Style.body_italic(true), 14, Style.MUTED)
none.mouse_filter = Control.MOUSE_FILTER_IGNORE
box.add_child(none)
return
for member: Dictionary in table.members:
box.add_child(_row(card, str(member["name"]), str(member["start"])))
static func _row(card: TableCard, member_name: String, start: String) -> HBoxContainer:
var main := card.main
var table := card.table
var row := TableCard.hbox(6)
row.custom_minimum_size = Vector2(0, 30)
row.set_meta("member", "@" + LedgerMembers.value_name(table, member_name))
var name_field := InspectorKit.line(RollContext.pretty_name(member_name), member_name, 0.0)
name_field.size_flags_horizontal = Control.SIZE_EXPAND_FILL
name_field.tooltip_text = "@" + LedgerMembers.value_name(table, member_name)
name_field.set_meta("ledger_name", member_name)
var commit_name := func() -> void:
LedgerEdits.rename(main, table, member_name, name_field.text)
name_field.text_submitted.connect(func(_text: String) -> void: commit_name.call())
name_field.focus_exited.connect(commit_name)
row.add_child(name_field)
var start_field := InspectorKit.amount(start, "0")
start_field.tooltip_text = "Where this entry starts"
var commit_start := func() -> void:
LedgerEdits.set_start(main, table, member_name, start_field.text)
start_field.text_submitted.connect(func(_text: String) -> void: commit_start.call())
start_field.focus_exited.connect(commit_start)
row.add_child(start_field)
var remove_button := InspectorKit.xbutton("Remove this entry")
remove_button.pressed.connect(func() -> void: LedgerEdits.remove(main, table, member_name))
row.add_child(remove_button)
return row
## Adds an entry to the card's table and puts the cursor in its name field.
static func add_and_focus(main: Main, table: RollTable) -> void:
var member_name := LedgerEdits.add(main, table)
var card := main.card_for(table.id)
if card == null:
return
var field := _find_name_field(card, member_name)
if field and field.is_inside_tree():
field.grab_focus()
field.select_all()
static func _find_name_field(node: Node, member_name: String) -> LineEdit:
for child in node.get_children():
if child is LineEdit and str(child.get_meta("ledger_name", "")) == member_name:
return child
var found := _find_name_field(child, member_name)
if found:
return found
return null