TableTower/ui/table_card_editing.gd
RobinFiredrake 33913889da Roll modifiers and resizable tray/inspector
- Ranged roll tables take a modifier (dice_mod, e.g. @luck). Ranges may run
  past the dice (-3 to 24 on a d20); totals past the ends use the end result.
- Range and chance code moved from RollTable into RollRange; chances follow
  the modifier's current value; plain wording and Why lines name it.
- ModifierEditor builds modifiers in plain words for roll and check cards;
  the inspector's roll section is now InspectorRoll.
- The Earlier tab scrolls; the tray and inspector resize with drag handles
  (PanelSplitter), sizes kept in local settings.
- Demo Room check uses 2d6 + Luck with a luck-only vault; new test_modifier
  suite; docs updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 10:01:10 -06:00

241 lines
9.7 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 TableCardEditing
extends RefCounted
## A TableCard's Build-mode controls: the inline result-text, weight and range
## fields, router case rows with move and delete, and the footer (add a
## result, option or case; delete the table). Everything else is edited in
## Main's inspector.
var card: TableCard
func _init(p_card: TableCard) -> void:
card = p_card
## Build mode: the inline result-text field of a row. Focusing it selects the
## result for the inspector.
func text_edit(e: TableEntry, index: int, chosen: bool) -> LineEdit:
var table := card.table
var edit := LineEdit.new()
edit.text = e.text
if chosen:
edit.placeholder_text = "(the option's name)"
edit.tooltip_text = "The result text. Leave it as Chosen (or empty) to show the picked option's name; {option_label}, {option_value} and {@[option]} work"
edit.size_flags_horizontal = Control.SIZE_EXPAND_FILL
edit.size_flags_vertical = Control.SIZE_SHRINK_CENTER
edit.custom_minimum_size = Vector2(40, 0)
edit.add_theme_font_size_override("font_size", 15)
var loop_rows := TableCardRows.LOOP_ROWS
if table.kind == RollTable.KIND_LOOP:
edit.placeholder_text = loop_rows[clampi(index, 0, 1)]
edit.tooltip_text = "%s: shown each pass ({i} is the pass number)" % loop_rows[0] if index == 0 else "%s: runs once when the loop ends" % loop_rows[1]
elif table.kind == RollTable.KIND_ROUTER:
edit.placeholder_text = "Result text"
edit.text_changed.connect(func(s: String) -> void:
e.text = s
card.main.mark_dirty()
)
edit.focus_entered.connect(func() -> void: card.select_entry(e.id))
return edit
## Build mode: a click on a row (outside its fields) selects the result for
## the inspector.
func hook_select(row: Control, eid: String) -> void:
row.gui_input.connect(func(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb and mb.button_index == MOUSE_BUTTON_LEFT and mb.pressed:
card.select_entry(eid)
)
## Build mode: one router case. The first line is its condition, in plain
## words or as written (edited in the inspector), with move up / down and
## delete; the second is its result text, then its effects chip.
func make_case_row(e: TableEntry, index: int, numw: float) -> Control:
var table := card.table
var main := card.main
var plain := card.plain
var row := PanelContainer.new()
row.mouse_filter = Control.MOUSE_FILTER_PASS
row.custom_minimum_size = Vector2(0, 36)
var outer := TableCard.hbox(4)
row.add_child(outer)
var num := Style.make_label(str(index + 1), Style.display(true), 16, Style.MUTED)
num.custom_minimum_size = Vector2(numw, 0)
num.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
num.size_flags_vertical = Control.SIZE_SHRINK_BEGIN
outer.add_child(num)
var v := TableCard.vbox(3)
v.size_flags_horizontal = Control.SIZE_EXPAND_FILL
outer.add_child(v)
var top := TableCard.hbox(4)
v.add_child(top)
var w := e.when.strip_edges()
var bad := w != "" and Expr.check(TemplateKit.probe(w)) != ""
var cond := Style.make_label(RulesView.case_label(e, main.data, plain), Style.body_bold(true), 14, Style.DANGER_INK if bad else Style.COPPER_DARK)
cond.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
cond.size_flags_horizontal = Control.SIZE_EXPAND_FILL
cond.size_flags_vertical = Control.SIZE_SHRINK_CENTER
cond.mouse_filter = Control.MOUSE_FILTER_PASS
cond.tooltip_text = ("⚠ " + Expr.check(TemplateKit.probe(w))) if bad else (("if " + w) if plain and w != "" else "Cases are checked top to bottom; the first true one is chosen")
cond.set_meta("case_line", true)
top.add_child(cond)
top.add_child(mini_button("↑", "Move this case up", index > 0, func() -> void: _move_case(index, -1)))
top.add_child(mini_button("↓", "Move this case down", index < table.entries.size() - 1, func() -> void: _move_case(index, 1)))
top.add_child(mini_button("×", "Delete this case", true, func() -> void: main.delete_entry(table.id, e.id)))
v.add_child(text_edit(e, index, false))
card.rows_ui.add_rule_chips(v, e)
hook_select(row, e.id)
card.rows_ui.register(e, row, num, null, index)
return row
static func mini_button(glyph: String, tip: String, enabled: bool, on_press: Callable) -> Button:
var b := Button.new()
b.text = glyph
b.tooltip_text = tip
b.disabled = not enabled
b.custom_minimum_size = Vector2(22, 22)
b.size_flags_vertical = Control.SIZE_SHRINK_CENTER
b.add_theme_font_size_override("font_size", 16 if glyph == "×" else 13)
Style.skin_button(b, Style.box(Color(0, 0, 0, 0), 5), Style.box(Color("#e6dfcd"), 5), Style.box(Color("#e6dfcd"), 5), Style.MUTED, Style.DANGER_INK if glyph == "×" else Style.INK)
b.add_theme_color_override("font_disabled_color", Color(Style.MUTED, 0.35))
b.pressed.connect(on_press)
return b
## Moves router case `index` up (-1) or down (+1).
func _move_case(index: int, by: int) -> void:
var table := card.table
var to := index + by
if to < 0 or to >= table.entries.size():
return
var e := table.entries[index]
table.entries.remove_at(index)
table.entries.insert(to, e)
card.rebuild()
card.main.mark_dirty()
## Adds a router case just before the Otherwise case (or at the end). It
## starts as "if false", so it never matches until its condition is written.
func add_case() -> void:
var table := card.table
var e := TableEntry.new(TowerBoard.new_id("e"), "New case")
e.when = "false"
var at := table.otherwise_index()
if at < 0 or at != table.entries.size() - 1:
at = table.entries.size()
table.entries.insert(at, e)
table.collapsed = false
card.rebuild()
card.main.mark_dirty()
## Weight on a plain table, shown as ×N. Also the number of copies in card mode.
func weight_edit(e: TableEntry) -> LineEdit:
var edit := LineEdit.new()
edit.text = "×%d" % e.weight
edit.custom_minimum_size = Vector2(34, 0)
edit.alignment = HORIZONTAL_ALIGNMENT_CENTER
edit.max_length = 4
edit.tooltip_text = "Weight: how likely this result is, relative to the others (0 = never)"
edit.size_flags_vertical = Control.SIZE_SHRINK_CENTER
edit.add_theme_font_override("font", Style.display(true))
edit.add_theme_font_size_override("font_size", 14)
edit.add_theme_color_override("font_color", Style.MUTED)
var commit := func() -> void:
var digits := ""
for ch in edit.text:
if ch >= "0" and ch <= "9":
digits += ch
var w := clampi(digits.to_int(), 0, 999) if digits != "" else e.weight
edit.text = "×%d" % w
if w != e.weight:
e.weight = w
card.update_deck()
card.main.mark_dirty()
edit.text_submitted.connect(func(_t: String) -> void: commit.call())
edit.focus_exited.connect(commit)
return edit
## Largest range value the card's range boxes take, either way.
const RANGE_LIMIT := 99999
func range_edit(e: TableEntry, is_lo: bool) -> LineEdit:
var table := card.table
var edit := LineEdit.new()
edit.text = str(e.range_lo if is_lo else e.range_hi)
edit.custom_minimum_size = Vector2(40, 0)
edit.alignment = HORIZONTAL_ALIGNMENT_CENTER
edit.max_length = 6
edit.tooltip_text = ("Range start" if is_lo else "Range end") + "\nValues past the dice (such as -3 or 24) need a modifier to come up"
edit.add_theme_font_override("font", Style.display(true))
edit.add_theme_font_size_override("font_size", 15)
edit.add_theme_stylebox_override("normal", Style.bordered(Style.box(Color.WHITE, 4, [2, 2, 2, 2]), 1, Style.RULE))
edit.add_theme_stylebox_override("focus", Style.bordered(Style.box(Color.WHITE, 4, [2, 2, 2, 2]), 1, Style.COPPER))
var commit := func() -> void:
# Any whole number: results past the dice are reached with a modifier.
var typed := edit.text.strip_edges()
var v := clampi(typed.to_int(), -RANGE_LIMIT, RANGE_LIMIT) if typed.is_valid_int() else (e.range_lo if is_lo else e.range_hi)
if is_lo:
e.range_lo = v
else:
e.range_hi = v
if e.range_lo > e.range_hi:
var tmp := e.range_lo
e.range_lo = e.range_hi
e.range_hi = tmp
edit.text = str(e.range_lo if is_lo else e.range_hi)
card.update_warning()
card.main.mark_dirty()
edit.text_submitted.connect(func(_t: String) -> void: commit.call())
edit.focus_exited.connect(commit)
return edit
func make_footer(ranged: bool) -> Control:
var table := card.table
var main := card.main
var wrap := MarginContainer.new()
wrap.mouse_filter = Control.MOUSE_FILTER_PASS
wrap.add_theme_constant_override("margin_left", 12)
wrap.add_theme_constant_override("margin_right", 10)
wrap.add_theme_constant_override("margin_bottom", 10)
var h := TableCard.hbox(8)
wrap.add_child(h)
# Loops, sheets, variables and number/text choices have fixed rows.
if table.kind == RollTable.KIND_ROLL or (table.kind == RollTable.KIND_CHOICE and table.choice == "pick"):
var label := "+ Add range" if ranged else ("+ Add option" if table.kind == RollTable.KIND_CHOICE else "+ Add result")
var add := footer_button(label, Style.INK)
add.pressed.connect(func() -> void: main.add_entry(table.id))
h.add_child(add)
elif table.kind == RollTable.KIND_ROUTER:
var addc := footer_button("+ Add case", Style.INK)
addc.tooltip_text = "Add a case before Otherwise"
addc.pressed.connect(add_case)
h.add_child(addc)
var spacer := Control.new()
spacer.size_flags_horizontal = Control.SIZE_EXPAND_FILL
spacer.mouse_filter = Control.MOUSE_FILTER_IGNORE
h.add_child(spacer)
var del := footer_button("Delete", Style.DANGER_INK)
del.tooltip_text = "Delete this table"
del.pressed.connect(func() -> void: main.delete_table(table.id))
h.add_child(del)
return wrap
static func footer_button(text: String, hover_color: Color) -> Button:
var b := Button.new()
b.text = text
b.add_theme_font_size_override("font_size", 14)
var normal := Style.bordered(Style.box(Color(0, 0, 0, 0), 6, [10, 4, 10, 4]), 1, Style.DASH)
var hover := Style.bordered(Style.box(Color(0, 0, 0, 0), 6, [10, 4, 10, 4]), 1, hover_color if hover_color != Style.INK else Style.MUTED)
Style.skin_button(b, normal, hover, hover, Style.MUTED, hover_color)
return b