class_name SheetTextToolbar extends HFlowContainer ## The formatting buttons above a sheet text box while you edit it: bold, ## italic, underline, strike-through, then Colour, Size and Effect menus, ## live values ({@name}) and "Link a Lore Entry…". The tags come from ## FormatMenu (the same as its right-click menu), which also wraps the ## selection. Buttons take no focus, so the text keeps its selection. ## A toolbar menu closed: the text box takes the focus back. signal menu_closed var ed: SheetEditor var box: SheetBox var edit: TextEdit ## The formatted text shown when not editing (SheetTextBox). var shown: RichTextLabel var _menus: Array[PopupMenu] = [] func _init(p_editor: SheetEditor, p_box: SheetBox, p_edit: TextEdit, ink: Color) -> void: ed = p_editor box = p_box edit = p_edit name = "Toolbar" add_theme_constant_override("h_separation", 3) add_theme_constant_override("v_separation", 3) var short := {"Bold": "B", "Italic": "I", "Underline": "U", "Strike-through": "S"} for style: Array in FormatMenu.STYLES: var button := _small(SheetFieldViews.step_button(short[style[0]], ink)) button.name = str(style[0]).validate_node_name() button.tooltip_text = str(style[0]) button.pressed.connect(FormatMenu.wrap_selection.bind(edit, style[1], style[2])) add_child(button) _menu("Colour", FormatMenu.colours(), ink) _menu("Size", FormatMenu.SIZES, ink) _menu("Effect", FormatMenu.EFFECTS, ink) var values := _menu("{@}", [], ink, "Values", "Show a live value: a box with Use in tables on") values.about_to_popup.connect(func() -> void: _fill_values(values)) var link := _small(SheetFieldViews.step_button("§ Link…", ink)) link.name = "LoreLink" link.tooltip_text = "Link a Lore Entry…" link.pressed.connect(pick_lore) add_child(link) ## Shows the formatted text again once the text box lost the focus (not to ## one of the toolbar's menus). func stop_editing() -> void: if edit.has_focus() or menu_open() or shown == null: return get_parent().visible = false shown.text = SheetTextBox.render(ed, edit.text) shown.visible = true ## Smaller than the boxes' own buttons, so the text keeps the room. func _small(button: Button) -> Button: button.custom_minimum_size = Vector2(20, 20) button.add_theme_font_size_override("font_size", 12) return button ## True while one of the toolbar's menus is open. func menu_open() -> bool: return _menus.any(func(menu: PopupMenu) -> bool: return menu.visible) ## A menu button whose items wrap the selection in [label, open, close] tags. func _menu(label: String, items: Array, ink: Color, node_name: String = "", tip: String = "") -> PopupMenu: var button := _small(SheetFieldViews.step_button(label + " ▾", ink, MenuButton.new())) button.name = node_name if node_name != "" else label button.tooltip_text = tip if tip != "" else label add_child(button) var menu := (button as MenuButton).get_popup() ModalCard.style_popup(menu) for i in items.size(): menu.add_item(str(items[i][0]), i) menu.id_pressed.connect(func(id: int) -> void: var current: Array = menu.get_meta("items", items) if id >= 0 and id < current.size(): FormatMenu.wrap_selection(edit, str(current[id][1]), str(current[id][2]))) menu.popup_hide.connect(func() -> void: menu_closed.emit()) _menus.append(menu) return menu ## The sheet's @names, each putting {@name} at the cursor. func _fill_values(menu: PopupMenu) -> void: menu.clear() var items: Array = [] for value_name: String in SheetValues.vars(ed.doc): items.append(["@" + value_name, "{@%s}" % value_name, ""]) menu.add_item("@" + value_name, items.size() - 1) if items.is_empty(): menu.add_item("No values yet: turn on Use in tables in a box (Build)", 999) menu.set_item_disabled(0, true) menu.set_meta("items", items) ## Asks for a Lore Entry, then links the selection to it (or adds its name). func pick_lore() -> void: var selected := FormatMenu.selection(edit) var text := edit.text var picker := LoreLinkPicker.new(ed.m, func(page: BookPage) -> void: var words := "" if selected.x != selected.y else SheetLoreLinks.shown_name(page) var result := TableSettingsRules.wrap_text(text, selected, SheetLoreLinks.open_tag(ed.m, page) + words, "[/url]") ed.set_value(box, str(result["text"]))) ed.m._add_modal(picker)