TableTower/ui/sheet_canvas.gd

204 lines
6.9 KiB
GDScript3
Raw Normal View History

class_name SheetCanvas
extends ScrollContainer
## The open tab's canvas: the theme's spread background (BoardBackdrop) with
## a free layout of boxes (SheetBoxView). The page starts at the canvas's
## left edge (not centred: a centred page left no room to drag boxes left of
## it), with a screen of room past its boxes to drag into.
## Drag empty space (or middle-drag) to pan; a box dragged near an edge
## scrolls the canvas. In Build it shows the snap grid as dots; clicking empty
## space clears the selection. The field palette drops new boxes here.
## 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
## The page's left edge, set when a tab opens (recentre) and kept after.
var _origin_x := -1.0
var _panning := false
var _pan_from := Vector2.ZERO
var _pan_scroll := Vector2.ZERO
## One grid step with a dot in its corner, drawn tiled (one draw call).
var _grid_tile: ImageTexture
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.texture_repeat = CanvasItem.TEXTURE_REPEAT_ENABLED
_grid_tile = _make_grid_tile()
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()
holder.queue_redraw()
if _origin_x < 0.0:
_fit_area.call_deferred()
else:
_fit_area()
## Lays the page out again the next time the canvas is laid out (a sheet or
## a tab was opened) and scrolls back to the top left.
func recentre() -> void:
_origin_x = -1.0
scroll_horizontal = 0
scroll_vertical = 0
## Redraws every box but `keep_id` (the one being typed in), so formulas and
## live text follow the change.
func rebuild_except(keep_id: String) -> void:
for box: SheetBox in ed.current_tab()["boxes"]:
if box.id == keep_id or not views.has(box.id):
continue
var old: SheetBoxView = views[box.id]
var view := SheetBoxView.new()
view.setup(ed, box)
area.add_child(view)
area.move_child(view, old.get_index())
area.remove_child(old)
old.queue_free()
views[box.id] = view
view.paint(box.id == ed.selected)
func paint_selection() -> void:
for id: String in views:
(views[id] as SheetBoxView).paint(id == ed.selected)
## Sizes the page to its boxes plus a screen of room right and below, its
## left edge at the canvas's left margin, 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)
var view_size := size
if _origin_x < 0.0 and view_size.x > 0.0:
_origin_x = MARGIN
area.position = Vector2(maxf(_origin_x, MARGIN), MARGIN)
area.size = page + view_size
var want := (area.position + area.size).max(view_size)
if want != holder.custom_minimum_size:
holder.custom_minimum_size = want
holder.queue_redraw()
## While a box is dragged near an edge, scrolls toward it. Returns how far
## the view moved, so the box can follow.
func edge_scroll(screen_point: Vector2) -> Vector2:
var r := get_global_rect()
var step := Vector2.ZERO
const EDGE := 40.0
const SPEED := 12.0
if screen_point.x > r.end.x - EDGE:
step.x = SPEED
elif screen_point.x < r.position.x + EDGE:
step.x = -SPEED
if screen_point.y > r.end.y - EDGE:
step.y = SPEED
elif screen_point.y < r.position.y + EDGE:
step.y = -SPEED
if step == Vector2.ZERO:
return step
var before := Vector2(scroll_horizontal, scroll_vertical)
if before.x + size.x + step.x > holder.custom_minimum_size.x or before.y + size.y + step.y > holder.custom_minimum_size.y:
_fit_area()
scroll_horizontal = int(before.x + step.x)
scroll_vertical = int(before.y + step.y)
return Vector2(scroll_horizontal, scroll_vertical) - before
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: one tiled texture.
func _draw_grid() -> void:
if ed.doc == null or not ed.building():
return
var start := area.position.posmod(SheetKinds.GRID) - Vector2.ONE * SheetKinds.GRID
holder.draw_texture_rect(_grid_tile, Rect2(start, holder.size + Vector2.ONE * SheetKinds.GRID * 2.0), true)
static func _make_grid_tile() -> ImageTexture:
var n := int(SheetKinds.GRID)
var img := Image.create(n, n, false, Image.FORMAT_RGBA8)
img.fill(Color(0, 0, 0, 0))
var dot := Color(Style.ON_BG, 0.22)
for p: Vector2i in [Vector2i(0, 0), Vector2i(1, 0), Vector2i(0, 1), Vector2i(1, 1)]:
img.set_pixelv(p, dot)
return ImageTexture.create_from_image(img)
## Empty space: a click clears the selection; a left- or middle-drag pans.
func _area_input(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb and (mb.button_index == MOUSE_BUTTON_LEFT or mb.button_index == MOUSE_BUTTON_MIDDLE):
if mb.pressed:
if mb.button_index == MOUSE_BUTTON_LEFT and ed.selected != "":
ed.select("")
_panning = true
_pan_from = mb.global_position
_pan_scroll = Vector2(scroll_horizontal, scroll_vertical)
holder.mouse_default_cursor_shape = Control.CURSOR_DRAG
else:
_panning = false
holder.mouse_default_cursor_shape = Control.CURSOR_ARROW
holder.accept_event()
return
var mm := ev as InputEventMouseMotion
if mm and _panning:
pan_to(_pan_scroll - (mm.global_position - _pan_from))
holder.accept_event()
## Scrolls to `at` (scroll pixels), growing the canvas when needed.
func pan_to(at: Vector2) -> void:
if at.x + size.x > holder.custom_minimum_size.x or at.y + size.y > holder.custom_minimum_size.y:
_fit_area()
scroll_horizontal = int(maxf(at.x, 0))
scroll_vertical = int(maxf(at.y, 0))
## 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