TableTower/ui/sheet_field_views.gd
RobinFiredrake 2661744b06 Sheets phase B: main menu, Sheets list, sheet editor
- Main menu (Spreads, Sheets, Settings, Quit) on the theme background,
  with recent spreads and sheets; logo or ☰ returns to it.
- Sheets: data model, store, templates, snapshot undo; New sheet dialog
  (blank or a template); editor with folder tabs, snap-grid canvas on the
  spread background, field palette, inspector, Play/Build, status strip.
- Field kinds: Number, Text box (BBCode, live {@name}), Heading.
- PaletteList shared by the card and field palettes; shared top bar brand.
- Keys go to the screen that shows; theme reloads keep the screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 15:53:56 -06:00

139 lines
5 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 SheetFieldViews
extends RefCounted
## The inside of a sheet box, by kind (SheetKinds): what the player sees and
## changes in Play. Changes go through SheetEditor.edit_value. More kinds come
## with phase C (docs/SHEETS.md).
static func make(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
match box.kind:
SheetKinds.HEADING:
return _heading(box, ink)
SheetKinds.NUMBER:
return _number(ed, box, ink)
_:
return _text(ed, box, ink)
static func _heading(box: SheetBox, ink: Color) -> Control:
var l := Style.make_label(box.title, Style.display(), 30, ink)
l.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
l.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
return l
## A number with − and + beside it; type to set it.
static func _number(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var row := HBoxContainer.new()
row.alignment = BoxContainer.ALIGNMENT_CENTER
row.add_theme_constant_override("separation", 4)
var value := LineEdit.new()
value.name = "Value"
value.text = _num_text(box.value)
value.alignment = HORIZONTAL_ALIGNMENT_CENTER
value.size_flags_horizontal = Control.SIZE_EXPAND_FILL
value.custom_minimum_size.x = 24
value.add_theme_constant_override("minimum_character_width", 1)
value.flat = true
value.add_theme_font_override("font", Style.display())
value.add_theme_font_size_override("font_size", 26)
value.add_theme_color_override("font_color", ink)
var commit := func(text: String) -> void:
var n: Variant = _parse(text, box.value)
value.text = _num_text(n)
if n != _as_number(box.value):
ed.edit_value(box, n)
value.text_submitted.connect(commit)
value.focus_exited.connect(func() -> void: commit.call(value.text))
for step: int in [-1, 1]:
var b := _step_button("−" if step < 0 else "+", ink)
b.name = "Minus" if step < 0 else "Plus"
b.focus_mode = Control.FOCUS_NONE
b.pressed.connect(func() -> void:
var n: Variant = _as_number(box.value) + step
value.text = _num_text(n)
ed.edit_value(box, n))
row.add_child(b)
if step < 0:
row.add_child(value)
return row
## A small − or + button drawn in the box's own ink colour.
static func _step_button(text: String, ink: Color) -> Button:
var b := Button.new()
b.text = text
b.custom_minimum_size = Vector2(26, 26)
b.size_flags_vertical = Control.SIZE_SHRINK_CENTER
b.focus_mode = Control.FOCUS_NONE
b.add_theme_font_override("font", Style.body_bold())
b.add_theme_font_size_override("font_size", 16)
var normal := Style.bordered(Style.box(Color(ink, 0.04), 6, [4, 0, 4, 0]), 1, Color(ink, 0.35))
var hover := Style.bordered(Style.box(Color(ink, 0.12), 6, [4, 0, 4, 0]), 1, Color(ink, 0.6))
Style.skin_button(b, normal, hover, hover, ink, ink, ink)
return b
## Shows the formatted text; click (Play) to edit it as BBCode, click away to see it again.
static func _text(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var stack := Control.new()
var shown := RichTextLabel.new()
shown.bbcode_enabled = true
shown.fit_content = false
shown.scroll_active = true
shown.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
shown.add_theme_color_override("default_color", ink)
shown.add_theme_font_override("normal_font", Style.body())
shown.text = _expand(ed, str(box.value if box.value != null else ""))
if shown.text == "" and not ed.building():
shown.text = "[i]Click to write…[/i]"
stack.add_child(shown)
var edit := TextEdit.new()
edit.name = "Edit"
edit.text = str(box.value if box.value != null else "")
edit.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY
edit.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
edit.visible = false
ModalCard.style_text_edit(edit)
stack.add_child(edit)
shown.gui_input.connect(func(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb and mb.pressed and mb.button_index == MOUSE_BUTTON_LEFT and not ed.building():
shown.visible = false
edit.visible = true
edit.grab_focus())
edit.text_changed.connect(func() -> void: ed.edit_value(box, edit.text))
edit.focus_exited.connect(func() -> void:
edit.visible = false
shown.text = _expand(ed, edit.text)
shown.visible = true)
return stack
## Live values: {@name} shows the value of the box used in tables as @name.
static func _expand(ed: SheetEditor, text: String) -> String:
if not text.contains("{@"):
return text
for tab in ed.doc.tabs:
for other: SheetBox in tab["boxes"]:
if other.var_name != "":
text = text.replace("{@%s}" % other.var_name, _num_text(other.value) if other.kind == SheetKinds.NUMBER else str(other.value))
return text
static func _as_number(v: Variant) -> float:
return float(v) if v is int or v is float or (v is String and (v as String).is_valid_float()) else 0.0
static func _parse(text: String, fallback: Variant) -> Variant:
var t := text.strip_edges()
if t.is_valid_int():
return t.to_int()
if t.is_valid_float():
return t.to_float()
return _as_number(fallback)
static func _num_text(v: Variant) -> String:
var n := _as_number(v)
return str(int(n)) if n == floorf(n) else str(n)