TableTower/ui/actions.gd
RobinFiredrake 48b57e284c Play shows that Build mode exists
A greyed + Card in Play's top bar explains that cards are added in Build,
offers Switch to Build and pulses the Build switch. The ☰ menu shows New
card greyed with a tip. The toast's button can now run any action.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:42:29 -06:00

207 lines
8.2 KiB
GDScript

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: the default (`default_label`), the ColorChoice
## presets and, with `m`, "Any colour…" (a full colour picker).
static func colours(current: String, on_pick: Callable, m: Main = null, default_label: String = "Theme colour") -> Dictionary:
var sub: Array = [{"text": ("✓ " if current == "" else "") + default_label, "do": on_pick.bind("")}]
var matched := current == ""
for p: Array in ColorChoice.PRESETS:
var on := current.to_lower() == str(p[1])
matched = matched or on
sub.append({"text": ("✓ " if on else "") + str(p[0]), "do": on_pick.bind(str(p[1]))})
if m:
sub.append({"text": ("✓ " if not matched else "") + "Any colour…", "do": func() -> void: ColourDialog.open(m, current, on_pick)})
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)})
list.append({"text": "Roll several times…", "do": BatchRoll.ask.bind(m, t.id),
"tip": "Roll the whole chain a number of times and see every result together"})
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), m))
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()
static func set_group_colour(m: Main, g: TableGroup, hex: String) -> void:
m.undo_redo._begin_edit()
g.color = hex
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(), m))
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(colours(g.color, func(hex: String) -> void: set_group_colour(m, g, hex), m, "Default (blue)"))
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 the selection Ctrl+G", "do": m.grouping.group_selection, "disabled": picked == 0 and m.selecting.groups.is_empty(),
"tip": "Select cards first: drag a box around them"})
list.append({"sep": ""})
else:
list.append(BuildModeHint.menu_item())
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({"text": "Hide the list" if m.list_view.visible else "List view", "do": m.list_view.toggle,
"tip": "The cards as a list with their groups"})
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.sound:
list.append({"text": "Sound…", "do": func() -> void: SoundPanel.open(m, m.get_global_mouse_position()),
"tip": "How loud the theme's sounds are"})
if m.mobile:
# The phone layout has no room for these on the bar.
var build := m.mode == Main.Mode.BUILD
list.append({"text": "Switch to Play" if build else "Switch to Build", "do": m.set_mode.bind(Main.Mode.PLAY if build else Main.Mode.BUILD)})
list.append({"text": "Help…", "do": func() -> void: m.top_bar.help_menu.open("")})
return list
static func has_decks(m: Main) -> bool:
for t in m.data.tables:
if t.card_mode:
return true
return false