- Style.menu_button / font_colors / field_colors replace five copies of the "copy a bar button's skin onto a MenuButton" block, nine single-colour font loops and four field colour blocks. - GroupCard uses TableCard.vbox/hbox instead of its own copies. - InspectorKit.focused_tag / clear / refocus / as_text_link replace the focus-keeping render frame and "Edit as text" link shared by the condition and effects builders. - InspectorCard._setting_line replaces three copies of a bound setting field. - Main.file_dialog / export_dialog replace four FileDialog setups. - JsonIO.entry_line / finish_import replace copies in canvas_io, dot_exporter and graphml_io. - Dead code: TableCard's unused inline-editor fields and the no-op --edit-details / --edit-effects flags, an unused error counter in the tray, and an unreachable "settings not available" fallback. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
300 lines
10 KiB
GDScript
300 lines
10 KiB
GDScript
class_name InspectorKit
|
||
extends RefCounted
|
||
## Small styled controls and helpers shared by the Build inspector and its
|
||
## builders (docs/PLAIN.md §6): fields, dropdowns, link buttons, error lines,
|
||
## and the rules for turning what a person types into rule tokens.
|
||
|
||
const FIELD_BG := Color.WHITE
|
||
const VAR_BG := Color("#fdf6dc")
|
||
const VAR_BORDER := Color("#e3c97a")
|
||
const BOX_BG := Color("#f7f5ee")
|
||
const TAKEN_BG := Color("#fbf3d6")
|
||
## Meta key naming a control inside the inspector, so focus can come back to
|
||
## the same field after a rebuild.
|
||
const KEY := "insp_key"
|
||
|
||
|
||
static func _field_box(border: Color) -> StyleBoxFlat:
|
||
return Style.bordered(Style.box(FIELD_BG, 6, [8, 4, 8, 4]), 1, border)
|
||
|
||
|
||
## A one-line field. `min_w` 0 lets it fill the row.
|
||
static func line(text: String, placeholder: String = "", min_w: float = 0.0) -> LineEdit:
|
||
var le := LineEdit.new()
|
||
le.text = text
|
||
le.placeholder_text = placeholder
|
||
le.add_theme_stylebox_override("normal", _field_box(Style.RULE))
|
||
le.add_theme_stylebox_override("focus", _field_box(Style.COPPER))
|
||
le.add_theme_stylebox_override("read_only", _field_box(Style.RULE))
|
||
Style.field_colors(le)
|
||
le.add_theme_font_override("font", Style.body())
|
||
le.add_theme_font_size_override("font_size", 15)
|
||
le.add_theme_constant_override("minimum_character_width", 0)
|
||
if min_w > 0.0:
|
||
le.custom_minimum_size = Vector2(min_w, 0)
|
||
else:
|
||
le.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
return le
|
||
|
||
|
||
## A short centred field for an amount ("1", "2d6", "Psionic"). It widens
|
||
## with its text up to `max_w`.
|
||
static func amount(text: String, placeholder: String = "") -> LineEdit:
|
||
var le := line(text, placeholder, 52.0)
|
||
le.alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||
le.add_theme_font_override("font", Style.body_bold())
|
||
le.expand_to_text_length = true
|
||
return le
|
||
|
||
|
||
static func text_box(text: String, placeholder: String = "", min_h: float = 70.0) -> TextEdit:
|
||
var te := TextEdit.new()
|
||
te.text = text
|
||
te.placeholder_text = placeholder
|
||
te.custom_minimum_size = Vector2(0, min_h)
|
||
te.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY
|
||
te.scroll_fit_content_height = true
|
||
te.add_theme_stylebox_override("normal", Style.bordered(Style.box(FIELD_BG, 6, [8, 6, 8, 6]), 1, Style.RULE))
|
||
te.add_theme_stylebox_override("focus", Style.bordered(Style.box(FIELD_BG, 6, [8, 6, 8, 6]), 1, Style.COPPER))
|
||
Style.field_colors(te)
|
||
te.add_theme_color_override("background_color", Color(0, 0, 0, 0))
|
||
te.add_theme_font_override("font", Style.body())
|
||
te.add_theme_font_size_override("font_size", 15)
|
||
return te
|
||
|
||
|
||
## Monospaced rule text (Text mode fields).
|
||
static func code(c: Control) -> Control:
|
||
var f := SystemFont.new()
|
||
f.font_names = PackedStringArray(["IBM Plex Mono", "DejaVu Sans Mono", "monospace"])
|
||
c.add_theme_font_override("font", f)
|
||
c.add_theme_font_size_override("font_size", 14)
|
||
return c
|
||
|
||
|
||
## Red border (and ink) on a field whose text can't be used.
|
||
static func mark(c: Control, ok: bool) -> void:
|
||
if c is LineEdit:
|
||
c.add_theme_stylebox_override("normal", _field_box(Style.RULE if ok else Style.DANGER))
|
||
c.add_theme_stylebox_override("focus", _field_box(Style.COPPER if ok else Style.DANGER))
|
||
elif c is TextEdit:
|
||
var b := Style.bordered(Style.box(FIELD_BG, 6, [8, 6, 8, 6]), 1, Style.RULE if ok else Style.DANGER)
|
||
var f := Style.bordered(Style.box(FIELD_BG, 6, [8, 6, 8, 6]), 1, Style.COPPER if ok else Style.DANGER)
|
||
c.add_theme_stylebox_override("normal", b)
|
||
c.add_theme_stylebox_override("focus", f)
|
||
|
||
|
||
## A dropdown styled like the mockup's .dd chips.
|
||
static func dropdown(items: Array, selected: int) -> OptionButton:
|
||
var ob := OptionButton.new()
|
||
for i in items.size():
|
||
ob.add_item(str(items[i]), i)
|
||
ob.select(selected)
|
||
ob.fit_to_longest_item = false
|
||
var n := Style.bordered(Style.box(FIELD_BG, 6, [8, 3, 6, 3]), 1, Style.RULE)
|
||
var h := Style.bordered(Style.box(Style.HOVER_ROW, 6, [8, 3, 6, 3]), 1, Style.DASH)
|
||
Style.skin_button(ob, n, h, h, Style.INK)
|
||
ob.add_theme_font_override("font", Style.body_bold())
|
||
ob.add_theme_font_size_override("font_size", 15)
|
||
ob.add_theme_color_override("font_focus_color", Style.INK)
|
||
ob.add_theme_constant_override("arrow_margin", 6)
|
||
ModalCard.style_popup(ob.get_popup())
|
||
return ob
|
||
|
||
|
||
## Small caps heading ("WHEN THIS COMES UP").
|
||
static func flabel(text: String) -> Label:
|
||
return Style.make_label(text.to_upper(), Style.caps(), 12, Style.MUTED)
|
||
|
||
|
||
## A word inside a builder row ("to", "from", "Only if").
|
||
static func word(text: String, color: Color = Style.INK, bold: bool = false) -> Label:
|
||
var l := Style.make_label(text, Style.body_bold() if bold else Style.body(), 15, color)
|
||
l.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
return l
|
||
|
||
|
||
static func note(text: String, color: Color = Style.MUTED, size: int = 13) -> Label:
|
||
var l := Style.make_label(text, Style.body(), size, color)
|
||
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
||
l.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
l.custom_minimum_size = Vector2(60, 0)
|
||
return l
|
||
|
||
|
||
## An error line in plain words (hidden while empty).
|
||
static func error_label() -> Label:
|
||
var l := note("", Style.DANGER_INK, 13)
|
||
l.visible = false
|
||
return l
|
||
|
||
|
||
static func show_error(l: Label, msg: String) -> void:
|
||
l.text = msg
|
||
l.visible = msg != ""
|
||
|
||
|
||
## The × that removes a row.
|
||
static func xbutton(tip: String) -> Button:
|
||
var b := Button.new()
|
||
b.text = "×"
|
||
b.tooltip_text = tip
|
||
b.custom_minimum_size = Vector2(24, 24)
|
||
var n := Style.box(Color(0, 0, 0, 0), 5, [4, 0, 4, 0])
|
||
var h := Style.box(Style.WARN_BG, 5, [4, 0, 4, 0])
|
||
Style.skin_button(b, n, h, h, Style.MUTED, Style.DANGER_INK)
|
||
b.add_theme_font_size_override("font_size", 17)
|
||
b.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
return b
|
||
|
||
|
||
## A text link ("+ Add a change", "Edit as text").
|
||
static func link(text: String, tip: String = "") -> Button:
|
||
var b := Button.new()
|
||
b.text = text
|
||
b.tooltip_text = tip
|
||
b.flat = true
|
||
var n := Style.box(Color(0, 0, 0, 0), 4, [2, 1, 2, 1])
|
||
var h := Style.box(Color(Style.COPPER, 0.12), 4, [2, 1, 2, 1])
|
||
Style.skin_button(b, n, h, h, Style.COPPER_DARK)
|
||
b.add_theme_font_override("font", Style.body_bold())
|
||
b.add_theme_font_size_override("font_size", 14)
|
||
b.add_theme_color_override("font_disabled_color", Style.MUTED)
|
||
b.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
|
||
return b
|
||
|
||
|
||
static func check_box(text: String, on: bool) -> CheckBox:
|
||
var cb := CheckBox.new()
|
||
cb.text = text
|
||
cb.button_pressed = on
|
||
cb.add_theme_font_override("font", Style.body_bold())
|
||
cb.add_theme_font_size_override("font_size", 15)
|
||
Style.font_colors(cb, Style.INK)
|
||
cb.add_theme_color_override("font_disabled_color", Style.MUTED)
|
||
return cb
|
||
|
||
|
||
## A wrapping row of builder parts.
|
||
static func flow() -> HFlowContainer:
|
||
var f := HFlowContainer.new()
|
||
f.add_theme_constant_override("h_separation", 6)
|
||
f.add_theme_constant_override("v_separation", 6)
|
||
f.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
return f
|
||
|
||
|
||
static func vbox(sep: int = 6) -> VBoxContainer:
|
||
var v := VBoxContainer.new()
|
||
v.add_theme_constant_override("separation", sep)
|
||
v.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
return v
|
||
|
||
|
||
static func hbox(sep: int = 6) -> HBoxContainer:
|
||
var h := HBoxContainer.new()
|
||
h.add_theme_constant_override("separation", sep)
|
||
return h
|
||
|
||
|
||
## A heading over a block of fields.
|
||
static func field(heading: String, c: Control) -> VBoxContainer:
|
||
var v := vbox(5)
|
||
v.add_child(flabel(heading))
|
||
v.add_child(c)
|
||
return v
|
||
|
||
|
||
## Names a control so focus can return to it after the inspector rebuilds.
|
||
static func tag(c: Control, key: String) -> Control:
|
||
c.set_meta(KEY, key)
|
||
return c
|
||
|
||
|
||
## The tag of the focused control inside `root` ("" if none), so a rebuild
|
||
## can give focus back with `refocus`.
|
||
static func focused_tag(root: Control) -> String:
|
||
var f := root.get_viewport().gui_get_focus_owner() if root.is_inside_tree() else null
|
||
if f and root.is_ancestor_of(f) and f.has_meta(KEY):
|
||
return str(f.get_meta(KEY))
|
||
return ""
|
||
|
||
|
||
static func refocus(root: Node, key: String) -> void:
|
||
if key == "":
|
||
return
|
||
var c := find_tagged(root, key)
|
||
if c:
|
||
c.grab_focus.call_deferred()
|
||
|
||
|
||
static func clear(box: Node) -> void:
|
||
for c in box.get_children():
|
||
box.remove_child(c)
|
||
c.queue_free()
|
||
|
||
|
||
## The muted "Edit as text" link at the foot of a builder.
|
||
static func as_text_link(tip: String, on_press: Callable) -> Button:
|
||
var b := link("Edit as text", tip)
|
||
b.add_theme_color_override("font_color", Style.MUTED)
|
||
b.pressed.connect(on_press)
|
||
return b
|
||
|
||
|
||
static func find_tagged(root: Node, key: String) -> Control:
|
||
for c in root.get_children():
|
||
if c is Control and c.has_meta(KEY) and str(c.get_meta(KEY)) == key:
|
||
return c
|
||
var r := find_tagged(c, key)
|
||
if r:
|
||
return r
|
||
return null
|
||
|
||
|
||
# ---------- Typed text <-> rule tokens ----------
|
||
|
||
## The rule token for a value a person typed into a comparison or a list
|
||
## item: numbers, @names, quoted text, true/false and the `known` chain names
|
||
## stay as they are; anything else becomes quoted text ("Aslan").
|
||
static func value_token(typed: String, known: Array = []) -> String:
|
||
var s := typed.strip_edges()
|
||
if s == "":
|
||
return ""
|
||
if s.is_valid_float() or s.begins_with("@") or Builder.is_text_literal(s) or s == "true" or s == "false" or known.has(s):
|
||
return s
|
||
return Builder.quote(s)
|
||
|
||
|
||
## How a value token shows in a field: quoted text without its quotes.
|
||
static func shown_token(tok: String) -> String:
|
||
return Builder.unquote(tok) if Builder.is_text_literal(tok) else tok
|
||
|
||
|
||
## The expression for an amount a person typed: an expression stays as
|
||
## written; a single word or phrase that isn't one (and isn't a `known`
|
||
## name) becomes quoted text, so "Millbrook" sets text rather than reading a
|
||
## value called Millbrook.
|
||
static func amount_expr(typed: String, known: Array = []) -> String:
|
||
var s := typed.strip_edges()
|
||
if s == "":
|
||
return ""
|
||
if s.is_valid_float() or Builder.is_text_literal(s) or known.has(s):
|
||
return s
|
||
var toks := Builder.tokens(s)
|
||
if toks.size() == 1 and str(toks[0]["t"]) == "name" and not ["true", "false"].has(s):
|
||
return Builder.quote(s)
|
||
if toks.is_empty() or Expr.check(TemplateKit.probe(s)) != "":
|
||
# Words with spaces ("the old mill") read as text too.
|
||
if not s.contains("(") and not s.contains("@") and not s.contains("\""):
|
||
return Builder.quote(s)
|
||
return s
|
||
|
||
|
||
## "" when an expression a person typed is usable, else why not, in plain words.
|
||
static func expr_error(src: String, what: String = "This") -> String:
|
||
var s := TemplateKit.probe(src.strip_edges())
|
||
if s == "":
|
||
return ""
|
||
var err := Expr.check(s)
|
||
return "" if err == "" else "%s can't be worked out: %s" % [what, err]
|