- 25 palette items over 23 kinds: basics, trackers (bars, ticks, clocks, dots, step/usage dice, ladders), formulas and dice buttons (rule language with sheet @names), lists (rows, mini table, chips, point pool that may go negative, bonds), slots, conditions, spell slots, XP, currency, heading, divider, picture. - Per-kind settings in the inspector; hover excerpts and an in-app details panel for list entries; live redraw of dependent boxes. - Canvas: the page stays put after edits (boxes can go anywhere), a screen of room past the boxes, drag-to-pan and edge scrolling. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
244 lines
9.1 KiB
GDScript
244 lines
9.1 KiB
GDScript
class_name SheetFieldViews
|
||
extends RefCounted
|
||
## The inside of a sheet box, by kind (SheetKinds): what the player sees and
|
||
## changes in Play. This file has the basic kinds and shared helpers; the
|
||
## others live in SheetFieldsTrackers, SheetFieldsSmart, SheetFieldsList and
|
||
## SheetFieldsNiche. Changes go through SheetEditor.edit_value (typing) or
|
||
## set_value (clicks), so they can be undone.
|
||
|
||
|
||
static func make(ed: SheetEditor, box: SheetBox, look: Dictionary) -> Control:
|
||
var ink: Color = look["ink"]
|
||
match box.kind:
|
||
SheetKinds.HEADING:
|
||
return _heading(box, ink)
|
||
SheetKinds.NUMBER:
|
||
return _number(ed, box, ink)
|
||
SheetKinds.SHORT_TEXT:
|
||
return _short_text(ed, box, ink)
|
||
SheetKinds.CHECKBOX:
|
||
return _checkbox(ed, box, ink)
|
||
SheetKinds.DROPDOWN:
|
||
return _dropdown(ed, box, ink)
|
||
SheetKinds.DIVIDER:
|
||
return _divider(ink)
|
||
SheetKinds.PICTURE:
|
||
return _picture(ed, box, look)
|
||
SheetKinds.RESOURCE, SheetKinds.TICKS, SheetKinds.CLOCK, SheetKinds.DOTS, SheetKinds.STEP_DIE, SheetKinds.USAGE_DIE, SheetKinds.LADDER:
|
||
return SheetFieldsTrackers.make(ed, box, look)
|
||
SheetKinds.FORMULA, SheetKinds.DICE:
|
||
return SheetFieldsSmart.make(ed, box, look)
|
||
SheetKinds.LIST:
|
||
return SheetFieldsList.make(ed, box, look)
|
||
SheetKinds.SLOTS, SheetKinds.CONDITIONS, SheetKinds.SPELL_SLOTS, SheetKinds.XP, SheetKinds.CURRENCY:
|
||
return SheetFieldsNiche.make(ed, box, look)
|
||
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
|
||
|
||
|
||
static func _divider(ink: Color) -> Control:
|
||
var c := Control.new()
|
||
c.draw.connect(func() -> void: c.draw_line(Vector2(0, c.size.y / 2.0), Vector2(c.size.x, c.size.y / 2.0), Color(ink, 0.5), 2.0))
|
||
return c
|
||
|
||
|
||
## 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 = SheetValues.show(SheetValues.number(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 = clamp_number(box, SheetValues.number(parse_number(text, box.value)))
|
||
value.text = SheetValues.show(n)
|
||
if SheetValues.number(n) != SheetValues.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.pressed.connect(func() -> void:
|
||
var n: Variant = clamp_number(box, SheetValues.number(box.value) + step)
|
||
value.text = SheetValues.show(n)
|
||
ed.edit_value(box, n))
|
||
row.add_child(b)
|
||
if step < 0:
|
||
row.add_child(value)
|
||
return row
|
||
|
||
|
||
## Keeps a number inside the box's min and max settings (when set).
|
||
static func clamp_number(box: SheetBox, n: float) -> Variant:
|
||
if str(box.config.get("min", "")) != "":
|
||
n = maxf(n, SheetValues.number(box.config["min"]))
|
||
if str(box.config.get("max", "")) != "":
|
||
n = minf(n, SheetValues.number(box.config["max"]))
|
||
return int(n) if n == floorf(n) else n
|
||
|
||
|
||
static func _short_text(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
|
||
var e := LineEdit.new()
|
||
e.name = "Value"
|
||
e.text = str(box.value if box.value != null else "")
|
||
e.placeholder_text = "Type here"
|
||
e.flat = true
|
||
e.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
e.add_theme_font_size_override("font_size", 17)
|
||
e.add_theme_color_override("font_color", ink)
|
||
e.add_theme_color_override("font_placeholder_color", Color(ink, 0.4))
|
||
e.text_changed.connect(func(text: String) -> void: ed.edit_value(box, text))
|
||
return e
|
||
|
||
|
||
static func _checkbox(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
|
||
var c := CheckBox.new()
|
||
c.name = "Value"
|
||
c.text = "Yes" if bool(box.value) else "No"
|
||
c.button_pressed = bool(box.value)
|
||
for key in ["font_color", "font_hover_color", "font_pressed_color", "font_hover_pressed_color"]:
|
||
c.add_theme_color_override(key, ink)
|
||
c.toggled.connect(func(on: bool) -> void:
|
||
c.text = "Yes" if on else "No"
|
||
ed.edit_value(box, on))
|
||
return c
|
||
|
||
|
||
static func _dropdown(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
|
||
var o := OptionButton.new()
|
||
o.name = "Value"
|
||
var options: Array = box.config.get("options", [])
|
||
for i in options.size():
|
||
o.add_item(str(options[i]))
|
||
if str(options[i]) == str(box.value):
|
||
o.select(i)
|
||
o.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
o.add_theme_color_override("font_color", ink)
|
||
o.item_selected.connect(func(i: int) -> void: ed.edit_value(box, str(options[i])))
|
||
return o
|
||
|
||
|
||
## The picture from its file, or a button to pick one (Play).
|
||
static func _picture(ed: SheetEditor, box: SheetBox, look: Dictionary) -> Control:
|
||
var path := str(box.value if box.value != null else "")
|
||
var tex: Texture2D = null
|
||
if path != "" and FileAccess.file_exists(path):
|
||
var img := Image.load_from_file(path)
|
||
if img:
|
||
tex = ImageTexture.create_from_image(img)
|
||
if tex:
|
||
var tr := TextureRect.new()
|
||
tr.texture = tex
|
||
tr.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
|
||
tr.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
|
||
tr.tooltip_text = "Double-click to change the picture"
|
||
tr.gui_input.connect(func(ev: InputEvent) -> void:
|
||
var mb := ev as InputEventMouseButton
|
||
if mb and mb.double_click and not ed.building():
|
||
pick_picture(ed, box))
|
||
return tr
|
||
var b := step_button("Choose a picture…", look["ink"])
|
||
b.name = "Choose"
|
||
b.pressed.connect(pick_picture.bind(ed, box))
|
||
return b
|
||
|
||
|
||
static func pick_picture(ed: SheetEditor, box: SheetBox) -> void:
|
||
ed.m.layout.file_dialog("Choose a picture", ["*.png, *.jpg, *.jpeg, *.webp ; Pictures"], FileDialog.FILE_MODE_OPEN_FILE, func(path: String) -> void:
|
||
var kept := ed.screen.store.keep_picture(path)
|
||
if kept == "":
|
||
ed.m.toast("Couldn't read that picture")
|
||
else:
|
||
ed.set_value(box, kept))
|
||
|
||
|
||
## 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.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
|
||
var values := SheetValues.vars(ed.doc)
|
||
for name: String in values:
|
||
text = text.replace("{@%s}" % name, "[b]%s[/b]" % SheetValues.show(values[name]))
|
||
return text
|
||
|
||
|
||
static func parse_number(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 SheetValues.number(fallback)
|
||
|
||
|
||
## A small 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", 15)
|
||
var normal := Style.bordered(Style.box(Color(ink, 0.04), 6, [6, 0, 6, 0]), 1, Color(ink, 0.35))
|
||
var hover := Style.bordered(Style.box(Color(ink, 0.12), 6, [6, 0, 6, 0]), 1, Color(ink, 0.6))
|
||
Style.skin_button(b, normal, hover, hover, ink, ink, ink)
|
||
return b
|
||
|
||
|
||
## A muted label in the box's ink.
|
||
static func note(text: String, ink: Color, size: int = 13) -> Label:
|
||
var l := Style.make_label(text, Style.body(), size, Color(ink, 0.7))
|
||
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
||
return l
|