class_name NewSheetDialog extends ModalCard ## "+ New sheet", like the mockup: a name, then a grid of starts drawn as small ## pictures: a blank canvas (a grid) or one of your sheet templates (its tint ## and a few boxes). The Create button names the pick. Calls ## on_create(name, template_id) ("" = blank). "Import a sheet file…" in the ## footer closes it and calls on_import (when given). var store: SheetStore var name_edit: LineEdit var create_button: Button var _choice := "" var _choices: Dictionary = {} # template id ("" = blank) -> Button var _names: Dictionary = {} # template id -> name var _on_create: Callable func _init(p_store: SheetStore, template_id: String, on_create: Callable, on_import: Callable = Callable()) -> void: super._init("New sheet", 720.0) store = p_store _on_create = on_create _choice = template_id var label := Style.make_label("Name", Style.body_bold(), 15, Style.INK) body.add_child(label) name_edit = LineEdit.new() name_edit.placeholder_text = "e.g. Brin Hollowmere, The Rusty Mule" ModalCard.style_line_edit(name_edit) name_edit.text_submitted.connect(func(_s: String) -> void: _create()) body.add_child(name_edit) var grid := GridContainer.new() grid.columns = 3 grid.add_theme_constant_override("h_separation", 10) grid.add_theme_constant_override("v_separation", 10) body.add_child(grid) var group := ButtonGroup.new() grid.add_child(_option(group, "", "Blank canvas", "One Main tab, a snap grid, nothing else", "")) for row: Dictionary in store.list(true): var tpl := store.load_sheet(str(row["id"]), true) if tpl == null: continue _names[tpl.id] = tpl.name var n := tpl.tabs.size() grid.add_child(_option(group, tpl.id, tpl.name, "Your template · %d %s" % [n, "tab" if n == 1 else "tabs"], tpl.tint)) if on_import.is_valid(): var import_button := add_button("Import a sheet file…", "plain", func() -> void: close() on_import.call()) import_button.name = "Import" var hint := Style.make_label("Templates: in a sheet, Save as template…", Style.body(), 13, Style.MUTED) hint.size_flags_horizontal = SIZE_EXPAND_FILL hint.size_flags_vertical = SIZE_SHRINK_CENTER footer.add_child(hint) add_button("Cancel", "plain", close) create_button = add_button("", "primary", _create) _update_create() ready.connect(func() -> void: name_edit.grab_focus()) ## One start in the grid: a small picture, a title and a line under it. func _option(group: ButtonGroup, id: String, title: String, sub: String, tint: String) -> Button: var b := Button.new() b.name = ("Blank" if id == "" else id).validate_node_name() b.toggle_mode = true b.button_group = group b.focus_mode = Control.FOCUS_NONE b.custom_minimum_size = Vector2(200, 172) b.size_flags_horizontal = SIZE_EXPAND_FILL var normal := Style.bordered(Style.box(Style.CARD, 10, [12, 12, 12, 12]), 2, Style.RULE) var hover := Style.bordered(Style.box(Style.CARD_SOFT, 10, [12, 12, 12, 12]), 2, Style.DASH) var on := Style.bordered(Style.box(Style.CARD_SOFT, 10, [12, 12, 12, 12]), 2, Style.TRIM) Style.skin_button(b, normal, hover, on, Style.INK) b.button_pressed = id == _choice b.pressed.connect(func() -> void: _choice = id _update_create()) var col := VBoxContainer.new() col.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) col.offset_left = 12 col.offset_top = 12 col.offset_right = -12 col.offset_bottom = -12 col.add_theme_constant_override("separation", 6) col.mouse_filter = Control.MOUSE_FILTER_IGNORE b.add_child(col) var thumb := Control.new() thumb.custom_minimum_size.y = 72 thumb.mouse_filter = Control.MOUSE_FILTER_IGNORE thumb.draw.connect(func() -> void: _draw_thumb(thumb, id == "", tint)) col.add_child(thumb) col.add_child(Style.make_label(title, Style.body_bold(), 15, Style.INK)) var line := Style.make_label(sub, Style.body(), 13, Style.MUTED) line.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART col.add_child(line) _choices[id] = b return b ## Blank: a card with grid lines. Template: its tint (or the cloth) with three boxes. static func _draw_thumb(c: Control, blank: bool, tint: String) -> void: var r := Rect2(Vector2.ZERO, c.size) if blank: c.draw_rect(r, Style.CARD_SOFT) var x := 0.0 while x < r.size.x: c.draw_line(Vector2(x, 0), Vector2(x, r.size.y), Style.RULE, 1.0) x += 12.0 var y := 0.0 while y < r.size.y: c.draw_line(Vector2(0, y), Vector2(r.size.x, y), Style.RULE, 1.0) y += 12.0 return c.draw_rect(r, Color(tint).darkened(0.45) if tint != "" else Style.BG) var box := Style.CARD if tint == "" else Style.CARD.lerp(Color(tint), 0.15) c.draw_rect(Rect2(8, 8, r.size.x * 0.38, r.size.y * 0.5), box) c.draw_rect(Rect2(r.size.x * 0.56 - 8, 8, r.size.x * 0.44, r.size.y * 0.3), box) c.draw_rect(Rect2(r.size.x * 0.56 - 8, r.size.y * 0.7 - 8, r.size.x * 0.44, r.size.y * 0.3), box) func _update_create() -> void: create_button.text = "Create blank sheet" if _choice == "" else "Create from %s" % _names.get(_choice, "template") ## Picks a start (tests use it). func choose(id: String) -> void: if _choices.has(id): (_choices[id] as Button).button_pressed = true _choice = id _update_create() func _create() -> void: var name := name_edit.text.strip_edges() if name == "": name = "New %s" % _names[_choice] if _choice != "" else "Untitled sheet" close() _on_create.call(name, _choice)