class_name SheetFieldsSmart extends RefCounted ## Smart boxes: a formula worked out from other boxes (the rule language with ## the sheet's values as @names, SheetValues), and a dice button that rolls ## its dice with them and shows the result. static func make(ed: SheetEditor, box: SheetBox, look: Dictionary) -> Control: var ink: Color = look["ink"] if box.kind == SheetKinds.FORMULA: return _formula(ed, box, ink) return _dice(ed, box, ink) static func _formula(ed: SheetEditor, box: SheetBox, ink: Color) -> Control: var result := SheetValues.formula(ed.doc, box) var col := VBoxContainer.new() col.alignment = BoxContainer.ALIGNMENT_CENTER var value := Style.make_label(SheetValues.show(result["value"]) if result["error"] == "" else "?", Style.display(), 26, ink) value.name = "Value" value.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER col.add_child(value) var expr := str(box.config.get("expr", "")) var line := SheetFieldViews.note(str(result["error"]) if result["error"] != "" else "ƒ " + expr, ink, 12) line.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER line.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS line.autowrap_mode = TextServer.AUTOWRAP_OFF col.add_child(line) col.tooltip_text = "Worked out from: %s" % expr return col ## [Roll], then the dice and the last result under it. static func _dice(ed: SheetEditor, box: SheetBox, ink: Color) -> Control: var col := VBoxContainer.new() col.alignment = BoxContainer.ALIGNMENT_CENTER col.add_theme_constant_override("separation", 4) var dice := str(box.config.get("dice", "1d20")) var b := SheetFieldViews.step_button("Roll", ink) b.tooltip_text = "Roll %s" % dice b.name = "Roll" b.custom_minimum_size.y = 32 b.size_flags_horizontal = Control.SIZE_SHRINK_CENTER col.add_child(b) var last := SheetFieldViews.note("%s · %s" % [dice, str(box.value) if box.value != null else "not rolled yet"], ink, 12) last.name = "Last" last.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER col.add_child(last) b.pressed.connect(func() -> void: var r := SheetValues.evaluate(ed.doc, dice, 0, ed.m.rng) if r["error"] != "": ed.m.toast("%s: %s" % [box.title, r["error"]]) return var text := "Rolled %s" % SheetValues.show(r["value"]) last.text = "%s · %s" % [dice, text] ed.m.toast("%s: %s (%s)" % [box.title, SheetValues.show(r["value"]), dice]) ed.edit_value(box, text)) return col