class_name FieldPalette extends VBoxContainer ## The sheet editor's field palette: every kind of box (SheetKinds) in a ## PaletteList, like the spread's card palette. Click adds a box in the middle ## of the canvas; drag drops it where you let go. Greyed in Play under a ## notice with Switch to Build. const MESSAGE := "Fields are added in Build mode." var ed: SheetEditor var list: PaletteList var notice: PanelContainer func setup(p_editor: SheetEditor) -> void: ed = p_editor add_theme_constant_override("separation", 8) notice = BuildModeHint.make_notice(ed.m, func() -> void: ed.set_mode(Main.Mode.BUILD), MESSAGE) add_child(notice) list = PaletteList.new() list.setup("Search fields", "Drag onto the sheet, or click to add", "Add a field", "“number”, “text” or “title”") list.size_flags_vertical = SIZE_EXPAND_FILL list.sections_for = _sections list.can_use = ed.building list.on_blocked = func() -> void: ed.m.toast(MESSAGE) list.drop_at = drop_at add_child(list) list.rebuild() func _process(_delta: float) -> void: notice.visible = ed.doc != null and not ed.building() func _sections(query: String) -> Array: var out: Array = [] for group in SheetKinds.groups(): var entries: Array = [] for item: Dictionary in SheetKinds.ITEMS: if item["group"] == group and PaletteList.matches(item, query): entries.append([item, _add.bind(str(item["id"]))]) out.append([group, entries]) return out func drop_at(on_add: Callable, screen_point: Vector2) -> bool: var at := ed.canvas.drop_point(screen_point) if at == Vector2.INF: return false list.use(on_add, at) return true func _add(at: Vector2, kind: String) -> void: if ed.phone: ed.phone.palette_open = false ed.add_box(kind, at)