class_name LoreLinkPicker extends ModalCard ## "Link a Lore Entry": a search box over the Lore Entries a sheet can link to ## (SheetLoreLinks.entries), each with the start of its text. Clicking one ## calls on_pick(page) and closes; Enter picks the first one shown. var main: Main var search: LineEdit var _on_pick: Callable var _list: VBoxContainer var _first: BookPage func _init(p_main: Main, on_pick: Callable) -> void: super("Link a Lore Entry", 460.0) main = p_main _on_pick = on_pick search = LineEdit.new() search.name = "Search" search.placeholder_text = "Search Lore Entries and tags" search.clear_button_enabled = true search.text_changed.connect(func(_text: String) -> void: fill()) search.text_submitted.connect(func(_text: String) -> void: if _first: choose(_first)) body.add_child(search) _list = VBoxContainer.new() _list.name = "Entries" _list.add_theme_constant_override("separation", 4) body.add_child(_list) body.add_child(ModalCard.note("From this spread's Lore Index. The selected words become the link; with none, the entry's name is added.")) add_button("Cancel", "plain", close) fill() func _ready() -> void: super() search.grab_focus() ## Lists the entries matching the search. func fill() -> void: for child in _list.get_children(): _list.remove_child(child) child.queue_free() var found := SheetLoreLinks.search(main, search.text) _first = found[0] if not found.is_empty() else null if found.is_empty(): var empty := "No Lore Entries yet. Add some in the spread's Lore Index." if SheetLoreLinks.entries(main).is_empty() else "Nothing matches ā€œ%sā€." % search.text.strip_edges() _list.add_child(ModalCard.note(empty)) for page in found: var button := ModalCard.make_button(page.label(), "plain") button.name = page.id.validate_node_name() button.alignment = HORIZONTAL_ALIGNMENT_LEFT button.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS button.tooltip_text = SheetLoreLinks.excerpt(page) button.pressed.connect(choose.bind(page)) _list.add_child(button) func choose(page: BookPage) -> void: _on_pick.call(page) close()