diff --git a/tests/test_actions.gd b/tests/test_actions.gd new file mode 100644 index 0000000..790dc5c --- /dev/null +++ b/tests/test_actions.gd @@ -0,0 +1,88 @@ +extends SceneTree +## The shared action menus (Actions): what the card, thread, group and spread +## menus offer in Play and Build, the ☰ menu, running an action, and placing +## a new card where the spread was right-clicked. +## Run: godot --headless --path . -s res://tests/test_actions.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) + await _test_lists() + await _test_popup_and_run() + print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) + quit(1 if _failures else 0) + + +func _texts(list: Array) -> Array[String]: + var out: Array[String] = [] + for a: Dictionary in list: + if a.has("text"): + out.append(str(a["text"])) + return out + + +func _test_lists() -> void: + var t := main.data.get_table("loot") + var play := _texts(Actions.card(main, t)) + _check(play.has("Roll") and not play.has("Delete table"), "Play: a card can be rolled, not deleted: %s" % str(play)) + var bg := _texts(Actions.background(main, Vector2.ZERO)) + _check(bg.has("Search… Ctrl+F") and bg.has("Shuffle all decks") and not bg.has("New card"), "Play spread menu: %s" % str(bg)) + main.set_mode(Main.Mode.BUILD) + await _frames() + var build := _texts(Actions.card(main, t)) + _check(build.has("Table settings…") and build.has("Colour") and build.has("Delete table"), "Build card menu: %s" % str(build)) + bg = _texts(Actions.background(main, Vector2.ZERO)) + _check(bg.has("New card") and bg.has("Group selected cards Ctrl+G"), "Build spread menu: %s" % str(bg)) + var key := "" + for e in t.entries: + if not e.pipes.is_empty(): + key = Roller.pipe_key(t.id, e.id, e.pipes[0]) + break + var th := _texts(Actions.thread(main, key)) + _check(th.has("Go to the end") and th.has("Delete thread") and th.has("Colour"), "thread menu: %s" % str(th)) + _check(Actions.thread(main, "nope:x>y").is_empty(), "a missing thread has no menu") + + +func _test_popup_and_run() -> void: + var at := Vector2(-900, -700) + var pm := Actions.popup_at(main, Actions.background(main, at), Vector2(50, 50)) + await _frames() + var sub: PopupMenu = null + for c in pm.get_children(): + if c is PopupMenu: + sub = c + _check(sub != null and sub.item_count == MainEditing.ADD_KINDS.size(), "New card ▸ lists every kind") + var before := main.data.tables.size() + _check(Actions.press(sub, "Roll table"), "pressing an item runs it") + await _frames() + var added: RollTable = main.data.tables[main.data.tables.size() - 1] + _check(main.data.tables.size() == before + 1 and added.position.distance_to(at) < 2.0, "the new card goes where the spread was clicked: %s" % str(added.position)) + pm.hide() + var t := main.data.get_table("loot") + Actions.set_card_colour(main, t, "#2f8a4f") + _check(t.color == "#2f8a4f", "Colour ▸ sets the card's colour") + main.undo_redo.undo() + _check(main.data.get_table("loot").color == "", "and it's undoable") + var bar_pm := main.top_bar.actions_menu.get_popup() + bar_pm.about_to_popup.emit() + _check(bar_pm.item_count > 4, "the ☰ menu fills with the spread's actions") + + +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_actions.gd.uid b/tests/test_actions.gd.uid new file mode 100644 index 0000000..76a5283 --- /dev/null +++ b/tests/test_actions.gd.uid @@ -0,0 +1 @@ +uid://hetav3d4sl5c diff --git a/tests/test_top_bar.gd b/tests/test_top_bar.gd index 371f989..f49b989 100644 --- a/tests/test_top_bar.gd +++ b/tests/test_top_bar.gd @@ -120,7 +120,7 @@ func _test_card_menu() -> void: _eq(del.modulate.a, 0.0, "and hides it again when deselected") var pm := (menus[0] as MenuButton).get_popup() - pm.id_pressed.emit(TableCardEditing.MENU_DELETE) + _check(Actions.press(pm, "Delete table"), "⋯ has Delete table") await _frames() _check(main.data.get_table("loot") == null, "⋯ → Delete table deletes it") main.undo_redo.undo() diff --git a/ui/actions.gd b/ui/actions.gd new file mode 100644 index 0000000..99c6261 --- /dev/null +++ b/ui/actions.gd @@ -0,0 +1,176 @@ +class_name Actions +extends RefCounted +## One list of actions per thing on the spread (a card, a thread, a group, the +## background), shared by the right-click menus, the ⋯ buttons, the top bar's +## ☰ menu and (later) long-press on touch. An action is a Dictionary: +## {text, do: Callable, tip?, disabled?, sub?: Array of actions} +## or {sep: "heading"} for a separator. + +const COLOUR_TIP := "Its own colour, apart from the theme" + + +## Fills a popup with actions; submenus become child PopupMenus. +static func fill(pm: PopupMenu, list: Array) -> void: + pm.clear() + for c in pm.get_children(): + if c is PopupMenu: + c.queue_free() + ModalCard.style_popup(pm) + var calls: Array[Callable] = [] + for a: Dictionary in list: + if a.has("sep"): + pm.add_separator(str(a["sep"])) + continue + var id := calls.size() + calls.append(a.get("do", Callable())) + if a.has("sub"): + var sub := PopupMenu.new() + sub.name = "sub%d" % id + pm.add_child(sub) + fill(sub, a["sub"]) + pm.add_submenu_node_item(str(a["text"]), sub, id) + else: + pm.add_item(str(a["text"]), id) + var i := pm.get_item_index(id) + pm.set_item_disabled(i, bool(a.get("disabled", false))) + if a.has("tip"): + pm.set_item_tooltip(i, str(a["tip"])) + if pm.id_pressed.is_connected(_run): + pm.id_pressed.disconnect(_run) + pm.set_meta("calls", calls) + pm.id_pressed.connect(_run.bind(pm)) + + +static func _run(id: int, pm: PopupMenu) -> void: + var calls: Array = pm.get_meta("calls", []) + if id >= 0 and id < calls.size() and (calls[id] as Callable).is_valid(): + (calls[id] as Callable).call() + + +## Runs the item with this text, as if clicked (tests, and keyboard paths). +static func press(pm: PopupMenu, text: String) -> bool: + for i in pm.item_count: + if pm.get_item_text(i) == text and not pm.is_item_disabled(i): + pm.id_pressed.emit(pm.get_item_id(i)) + return true + return false + + +## Opens a menu of actions at a screen position (right-click). +static func popup_at(host: Control, list: Array, at: Vector2) -> PopupMenu: + var pm := PopupMenu.new() + host.add_child(pm) + fill(pm, list) + pm.popup_hide.connect(pm.queue_free.call_deferred) + pm.popup(Rect2i(Vector2i(at), Vector2i.ZERO)) + return pm + + +## A "Colour ▸" submenu: Theme plus the ColorChoice presets. +static func colours(current: String, on_pick: Callable) -> Dictionary: + var sub: Array = [{"text": ("✓ " if current == "" else "") + "Theme colour", "do": on_pick.bind("")}] + for p: Array in ColorChoice.PRESETS: + sub.append({"text": ("✓ " if current.to_lower() == str(p[1]) else "") + str(p[0]), "do": on_pick.bind(str(p[1]))}) + return {"text": "Colour", "tip": COLOUR_TIP, "sub": sub} + + +# ---------- Providers ---------- + +static func card(m: Main, t: RollTable) -> Array: + var build := m.mode == Main.Mode.BUILD + var c := m.card_for(t.id) + var list: Array = [] + if t.kind != RollTable.KIND_VARIABLE: + list.append({"text": "Roll", "do": m.rolling.start_roll.bind(t.id)}) + if t.card_mode: + list.append({"text": "Shuffle the deck", "do": m.reshuffle_table.bind(t.id)}) + if c and c.can_collapse(): + list.append({"text": "Expand" if t.collapsed else "Collapse", "do": m.toggle_collapse.bind(t.id)}) + if build: + list.append({"sep": ""}) + list.append({"text": "Table settings…", "do": m.request_table_settings.bind(t.id)}) + list.append(colours(t.color, func(hex: String) -> void: set_card_colour(m, t, hex))) + list.append({"text": "Save as template…", "do": m.save_template_from.bind([t.id], "card")}) + list.append({"sep": ""}) + list.append({"text": "Delete table", "do": m.delete_table.bind(t.id)}) + return list + + +static func set_card_colour(m: Main, t: RollTable, hex: String) -> void: + m.undo_redo._begin_edit() + t.color = hex + m.inspector_edited(t.id) + m.mark_dirty() + m.undo_redo._end_edit() + + +## `key` is a pipe key, "tid:eid>to". +static func thread(m: Main, key: String) -> Array: + var tid := key.get_slice(">", 0).get_slice(":", 0) + var eid := key.get_slice(">", 0).get_slice(":", 1) + var to := key.get_slice(">", 1) + var t := m.data.get_table(tid) + var e := t.find_entry(eid) if t else null + if e == null: + return [] + var list: Array = [ + {"text": "Go to the start", "do": m.focus_table.bind(tid, eid)}, + {"text": "Go to the end", "do": m.focus_table.bind(to, "")}, + ] + if m.mode == Main.Mode.BUILD: + list.append({"sep": ""}) + list.append({"text": "Edit thread…", "do": m.inspecting.select_pipe.bind(key)}) + list.append(colours(str(e.pipe_color.get(to, "")), func(hex: String) -> void: + m.undo_redo._begin_edit() + if hex == "": + e.pipe_color.erase(to) + else: + e.pipe_color[to] = hex + m.mark_dirty() + m.board.geometry_rev += 1 + m.undo_redo._end_edit())) + list.append({"sep": ""}) + list.append({"text": "Delete thread", "do": m.remove_pipe.bind(key)}) + return list + + +static func group(m: Main, g: TableGroup) -> Array: + var list: Array = [{"text": "Expand" if g.collapsed else "Collapse", "do": m.toggle_group.bind(g.id)}] + if m.mode == Main.Mode.BUILD: + list.append({"text": "Details…", "do": m.edit_group_details.bind(g.id)}) + list.append({"text": "Save as template…", "do": m.save_template_from.bind([], "group", [g.id])}) + list.append({"sep": ""}) + list.append({"text": "Ungroup (keep the cards)", "do": m.ungroup.bind(g.id)}) + list.append({"text": "Delete group and its cards…", "do": m.confirm_delete_group.bind(g.id)}) + return list + + +## The empty spread under the mouse (`at`, in world coordinates), and the top +## bar's ☰ menu (at = the view's centre). +static func background(m: Main, at: Vector2) -> Array: + var list: Array = [] + if m.mode == Main.Mode.BUILD: + var kinds: Array = [] + for i in MainEditing.ADD_KINDS.size(): + var k: Array = MainEditing.ADD_KINDS[i] + kinds.append({"text": str(k[0]), "do": func() -> void: m.editing.add_table(str(k[1]), str(k[2]), at)}) + list.append({"text": "New card", "sub": kinds}) + list.append({"text": "New card from template…", "do": m.templating.open_template_picker}) + var picked := m.selection.size() + list.append({"text": "Group selected cards Ctrl+G", "do": m.grouping.group_selection, "disabled": picked == 0, + "tip": "Select cards first: drag a box around them"}) + list.append({"sep": ""}) + list.append({"text": "Search… Ctrl+F", "do": m.nav.open_search}) + list.append({"text": "Shuffle all decks", "do": m.local_state.reshuffle_all, "disabled": not has_decks(m), + "tip": "Put every Deck-mode card's results back"}) + list.append({"text": "Fit spread", "do": m.board.fit}) + list.append({"sep": ""}) + list.append({"text": "Theme…", "do": func() -> void: m.add_child(ThemeDialog.new(m))}) + return list + + +static func has_decks(m: Main) -> bool: + for t in m.data.tables: + if t.card_mode: + return true + return false diff --git a/ui/actions.gd.uid b/ui/actions.gd.uid new file mode 100644 index 0000000..8d7cd92 --- /dev/null +++ b/ui/actions.gd.uid @@ -0,0 +1 @@ +uid://bg6dsgbinbb7 diff --git a/ui/board_view.gd b/ui/board_view.gd index cc2585a..d199cc9 100644 --- a/ui/board_view.gd +++ b/ui/board_view.gd @@ -341,6 +341,14 @@ func _gui_input(ev: InputEvent) -> void: _panning = false mouse_default_cursor_shape = CURSOR_ARROW accept_event() + MOUSE_BUTTON_RIGHT: + if mb.pressed: + # A thread under the mouse, else the empty spread. + var key := pipes.pipe_at(world_mouse(), 11.0) + var list := Actions.thread(main, key) if key != "" else Actions.background(main, world_mouse()) + if not list.is_empty(): + Actions.popup_at(main, list, mb.global_position) + accept_event() MOUSE_BUTTON_WHEEL_UP, MOUSE_BUTTON_WHEEL_DOWN: if mb.pressed: var up := mb.button_index == MOUSE_BUTTON_WHEEL_UP diff --git a/ui/group_card.gd b/ui/group_card.gd index fecf236..51cbc70 100644 --- a/ui/group_card.gd +++ b/ui/group_card.gd @@ -16,11 +16,6 @@ extends MarginContainer const INLET_Y := 30.0 ## Most lit results a collapsed group lists. const MAX_LIT := 6 -const MENU_DETAILS := 0 -const MENU_TEMPLATE := 1 -const MENU_UNGROUP := 2 -const MENU_DELETE := 3 -const MENU_TOGGLE := 4 var main: Main var group: TableGroup @@ -204,6 +199,8 @@ func _build_compact() -> void: var mb := ev as InputEventMouseButton if mb and mb.button_index == MOUSE_BUTTON_LEFT: _card.accept_event() + elif _open_menu_on(mb): + _card.accept_event() ) add_child(_card) var col := TableCard.vbox(0) @@ -386,25 +383,8 @@ func _menu_button(on_felt: bool) -> MenuButton: var mb := Style.overflow_button("Group options", on_felt) menu_button = mb var pm := mb.get_popup() - pm.add_item("Expand" if group.collapsed else "Collapse", MENU_TOGGLE) - pm.add_item("Details…", MENU_DETAILS) - pm.add_item("Save as template…", MENU_TEMPLATE) - pm.add_separator() - pm.add_item("Ungroup (keep the cards)", MENU_UNGROUP) - pm.add_item("Delete group and its cards…", MENU_DELETE) - pm.id_pressed.connect(func(id: int) -> void: - match id: - MENU_TOGGLE: - main.toggle_group(group.id) - MENU_DETAILS: - main.edit_group_details(group.id) - MENU_TEMPLATE: - main.save_template_from([], "group", [group.id]) - MENU_UNGROUP: - main.ungroup(group.id) - MENU_DELETE: - main.confirm_delete_group(group.id) - ) + Actions.fill(pm, Actions.group(main, group)) + pm.about_to_popup.connect(func() -> void: Actions.fill(pm, Actions.group(main, group))) return mb @@ -434,8 +414,19 @@ func anchor_for(tid: String, eid: String) -> Vector2: # ---------- Input ---------- +## Right-click: the group's actions menu (Actions.group). +func _open_menu_on(mb: InputEventMouseButton) -> bool: + if mb == null or mb.button_index != MOUSE_BUTTON_RIGHT or not mb.pressed: + return false + Actions.popup_at(main, Actions.group(main, group), mb.global_position) + return true + + func _on_title_input(ev: InputEvent, owner: Control) -> void: var mb := ev as InputEventMouseButton + if _open_menu_on(mb): + owner.accept_event() + return if mb and mb.button_index == MOUSE_BUTTON_LEFT: if mb.pressed: main.details().hide_for() diff --git a/ui/main_editing.gd b/ui/main_editing.gd index 7e1fa86..21ba3fe 100644 --- a/ui/main_editing.gd +++ b/ui/main_editing.gd @@ -65,10 +65,11 @@ func _make_add_menu() -> MenuButton: return mb -## Adds a card of any kind (RollTable.create) in the middle of the view. -func add_table(kind: String = RollTable.KIND_ROLL, sub: String = "") -> RollTable: +## Adds a card of any kind (RollTable.create) in the middle of the view, or +## with its top-left corner at `at` (world coordinates). +func add_table(kind: String = RollTable.KIND_ROLL, sub: String = "", at: Vector2 = Vector2.INF) -> RollTable: m.undo_redo._begin_edit() - var center := m.board.screen_to_world(m.board.size / 2.0) + var center := m.board.screen_to_world(m.board.size / 2.0) if at == Vector2.INF else at + Vector2(128, 120) var name := "New table" var found := false for k: Array in ADD_KINDS: diff --git a/ui/main_state.gd b/ui/main_state.gd index c4d0c04..43b07c8 100644 --- a/ui/main_state.gd +++ b/ui/main_state.gd @@ -116,6 +116,23 @@ func reshuffle_table(tid: String) -> void: m.toast("Reshuffled %s" % Formatter.strip_tags(t.name)) +## Shuffles every Deck-mode card back to full. +func reshuffle_all() -> void: + var n := 0 + for t in m.data.tables: + if t.card_mode: + m.decks.erase(t.id) + if m.state: + m.state.clear_deck(m.data.id, t.id) + var card := m.card_for(t.id) + if card: + card.update_deck() + n += 1 + if m.state: + m.state.save() + m.toast("Shuffled %d %s" % [n, "deck" if n == 1 else "decks"]) + + ## The expanded text an entry showed in the last roll, or "" if it isn't lit. func lit_text(tid: String, eid: String) -> String: return str(m.lit_texts.get(Roller.entry_key(tid, eid), "")) diff --git a/ui/main_top_bar.gd b/ui/main_top_bar.gd index 6dee605..685042f 100644 --- a/ui/main_top_bar.gd +++ b/ui/main_top_bar.gd @@ -12,6 +12,8 @@ var _build_button: Button var _build_tools: Control var _switcher: CollectionSwitcher var _board_menu: IoMenu +## The bar's actions menu (Actions.background), like right-clicking the spread. +var actions_menu: MenuButton var view_menu: ViewMenu var help_menu: HelpMenu var zoom: ZoomControl @@ -117,6 +119,14 @@ func _build_top_bar() -> Control: _endless_chip = _make_endless_chip() h.add_child(_endless_chip) + # The same actions as right-clicking the empty spread, for people who don't right-click. + actions_menu = Style.menu_button("☰") + actions_menu.tooltip_text = "Actions: new cards, search, shuffle all decks, theme…" + var apm := actions_menu.get_popup() + apm.about_to_popup.connect(func() -> void: + Actions.fill(apm, Actions.background(m, m.board.screen_to_world(m.board.size / 2.0) - Vector2(128, 120)))) + h.add_child(actions_menu) + m.nav._search_button = _make_search_button() h.add_child(m.nav._search_button) diff --git a/ui/table_card.gd b/ui/table_card.gd index ee68e05..f9129e1 100644 --- a/ui/table_card.gd +++ b/ui/table_card.gd @@ -543,6 +543,17 @@ func _gui_input(ev: InputEvent) -> void: var mb := ev as InputEventMouseButton if mb and mb.button_index == MOUSE_BUTTON_LEFT: accept_event() + elif open_menu_on(mb): + accept_event() + + +## Right-click: the card's actions menu (Actions.card). True when it opened. +func open_menu_on(mb: InputEventMouseButton) -> bool: + if mb == null or mb.button_index != MOUSE_BUTTON_RIGHT or not mb.pressed: + return false + main.details().hide_for() + Actions.popup_at(main, Actions.card(main, table), mb.global_position) + return true ## Dragging the header moves the card (and, in Build, every selected card). @@ -550,6 +561,9 @@ func _gui_input(ev: InputEvent) -> void: ## out; dropping a card into a group frame adds it to that group (Main). func _on_header_input(ev: InputEvent, header: Control) -> void: var mb := ev as InputEventMouseButton + if open_menu_on(mb): + header.accept_event() + return if mb and mb.button_index == MOUSE_BUTTON_LEFT: if mb.pressed: main.details().hide_for() diff --git a/ui/table_card_editing.gd b/ui/table_card_editing.gd index e7f6071..0531478 100644 --- a/ui/table_card_editing.gd +++ b/ui/table_card_editing.gd @@ -6,11 +6,6 @@ extends RefCounted ## and the row × that shows only on hover or selection. Everything else is ## edited in Main's inspector. -## The card's ⋯ menu items. -const MENU_SETTINGS := 0 -const MENU_TEMPLATE := 1 -const MENU_COLLAPSE := 2 -const MENU_DELETE := 3 var card: TableCard @@ -246,27 +241,10 @@ static func footer_button(text: String, hover_color: Color) -> Button: ## Build mode's "⋯" menu on the card: settings, save as template, collapse ## and delete (the same pattern as a group card's ⋯ menu). func make_card_menu() -> MenuButton: - var table := card.table - var main := card.main var mb := Style.overflow_button("Table actions") var pm := mb.get_popup() - pm.add_item("Table settings…", MENU_SETTINGS) - pm.add_item("Save as template…", MENU_TEMPLATE) - if card.can_collapse(): - pm.add_item("Expand" if table.collapsed else "Collapse", MENU_COLLAPSE) - pm.add_separator() - pm.add_item("Delete table", MENU_DELETE) - pm.id_pressed.connect(func(id: int) -> void: - match id: - MENU_SETTINGS: - main.request_table_settings(table.id) - MENU_TEMPLATE: - main.save_template_from([table.id], "card") - MENU_COLLAPSE: - main.toggle_collapse(table.id) - MENU_DELETE: - main.delete_table(table.id) - ) + Actions.fill(pm, Actions.card(card.main, card.table)) + pm.about_to_popup.connect(func() -> void: Actions.fill(pm, Actions.card(card.main, card.table))) return mb