TableTower/ui/book_panel.gd

211 lines
6.6 KiB
GDScript3
Raw Normal View History

class_name BookPanel
extends PanelContainer
## The Lore Index (Book in code): the spread's reusable results, its Lore
## Entries (BookPage), docked beside the spread; in Build it is a tab beside
## the inspector (SideTabs).
## 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 := ""
var _tabs: Control
var _title: Label
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)
_tabs = SideTabs.make_row(m, SideTabs.LORE)
col.add_child(_tabs)
var head := HBoxContainer.new()
head.add_theme_constant_override("separation", 6)
col.add_child(head)
_title = Style.make_label("Lore Index", Style.display(), 20, Style.ON_BG)
_title.size_flags_horizontal = SIZE_EXPAND_FILL
head.add_child(_title)
var add := Style.bar_button("+ Lore Entry")
add.tooltip_text = "A new Lore Entry: 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 Lore Index"
close.pressed.connect(func() -> void: set_shown(false))
head.add_child(close)
_search = LineEdit.new()
_search.placeholder_text = "Search Lore Entries 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)
## After undo or redo: the page in the editor is shown again from the
## restored spread, so its fields show the undone text at once.
func reload_page() -> void:
if editor == null or editor.page == null:
return
editor.show_page(m.data.get_page(editor.page.id))
refresh()
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()
SideTabs.sync(m)
func toggle() -> void:
SideTabs.show_tab(m, SideTabs.INSPECTOR if visible else SideTabs.LORE)
## Build shows the Inspector | Lore Index tabs in place of the title.
func show_tabs(on: bool) -> void:
_tabs.visible = on
_title.visible = not on
func new_page() -> BookPage:
m.undo_redo._begin_edit()
var p := m.data.add_page("New Lore Entry")
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)
p.image = e.image
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 Lore Index" % 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