class_name SheetCanvas extends ScrollContainer ## The open tab's canvas: the theme's spread background (BoardBackdrop) with ## a free layout of boxes (SheetBoxView) centred on it, scrolling when the ## boxes reach past the window. In Build it shows the snap grid as dots; ## clicking empty space clears the selection. The field palette drops new ## boxes here (drop_point). ## Room kept around the boxes, and below them to place more. const MARGIN := 16.0 const ROOM := 240.0 var ed: SheetEditor ## Fills the scroll area; draws the background. var holder: Control ## The page the boxes sit on (box coordinates start at its top-left). var area: Control var views: Dictionary = {} # box id -> SheetBoxView var _backdrop: BoardBackdrop func setup(p_editor: SheetEditor) -> void: ed = p_editor add_theme_stylebox_override("panel", StyleBoxEmpty.new()) _backdrop = BoardBackdrop.new(ThemeStore.current) holder = Control.new() holder.mouse_filter = Control.MOUSE_FILTER_STOP holder.draw.connect(_draw_back) holder.gui_input.connect(_area_input) add_child(holder) area = Control.new() area.mouse_filter = Control.MOUSE_FILTER_PASS holder.add_child(area) resized.connect(_fit_area) func rebuild() -> void: for c in area.get_children(): area.remove_child(c) c.queue_free() views.clear() for box: SheetBox in ed.current_tab()["boxes"]: var view := SheetBoxView.new() view.setup(ed, box) area.add_child(view) views[box.id] = view paint_selection() _fit_area() func paint_selection() -> void: for id: String in views: (views[id] as SheetBoxView).paint(id == ed.selected) ## Sizes the page to its boxes (plus room below), centres it across, and ## grows the scroll area to hold it. func _fit_area() -> void: var page := Vector2.ZERO for view: SheetBoxView in views.values(): page = page.max(view.position + view.size) page += Vector2(MARGIN, ROOM) var view_size := size var x := maxf(MARGIN, floorf((view_size.x - page.x) / 2.0)) area.position = Vector2(x, MARGIN) area.size = Vector2(maxf(page.x, view_size.x - x - MARGIN), maxf(page.y, view_size.y - MARGIN * 2)) holder.custom_minimum_size = (area.position + area.size + Vector2(MARGIN, MARGIN)).max(view_size) holder.queue_redraw() func _draw_back() -> void: _backdrop.draw(holder, Rect2(Vector2.ZERO, holder.size)) if ed.doc and ed.doc.tint != "": holder.draw_rect(Rect2(Vector2.ZERO, holder.size), Color(Color(ed.doc.tint), 0.28)) _draw_grid() ## The snap grid as dots over the whole canvas (Build only), lined up with the ## page's corner so boxes snap onto the dots. func _draw_grid() -> void: if ed.doc == null or not ed.building(): return var step := SheetKinds.GRID var dot := Color(Style.ON_BG, 0.2) var start := area.position.posmod(step) var y := start.y while y <= holder.size.y: var x := start.x while x <= holder.size.x: holder.draw_circle(Vector2(x, y), 1.0, dot) x += step y += step func _area_input(ev: InputEvent) -> void: var mb := ev as InputEventMouseButton if mb and mb.pressed and mb.button_index == MOUSE_BUTTON_LEFT and ed.selected != "": ed.select("") ## The middle of what is on screen, in page coordinates. func visible_center() -> Vector2: return Vector2(scroll_horizontal, scroll_vertical) + size / 2.0 - area.position ## A screen point in page coordinates, or Vector2.INF when it isn't over the canvas. func drop_point(screen_point: Vector2) -> Vector2: if not is_visible_in_tree() or not get_global_rect().has_point(screen_point): return Vector2.INF return screen_point - area.global_position