class_name TableCardEditing extends RefCounted ## A TableCard's Build-mode controls: the inline result-text, weight and range ## fields, router case rows with move and delete, the footer (add a result, ## option or case), the card's ⋯ menu (settings, template, collapse, delete) ## and the row × that shows only on hover or selection. Everything else is ## edited in Main's inspector. ## The card's ⋯ menu items. const MENU_SETTINGS := 0 const MENU_TEMPLATE := 1 const MENU_COLLAPSE := 2 const MENU_DELETE := 3 var card: TableCard func _init(p_card: TableCard) -> void: card = p_card ## Build mode: the inline result-text field of a row. Focusing it selects the ## result for the inspector. func text_edit(e: TableEntry, index: int, chosen: bool) -> LineEdit: var table := card.table var edit := LineEdit.new() edit.text = e.text if chosen: edit.placeholder_text = "(the option's name)" edit.tooltip_text = "The result text. Leave it as Chosen (or empty) to show the picked option's name; {option_label}, {option_value} and {@[option]} work" edit.size_flags_horizontal = Control.SIZE_EXPAND_FILL edit.size_flags_vertical = Control.SIZE_SHRINK_CENTER edit.custom_minimum_size = Vector2(40, 0) edit.add_theme_font_size_override("font_size", 15) var loop_rows := TableCardRows.LOOP_ROWS if table.kind == RollTable.KIND_LOOP: edit.placeholder_text = loop_rows[clampi(index, 0, 1)] edit.tooltip_text = "%s: shown each pass ({i} is the pass number)" % loop_rows[0] if index == 0 else "%s: runs once when the loop ends" % loop_rows[1] elif table.kind == RollTable.KIND_ROUTER: edit.placeholder_text = "Result text" edit.text_changed.connect(func(s: String) -> void: e.text = s card.main.mark_dirty() ) edit.focus_entered.connect(func() -> void: card.select_entry(e.id)) return edit ## Build mode: a click on a row (outside its fields) selects the result for ## the inspector. func hook_select(row: Control, eid: String) -> void: row.gui_input.connect(func(ev: InputEvent) -> void: var mb := ev as InputEventMouseButton if mb and mb.button_index == MOUSE_BUTTON_LEFT and mb.pressed: card.select_entry(eid) ) ## Build mode: one router case. The first line is its condition, in plain ## words or as written (edited in the inspector), with move up / down and ## delete; the second is its result text, then its effects chip. func make_case_row(e: TableEntry, index: int, numw: float) -> Control: var table := card.table var main := card.main var plain := card.plain var row := PanelContainer.new() row.mouse_filter = Control.MOUSE_FILTER_PASS row.custom_minimum_size = Vector2(0, 36) var outer := TableCard.hbox(4) row.add_child(outer) var num := Style.make_label(str(index + 1), Style.display(true), 16, Style.MUTED) num.custom_minimum_size = Vector2(numw, 0) num.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT num.size_flags_vertical = Control.SIZE_SHRINK_BEGIN outer.add_child(num) var v := TableCard.vbox(3) v.size_flags_horizontal = Control.SIZE_EXPAND_FILL outer.add_child(v) var top := TableCard.hbox(4) v.add_child(top) var w := e.when.strip_edges() var bad := w != "" and Expr.check(TemplateKit.probe(w)) != "" var cond := Style.make_label(RulesView.case_label(e, main.data, plain), Style.body_bold(true), 14, Style.DANGER_INK if bad else Style.TRIM_DARK) cond.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART cond.size_flags_horizontal = Control.SIZE_EXPAND_FILL cond.size_flags_vertical = Control.SIZE_SHRINK_CENTER cond.mouse_filter = Control.MOUSE_FILTER_PASS cond.tooltip_text = ("⚠ " + Expr.check(TemplateKit.probe(w))) if bad else (("if " + w) if plain and w != "" else "Cases are checked top to bottom; the first true one is chosen") cond.set_meta("case_line", true) top.add_child(cond) top.add_child(mini_button("↑", "Move this case up", index > 0, func() -> void: _move_case(index, -1))) top.add_child(mini_button("↓", "Move this case down", index < table.entries.size() - 1, func() -> void: _move_case(index, 1))) var del := mini_button("×", "Delete this case", true, func() -> void: main.delete_entry(table.id, e.id)) top.add_child(del) v.add_child(text_edit(e, index, false)) card.rows_ui.add_rule_chips(v, e) hook_select(row, e.id) card.rows_ui.register(e, row, num, null, index) reveal_on_hover(e.id, row, del) return row static func mini_button(glyph: String, tip: String, enabled: bool, on_press: Callable) -> Button: var b := Button.new() b.text = glyph b.tooltip_text = tip b.disabled = not enabled b.custom_minimum_size = Vector2(22, 22) b.size_flags_vertical = Control.SIZE_SHRINK_CENTER b.add_theme_font_size_override("font_size", 16 if glyph == "×" else 13) Style.skin_button(b, Style.box(Color(0, 0, 0, 0), 5), Style.box(Style.CARD_HOVER, 5), Style.box(Style.CARD_HOVER, 5), Style.MUTED, Style.DANGER_INK if glyph == "×" else Style.INK) b.add_theme_color_override("font_disabled_color", Color(Style.MUTED, 0.35)) b.pressed.connect(on_press) return b ## Moves router case `index` up (-1) or down (+1). func _move_case(index: int, by: int) -> void: var table := card.table var to := index + by if to < 0 or to >= table.entries.size(): return var e := table.entries[index] table.entries.remove_at(index) table.entries.insert(to, e) card.rebuild() card.main.mark_dirty() ## Adds a router case just before the Otherwise case (or at the end). It ## starts as "if false", so it never matches until its condition is written. func add_case() -> void: var table := card.table var e := TableEntry.new(TowerBoard.new_id("e"), "New case") e.when = "false" var at := table.otherwise_index() if at < 0 or at != table.entries.size() - 1: at = table.entries.size() table.entries.insert(at, e) table.collapsed = false card.rebuild() card.main.mark_dirty() ## Weight on a plain table, shown as ×N. Also the number of copies in card mode. func weight_edit(e: TableEntry) -> LineEdit: var edit := LineEdit.new() edit.text = "×%d" % e.weight edit.custom_minimum_size = Vector2(34, 0) edit.alignment = HORIZONTAL_ALIGNMENT_CENTER edit.max_length = 4 edit.tooltip_text = "Weight: how likely this result is, relative to the others (0 = never)" edit.size_flags_vertical = Control.SIZE_SHRINK_CENTER edit.add_theme_font_override("font", Style.display(true)) edit.add_theme_font_size_override("font_size", 14) edit.add_theme_color_override("font_color", Style.MUTED) var commit := func() -> void: var digits := "" for ch in edit.text: if ch >= "0" and ch <= "9": digits += ch var w := clampi(digits.to_int(), 0, 999) if digits != "" else e.weight edit.text = "×%d" % w if w != e.weight: e.weight = w card.update_deck() card.main.mark_dirty() edit.text_submitted.connect(func(_t: String) -> void: commit.call()) edit.focus_exited.connect(commit) return edit ## Largest range value the card's range boxes take, either way. const RANGE_LIMIT := 99999 func range_edit(e: TableEntry, is_lo: bool) -> LineEdit: var table := card.table var edit := LineEdit.new() edit.text = str(e.range_lo if is_lo else e.range_hi) edit.custom_minimum_size = Vector2(40, 0) edit.alignment = HORIZONTAL_ALIGNMENT_CENTER edit.max_length = 6 edit.tooltip_text = ("Range start" if is_lo else "Range end") + "\nValues past the dice (such as -3 or 24) need a modifier to come up" edit.add_theme_font_override("font", Style.display(true)) edit.add_theme_font_size_override("font_size", 15) edit.add_theme_stylebox_override("normal", Style.bordered(Style.box(Color.WHITE, 4, [2, 2, 2, 2]), 1, Style.RULE)) edit.add_theme_stylebox_override("focus", Style.bordered(Style.box(Color.WHITE, 4, [2, 2, 2, 2]), 1, Style.TRIM)) var commit := func() -> void: # Any whole number: results past the dice are reached with a modifier. var typed := edit.text.strip_edges() var v := clampi(typed.to_int(), -RANGE_LIMIT, RANGE_LIMIT) if typed.is_valid_int() else (e.range_lo if is_lo else e.range_hi) if is_lo: e.range_lo = v else: e.range_hi = v if e.range_lo > e.range_hi: var tmp := e.range_lo e.range_lo = e.range_hi e.range_hi = tmp edit.text = str(e.range_lo if is_lo else e.range_hi) card.update_warning() card.main.mark_dirty() edit.text_submitted.connect(func(_t: String) -> void: commit.call()) edit.focus_exited.connect(commit) return edit func make_footer(ranged: bool) -> Control: var table := card.table var main := card.main var wrap := MarginContainer.new() wrap.mouse_filter = Control.MOUSE_FILTER_PASS wrap.add_theme_constant_override("margin_left", 12) wrap.add_theme_constant_override("margin_right", 10) wrap.add_theme_constant_override("margin_bottom", 10) var h := TableCard.hbox(8) wrap.add_child(h) # Loops, sheets, variables and number/text choices have fixed rows. if table.kind == RollTable.KIND_ROLL or (table.kind == RollTable.KIND_CHOICE and table.choice == "pick"): var label := "+ Add range" if ranged else ("+ Add option" if table.kind == RollTable.KIND_CHOICE else "+ Add result") var add := footer_button(label, Style.INK) add.pressed.connect(func() -> void: main.add_entry(table.id)) h.add_child(add) elif table.kind == RollTable.KIND_ROUTER: var addc := footer_button("+ Add case", Style.INK) addc.tooltip_text = "Add a case before Otherwise" addc.pressed.connect(add_case) h.add_child(addc) # Deleting the table lives in the card's ⋯ menu. return wrap static func footer_button(text: String, hover_color: Color) -> Button: var b := Button.new() b.text = text b.add_theme_font_size_override("font_size", 14) var normal := Style.bordered(Style.box(Color(0, 0, 0, 0), 6, [10, 4, 10, 4]), 1, Style.DASH) var hover := Style.bordered(Style.box(Color(0, 0, 0, 0), 6, [10, 4, 10, 4]), 1, hover_color if hover_color != Style.INK else Style.MUTED) Style.skin_button(b, normal, hover, hover, Style.MUTED, hover_color) return b ## Build mode's "⋯" menu on the card: settings, save as template, collapse ## and delete (the same pattern as a group card's ⋯ menu). func make_card_menu() -> MenuButton: var table := card.table var main := card.main var mb := Style.overflow_button("Table actions") var pm := mb.get_popup() pm.add_item("Table settings…", MENU_SETTINGS) pm.add_item("Save as template…", MENU_TEMPLATE) if card.can_collapse(): pm.add_item("Expand" if table.collapsed else "Collapse", MENU_COLLAPSE) pm.add_separator() pm.add_item("Delete table", MENU_DELETE) pm.id_pressed.connect(func(id: int) -> void: match id: MENU_SETTINGS: main.request_table_settings(table.id) MENU_TEMPLATE: main.save_template_from([table.id], "card") MENU_COLLAPSE: main.toggle_collapse(table.id) MENU_DELETE: main.delete_table(table.id) ) return mb ## Build mode: a row's delete × shows only while the row is hovered, the × ## has keyboard focus, or the row is the inspector's selected result. func reveal_on_hover(eid: String, row: Control, button: Button) -> void: card.rows_ui.parts[eid]["delete"] = button row.mouse_entered.connect(func() -> void: row.set_meta("hovered", true) sync_delete(eid)) row.mouse_exited.connect(func() -> void: # Moving onto a child control still counts as inside the row. row.set_meta("hovered", row.get_global_rect().has_point(row.get_global_mouse_position())) sync_delete(eid)) button.focus_entered.connect(sync_delete.bind(eid)) button.focus_exited.connect(sync_delete.bind(eid)) sync_delete(eid) ## Shows or hides a row's ×; TableCardRows.set_row_state calls it too. func sync_delete(eid: String) -> void: var b: Button = card.rows_ui.parts.get(eid, {}).get("delete") if b == null or not is_instance_valid(b): return var row: Control = card.rows.get(eid) var hovered := row != null and bool(row.get_meta("hovered", false)) b.modulate.a = 1.0 if hovered or b.has_focus() or eid == card.selected_entry() else 0.0