303 lines
11 KiB
GDScript
303 lines
11 KiB
GDScript
class_name MainTopBar
|
|
extends RefCounted
|
|
## Main's top bar (brand, Play/Build, collections, Build tools, search,
|
|
## View ▾, Help ▾ and the zoom control), the toast at the bottom and the
|
|
## endless-loop warning chip.
|
|
|
|
var m: Main
|
|
|
|
var _hint: Label
|
|
var _play_button: Button
|
|
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
|
|
## The name beside the logo (hidden on phones).
|
|
var brand_title: Label
|
|
var view_menu: ViewMenu
|
|
var help_menu: HelpMenu
|
|
var zoom: ZoomControl
|
|
var _toast: PanelContainer
|
|
var _toast_label: Label
|
|
var _toast_undo: Button
|
|
var _toast_timer: Timer
|
|
## The undo step the toast's Undo button undoes (EditHistory serial), or -1.
|
|
var _toast_serial := -1
|
|
var _endless_chip: PanelContainer
|
|
var _endless_label: Label
|
|
var _endless_timer: Timer
|
|
|
|
|
|
func _init(p_main: Main) -> void:
|
|
m = p_main
|
|
|
|
|
|
## The theme's logo (Style.LOGO): Morgan Fortuna, the fortune-teller mascot, the old tower
|
|
## mark, or none.
|
|
func _logo() -> Control:
|
|
match Style.LOGO:
|
|
"fortune_teller":
|
|
var tr := TextureRect.new()
|
|
tr.texture = preload("res://assets/art/fortune_teller_logo.svg")
|
|
tr.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
|
|
tr.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
|
|
tr.texture_filter = CanvasItem.TEXTURE_FILTER_LINEAR_WITH_MIPMAPS
|
|
tr.custom_minimum_size = Vector2(46, 46)
|
|
tr.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
|
return tr
|
|
"tower":
|
|
var mark := Control.new()
|
|
mark.custom_minimum_size = Vector2(20, 24)
|
|
mark.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
|
mark.draw.connect(func() -> void: Icons.draw_tower(mark, Vector2(0, 0), 20.0 / 22.0, Style.ON_BG, Style.ACCENT))
|
|
return mark
|
|
return null
|
|
|
|
|
|
func _build_top_bar() -> Control:
|
|
var bar := PanelContainer.new()
|
|
bar.add_theme_stylebox_override("panel", Style.panel_box([16, 10, 16, 10]))
|
|
var h := HBoxContainer.new()
|
|
h.add_theme_constant_override("separation", 10)
|
|
bar.add_child(h)
|
|
|
|
var brand := HBoxContainer.new()
|
|
brand.add_theme_constant_override("separation", 9)
|
|
var mark := _logo()
|
|
if mark:
|
|
brand.add_child(mark)
|
|
var title := Style.make_label("Table Tarot", Style.display(), 23, Style.ON_BG)
|
|
brand_title = title
|
|
title.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
|
brand.add_child(title)
|
|
h.add_child(brand)
|
|
|
|
var seg := PanelContainer.new()
|
|
seg.add_theme_stylebox_override("panel", Style.box(Color(0, 0, 0, 0.28), 9, [3, 3, 3, 3]))
|
|
seg.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
|
var seg_row := HBoxContainer.new()
|
|
seg_row.add_theme_constant_override("separation", 0)
|
|
seg.add_child(seg_row)
|
|
var group := ButtonGroup.new()
|
|
_play_button = _seg_button("Play", group)
|
|
_build_button = _seg_button("Build", group)
|
|
_play_button.button_pressed = true
|
|
_play_button.pressed.connect(func() -> void: m.set_mode(Main.Mode.PLAY))
|
|
_build_button.pressed.connect(func() -> void: m.set_mode(Main.Mode.BUILD))
|
|
seg_row.add_child(_play_button)
|
|
seg_row.add_child(_build_button)
|
|
h.add_child(seg)
|
|
|
|
_switcher = CollectionSwitcher.new()
|
|
_switcher.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
|
_switcher.setup(m)
|
|
h.add_child(_switcher)
|
|
|
|
_hint = Style.make_label(Main.HINTS[m.mode], Style.body(), 15, Style.ON_BG_MUTED)
|
|
_hint.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
_hint.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
|
_hint.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
|
|
_hint.custom_minimum_size = Vector2(60, 0)
|
|
h.add_child(_hint)
|
|
|
|
var tools := HBoxContainer.new()
|
|
tools.add_theme_constant_override("separation", 6)
|
|
_build_tools = tools
|
|
m.undo_redo._make_history_buttons(tools)
|
|
m.editing._add_menu = m.editing._make_add_menu()
|
|
tools.add_child(m.editing._add_menu)
|
|
_board_menu = IoMenu.new()
|
|
_board_menu.setup(true, m.collections_enabled())
|
|
_board_menu.reset_requested.connect(m.editing.reset_sample)
|
|
_board_menu.import_requested.connect(m.collections.import_collection)
|
|
_board_menu.test_requested.connect(m.collections.open_test_board)
|
|
_board_menu.export_requested.connect(m.collections.export_collection)
|
|
_board_menu.pack_requested.connect(m.nav.pack_collapsed_groups)
|
|
tools.add_child(_board_menu)
|
|
tools.visible = false
|
|
h.add_child(tools)
|
|
|
|
_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)
|
|
|
|
view_menu = ViewMenu.new()
|
|
view_menu.setup(m)
|
|
h.add_child(view_menu)
|
|
help_menu = HelpMenu.new()
|
|
help_menu.setup(m)
|
|
h.add_child(help_menu)
|
|
zoom = ZoomControl.new()
|
|
zoom.setup(m)
|
|
h.add_child(zoom)
|
|
# Show or hide the minimap as remembered for the starting mode.
|
|
m.nav._nav_mode_changed.call_deferred()
|
|
return bar
|
|
|
|
|
|
func _seg_button(text: String, group: ButtonGroup) -> Button:
|
|
var b := Button.new()
|
|
b.text = text
|
|
b.toggle_mode = true
|
|
b.button_group = group
|
|
b.add_theme_font_override("font", Style.body_bold())
|
|
b.add_theme_font_size_override("font_size", 16)
|
|
var normal := Style.box(Color(0, 0, 0, 0), 7, [16, 6, 16, 6])
|
|
var hover := Style.box(Color(1, 1, 1, 0.06), 7, [16, 6, 16, 6])
|
|
var pressed := Style.box(Style.CARD, 7, [16, 6, 16, 6])
|
|
Style.skin_button(b, normal, hover, pressed, Style.ON_BG_MUTED, Style.ON_BG, Style.INK)
|
|
return b
|
|
|
|
|
|
func _make_search_button() -> Button:
|
|
var b := Style.bar_button("")
|
|
b.custom_minimum_size = Vector2(38, 0)
|
|
b.tooltip_text = "Find a card by its name, results, prompt, variables or group (Ctrl+F)"
|
|
_bar_icon(b, func(c: Control) -> void:
|
|
c.draw_arc(Vector2(6.5, 6.5), 5.0, 0.0, TAU, 24, Style.ON_BG, 1.8, true)
|
|
c.draw_line(Vector2(10.3, 10.3), Vector2(14.5, 14.5), Style.ON_BG, 2.4, true))
|
|
b.pressed.connect(m.nav.toggle_search)
|
|
return b
|
|
|
|
|
|
## Puts a 16 px drawn icon on a top-bar button: at the left of its text, or
|
|
## centred when the button has no text.
|
|
func _bar_icon(b: Button, paint: Callable) -> void:
|
|
var icon_only := b.text == ""
|
|
for n in ["normal", "hover", "pressed", "hover_pressed", "disabled"]:
|
|
var sb := b.get_theme_stylebox(n) as StyleBoxFlat
|
|
if sb and not icon_only:
|
|
sb.content_margin_left = 32
|
|
var icon := Control.new()
|
|
icon.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
icon.size = Vector2(16, 16)
|
|
icon.draw.connect(paint.bind(icon))
|
|
b.add_child(icon)
|
|
b.resized.connect(func() -> void:
|
|
var x := roundf((b.size.x - 16.0) / 2.0) if icon_only else 11.0
|
|
icon.position = Vector2(x, roundf((b.size.y - 16.0) / 2.0)))
|
|
|
|
|
|
# ----- Toast -----
|
|
|
|
func _build_toast() -> void:
|
|
_toast = PanelContainer.new()
|
|
var sb := Style.box(Style.CARD, 10, [16, 9, 10, 9])
|
|
sb.shadow_color = Color(0, 0, 0, 0.4)
|
|
sb.shadow_size = 18
|
|
sb.shadow_offset = Vector2(0, 8)
|
|
_toast.add_theme_stylebox_override("panel", sb)
|
|
var h := HBoxContainer.new()
|
|
h.add_theme_constant_override("separation", 12)
|
|
_toast.add_child(h)
|
|
_toast_label = Style.make_label("", Style.body(), 15, Style.INK)
|
|
_toast_label.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
|
h.add_child(_toast_label)
|
|
_toast_undo = Button.new()
|
|
_toast_undo.text = "Undo"
|
|
_toast_undo.add_theme_font_override("font", Style.body_bold())
|
|
_toast_undo.add_theme_font_size_override("font_size", 15)
|
|
var un := Style.box(Style.BG, 7, [12, 5, 12, 5])
|
|
var uh := Style.box(Style.BG_HOVER, 7, [12, 5, 12, 5])
|
|
Style.skin_button(_toast_undo, un, uh, uh, Style.ACCENT)
|
|
_toast_undo.pressed.connect(_undo)
|
|
_toast_undo.focus_mode = Control.FOCUS_NONE
|
|
h.add_child(_toast_undo)
|
|
_toast.hide()
|
|
m.add_child(_toast)
|
|
_toast_timer = Timer.new()
|
|
_toast_timer.one_shot = true
|
|
_toast_timer.wait_time = 5.0
|
|
_toast_timer.timeout.connect(func() -> void:
|
|
_toast.hide()
|
|
_toast_serial = -1
|
|
)
|
|
m.add_child(_toast_timer)
|
|
|
|
|
|
## Shows a message at the bottom. With `undoable`, it has an Undo button for
|
|
## the edit just committed (it goes through the edit history, like Ctrl+Z).
|
|
func toast(message: String, undoable: bool = false) -> void:
|
|
_toast_serial = m.undo_redo.edits.top_serial() if undoable and m.undo_redo.edits else -1
|
|
_toast_label.text = message
|
|
_toast_undo.visible = _toast_serial >= 0
|
|
_toast.show()
|
|
_toast.reset_size()
|
|
_place_toast()
|
|
_toast_timer.start()
|
|
|
|
|
|
func _place_toast() -> void:
|
|
if _toast and _toast.visible:
|
|
_toast.reset_size()
|
|
_toast.position = Vector2((m.size.x - _toast.size.x) / 2.0, m.size.y - _toast.size.y - 20.0)
|
|
|
|
|
|
## The toast's Undo button: undoes the step the toast was about, if it is
|
|
## still the newest one.
|
|
func _undo() -> void:
|
|
m.undo_redo._commit_edits()
|
|
if _toast_serial < 0 or m.undo_redo.edits == null or m.undo_redo.edits.top_serial() != _toast_serial:
|
|
_toast_undo.hide()
|
|
if m.undo_redo.edits and m.undo_redo.edits.can_undo():
|
|
toast("Newer edits came after that one. Undo (Ctrl+Z) steps back through them.")
|
|
return
|
|
m.undo_redo.undo()
|
|
|
|
|
|
# ----- Endless-loop warning -----
|
|
|
|
func _make_endless_chip() -> PanelContainer:
|
|
var chip := PanelContainer.new()
|
|
chip.add_theme_stylebox_override("panel", Style.box(Style.WARN_BG, 8, [10, 4, 10, 4]))
|
|
chip.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
|
_endless_label = Style.make_label("", Style.body_bold(), 14, Style.WARN_INK)
|
|
_endless_label.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
|
|
chip.add_child(_endless_label)
|
|
chip.custom_minimum_size.x = 0
|
|
chip.hide()
|
|
return chip
|
|
|
|
|
|
func _update_endless_chip() -> void:
|
|
if _endless_chip == null or m.data == null:
|
|
return
|
|
var ids := Roller.endless_cycles(m.data)
|
|
if m.mode != Main.Mode.BUILD or ids.is_empty():
|
|
_endless_chip.hide()
|
|
return
|
|
var names := PackedStringArray()
|
|
for id in ids:
|
|
var t := m.data.get_table(id)
|
|
names.append(t.name if t else id)
|
|
var list := ", ".join(names)
|
|
_endless_label.text = "⚠ Endless loop: " + list
|
|
var w := Style.body_bold().get_string_size(_endless_label.text, HORIZONTAL_ALIGNMENT_LEFT, -1, 14).x
|
|
_endless_label.custom_minimum_size.x = clampf(w + 4.0, 120.0, 300.0)
|
|
_endless_chip.tooltip_text = "These tables can never finish rolling, because every result leads back into a loop:\n" + "\n".join(names)
|
|
_endless_chip.show()
|
|
|
|
|
|
## The endless-loop check walks the whole board (about 30 ms on an 800-card
|
|
## board), so edits schedule it to run once typing pauses instead of on every
|
|
## keystroke.
|
|
func _schedule_endless_check() -> void:
|
|
if _endless_timer == null:
|
|
_endless_timer = Timer.new()
|
|
_endless_timer.one_shot = true
|
|
_endless_timer.wait_time = 0.4
|
|
_endless_timer.timeout.connect(_update_endless_chip)
|
|
m.add_child(_endless_timer)
|
|
_endless_timer.start()
|