class_name ModifierEditor extends VBoxContainer ## Builds a roll modifier in plain words, for roll tables (dice_mod) and ## check cards (check_mod): [Add ▾] [Luck ▾]. The ways: ## none "" ## Add a value "@luck" (or a roll value such as an input: "tier") ## Subtract a value "-@fatigue" ## Add a value's modifier "dm(@str)" (the 2D-game characteristic modifier) ## Add a number "2", "-1" ## Custom any expression, typed as text ## A modifier that fits none of the plain forms opens as Custom. In Text view ## (plain = false) it's only the text field. `changed` gives the new text. signal changed(text: String) enum Way { NONE, ADD, SUBTRACT, DM, NUMBER, CUSTOM } const WAY_LABELS := ["No modifier", "Add", "Subtract", "Add the modifier of", "Add the number", "Custom…"] const DM_NOTE := "A value's modifier is dm(value): −1 at 3–5, 0 at 6–8, +1 at 9–11 and so on." var main: Main var text := "" var way: int = Way.NONE var plain := true ## The inspector tag key for the text field (keeps focus across rebuilds). var tag_key := "" ## Parts, for tests: the way dropdown, value picker, number and text fields. var way_menu: OptionButton var picker: ValuePicker var number: LineEdit var field: LineEdit var _value := "" var _row: HFlowContainer var _foot: VBoxContainer func _init(p_main: Main, p_text: String, p_plain: bool = true, p_tag: String = "") -> void: main = p_main text = p_text.strip_edges() plain = p_plain tag_key = p_tag add_theme_constant_override("separation", 5) var p := parse(text) way = p["way"] _value = p["value"] _build() ## The plain form of a modifier: {"way": Way, "value": String}. The value is ## the value's name ("@luck", "tier") or the number as text. static func parse(src: String) -> Dictionary: var s := src.strip_edges() if s == "": return {"way": Way.NONE, "value": ""} var toks := Builder.tokens(s) var kinds := PackedStringArray() for tk: Dictionary in toks: kinds.append(str(tk["t"]) if str(tk["t"]) != "op" else str(tk["s"])) var shape := " ".join(kinds) match shape: "var", "name": return {"way": Way.ADD, "value": toks[0]["s"]} "- var", "- name": return {"way": Way.SUBTRACT, "value": toks[1]["s"]} "num", "- num": if s.replace(" ", "").is_valid_int(): return {"way": Way.NUMBER, "value": s.replace(" ", "")} "name ( var )", "name ( name )": if toks[0]["s"] == "dm": return {"way": Way.DM, "value": toks[2]["s"]} return {"way": Way.CUSTOM, "value": s} ## The modifier text for a way and value ("" when a value is still missing). static func compose(p_way: int, value: String) -> String: var v := value.strip_edges() match p_way: Way.ADD: return v Way.SUBTRACT: return ("-" + v) if v != "" else "" Way.DM: return ("dm(%s)" % v) if v != "" else "" Way.NUMBER, Way.CUSTOM: return v return "" ## Switches the way (as the dropdown does). A way that needs a value keeps ## the current one when it fits, and waits for a pick otherwise. func set_way(w: int) -> void: var keep := _value if way in [Way.ADD, Way.SUBTRACT, Way.DM] and w in [Way.ADD, Way.SUBTRACT, Way.DM] else "" if w == Way.NUMBER: keep = _value if way == Way.NUMBER else "1" elif w == Way.CUSTOM: keep = text way = w _value = keep _build() var t := compose(way, _value) if w == Way.NONE or t != "": _emit(t) ## Picks the value (as the value picker does). func choose_value(n: String) -> void: _value = n if picker: picker.set_value(n) _emit(compose(way, _value)) func _emit(t: String) -> void: if t == text: return text = t changed.emit(t) func _build() -> void: for c in get_children(): remove_child(c) c.queue_free() way_menu = null picker = null number = null field = null _row = InspectorKit.flow() add_child(_row) _foot = InspectorKit.vbox(3) add_child(_foot) if not plain: _add_field() return way_menu = InspectorKit.dropdown(WAY_LABELS, way) way_menu.item_selected.connect(set_way) _row.add_child(way_menu) match way: Way.ADD, Way.SUBTRACT, Way.DM: picker = ValuePicker.new(main, _value) picker.kinds = ["number"] picker.set_value(_value) picker.value_chosen.connect(choose_value) _row.add_child(picker) if way == Way.DM: _foot.add_child(InspectorKit.note(DM_NOTE, Style.MUTED, 12)) Way.NUMBER: number = InspectorKit.amount(_value, "1") if tag_key != "": InspectorKit.tag(number, tag_key) number.text_changed.connect(func(s: String) -> void: var ok := s.strip_edges().is_valid_int() InspectorKit.mark(number, ok) if ok: _value = str(s.strip_edges().to_int()) _emit(_value) ) _row.add_child(number) Way.CUSTOM: _add_field() func _add_field() -> void: field = InspectorKit.line(text, "e.g. @luck, -@fatigue or dm(@str)") InspectorKit.code(field) if tag_key != "": InspectorKit.tag(field, tag_key) var err := InspectorKit.error_label() field.text_changed.connect(func(s: String) -> void: var msg := InspectorKit.expr_error(s, "The modifier") if s.strip_edges() != "" else "" InspectorKit.show_error(err, msg) InspectorKit.mark(field, msg == "") if msg == "": _emit(s.strip_edges()) ) field.size_flags_horizontal = Control.SIZE_EXPAND_FILL if plain: _foot.add_child(field) else: _row.add_child(field) _foot.add_child(err)