TableTower/ui/table_card_rebuild.gd
RobinFiredrake 7ea2bbf3a8 Speed: lazy format menus, one-call dashed threads, kept inspector sections, queued card rebuilds
Profiling showed the delays come from Godot's own calls, not GDScript:
- Right-click format menus are made on the first right-click, not for
  every text field at build time.
- Dashed reference threads draw all their dashes in one call.
- Inspector rebuilds keep the closed sections and only remake open ones.
- Card rows are styled before they join the card.
- A Play/Build switch rebuilds cards in view at once and the rest a few
  per frame (CardRefreshQueue); threads redraw only now and then meanwhile.

Adds tests/perf_actions.gd, a timing tool for everyday actions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 16:20:31 -06:00

87 lines
3.1 KiB
GDScript

class_name TableCardRebuild
extends RefCounted
## Rebuilds a TableCard from its RollTable. It throws away the old contents
## (but keeps the frame, group mark, grip and shield), then adds the header,
## the inputs box, the body (a variable's value or the result list), the
## warning line and, in Build mode, the footer. TableCard.rebuild() calls run().
var card: TableCard
func _init(p_card: TableCard) -> void:
card = p_card
func run() -> void:
var table := card.table
var main := card.main
main.card_refresh.forget(table.id)
for child in card.get_children():
if child == card.frame or child == card.group_mark or child == card.grip or child == card.shield:
continue
card.remove_child(child)
child.queue_free()
card.skin.sync_frame()
card.rows.clear()
card.rows_ui.clear()
card.edit_ui.text_fields.clear()
card.more_row = null
card.list_scroll = null
card.header_ui.clear()
card.value_ui.clear()
card.plain = RulesView.plain(main)
if main.has_method("inspector_selection"):
card._selected_eid = read_selected_entry(main.call("inspector_selection"))
var build := main.mode == Main.Mode.BUILD
card.custom_minimum_size = Vector2(card.card_width(), 0)
card._shrink.call_deferred()
var ranged := table.is_ranged()
var collapsed := card.is_collapsed()
# Built before it joins the card: styling rows already on screen makes their
# text be measured again each time.
var column := TableCard.vbox(0)
column.add_child(card.header_ui.make(build, ranged))
if not table.inputs.is_empty() and table.kind != RollTable.KIND_VARIABLE:
column.add_child(card.header_ui.make_inputs_box())
if table.kind == RollTable.KIND_VARIABLE:
column.add_child(card.value_ui.make_collection_body(build) if table.is_collection() else card.value_ui.make_variable_body(build))
_finish(column, build, ranged)
return
card.list_ui.fill(column, build, ranged, collapsed)
_finish(column, build, ranged)
card.skin.dim_drawn()
if card.list_scroll != null and card.edit_ui.adding and not table.entries.is_empty():
card._reveal_when_laid_out.call_deferred(table.entries[-1].id)
elif card.list_scroll != null:
for eid in card.rows:
if main.is_entry_lit(table.id, eid):
card._reveal_when_laid_out.call_deferred(eid)
break
## The inspector's selected result on this card, or "". The inspector reports
## {kind, tid, eid}; older callers used {table_id, entry_id}.
func read_selected_entry(selection: Variant) -> String:
if selection is Dictionary:
var fields := selection as Dictionary
var tid := str(fields.get("tid", fields.get("table_id", "")))
if tid == card.table.id and str(fields.get("kind", "result")) == "result":
return str(fields.get("eid", fields.get("entry_id", "")))
return ""
## The parts every card ends with: the warning line, the Build footer, the
## card's style and its live labels.
func _finish(column: VBoxContainer, build: bool, ranged: bool) -> void:
card._warn_panel = card.skin.make_warning()
column.add_child(card._warn_panel)
card.update_warning()
if build:
column.add_child(card.edit_ui.make_footer(ranged))
card.add_child(column)
card.skin.apply_style()
card.refresh_live()
card.input.raise_covers()