diff --git a/core/book_page.gd b/core/book_page.gd new file mode 100644 index 0000000..610da3d --- /dev/null +++ b/core/book_page.gd @@ -0,0 +1,39 @@ +class_name BookPage +extends RefCounted +## A Page in the spread's Book: a reusable result (its text with any BBCode, +## its details tooltip and tags). Results linked to a page (TableEntry.page) +## show its text and details; editing the page updates every linked result +## (TowerBoard.sync_page). Saved in the spread file under "book". + +var id: String = "" +## The page's name in the Book's list ("" = its text without formatting). +var name: String = "" +var text: String = "" +var details: String = "" +var tags: Array[String] = [] + + +func label() -> String: + return name if name.strip_edges() != "" else Formatter.strip_tags(text).strip_edges() + + +func to_dict() -> Dictionary: + var d := {"id": id, "text": text} + if name != "": + d["name"] = name + if details != "": + d["details"] = details + if not tags.is_empty(): + d["tags"] = tags.duplicate() + return d + + +static func from_dict(d: Dictionary) -> BookPage: + var p := BookPage.new() + p.id = str(d.get("id", "")) + p.name = str(d.get("name", "")) + p.text = str(d.get("text", "")) + p.details = str(d.get("details", "")) + for t: Variant in d.get("tags", []): + p.tags.append(str(t)) + return p diff --git a/core/book_page.gd.uid b/core/book_page.gd.uid new file mode 100644 index 0000000..65efd9c --- /dev/null +++ b/core/book_page.gd.uid @@ -0,0 +1 @@ +uid://cs20opv3eneki diff --git a/core/table_entry.gd b/core/table_entry.gd index 4c42bc2..8c873af 100644 --- a/core/table_entry.gd +++ b/core/table_entry.gd @@ -36,6 +36,9 @@ var pipe_otherwise: Dictionary = {} var pipe_with: Dictionary = {} ## target table id -> the thread's own colour "#hex" (missing = the theme's). var pipe_color: Dictionary = {} +## The Book page this result is linked to ("" = none). Its text and details +## follow the page (TowerBoard.sync_page). +var page: String = "" ## Router cards: this case's condition. "" is the Otherwise case (always ## matches). Ignored on other kinds. var when: String = "" @@ -250,6 +253,8 @@ func to_dict() -> Dictionary: d["pipes"] = list if has_range: d["range"] = [range_lo, range_hi] + if page != "": + d["page"] = page return d @@ -258,6 +263,7 @@ static func from_dict(d: Dictionary) -> TableEntry: e.details = str(d.get("details", "")) e.weight = maxi(0, int(d.get("weight", 1))) e.effects = str(d.get("effects", "")) + e.page = str(d.get("page", "")) e.when = str(d.get("when", "")).strip_edges() if d.get("when") != null else "" e.show_if = str(d.get("show_if", "")).strip_edges() if d.get("show_if") != null else "" for p: Variant in d.get("pipes", []): diff --git a/core/tower_board.gd b/core/tower_board.gd index 65ae642..22b5ffc 100644 --- a/core/tower_board.gd +++ b/core/tower_board.gd @@ -17,6 +17,8 @@ var tables: Array[RollTable] = [] ## Group cards (frames around blocks of tables). Optional; a table is in at ## most one group. See TableGroup. var groups: Array[TableGroup] = [] +## The Book: reusable results (Pages) for this spread. +var book: Array[BookPage] = [] static func new_id(prefix: String) -> String: @@ -72,6 +74,87 @@ func is_hidden(table_id: String) -> bool: return false +# ----- The Book (reusable results) ----- + +func book_list() -> Array: + var out: Array = [] + for p in book: + out.append(p.to_dict()) + return out + + +func load_book(src: Variant) -> void: + var pages: Array[BookPage] = [] + if src is Array: + for v: Variant in src: + if v is Dictionary: + var p := BookPage.from_dict(v) + if p.id == "": + p.id = new_id("p") + pages.append(p) + book = pages + + +func get_page(pid: String) -> BookPage: + for p in book: + if p.id == pid: + return p + return null + + +func add_page(text: String, details: String = "") -> BookPage: + var p := BookPage.new() + p.id = new_id("p") + p.text = text + p.details = details + book.append(p) + return p + + +## Deletes a page; results linked to it keep their text and become ordinary. +func remove_page(pid: String) -> void: + for t in tables: + for e in t.entries: + if e.page == pid: + e.page = "" + book = book.filter(func(p: BookPage) -> bool: return p.id != pid) + + +## Links a result to a page and copies the page's text and details onto it. +func link_page(e: TableEntry, pid: String) -> void: + var p := get_page(pid) + if p: + e.page = pid + e.text = p.text + e.details = p.details + + +## Copies a page onto every result linked to it. Returns the tables touched. +func sync_page(pid: String) -> Array[String]: + var p := get_page(pid) + var touched: Array[String] = [] + if p == null: + return touched + for t in tables: + for e in t.entries: + if e.page == pid: + e.text = p.text + e.details = p.details + if not touched.has(t.id): + touched.append(t.id) + return touched + + +## How many results are linked to a page. +func page_uses(pid: String) -> int: + var n := 0 + for t in tables: + for e in t.entries: + if e.page == pid: + n += 1 + return n + + # ----- Nested groups ----- func child_groups(gid: String) -> Array[TableGroup]: @@ -282,6 +365,8 @@ func to_dict() -> Dictionary: for g in groups: gl.append(g.to_dict()) d["groups"] = gl + if not book.is_empty(): + d["book"] = book_list() return d @@ -316,6 +401,7 @@ func load_dict(src: Dictionary) -> bool: loaded_groups.append(TableGroup.from_dict(gd)) groups = loaded_groups clean_groups() + load_book(d.get("book", [])) if str(d.get("id", "")) != "": id = str(d["id"]) if str(d.get("name", "")) != "": diff --git a/tests/test_book.gd b/tests/test_book.gd new file mode 100644 index 0000000..7838518 --- /dev/null +++ b/tests/test_book.gd @@ -0,0 +1,82 @@ +extends SceneTree +## The Book: pages saved from results, linked results following a page's edits, +## dropping a page onto a result or a card, unlinking, saving and undo. +## Run: godot --headless --path . -s res://tests/test_book.gd -- --sample + +var _failures := 0 +var main: Main + + +func _init() -> void: + if not OS.get_cmdline_user_args().has("--sample"): + print("Run with -- --sample so the test can't touch saved data.") + quit(2) + return + main = load("res://ui/main.tscn").instantiate() + root.add_child(main) + await _frames(5) + main.set_mode(Main.Mode.BUILD) + await _frames() + await _test_book() + print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) + quit(1 if _failures else 0) + + +func _test_book() -> void: + var weapons := main.data.get_table("weapons") + var sword := weapons.entries[1] + var p := main.book.save_result(weapons, sword) + await _frames() + _check(main.book.visible and sword.page == p.id and p.text == sword.text, "Save to the Book makes a page and links the result") + main.book.editor.show_page(p) + p.text = "[b]The Sword of Holy Light[/b]" + p.details = "Glows near the undead." + main.book.editor._changed() + _check(sword.text == p.text and sword.details == p.details, "editing the page changes the linked result") + var loot := main.data.get_table("loot") + var target := loot.entries[0] + await _frames(2) + var row: Control = main.card_for("loot").rows[target.id] + _check(main.book.drop_page(p.id, row.get_global_rect().get_center()), "dropping a page on a result lands") + _check(target.page == p.id and target.text == p.text, "and links that result") + var n := loot.entries.size() + var card := main.card_for("loot") + main.book.drop_page(p.id, card.get_global_rect().position + Vector2(20, 12)) + _check(loot.entries.size() == n + 1 and loot.entries[n].page == p.id, "dropping on the card's header adds a new linked result") + p.text = "Holy Sword" + main.book.editor._changed() + _check(target.text == "Holy Sword" and loot.entries[n].text == "Holy Sword", "one edit reaches every copy") + _check(main.data.page_uses(p.id) == 3, "the page counts its uses") + main.select_result("loot", target.id) + await _frames() + main.inspector.set_entry_text("A plain sword") + _check(target.page == "", "editing a result's own text unlinks it") + var d := main.data.to_dict() + var back := TowerBoard.new() + back.load_dict(d) + _check(back.book.size() == 1 and back.get_page(p.id).text == "Holy Sword", "the Book is saved in the spread") + main.book.list.clear() + main.book.refresh() + main.book._search.text = "nothing like it" + main.book.refresh() + _check(main.book.list.item_count == 0, "search filters pages") + main.book._search.text = "" + main.undo_redo._begin_edit() + main.data.remove_page(p.id) + main.mark_dirty() + main.undo_redo._end_edit() + _check(sword.page == "" and main.data.book.is_empty(), "deleting a page leaves its results as plain text") + main.undo_redo.undo() + await _frames() + _check(main.data.book.size() == 1, "undo brings the page back") + + +func _frames(n: int = 2) -> void: + for i in n: + await process_frame + + +func _check(ok: bool, what: String) -> void: + if not ok: + _failures += 1 + print("FAIL ", what) diff --git a/tests/test_book.gd.uid b/tests/test_book.gd.uid new file mode 100644 index 0000000..9321372 --- /dev/null +++ b/tests/test_book.gd.uid @@ -0,0 +1 @@ +uid://cadxlxxgym4mh diff --git a/ui/actions.gd b/ui/actions.gd index a639309..40226e3 100644 --- a/ui/actions.gd +++ b/ui/actions.gd @@ -166,6 +166,8 @@ static func background(m: Main, at: Vector2) -> Array: list.append({"text": "Fit spread", "do": m.board.fit}) list.append({"text": "Hide the list" if m.list_view.visible else "List view", "do": m.list_view.toggle, "tip": "The cards as a list with folders"}) + list.append({"text": "Close the Book" if m.book.visible else "Book", "do": m.book.toggle, + "tip": "Reusable results (Pages) to drag onto tables"}) list.append({"sep": ""}) list.append({"text": "Theme…", "do": func() -> void: m.add_child(ThemeDialog.new(m))}) if m.mobile: diff --git a/ui/book_panel.gd b/ui/book_panel.gd new file mode 100644 index 0000000..e826e5e --- /dev/null +++ b/ui/book_panel.gd @@ -0,0 +1,187 @@ +class_name BookPanel +extends PanelContainer +## The Book: the spread's reusable results (Pages), docked beside the spread. +## A searchable list (name, text and tags) above the page editor. Drag a page +## onto a result to link it there, or onto a card to add it as a new result. +## Open from View ▾, ☰ or right-clicking the spread; × closes it. + +const WIDTH := 340.0 +const SETTING := "book" + +var m: Main +var list: ItemList +var editor: PageEditor +var splitter: PanelSplitter +var _search: LineEdit +var _ids: Array[String] = [] +var _rev := -1 +var _dragging := "" + + +func setup(p_main: Main) -> void: + m = p_main + add_theme_stylebox_override("panel", Style.panel_box([10, 10, 10, 10])) + var col := VBoxContainer.new() + col.add_theme_constant_override("separation", 8) + add_child(col) + var head := HBoxContainer.new() + head.add_theme_constant_override("separation", 6) + col.add_child(head) + var title := Style.make_label("Book", Style.display(), 20, Style.ON_BG) + title.size_flags_horizontal = SIZE_EXPAND_FILL + head.add_child(title) + var add := Style.bar_button("+ Page") + add.tooltip_text = "A new page: a result you can reuse on any table" + add.pressed.connect(new_page) + head.add_child(add) + var close := Style.bar_button("×") + close.tooltip_text = "Close the Book" + close.pressed.connect(func() -> void: set_shown(false)) + head.add_child(close) + _search = LineEdit.new() + _search.placeholder_text = "Search pages and tags" + _search.clear_button_enabled = true + ModalCard.style_line_edit(_search) + _search.text_changed.connect(func(_s: String) -> void: refresh()) + col.add_child(_search) + list = ItemList.new() + list.custom_minimum_size = Vector2(0, 170) + list.add_theme_stylebox_override("panel", Style.box(Style.CARD, 6, [6, 6, 6, 6])) + list.add_theme_color_override("font_color", Style.INK) + list.add_theme_color_override("font_selected_color", Style.INK) + list.add_theme_stylebox_override("selected", Style.box(Style.LIT, 4)) + list.add_theme_stylebox_override("selected_focus", Style.box(Style.LIT, 4)) + list.item_selected.connect(func(i: int) -> void: editor.show_page(m.data.get_page(_ids[i]))) + list.set_drag_forwarding(_drag, Callable(), Callable()) + col.add_child(list) + var scroll := ScrollContainer.new() + scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED + scroll.size_flags_vertical = SIZE_EXPAND_FILL + col.add_child(scroll) + editor = PageEditor.new(m) + editor.size_flags_horizontal = SIZE_EXPAND_FILL + scroll.add_child(editor) + editor.show_page(null) + + +func _process(_delta: float) -> void: + if visible and m.data and m.edit_rev != _rev: + _rev = m.edit_rev + refresh() + + +## Refills the list (search applied), keeping the selection. +func refresh() -> void: + var keep := editor.page.id if editor.page else "" + var q := _search.text.strip_edges().to_lower() + list.clear() + _ids.clear() + for p in m.data.book: + var hay := (p.label() + " " + Formatter.strip_tags(p.text) + " " + " ".join(p.tags)).to_lower() + if q != "" and not hay.contains(q): + continue + var i := list.add_item(p.label()) + list.set_item_tooltip(i, ("Tags: " + ", ".join(p.tags) + "\n" if not p.tags.is_empty() else "") + "Drag onto a result to use it") + _ids.append(p.id) + if p.id == keep: + list.select(i) + if keep != "" and m.data.get_page(keep) == null: + editor.show_page(null) + + +func set_width(px: float) -> void: + custom_minimum_size.x = px + + +func set_shown(on: bool) -> void: + visible = on + if splitter: + splitter.visible = on + _rev = -1 + if m.state: + m.state.set_setting(SETTING, on) + m.state.save() + + +func toggle() -> void: + set_shown(not visible) + + +func new_page() -> BookPage: + m.undo_redo._begin_edit() + var p := m.data.add_page("New page") + m.mark_dirty() + m.undo_redo._end_edit() + if not visible: + set_shown(true) + refresh() + select_page(p.id) + return p + + +## Adds a result's text and details to the Book and links the result to it. +func save_result(t: RollTable, e: TableEntry) -> BookPage: + m.undo_redo._begin_edit() + var p := m.data.add_page(e.text, e.details) + e.page = p.id + m.inspector_edited(t.id) + m.mark_dirty() + m.undo_redo._end_edit() + set_shown(true) + refresh() + select_page(p.id) + m.toast("Saved “%s” to the Book" % p.label()) + return p + + +func select_page(pid: String) -> void: + var i := _ids.find(pid) + if i >= 0: + list.select(i) + editor.show_page(m.data.get_page(pid)) + + +func _drag(at: Vector2) -> Variant: + var i := list.get_item_at_position(at, true) + if i < 0: + return null + _dragging = _ids[i] + var l := Style.make_label(list.get_item_text(i), Style.body_bold(), 15, Style.INK) + var pc := PanelContainer.new() + pc.add_theme_stylebox_override("panel", Style.box(Style.LIT, 6, [8, 4, 8, 4])) + pc.add_child(l) + list.set_drag_preview(pc) + return {"book_page": _dragging} + + +## Nothing took the drop (cards' text fields don't), so the Book places it. +func _notification(what: int) -> void: + if what == NOTIFICATION_DRAG_END and _dragging != "": + var pid := _dragging + _dragging = "" + if not get_viewport().gui_is_drag_successful(): + drop_page(pid, get_global_mouse_position()) + + +## Drops a page at a screen point: onto a result links it there; onto a card +## (not a result) adds it as a new result. True when it landed somewhere. +func drop_page(pid: String, at: Vector2) -> bool: + for c: TableCard in m.cards.values(): + if not c.is_visible_in_tree() or not c.get_global_rect().has_point(at) or c.table.kind == RollTable.KIND_VARIABLE: + continue + m.undo_redo._begin_edit() + var target: TableEntry = null + for eid: String in c.rows: + var row: Control = c.rows[eid] + if is_instance_valid(row) and row.get_global_rect().has_point(at): + target = c.table.find_entry(eid) + if target == null: + m.editing.add_entry(c.table.id) + target = c.table.entries[c.table.entries.size() - 1] + m.data.link_page(target, pid) + m.inspector_edited(c.table.id) + m.mark_dirty() + m.undo_redo._end_edit() + m.toast("“%s” now shows on %s" % [m.data.get_page(pid).label(), Formatter.strip_tags(c.table.name)]) + return true + return false diff --git a/ui/book_panel.gd.uid b/ui/book_panel.gd.uid new file mode 100644 index 0000000..cd349c2 --- /dev/null +++ b/ui/book_panel.gd.uid @@ -0,0 +1 @@ +uid://c4jvmoy7non2p diff --git a/ui/edit_history.gd b/ui/edit_history.gd index cc5ef57..8943102 100644 --- a/ui/edit_history.gd +++ b/ui/edit_history.gd @@ -184,7 +184,12 @@ static func capture(board: TowerBoard, prev: Dictionary = {}) -> Dictionary: if g.collapsed: collapsed["g:" + g.id] = true n += (ids.size() + gids.size()) * 16 - return {"ids": ids, "tables": tables, "gids": gids, "groups": groups, "collapsed": collapsed, "bytes": n} + var book := JSON.stringify(board.book_list()) + if prev.get("book", "") == book: + book = prev["book"] + else: + n += book.length() + return {"ids": ids, "tables": tables, "gids": gids, "groups": groups, "collapsed": collapsed, "book": book, "bytes": n} ## A snapshot's size if it shared nothing. @@ -239,7 +244,8 @@ static func restore(board: TowerBoard, snap: Dictionary) -> Dictionary: d["collapsed"] = true collapsed[k] = true groups.append(d) - board.load_dict({"format_version": TowerBoard.FORMAT_VERSION, "tables": tables, "groups": groups}) + board.load_dict({"format_version": TowerBoard.FORMAT_VERSION, "tables": tables, "groups": groups, + "book": JSON.parse_string(str(snap.get("book", "[]")))}) var out := snap.duplicate() out["collapsed"] = collapsed return out @@ -255,6 +261,8 @@ static func restore(board: TowerBoard, snap: Dictionary) -> Dictionary: static func patch(board: TowerBoard, snap: Dictionary, from: Dictionary) -> Dictionary: if from.is_empty(): return {} + if str(snap.get("book", "[]")) != str(from.get("book", "[]")): + board.load_book(JSON.parse_string(str(snap.get("book", "[]")))) var old_t := _strings_by_id(from, "ids", "tables") var old_g := _strings_by_id(from, "gids", "groups") var was: Dictionary = snap["collapsed"] @@ -337,6 +345,7 @@ static func diff(a: Dictionary, b: Dictionary) -> Dictionary: ["x", "y"], out, "added", "removed", "changed", "moved") _diff_lists(_strings_by_id(a, "gids", "groups"), _strings_by_id(b, "gids", "groups"), ["x", "y", "w", "h"], out, "gadded", "gremoved", "gchanged", "gmoved") + out["book"] = str(a.get("book", "[]")) != str(b.get("book", "[]")) var gb := _strings_by_id(b, "gids", "groups") for gid: String in out["gmoved"]: var d: Variant = JSON.parse_string(gb[gid]) @@ -363,6 +372,8 @@ static func _diff_lists(ma: Dictionary, mb: Dictionary, geometry: Array, out: Di static func is_empty_diff(d: Dictionary) -> bool: + if d.get("book", false): + return false for k in ["added", "removed", "changed", "moved", "gadded", "gremoved", "gchanged", "gmoved_empty"]: if not (d[k] as Array).is_empty(): return false @@ -402,6 +413,8 @@ static func coalesce_key(a: Dictionary, b: Dictionary, d: Dictionary) -> String: static func label_for(a: Dictionary, b: Dictionary, d: Dictionary = {}) -> String: if d.is_empty(): d = diff(a, b) + if d.get("book", false) and (d["changed"] as Array).is_empty() and (d["added"] as Array).is_empty(): + return "Edit the Book" var added: Array = d["added"] var removed: Array = d["removed"] var gadded: Array = d["gadded"] diff --git a/ui/inspector.gd b/ui/inspector.gd index 8549367..40174ea 100644 --- a/ui/inspector.gd +++ b/ui/inspector.gd @@ -236,6 +236,7 @@ func set_entry_text(s: String) -> void: var e := selected_entry() if e and e.text != s: e.text = s + e.page = "" wrote(selected_table()) @@ -243,6 +244,7 @@ func set_entry_details(s: String) -> void: var e := selected_entry() if e and e.details != s: e.details = s + e.page = "" wrote(selected_table()) @@ -471,6 +473,28 @@ static func _title_of(s: String, fallback: String) -> String: ## A link back to the card the result or pipe belongs to. +## "From the Book page X · unlink" for a linked result, else "Save to the Book". +func _book_line(t: RollTable, e: TableEntry) -> Control: + var p := main.data.get_page(e.page) + if p == null: + var save := InspectorKit.link("Save to the Book", "Keep this result as a Page to reuse on other tables") + save.pressed.connect(func() -> void: main.book.save_result(t, e)) + return save + var row := InspectorKit.hbox(8) + var open := InspectorKit.link("§ From the Book: %s" % p.label(), "Edit the page; every result using it changes") + open.pressed.connect(func() -> void: + main.book.set_shown(true) + main.book.refresh() + main.book.select_page(p.id)) + row.add_child(open) + var unlink := InspectorKit.link("Unlink", "Keep the text here but stop following the page") + unlink.pressed.connect(func() -> void: + e.page = "" + wrote(t, false, true)) + row.add_child(unlink) + return row + + func _card_link(t: RollTable) -> Button: var b := InspectorKit.link("On %s · edit the card" % _title_of(t.name, "this card"), "Show the card's settings") b.add_theme_color_override("font_color", Style.MUTED) @@ -495,6 +519,7 @@ func _result_section(t: RollTable, e: TableEntry) -> void: det.text_changed.connect(func() -> void: set_entry_details(det.text)) fields["details"] = det _content.add_child(InspectorKit.field("Details", det)) + _content.add_child(_book_line(t, e)) if t.kind == RollTable.KIND_CHOICE and t.choice == "pick" and not (t.is_dynamic() and t.entries.find(e) == 0): show_if_builder = ConditionBuilder.new() diff --git a/ui/main.gd b/ui/main.gd index 870762a..64f6f08 100644 --- a/ui/main.gd +++ b/ui/main.gd @@ -47,6 +47,8 @@ var selecting: MainSelection var list_view: ListView ## The phone layout, when it is on (MobileLayout.wanted). var mobile: MobileLayout +## The Book panel (reusable results). +var book: BookPanel var _save_timer: Timer var _saving_enabled := true @@ -192,6 +194,17 @@ func _build_ui() -> void: board.setup(self) board.size_flags_horizontal = SIZE_EXPAND_FILL middle.add_child(board) + book = BookPanel.new() + book.setup(self) + var bside := PanelSplitter.new(self, false, "book_width", BookPanel.WIDTH, 260.0) + bside.size_changed.connect(book.set_width) + book.splitter = bside + book.set_width(bside.size_px) + middle.add_child(bside) + middle.add_child(book) + var book_on := state != null and bool(state.get_setting(BookPanel.SETTING, false)) + book.visible = book_on + bside.visible = book_on inspector = Inspector.new() inspector.setup(self) inspector.visible = false diff --git a/ui/main_cli.gd b/ui/main_cli.gd index 1d003ec..6bdc2a1 100644 --- a/ui/main_cli.gd +++ b/ui/main_cli.gd @@ -384,13 +384,15 @@ func _perf_frames(n: int, pan: Vector2) -> String: # --minimap / --no-minimap show or hide the overview # --pack Build ▸ Arrange ▸ Pack collapsed groups # --perf print load and frame timings, then quit -# --list show List view +# --list, --book show List view, the Book 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("--book"): + m.book.set_shown(true) if args.has("--list"): m.list_view.set_shown(true) if args.has("--pack"): diff --git a/ui/page_editor.gd b/ui/page_editor.gd new file mode 100644 index 0000000..aded1d0 --- /dev/null +++ b/ui/page_editor.gd @@ -0,0 +1,110 @@ +class_name PageEditor +extends VBoxContainer +## The Book's editor for one Page: its name in the list, the result text (with +## BBCode and a live preview), the details tooltip and tags. Every change goes +## to all results linked to the page at once (TowerBoard.sync_page). + +var m: Main +var page: BookPage +var _preview: RichTextLabel +var _uses: Label + + +func _init(p_main: Main) -> void: + m = p_main + add_theme_constant_override("separation", 8) + + +func show_page(p: BookPage) -> void: + page = p + for c in get_children(): + c.queue_free() + if p == null: + add_child(_note("Pick a page to edit it, or press + Page.")) + return + var name_edit := _line(p.name, p.label(), func(s: String) -> void: p.name = s.strip_edges()) + add_child(_field("Name in the Book", name_edit)) + var text := TextEdit.new() + text.text = p.text + text.placeholder_text = "The result, e.g. [b][color=gold]The Sword of Holy Light[/color][/b]" + text.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY + text.custom_minimum_size = Vector2(0, 70) + ModalCard.style_text_edit(text) + text.text_changed.connect(func() -> void: + p.text = text.text + _preview.text = RichText.decorate(p.text) + _changed()) + add_child(_field("Result text", text)) + _preview = RichText.label(RichText.decorate(p.text), 16, Style.INK, false, true) + var pv := PanelContainer.new() + pv.add_theme_stylebox_override("panel", Style.box(Style.CARD, 6, [10, 6, 10, 6])) + pv.add_child(_preview) + add_child(pv) + var det := TextEdit.new() + det.text = p.details + det.placeholder_text = "Shown when the result is hovered (optional)" + det.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY + det.custom_minimum_size = Vector2(0, 60) + ModalCard.style_text_edit(det) + det.text_changed.connect(func() -> void: + p.details = det.text + _changed()) + add_child(_field("Details", det)) + var tags := _line(", ".join(p.tags), "e.g. weapon, holy", func(s: String) -> void: + var list: Array[String] = [] + for t in s.split(","): + if t.strip_edges() != "": + list.append(t.strip_edges()) + p.tags = list) + add_child(_field("Tags", tags)) + _uses = _note("") + add_child(_uses) + _refresh_uses() + var del := Style.bar_button("Delete page") + del.tooltip_text = "Results using it keep their text and stop following the page" + del.size_flags_horizontal = SIZE_SHRINK_BEGIN + del.pressed.connect(func() -> void: + m.undo_redo._begin_edit() + m.data.remove_page(p.id) + m.mark_dirty() + m.undo_redo._end_edit() + show_page(null)) + add_child(del) + + +## Copies the page onto its results and redraws their cards. +func _changed() -> void: + for tid in m.data.sync_page(page.id): + m.inspector_edited(tid) + m.mark_dirty() + _refresh_uses() + + +func _refresh_uses() -> void: + var n := m.data.page_uses(page.id) + _uses.text = "Used by %d %s. Drag the page from the list onto a result to use it there." % [n, "result" if n == 1 else "results"] + + +func _line(text: String, placeholder: String, on_change: Callable) -> LineEdit: + var le := LineEdit.new() + le.text = text + le.placeholder_text = placeholder + ModalCard.style_line_edit(le) + le.text_changed.connect(func(s: String) -> void: + on_change.call(s) + _changed()) + return le + + +static func _field(heading: String, c: Control) -> Control: + var v := VBoxContainer.new() + v.add_theme_constant_override("separation", 3) + v.add_child(Style.make_label(heading.to_upper(), Style.caps(), 11, Style.ON_BG_MUTED)) + v.add_child(c) + return v + + +static func _note(text: String) -> Label: + var l := Style.make_label(text, Style.body(), 13, Style.ON_BG_MUTED) + l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + return l diff --git a/ui/page_editor.gd.uid b/ui/page_editor.gd.uid new file mode 100644 index 0000000..7ada237 --- /dev/null +++ b/ui/page_editor.gd.uid @@ -0,0 +1 @@ +uid://dvunkobnpexq diff --git a/ui/table_card_editing.gd b/ui/table_card_editing.gd index 9904084..481d351 100644 --- a/ui/table_card_editing.gd +++ b/ui/table_card_editing.gd @@ -35,6 +35,7 @@ func text_edit(e: TableEntry, index: int, chosen: bool) -> LineEdit: edit.placeholder_text = "Result text" edit.text_changed.connect(func(s: String) -> void: e.text = s + e.page = "" card.main.mark_dirty() ) edit.focus_entered.connect(func() -> void: card.select_entry(e.id)) diff --git a/ui/table_card_rows.gd b/ui/table_card_rows.gd index fac66af..743108f 100644 --- a/ui/table_card_rows.gd +++ b/ui/table_card_rows.gd @@ -157,6 +157,15 @@ func make_row(e: TableEntry, index: int, build: bool, ranged: bool, numw: float) num.tooltip_text = "Only a modifier reaches this: the dice alone roll %s–%s" % [table.fmt(nat.x), table.fmt(nat.y)] num.mouse_filter = Control.MOUSE_FILTER_PASS + if e.page != "": + # A small § says the result follows a Book page. + var pg := main.data.get_page(e.page) + var mark := Style.make_label("§", Style.body_bold(true), 13, Style.TRIM) + mark.size_flags_vertical = Control.SIZE_SHRINK_CENTER + mark.mouse_filter = Control.MOUSE_FILTER_PASS + mark.tooltip_text = "From the Book: %s" % (pg.label() if pg else "a deleted page") + h.add_child(mark) + h.move_child(mark, mini(1, h.get_child_count() - 1)) register(e, row, num, text, index) if delete_button: edit.reveal_on_hover(e.id, row, delete_button) @@ -329,6 +338,7 @@ func make_sheet_row(e: TableEntry, build: bool) -> Control: te.tooltip_text = "The sheet's template: BBCode with {@var}, {@prefix_*} and {= expr}" te.text_changed.connect(func() -> void: e.text = te.text + e.page = "" main.mark_dirty() ) te.focus_entered.connect(func() -> void: card.select_entry(e.id)) diff --git a/ui/view_menu.gd b/ui/view_menu.gd index 023f8ea..6fc8fb8 100644 --- a/ui/view_menu.gd +++ b/ui/view_menu.gd @@ -11,6 +11,7 @@ const ID_STRIPES := 3 const ID_MINIMAP := 4 const ID_THEME := 5 const ID_LIST := 6 +const ID_BOOK := 7 var m: Main @@ -32,6 +33,8 @@ func setup(p_main: Main) -> void: pm.add_check_item("Striped rows", ID_STRIPES) pm.add_check_item("Minimap (M)", ID_MINIMAP) pm.add_check_item("List view", ID_LIST) + pm.add_check_item("Book", ID_BOOK) + pm.set_item_tooltip(pm.get_item_index(ID_BOOK), "Reusable results (Pages) to drag onto tables") pm.set_item_tooltip(pm.get_item_index(ID_LIST), "The cards as a list with folders, beside the spread") pm.add_separator() pm.add_item("Theme…", ID_THEME) @@ -49,6 +52,7 @@ func sync() -> void: pm.set_item_checked(pm.get_item_index(ID_STRIPES), m.local_state.striped_rows()) pm.set_item_checked(pm.get_item_index(ID_MINIMAP), m.board.minimap.visible) pm.set_item_checked(pm.get_item_index(ID_LIST), m.list_view.visible) + pm.set_item_checked(pm.get_item_index(ID_BOOK), m.book.visible) func collapse_label() -> String: @@ -69,5 +73,7 @@ func activate(id: int) -> void: m.nav.set_minimap_visible(not m.board.minimap.visible, true) ID_LIST: m.list_view.toggle() + ID_BOOK: + m.book.toggle() ID_THEME: m.add_child(ThemeDialog.new(m))