TableTower/ui/lore_list.gd

157 lines
5.5 KiB
GDScript3
Raw Permalink Normal View History

class_name LoreList
extends VBoxContainer
## The Lore Index's shared part: the title with "+ Lore Entry" and ×, the
## "This spread | My library" switch (LoreLevel), a search box (name, text
## and tags), the list and the page editor (PageEditor). The spread's dock
## (BookPanel) and the sheet editor's (SheetLoreDock) both show one.
## Dragging an entry that nothing takes ends in `dropped`: the host places it
## (on a spread card, or into a sheet box).
## A drag from the list ended where no control took it.
signal dropped(page: BookPage, level: LoreLevel, at: Vector2)
var m: Main
var level: LoreLevel
var levels := {}
var title: Label
var level_tabs: HBoxContainer
var search: LineEdit
var list: ItemList
var editor: PageEditor
## Opens an entry's item sheet (sheet id); set by the host.
var open_sheet: Callable
var _ids: Array[String] = []
var _rev := -1
var _dragging := ""
## `on_close` runs when × is pressed.
func setup(p_main: Main, on_close: Callable) -> void:
m = p_main
levels = {LoreLevel.SPREAD: SpreadLoreLevel.new(m), LoreLevel.LIBRARY: LibraryLoreLevel.new(m)}
level = levels[LoreLevel.SPREAD]
add_theme_constant_override("separation", 8)
var head := HBoxContainer.new()
head.add_theme_constant_override("separation", 6)
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.name = "NewEntry"
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(on_close)
head.add_child(close)
level_tabs = SideTabs.tab_row([[LoreLevel.SPREAD, "This spread"], [LoreLevel.LIBRARY, "My library"]], level.key, set_level)
level_tabs.name = "LoreLevels"
level_tabs.tooltip_text = "This spread's Lore Entries, or My library: shared by every spread and sheet"
add_child(level_tabs)
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())
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(level.get_page(_ids[i])))
list.set_drag_forwarding(_drag, Callable(), Callable())
add_child(list)
var scroll := ScrollContainer.new()
scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
scroll.size_flags_vertical = SIZE_EXPAND_FILL
add_child(scroll)
editor = PageEditor.new(self)
editor.size_flags_horizontal = SIZE_EXPAND_FILL
scroll.add_child(editor)
editor.show_page(null)
func _process(_delta: float) -> void:
if is_visible_in_tree() and m.data and level.revision() != _rev:
_rev = level.revision()
refresh()
## Shows level `key` (LoreLevel.SPREAD or LIBRARY).
func set_level(key: String) -> void:
level = levels[key]
SideTabs.press(level_tabs, key)
editor.show_page(null)
refresh()
## Refills the list (search applied), keeping the selection.
func refresh() -> void:
_rev = level.revision()
var keep := editor.page.id if editor.page else ""
var query := search.text.strip_edges().to_lower()
list.clear()
_ids.clear()
for page in level.pages():
var hay := (page.label() + " " + Formatter.strip_tags(page.text) + " " + " ".join(page.tags)).to_lower()
if query != "" and not hay.contains(query):
continue
var i := list.add_item(page.label() + (" ▤" if page.sheet != "" else ""))
list.set_item_tooltip(i, ("Tags: " + ", ".join(page.tags) + "\n" if not page.tags.is_empty() else "") + ("Has an item sheet\n" if page.sheet != "" else "") + "Drag onto a result or a sheet box to use it")
_ids.append(page.id)
if page.id == keep:
list.select(i)
if keep != "" and level.get_page(keep) == null:
editor.show_page(null)
## After undo or redo: the entry 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.page == null:
return
editor.show_page(level.get_page(editor.page.id))
refresh()
func new_page() -> BookPage:
var page := level.add_page("New Lore Entry")
refresh()
select_page(page.id)
return page
func select_page(id: String) -> void:
var i := _ids.find(id)
if i >= 0:
list.select(i)
editor.show_page(level.get_page(id))
func _drag(at: Vector2) -> Variant:
var i := list.get_item_at_position(at, true)
if i < 0:
return null
_dragging = _ids[i]
var label := Style.make_label(list.get_item_text(i), Style.body_bold(), 15, Style.INK)
var chip := PanelContainer.new()
chip.add_theme_stylebox_override("panel", Style.box(Style.LIT, 6, [8, 4, 8, 4]))
chip.add_child(label)
list.set_drag_preview(chip)
return {"book_page": _dragging, "lore_level": level.key}
## Nothing took the drop (cards' and boxes' fields don't), so the host places it.
func _notification(what: int) -> void:
if what == NOTIFICATION_DRAG_END and _dragging != "":
var id := _dragging
_dragging = ""
var page := level.get_page(id)
if page and not get_viewport().gui_is_drag_successful():
dropped.emit(page, level, get_global_mouse_position())