class_name SheetBoxView extends Panel ## One box on the sheet canvas: a card with a title band on top (headings and ## frameless boxes have none). Its colour tints the card, band and border ## (look). In Play its field takes input (SheetFieldViews); in Build the ## whole box is a handle: click selects, drag moves (snapped to the grid; Alt ## places freely), the corner grip resizes. const GRIP := 14.0 const MIN_SIZE := Vector2(48, 32) const BAND := 30.0 var ed: SheetEditor var box: SheetBox var colors: Dictionary var _drag := "" # "", "move" or "resize" var _from := Vector2.ZERO var _start := Rect2() var _before: Dictionary = {} var _selected := false ## The box's colours: card, band, border, ink and muted ink. A colour tints ## the card lightly, the band more and the border most; frameless boxes sit ## on the cloth with light ink. static func look(hex: String, frameless: bool) -> Dictionary: if frameless: return {"bg": Color(0, 0, 0, 0), "head": Color(0, 0, 0, 0), "border": Color(0, 0, 0, 0), "ink": Style.ON_BG, "muted": Style.ON_BG_MUTED} if hex == "": return {"bg": Style.CARD, "head": Style.CARD_HEAD, "border": Style.DASH, "ink": Style.INK, "muted": Style.MUTED} var tint := Color(hex) return {"bg": Style.CARD.lerp(tint, 0.1), "head": Style.CARD.lerp(tint, 0.3), "border": Style.CARD.lerp(tint, 0.6), "ink": Style.INK, "muted": Style.MUTED} func setup(p_editor: SheetEditor, p_box: SheetBox) -> void: ed = p_editor box = p_box name = box.id colors = look(box.color, box.frameless) position = Vector2(box.x, box.y) size = Vector2(box.w, box.h) custom_minimum_size = Vector2(box.w, box.h) clip_contents = true # A Panel doesn't grow to fit its content, so a box keeps the size it was given. var col := VBoxContainer.new() col.add_theme_constant_override("separation", 0) col.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) add_child(col) var banded := box.kind != SheetKinds.HEADING and not box.frameless and box.title != "" if banded: col.add_child(_band()) elif box.kind != SheetKinds.HEADING and box.title != "": var caption := Style.make_label(box.title.to_upper(), Style.caps(), 11, colors["muted"]) caption.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS var pad := MarginContainer.new() _margins(pad, 10, 6, 10, 0) pad.add_child(caption) col.add_child(pad) var body := MarginContainer.new() body.size_flags_vertical = SIZE_EXPAND_FILL _margins(body, 10, 6, 10, 8) col.add_child(body) body.add_child(SheetFieldViews.make(ed, box, colors["ink"])) if ed.building(): mouse_filter = Control.MOUSE_FILTER_STOP _ignore_mouse(col) gui_input.connect(_build_input) mouse_default_cursor_shape = Control.CURSOR_MOVE tooltip_text = "Drag to move · corner to resize · Alt: off the grid" paint(false) func _band() -> PanelContainer: var band := PanelContainer.new() band.custom_minimum_size.y = BAND var sb := Style.box(colors["head"], 7, [10, 4, 10, 4]) sb.corner_radius_bottom_left = 0 sb.corner_radius_bottom_right = 0 band.add_theme_stylebox_override("panel", sb) var title := Style.make_label(box.title, Style.display(), 17, colors["ink"]) title.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS title.vertical_alignment = VERTICAL_ALIGNMENT_CENTER band.add_child(title) return band static func _margins(c: MarginContainer, left: int, top: int, right: int, bottom: int) -> void: c.add_theme_constant_override("margin_left", left) c.add_theme_constant_override("margin_top", top) c.add_theme_constant_override("margin_right", right) c.add_theme_constant_override("margin_bottom", bottom) func paint(selected: bool) -> void: _selected = selected var sb := Style.box(colors["bg"], 8, [0, 0, 0, 0]) if box.frameless: if ed.building(): sb = Style.bordered(sb, 1, Color(Style.ON_BG, 0.35)) else: sb = Style.bordered(sb, 1, colors["border"]) if selected: sb = Style.bordered(sb, 2, Style.SELECTED) sb.shadow_color = Color(Style.SELECTED, 0.45) sb.shadow_size = 9 add_theme_stylebox_override("panel", sb) queue_redraw() func _draw() -> void: if not ed.building(): return var c := Style.SELECTED if _selected else Color(colors["muted"], 0.7) var corner := size - Vector2(4, 4) for i in 3: var d := 4.0 + i * 4.0 draw_line(corner - Vector2(d, 0), corner - Vector2(0, d), c, 1.5, true) func _ignore_mouse(node: Node) -> void: for child in node.get_children(): if child is Control: (child as Control).mouse_filter = Control.MOUSE_FILTER_IGNORE _ignore_mouse(child) func _build_input(ev: InputEvent) -> void: var mb := ev as InputEventMouseButton if mb and mb.button_index == MOUSE_BUTTON_LEFT: if mb.pressed: ed.select(box.id) var in_grip := mb.position.x > size.x - GRIP * 1.5 and mb.position.y > size.y - GRIP * 1.5 _drag = "resize" if in_grip else "move" _from = mb.global_position _start = box.rect() _before = ed.doc.to_dict() elif _drag != "": end_drag() ed.canvas._fit_area() accept_event() return var mm := ev as InputEventMouseMotion if mm and _drag != "": drag_to(mm.global_position - _from, Input.is_key_pressed(KEY_ALT)) accept_event() ## Moves or resizes by `delta` screen pixels from where the drag began. func drag_to(delta: Vector2, free: bool) -> void: var r := _start if _drag == "move": r.position = (_start.position + delta).max(Vector2.ZERO) if not free: r.position = Vector2(SheetKinds.snap(r.position.x), SheetKinds.snap(r.position.y)) else: r.size = (_start.size + delta).max(MIN_SIZE) if not free: r.size = Vector2(maxf(SheetKinds.snap(r.size.x), MIN_SIZE.x), maxf(SheetKinds.snap(r.size.y), MIN_SIZE.y)) box.set_rect(r) position = r.position custom_minimum_size = r.size size = r.size ed.update_status() ## Starts a drag without a mouse press (tests use it). func begin_drag(kind: String) -> void: _drag = kind _start = box.rect() _before = ed.doc.to_dict() func end_drag() -> void: _drag = "" ed.record_from(_before, "Resize box" if _start.size != box.rect().size else "Move box")