class_name InspectorRouter extends RefCounted ## The router card's part of the inspector: its list of cases, each with the ## condition that picks it, and the last case that can be "Otherwise". ## InspectorCard.build() calls build() for router cards. static func build(insp: Inspector, box: VBoxContainer, card: RollTable) -> void: var column := InspectorKit.vbox(8) for entry in card.entries: column.add_child(case_block(insp, card, entry, true)) var add := InspectorKit.link("+ Add case", "Add a case before the Otherwise case") add.pressed.connect(func() -> void: add_case(insp)) column.add_child(add) var lint := InspectorKit.error_label() var fill := func() -> void: var msgs := PackedStringArray() for issue: Dictionary in Roller.lint_router(card): msgs.append(str(issue["message"]) + ".") InspectorKit.show_error(lint, "\n".join(msgs)) fill.call() insp._live.append(fill) column.add_child(lint) box.add_child(InspectorKit.field("Cases (the first that holds is taken)", column)) ## One router case: its condition, or Otherwise for the last case. static func case_block(insp: Inspector, card: RollTable, entry: TableEntry, framed: bool = false) -> Control: var index := card.entries.find(entry) var last := index == card.entries.size() - 1 var column := InspectorKit.vbox(6) if framed: var head := InspectorKit.hbox(6) var cap := InspectorKit.word("%d. %s" % [index + 1, Inspector._title_of(entry.text, "(no text)")], Style.INK, true) cap.size_flags_horizontal = Control.SIZE_EXPAND_FILL cap.clip_text = true head.add_child(cap) var open := InspectorKit.link("Edit", "Edit this case's result") open.pressed.connect(func() -> void: insp.select({"kind": "result", "tid": card.id, "eid": entry.id})) head.add_child(open) if card.entries.size() > 1: var delete_button := InspectorKit.xbutton("Delete this case") delete_button.pressed.connect(func() -> void: insp.main.delete_entry(card.id, entry.id)) head.add_child(delete_button) column.add_child(head) var other := entry.when.strip_edges() == "" if last: var otherwise_box := InspectorKit.check_box("Otherwise (when no case above holds)", other) otherwise_box.toggled.connect(func(on: bool) -> void: if on: insp.set_case_when(entry.id, "") insp.rebuild() else: insp.set_meta("open_case", entry.id) insp.rebuild() ) column.add_child(otherwise_box) var opening := insp.has_meta("open_case") and str(insp.get_meta("open_case")) == entry.id if not (last and other) or opening: var cond := ConditionBuilder.new() cond.setup(insp.main, entry.when, not insp.main.rules_plain(), "case.%s" % entry.id, {}) if other and cond.clauses.is_empty() and not cond.text_mode: cond.add_clause() cond.changed.connect(func(text: String) -> void: if text.strip_edges() != "": insp.set_case_when(entry.id, text)) if not framed: insp.case_builder = cond var cap := "Taken when" if framed else "This case is taken when" var field_column := InspectorKit.vbox(4) field_column.add_child(InspectorKit.flabel(cap)) field_column.add_child(cond) if other and not last: field_column.add_child(InspectorKit.note("Empty, so this case is an Otherwise and the cases after it never run.", Style.WARN_INK, 13)) column.add_child(field_column) if opening: insp.remove_meta("open_case") if framed: var frame := PanelContainer.new() frame.add_theme_stylebox_override("panel", Style.bordered(Style.box(Style.CARD_SOFT, 8, [10, 8, 10, 9]), 1, Style.RULE)) frame.add_child(column) return frame return column ## Adds a case before a final Otherwise case (or at the end). static func add_case(insp: Inspector) -> void: var card := insp.selected_table() if card == null: return var entry := TableEntry.new(TowerBoard.new_id("e"), "Case %d" % (card.entries.size() + 1)) var at := card.entries.size() if not card.entries.is_empty() and card.entries.back().when.strip_edges() == "": at -= 1 card.entries.insert(at, entry) insp.set_meta("open_case", entry.id) insp.wrote(card, false, true)