class_name TemplateForms extends RefCounted ## Card-styled dialogs for templates: saving one (name, description and the ## detected {$parameters}), filling in parameters before an insert, and the ## small board preview the template list shows. ## The "Save as template" dialog. `on_save` gets the template with the name, ## description and parameter labels and defaults the user typed. static func save_dialog(tpl: Dictionary, on_save: Callable) -> ModalCard: var d := ModalCard.new("Save as template", 580.0) var top := HBoxContainer.new() top.add_theme_constant_override("separation", 16) var pv := preview(tpl, Vector2(150, 92)) pv.size_flags_vertical = Control.SIZE_SHRINK_BEGIN top.add_child(pv) var fields := VBoxContainer.new() fields.add_theme_constant_override("separation", 10) fields.size_flags_horizontal = Control.SIZE_EXPAND_FILL top.add_child(fields) var name_edit := LineEdit.new() name_edit.text = str(tpl.get("name", "")) ModalCard.style_line_edit(name_edit) fields.add_child(ModalCard.field("Name", name_edit)) fields.add_child(ModalCard.note(TemplateKit.summary(tpl))) d.body.add_child(top) var desc := TextEdit.new() desc.text = str(tpl.get("description", "")) desc.placeholder_text = "What it's for, and what to fill in (shown in the template list)" desc.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY desc.custom_minimum_size = Vector2(0, 64) desc.scroll_fit_content_height = true ModalCard.style_text_edit(desc) d.body.add_child(ModalCard.field("Description", desc)) var outside := 0 for t: Dictionary in tpl.get("tables", []): for e: Dictionary in t.get("entries", []): for p: Variant in e.get("pipes", []): if p is Dictionary and bool((p as Dictionary).get("outside", false)): outside += 1 if outside > 0: d.body.add_child(ModalCard.note("%d %s to cards outside the template. When it's inserted they relink to a card with the same name, if there is one." % [outside, "pipe leads" if outside == 1 else "pipes lead"])) d.body.add_child(ModalCard.section("Parameters")) var rows: Array = [] var params: Array = tpl.get("params", []) if params.is_empty(): d.body.add_child(ModalCard.note("None found. Write {$name} in any text (card names, results, changes, expressions, prompts, variable names) and it becomes a parameter you fill in when inserting.")) else: d.body.add_child(ModalCard.note("Found in the cards' text. Give each a label and a default for the insert form.")) for p: Dictionary in params: var row := HBoxContainer.new() row.add_theme_constant_override("separation", 8) var chip := PanelContainer.new() chip.add_theme_stylebox_override("panel", Style.bordered(Style.box(Color(Style.ACCENT, 0.18), 6, [8, 4, 8, 4]), 1, Color(Style.ACCENT_DARK, 0.4))) chip.custom_minimum_size = Vector2(120, 0) chip.size_flags_vertical = Control.SIZE_SHRINK_CENTER chip.add_child(Style.make_label("{$%s}" % p["name"], Style.body_bold(), 14, Style.TRIM_DARK)) row.add_child(chip) var label := LineEdit.new() label.text = str(p.get("label", "")) label.placeholder_text = "Label" ModalCard.style_line_edit(label) row.add_child(label) var def := LineEdit.new() def.text = str(p.get("default", "")) def.placeholder_text = "Default" ModalCard.style_line_edit(def) row.add_child(def) d.body.add_child(row) rows.append([p, label, def]) d.add_button("Cancel", "plain", d.close) d.add_button("Save template", "primary", func() -> void: var out := tpl.duplicate(true) out["name"] = name_edit.text.strip_edges() if name_edit.text.strip_edges() != "" else "Untitled template" out["description"] = desc.text.strip_edges() var plist: Array = [] for r: Array in rows: var p: Dictionary = r[0] plist.append({"name": p["name"], "label": (r[1] as LineEdit).text.strip_edges(), "default": (r[2] as LineEdit).text}) out["params"] = plist d.close() on_save.call(out) ) d.ready.connect(func() -> void: name_edit.grab_focus() name_edit.select_all() ) return d ## Asks for the template's parameter values; `on_ok` gets {name: value}. static func params_dialog(tpl: Dictionary, on_ok: Callable) -> ModalCard: var d := ModalCard.new("Insert ā€œ%sā€" % str(tpl.get("name", "template")), 480.0) if str(tpl.get("description", "")) != "": d.body.add_child(ModalCard.note(str(tpl["description"]))) var edits: Array = [] for p: Dictionary in tpl.get("params", []): var le := LineEdit.new() le.text = str(p.get("default", "")) le.placeholder_text = "{$%s}" % p["name"] ModalCard.style_line_edit(le) var label := str(p.get("label", "")) d.body.add_child(ModalCard.field(label if label != "" else str(p["name"]).capitalize(), le)) edits.append([str(p["name"]), le]) var accept := func() -> void: var values := {} for pair: Array in edits: values[pair[0]] = (pair[1] as LineEdit).text d.close() on_ok.call(values) for pair: Array in edits: (pair[1] as LineEdit).text_submitted.connect(func(_t: String) -> void: accept.call()) d.add_button("Cancel", "plain", d.close) d.add_button("Insert", "primary", accept) d.ready.connect(func() -> void: if not edits.is_empty(): (edits[0][1] as LineEdit).grab_focus() (edits[0][1] as LineEdit).select_all() ) return d ## A small picture of the template's layout: group frames on a felt tile and ## each card as a little paper rectangle (choice cards brass, loops copper, ## variables and sheets outlined). static func preview(tpl: Dictionary, box: Vector2 = Vector2(120, 74)) -> Control: var c := Control.new() c.custom_minimum_size = box c.mouse_filter = Control.MOUSE_FILTER_IGNORE c.draw.connect(func() -> void: c.draw_style_box(Style.box(Style.BG, 8), Rect2(Vector2.ZERO, box)) var ext := TemplateKit.extent(tpl) if ext.x <= 0.0 or ext.y <= 0.0: return var inner := box - Vector2(16, 16) var s := minf(inner.x / ext.x, inner.y / ext.y) var off := Vector2(8, 8) + (inner - ext * s) / 2.0 for g: Dictionary in tpl.get("groups", []): var gr := Rect2(off + Vector2(float(g.get("x", 0)), float(g.get("y", 0))) * s, Vector2(float(g.get("w", 0)), float(g.get("h", 0))) * s) c.draw_style_box(Style.box(Color(0, 0, 0, 0.22), 4), gr) for t: Dictionary in tpl.get("tables", []): var r := Rect2(off + Vector2(float(t.get("x", 0)), float(t.get("y", 0))) * s, TemplateKit.CARD_SIZE * s * Vector2(1.0, 0.8)) var kind := str(t.get("kind", "roll")) var fill := Style.CARD if kind == "choice": fill = Style.LIT c.draw_style_box(Style.box(fill, 3), r) var band := Color(Style.TRIM, 0.9) if kind == "loop" else (Style.BG_HOVER if kind == "variable" else Style.RULE) if kind == "sheet": band = Style.ACCENT_DARK c.draw_rect(Rect2(r.position + Vector2(0, 2), Vector2(r.size.x, maxf(2.0, r.size.y * 0.16))), band) ) return c