- 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>
479 lines
18 KiB
GDScript
479 lines
18 KiB
GDScript
class_name MainCli
|
|
extends RefCounted
|
|
## Main's command-line flags, used for screenshots and checks (see
|
|
## docs/DESIGN.md §5). Only automation needs this file.
|
|
|
|
var m: Main
|
|
|
|
|
|
func _init(p_main: Main) -> void:
|
|
m = p_main
|
|
|
|
|
|
# ---------- Command line (used for screenshots and checks) ----------
|
|
# godot --path . -- --screenshot=/tmp/shot.png [--build] [--expand-all] [--collapse-all]
|
|
# [--roll=<table id>] [--fast] [--shot-delay=<seconds>]
|
|
# [--board=<collection.json>] (with --sample/--screenshot only: show that board instead)
|
|
# [--tray=variables] [--hover=<x>,<y>] [--details=<table id>] [--pipe=<pipe key>]
|
|
|
|
static func _arg_value(args: PackedStringArray, name: String) -> String:
|
|
for a in args:
|
|
if a.begins_with(name + "="):
|
|
return a.substr(name.length() + 1)
|
|
return ""
|
|
|
|
|
|
func _run_cli(args: PackedStringArray) -> void:
|
|
var board_file := _arg_value(args, "--board")
|
|
if board_file != "" and not m._saving_enabled:
|
|
await _load_board_file(board_file)
|
|
if args.has("--build"):
|
|
m.set_mode(Main.Mode.BUILD)
|
|
if args.has("--expand-all"):
|
|
m.set_all_collapsed(false)
|
|
if args.has("--collapse-all"):
|
|
m.set_all_collapsed(true)
|
|
_run_shell_cli(args)
|
|
_run_logic_cli(args)
|
|
await _run_group_cli(args)
|
|
var roll := _arg_value(args, "--roll")
|
|
if roll != "":
|
|
m.start_roll(roll, args.has("--fast"))
|
|
_run_cli_extras(args)
|
|
_run_test_board_cli(args)
|
|
var shot := _arg_value(args, "--screenshot")
|
|
if shot == "":
|
|
return
|
|
var delay := _arg_value(args, "--shot-delay")
|
|
if args.has("--fit") or _arg_value(args, "--view") != "":
|
|
# Let the tray settle first (it can change the board's size).
|
|
await m.get_tree().process_frame
|
|
await m.get_tree().process_frame
|
|
if args.has("--fit"):
|
|
m.board.fit()
|
|
_apply_view_flag(args)
|
|
await m.get_tree().create_timer(delay.to_float() if delay != "" else (2.5 if roll != "" else 0.8)).timeout
|
|
var img := m.get_viewport().get_texture().get_image()
|
|
img.save_png(shot)
|
|
print("Saved screenshot to ", shot)
|
|
m.get_tree().quit()
|
|
|
|
|
|
# ----- Extra screenshot flags -----
|
|
# --open-settings=<table id> open the settings dialog
|
|
# --demo-import=<file> show the import report for a file (nothing is saved)
|
|
# --demo-endless pipe the first two tables into each other
|
|
# --menu=board|collections pop up a top-bar menu
|
|
|
|
func _run_shell_cli(args: PackedStringArray) -> void:
|
|
var tid := _arg_value(args, "--open-settings")
|
|
if tid != "":
|
|
m.open_table_settings(tid)
|
|
var file := _arg_value(args, "--demo-import")
|
|
if file != "":
|
|
var text := FileAccess.get_file_as_string(file)
|
|
var r := IoMenu.parse_import(text, IoMenu.detect_format(file, text))
|
|
m._add_modal(IoMenu.report_dialog("Imported “%s”" % file.get_file().get_basename(), r))
|
|
if args.has("--demo-endless") and m.data.tables.size() >= 2:
|
|
var a := m.data.tables[0]
|
|
var b := m.data.tables[1]
|
|
for e in a.entries:
|
|
e.pipes = [b.id] as Array[String]
|
|
for e in b.entries:
|
|
e.pipes = [a.id] as Array[String]
|
|
m._rebuild_cards()
|
|
m.mark_dirty()
|
|
var menu := _arg_value(args, "--menu")
|
|
if menu == "board":
|
|
m.top_bar._board_menu.show_popup()
|
|
elif menu == "collections":
|
|
m.top_bar._switcher.show_popup()
|
|
elif menu == "add":
|
|
# The Build tools were only just shown; let them lay out first.
|
|
await m.get_tree().process_frame
|
|
await m.get_tree().process_frame
|
|
m.editing._add_menu.show_popup()
|
|
|
|
|
|
# ----- Logic-card screenshot flags -----
|
|
# --answers=<a>|<b>|… answer the first questions automatically, in order
|
|
# (an option index or -1 for a pick, a number, or text);
|
|
# later questions open the pop-up
|
|
# --demo-long=<n> add a table that pipes into itself n times, and fast-roll it
|
|
# --confirm-reset open the Variables tab's "Reset all…" confirmation
|
|
|
|
func _run_logic_cli(args: PackedStringArray) -> void:
|
|
var answers := _arg_value(args, "--answers")
|
|
if answers != "":
|
|
var queue: Array = Array(answers.split("|"))
|
|
m.prompt_hook = func(req: Dictionary) -> Variant:
|
|
if queue.is_empty():
|
|
return await m.rolling._ask_player(req)
|
|
var a := str(queue.pop_front())
|
|
return a if str(req.get("kind", "")) == "text" else a.to_int()
|
|
var n := _arg_value(args, "--demo-long").to_int()
|
|
if n > 0:
|
|
var head := RollTable.create(RollTable.KIND_ROLL, "long_start", "Long chain")
|
|
head.entries.resize(1)
|
|
head.entries[0].text = "Off we go"
|
|
head.entries[0].effects = "@k = 0"
|
|
head.entries[0].add_pipe("long_link")
|
|
var link := RollTable.create(RollTable.KIND_ROLL, "long_link", "Link")
|
|
link.entries.resize(1)
|
|
link.entries[0].text = "Link {@k}"
|
|
link.entries[0].effects = "@k += 1"
|
|
link.entries[0].add_pipe("long_link", "@k < %d" % n)
|
|
var y := 40.0
|
|
for t in m.data.tables:
|
|
y = maxf(y, t.position.y + 80.0)
|
|
head.position = Vector2(40, y + 400)
|
|
link.position = Vector2(360, y + 400)
|
|
m.data.tables.append(head)
|
|
m.data.tables.append(link)
|
|
m._rebuild_cards()
|
|
m.start_roll("long_start", true)
|
|
if args.has("--confirm-reset"):
|
|
m.tray.set_side(ResultTray.Side.VARIABLES)
|
|
m.tray._vars.confirm_reset_all()
|
|
|
|
|
|
# ----- Group and template screenshot flags -----
|
|
# --select=<id>,<id>… select cards (Build)
|
|
# --group-selected[=<name>] group the selection
|
|
# --collapse-groups / --expand-groups every group
|
|
# --toggle-group=<group id> collapse or expand one group
|
|
# --templates[=<search>] open the template list
|
|
# --insert-template=<id> insert a template (builtin:<file> or a user id)
|
|
# [--template-values=<name>=<value>|…]
|
|
# --insert-form=<id> open a template's parameter form
|
|
# --save-template=<table id or name> open the save dialog for one card
|
|
# --save-group-template=<group id|first|last>
|
|
# --group-menu open the first group's ⋯ menu (Build)
|
|
# --drag-demo hold a card over the first group's frame (Build)
|
|
# --clear-result empty the tray (the board stays lit)
|
|
# --view=<x>,<y>[,<zoom>] centre the view on a world point (after Fit)
|
|
|
|
func _run_group_cli(args: PackedStringArray) -> void:
|
|
var sel := _arg_value(args, "--select")
|
|
if sel != "":
|
|
m.editing.set_selection(Array(sel.split(",")))
|
|
for a in args:
|
|
if a == "--group-selected" or a.begins_with("--group-selected="):
|
|
var g := m.grouping.group_selection()
|
|
if g and a.contains("="):
|
|
g.name = a.get_slice("=", 1)
|
|
m.grouping._groups_changed()
|
|
if args.has("--collapse-groups") or args.has("--expand-groups"):
|
|
for g in m.data.groups:
|
|
g.collapsed = args.has("--collapse-groups")
|
|
m.grouping._groups_changed()
|
|
var tg := _arg_value(args, "--toggle-group")
|
|
if tg != "":
|
|
for gid in tg.split(","):
|
|
m.toggle_group(gid)
|
|
var ins := _arg_value(args, "--insert-template")
|
|
if ins != "":
|
|
var tpl := m.templating.templates().get_template(ins)
|
|
var values := {}
|
|
for pair in _arg_value(args, "--template-values").split("|", false):
|
|
values[pair.get_slice("=", 0)] = pair.substr(pair.find("=") + 1)
|
|
if tpl.is_empty():
|
|
push_warning("No template %s" % ins)
|
|
else:
|
|
m.templating.insert_template(tpl, values)
|
|
var form := _arg_value(args, "--insert-form")
|
|
if form != "" and not m.templating.templates().get_template(form).is_empty():
|
|
m.begin_template_insert(m.templating.templates().get_template(form))
|
|
var st := _arg_value(args, "--save-template")
|
|
if st != "":
|
|
m.save_template_from([Roller.resolve(m.data, st)], "card")
|
|
var sg := _arg_value(args, "--save-group-template")
|
|
if sg != "" and not m.data.groups.is_empty():
|
|
var gid: String = sg
|
|
if sg == "first":
|
|
gid = m.data.groups[0].id
|
|
elif sg == "last":
|
|
gid = m.data.groups[m.data.groups.size() - 1].id
|
|
m.save_template_from([], "group", [gid])
|
|
for a in args:
|
|
if a == "--templates" or a.begins_with("--templates="):
|
|
var p := m.templating.open_template_picker()
|
|
if a.contains("="):
|
|
p._search.text = a.get_slice("=", 1)
|
|
p.refresh()
|
|
var shown := p.shown_ids()
|
|
if not shown.is_empty():
|
|
p.select(str(shown[0]))
|
|
if args.has("--group-selected") or sel != "" or ins != "":
|
|
# Let new cards and frames lay out.
|
|
await m.get_tree().process_frame
|
|
await m.get_tree().process_frame
|
|
if args.has("--group-menu") and not m.data.groups.is_empty():
|
|
await m.get_tree().process_frame
|
|
var gn := m.group_node(m.data.groups[0].id)
|
|
if gn and gn.menu_button:
|
|
gn.menu_button.show_popup()
|
|
if args.has("--drag-demo") and not m.data.groups.is_empty():
|
|
# A card held over the first group's frame, to show the drop highlight.
|
|
var g := m.data.groups[0]
|
|
for t in m.data.tables:
|
|
if not g.has(t.id) and m.card_for(t.id) and m.card_for(t.id).visible:
|
|
var c := m.card_for(t.id)
|
|
c.get_parent().move_child(c, -1)
|
|
m.begin_card_drag(c)
|
|
m.drag_card(c, g.position + Vector2(g.size.x * 0.5, TableGroup.TITLE_H + 40))
|
|
break
|
|
|
|
|
|
func _apply_view_flag(args: PackedStringArray) -> void:
|
|
var v := _arg_value(args, "--view")
|
|
if v == "":
|
|
return
|
|
var parts := v.split(",")
|
|
if parts.size() >= 3:
|
|
m.board.set_zoom(parts[2].to_float())
|
|
var p := Vector2(parts[0].to_float(), parts[1].to_float())
|
|
m.board.world.position = m.board.size / 2.0 - p * m.board.zoom
|
|
|
|
|
|
# ---------- Command line extras ----------
|
|
|
|
## Shows a collection file instead of the sample (screenshots only; nothing is saved).
|
|
func _load_board_file(path: String) -> void:
|
|
var text := FileAccess.get_file_as_string(path)
|
|
if text == "" or not m.data.load_json(text):
|
|
push_warning("Couldn't load board file %s" % path)
|
|
return
|
|
m.rolling._token += 1
|
|
m.rolling.cancel_choice()
|
|
m.top_bar._switcher.refresh()
|
|
m._rebuild_cards()
|
|
m.rolling._seed_session()
|
|
await m.get_tree().process_frame
|
|
await m.get_tree().process_frame
|
|
m.board.fit()
|
|
|
|
|
|
func _run_cli_extras(args: PackedStringArray) -> void:
|
|
if args.has("--perf"):
|
|
_run_perf()
|
|
await _run_undo_cli(args)
|
|
_run_nav_cli(args)
|
|
if args.has("--clear-result"):
|
|
# Keeps the board lit but empties the tray (a tall sheet result can fill it).
|
|
m.current = {}
|
|
m.tray.render()
|
|
if _arg_value(args, "--tray") == "variables":
|
|
m.tray.set_side(ResultTray.Side.VARIABLES)
|
|
var pipe := _arg_value(args, "--pipe")
|
|
if pipe != "":
|
|
m.open_pipe_options(pipe, Vector2(m.size.x * 0.45, m.size.y * 0.3))
|
|
await _run_inspector_cli(args)
|
|
var hover := _arg_value(args, "--hover")
|
|
if hover.contains(","):
|
|
# Synthetic mouse move, to check hover pop-ups in screenshots.
|
|
await m.get_tree().create_timer(0.3).timeout
|
|
var ev := InputEventMouseMotion.new()
|
|
ev.position = Vector2(hover.get_slice(",", 0).to_float(), hover.get_slice(",", 1).to_float())
|
|
ev.global_position = ev.position
|
|
m.get_viewport().notification(Control.NOTIFICATION_VP_MOUSE_ENTER)
|
|
m.get_viewport().push_input(ev)
|
|
var det := _arg_value(args, "--details")
|
|
var card := m.card_for(det)
|
|
if card:
|
|
await m.get_tree().process_frame
|
|
m.details().show_table(card, det)
|
|
m.details().pin(card.get_global_rect().position + Vector2(60, 46))
|
|
|
|
|
|
# --test-board open the Test board dialog (shows the last report if any)
|
|
# --test-board-run=<runs> ... and start a test with that many runs (100, 500 or 1000)
|
|
func _run_test_board_cli(args: PackedStringArray) -> void:
|
|
var n := _arg_value(args, "--test-board-run")
|
|
if not args.has("--test-board") and n == "":
|
|
return
|
|
m.set_mode(Main.Mode.BUILD)
|
|
var d := TestBoardDialog.new(m)
|
|
m._add_modal(d)
|
|
if n != "":
|
|
d.runs = n.to_int()
|
|
d.run_test()
|
|
|
|
## --perf: times a reload of the current board and the frame cost while idle
|
|
## and while panning (vsync off), then prints the numbers and quits.
|
|
func _run_perf() -> void:
|
|
DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)
|
|
Engine.max_fps = 0
|
|
var text := m.data.to_json()
|
|
for i in 10:
|
|
await m.get_tree().process_frame
|
|
var t0 := Time.get_ticks_usec()
|
|
m.data.load_json(text)
|
|
var tj := Time.get_ticks_usec()
|
|
m._rebuild_cards()
|
|
var t1 := Time.get_ticks_usec()
|
|
m.rolling._seed_session()
|
|
var t2 := Time.get_ticks_usec()
|
|
await m.get_tree().process_frame
|
|
await m.get_tree().process_frame
|
|
var t3 := Time.get_ticks_usec()
|
|
m.board.fit()
|
|
print("PERF load: parse %.1f ms, rebuild_cards %.1f ms, seed %.1f ms, first two frames %.1f ms, total %.1f ms" % [
|
|
(tj - t0) / 1000.0, (t1 - tj) / 1000.0, (t2 - t1) / 1000.0, (t3 - t2) / 1000.0, (t3 - t0) / 1000.0])
|
|
print("PERF nodes: %d, table cards built: %d of %d" % [Performance.get_monitor(Performance.OBJECT_NODE_COUNT), m.cards.size(), m.data.tables.size()])
|
|
for i in 30:
|
|
await m.get_tree().process_frame
|
|
print("PERF idle (fit view): %s" % await _perf_frames(240, Vector2.ZERO))
|
|
m.board.set_zoom(0.6)
|
|
print("PERF pan (zoom 0.6): %s" % await _perf_frames(240, Vector2(-6, -2)))
|
|
m.board.set_zoom(1.0)
|
|
print("PERF idle (zoom 1): %s" % await _perf_frames(240, Vector2.ZERO))
|
|
print("PERF pan (zoom 1): %s" % await _perf_frames(240, Vector2(-6, -2)))
|
|
if OS.get_cmdline_user_args().has("--perf-layers"):
|
|
m.board.fit()
|
|
print("PERF idle (fit view again): %s" % await _perf_frames(120, Vector2.ZERO))
|
|
m.board.pipes.visible = false
|
|
print("PERF idle, no pipes: %s" % await _perf_frames(120, Vector2.ZERO))
|
|
m.board.ports.visible = false
|
|
print("PERF idle, no pipes or ports: %s" % await _perf_frames(120, Vector2.ZERO))
|
|
m.board.cards.visible = false
|
|
print("PERF idle, no cards either: %s" % await _perf_frames(120, Vector2.ZERO))
|
|
m.board.set_process(false)
|
|
m.board.groups.set_process(false)
|
|
print("PERF idle, board and group processing off: %s" % await _perf_frames(120, Vector2.ZERO))
|
|
m.get_tree().quit()
|
|
|
|
|
|
func _perf_frames(n: int, pan: Vector2) -> String:
|
|
var times: Array[float] = []
|
|
var last := Time.get_ticks_usec()
|
|
for i in n:
|
|
if pan != Vector2.ZERO:
|
|
m.board.world.position += pan
|
|
await m.get_tree().process_frame
|
|
var now := Time.get_ticks_usec()
|
|
times.append((now - last) / 1000.0)
|
|
last = now
|
|
times.sort()
|
|
var total := 0.0
|
|
for t in times:
|
|
total += t
|
|
return "mean %.2f ms, median %.2f ms, p95 %.2f ms" % [total / n, times[n / 2], times[int(n * 0.95)]]
|
|
|
|
|
|
# ----- Navigation screenshot flags -----
|
|
# --search=<text> open the search panel with that query
|
|
# --focus=<table id>[:<entry id>] jump to a card (and result)
|
|
# --minimap / --no-minimap show or hide the overview
|
|
# --pack Build ▸ Arrange ▸ Pack collapsed groups
|
|
# --perf print load and frame timings, then quit
|
|
|
|
func _run_nav_cli(args: PackedStringArray) -> void:
|
|
if args.has("--minimap"):
|
|
m.nav.set_minimap_visible(true)
|
|
if args.has("--no-minimap"):
|
|
m.nav.set_minimap_visible(false)
|
|
if args.has("--pack"):
|
|
await m.nav.pack_collapsed_groups()
|
|
var f := _arg_value(args, "--focus")
|
|
if f != "":
|
|
await m.get_tree().process_frame
|
|
m.focus_table(f.get_slice(":", 0), f.get_slice(":", 1) if f.contains(":") else "")
|
|
var q := _arg_value(args, "--search")
|
|
if q != "":
|
|
await m.get_tree().process_frame
|
|
var p := m.nav.open_search()
|
|
p.field.text = q
|
|
p.refresh()
|
|
|
|
|
|
# ----- Undo screenshot flags -----
|
|
# --undo-demo=<table id> delete that table (or the last one), then undo: the toast says so
|
|
# --undo-tooltip make an edit, then hover the Undo button so its tooltip shows
|
|
|
|
func _run_undo_cli(args: PackedStringArray) -> void:
|
|
var demo := _arg_value(args, "--undo-demo")
|
|
if args.has("--undo-demo") or demo != "":
|
|
m.set_mode(Main.Mode.BUILD)
|
|
var t: RollTable = m.data.get_table(demo) if demo != "" else (m.data.tables.back() if not m.data.tables.is_empty() else null)
|
|
if t:
|
|
m.delete_table(t.id)
|
|
await m.get_tree().process_frame
|
|
m.undo_redo.undo()
|
|
if args.has("--undo-tooltip"):
|
|
m.set_mode(Main.Mode.BUILD)
|
|
if not m.data.tables.is_empty():
|
|
m.delete_table(m.data.tables.back().id)
|
|
m.top_bar._toast.hide()
|
|
for i in 3:
|
|
await m.get_tree().process_frame
|
|
var at := m.undo_redo._undo_button.get_global_rect().get_center()
|
|
m.get_viewport().notification(Control.NOTIFICATION_VP_MOUSE_ENTER)
|
|
for i in 2:
|
|
var ev := InputEventMouseMotion.new()
|
|
ev.position = at + Vector2(i, 0)
|
|
ev.global_position = ev.position
|
|
m.get_viewport().push_input(ev)
|
|
await m.get_tree().process_frame
|
|
|
|
|
|
# --inspect=<table id>[:<entry id>][><target id>] select that card, result or pipe
|
|
# --rules=text show rules as text
|
|
# --pick-open open the inspector's first value picker
|
|
# --vars=name=value,… set board values (e.g. collection members)
|
|
# --light=<pipe key>,… light pipes and their results, as if rolled
|
|
# --window=<w>x<h> resize the window first (screenshots)
|
|
# --inspect-scroll=<px> scroll the inspector down
|
|
func _run_inspector_cli(args: PackedStringArray) -> void:
|
|
var win := _arg_value(args, "--window")
|
|
if win.contains("x"):
|
|
m.get_window().size = Vector2i(win.get_slice("x", 0).to_int(), win.get_slice("x", 1).to_int())
|
|
for i in 3:
|
|
await m.get_tree().process_frame
|
|
var rv := _arg_value(args, "--rules")
|
|
if rv != "":
|
|
m.set_rules_view(rv)
|
|
var vars := _arg_value(args, "--vars")
|
|
if vars != "":
|
|
for pair in vars.split(",", false):
|
|
var v := pair.get_slice("=", 1)
|
|
m.persistent_vars[pair.get_slice("=", 0)] = {"value": Expr.to_value(v) if Expr.is_number(v) else v, "source": ""}
|
|
m.local_state._variables_changed()
|
|
var light := _arg_value(args, "--light")
|
|
if light != "":
|
|
for k in light.split(",", false):
|
|
m.lit_pipes[k] = true
|
|
m.lit_entries[k.get_slice(">", 0)] = true
|
|
m._refresh_lit()
|
|
var sel := _arg_value(args, "--inspect")
|
|
if sel == "":
|
|
return
|
|
if m.mode != Main.Mode.BUILD:
|
|
m.set_mode(Main.Mode.BUILD)
|
|
if sel.contains(">"):
|
|
m.inspecting.select_pipe(sel)
|
|
elif sel.contains(":"):
|
|
m.select_result(sel.get_slice(":", 0), sel.get_slice(":", 1))
|
|
else:
|
|
m.select_card(sel)
|
|
var scroll := _arg_value(args, "--inspect-scroll")
|
|
if scroll != "":
|
|
for i in 3:
|
|
await m.get_tree().process_frame
|
|
m.inspector._scroll.scroll_vertical = scroll.to_int()
|
|
if args.has("--pick-open"):
|
|
for i in 3:
|
|
await m.get_tree().process_frame
|
|
var p := _first_picker(m.inspector)
|
|
if p:
|
|
p.open_menu()
|
|
|
|
|
|
func _first_picker(n: Node) -> ValuePicker:
|
|
for c in n.get_children():
|
|
if c is ValuePicker and (c as ValuePicker).is_visible_in_tree():
|
|
return c
|
|
var r := _first_picker(c)
|
|
if r:
|
|
return r
|
|
return null
|