TableTower/ui/sheet_canvas.gd
RobinFiredrake dd0323c53f Sheets phase C: every field kind, details panel, canvas panning
- 25 palette items over 23 kinds: basics, trackers (bars, ticks, clocks,
  dots, step/usage dice, ladders), formulas and dice buttons (rule
  language with sheet @names), lists (rows, mini table, chips, point pool
  that may go negative, bonds), slots, conditions, spell slots, XP,
  currency, heading, divider, picture.
- Per-kind settings in the inspector; hover excerpts and an in-app
  details panel for list entries; live redraw of dependent boxes.
- Canvas: the page stays put after edits (boxes can go anywhere), a
  screen of room past the boxes, drag-to-pan and edge scrolling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 16:28:04 -06:00

192 lines
6.1 KiB
GDScript

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 is centred when a tab opens
## and then stays put, 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
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()
if _origin_x < 0.0:
_fit_area.call_deferred()
else:
_fit_area()
## Centres the page again the next time the canvas is laid out (a sheet or a
## tab was opened) and scrolls back to the top.
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, keeps
## its left edge where the tab opened it (centred then), 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 = maxf(MARGIN, floorf((view_size.x - page.x - MARGIN) / 2.0))
area.position = Vector2(maxf(_origin_x, MARGIN), MARGIN)
area.size = page + view_size
holder.custom_minimum_size = (area.position + area.size).max(view_size)
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)
_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.
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
## 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:
_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