TableTower/ui/main_top_bar.gd

319 lines
11 KiB
GDScript3
Raw Normal View History

class_name MainTopBar
extends RefCounted
## Main's top bar (brand, Play/Build, collections, Build tools, rules switch,
## search, zoom), 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 _collapse_all: Button
var _switcher: CollectionSwitcher
var _board_menu: IoMenu
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
var _rules_buttons: Array[Button] = []
func _init(p_main: Main) -> void:
m = p_main
func _build_top_bar() -> Control:
var bar := PanelContainer.new()
var sb := Style.box(Style.FELT_DEEP, 0, [16, 10, 16, 10])
sb.border_width_bottom = 1
sb.border_color = Color(1, 1, 1, 0.07)
bar.add_theme_stylebox_override("panel", sb)
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 := 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_FELT, Style.BRASS))
brand.add_child(mark)
var title := Style.make_label("TableTower", Style.display(), 23, Style.ON_FELT)
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_FELT_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)
tools.add_child(_board_menu)
# Test board… lives in the Board ▾ menu to keep the Build bar from overflowing.
tools.add_child(m.nav._make_arrange_menu())
tools.visible = false
h.add_child(tools)
_endless_chip = _make_endless_chip()
h.add_child(_endless_chip)
h.add_child(_make_rules_switch())
m.nav._search_button = _make_search_button()
h.add_child(m.nav._search_button)
_collapse_all = Style.bar_button("Collapse all")
_collapse_all.tooltip_text = "Collapse or expand every table at once"
_collapse_all.pressed.connect(func() -> void: m.set_all_collapsed(m._any_expanded()))
h.add_child(_collapse_all)
var zoom := HBoxContainer.new()
zoom.add_theme_constant_override("separation", 6)
var zout := Style.bar_button("−")
zout.tooltip_text = "Zoom out"
zout.custom_minimum_size = Vector2(34, 0)
zout.pressed.connect(func() -> void: m.board.set_zoom(m.board.zoom / 1.15))
var zfit := Style.bar_button("Fit")
zfit.tooltip_text = "Show every table"
zfit.pressed.connect(func() -> void: m.board.fit())
var zin := Style.bar_button("+")
zin.tooltip_text = "Zoom in"
zin.custom_minimum_size = Vector2(34, 0)
zin.pressed.connect(func() -> void: m.board.set_zoom(m.board.zoom * 1.15))
for b in [zout, zfit, zin]:
zoom.add_child(b)
m.nav._map_toggle = m.nav._make_map_toggle()
zoom.add_child(m.nav._map_toggle)
h.add_child(zoom)
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_FELT_MUTED, Style.ON_FELT, Style.INK)
return b
func _make_search_button() -> Button:
var b := Style.bar_button("Search")
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_FELT, 1.8, true)
c.draw_line(Vector2(10.3, 10.3), Vector2(14.5, 14.5), Style.ON_FELT, 2.4, true))
b.pressed.connect(m.nav.toggle_search)
return b
## Puts a 16 px drawn icon at the left of a top-bar button.
func _bar_icon(b: Button, paint: Callable) -> void:
for n in ["normal", "hover", "pressed", "hover_pressed", "disabled"]:
var sb := b.get_theme_stylebox(n) as StyleBoxFlat
if sb:
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: icon.position = Vector2(11, roundf((b.size.y - 16.0) / 2.0)))
func _sync_rules_switch() -> void:
if _rules_buttons.size() == 2:
_rules_buttons[0].set_pressed_no_signal(m.rules_plain())
_rules_buttons[1].set_pressed_no_signal(not m.rules_plain())
## The top bar's compact "Plain | Text" rules switch.
func _make_rules_switch() -> Control:
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
seg.tooltip_text = "Show rules in plain words, or as the rule text they're stored as"
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 0)
seg.add_child(row)
var group := ButtonGroup.new()
_rules_buttons.clear()
for pair in [["Plain", "plain"], ["Text", "text"]]:
var b := _seg_button(str(pair[0]), group)
b.add_theme_font_size_override("font_size", 14)
for n in ["normal", "hover", "pressed", "hover_pressed"]:
var sb := b.get_theme_stylebox(n) as StyleBoxFlat
if sb:
sb.content_margin_left = 10
sb.content_margin_right = 10
sb.content_margin_top = 4
sb.content_margin_bottom = 4
b.tooltip_text = "Rules: show them in plain words" if pair[1] == "plain" else "Rules: show them as rule text (@luck -= 1)"
b.focus_mode = Control.FOCUS_NONE
var v: String = pair[1]
b.pressed.connect(func() -> void: m.set_rules_view(v))
row.add_child(b)
_rules_buttons.append(b)
_sync_rules_switch()
return seg
# ----- 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.FELT, 7, [12, 5, 12, 5])
var uh := Style.box(Style.FELT_HOVER, 7, [12, 5, 12, 5])
Style.skin_button(_toast_undo, un, uh, uh, Style.BRASS)
_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()