TableTower/ui/board_view.gd
RobinFiredrake 60c2d03147 Build the pipe-board app in Godot
Tables are cards on a felt board, joined by copper pipes that carry a
roll from one table to the next. Play mode rolls with a flicker and a
bead down each pipe; Build mode edits names, results, ranges and pipes.

- core/: data model (RollTable, TableEntry, TowerBoard), sample board
  with a d100 table of 100 trinkets and a weighted d100 range table,
  and a Roller with a depth limit
- ui/: board with pan and zoom, pipe and port layers, collapsible table
  cards, result tray with earlier rolls, undo toasts, and a Collapse all
  / Expand all button
- assets/fonts/: Rokkitt and Atkinson Hyperlegible (SIL OFL)
- tests/test_roller.gd: headless checks for the model and roller

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:05:41 -06:00

154 lines
4.3 KiB
GDScript

class_name BoardView
extends Control
## The felt board. Holds a scaled "world" with three layers: pipes behind the
## cards, the cards, and the ports (pipe ends) on top. Drag the felt to pan,
## Ctrl+wheel or pinch to zoom.
const MIN_ZOOM := 0.35
const MAX_ZOOM := 1.5
const WORLD_SIZE := 40000.0
var main: Main
var world: Control
var pipes: PipeLayer
var cards: CardLayer
var ports: PortLayer
var zoom: float = 1.0
var _panning := false
var _glow: GradientTexture2D
var _dots: ImageTexture
func setup(p_main: Main) -> void:
main = p_main
clip_contents = true
mouse_filter = MOUSE_FILTER_STOP
size_flags_vertical = SIZE_EXPAND_FILL
texture_repeat = CanvasItem.TEXTURE_REPEAT_ENABLED
world = Control.new()
world.mouse_filter = MOUSE_FILTER_IGNORE
add_child(world)
pipes = PipeLayer.new()
pipes.main = main
world.add_child(pipes)
cards = CardLayer.new()
world.add_child(cards)
ports = PortLayer.new()
ports.main = main
world.add_child(ports)
var img := Image.create(6, 6, false, Image.FORMAT_RGBA8)
img.fill(Color(0, 0, 0, 0))
img.set_pixel(1, 1, Color(1, 1, 1, 0.06))
_dots = ImageTexture.create_from_image(img)
var g := Gradient.new()
g.set_color(0, Color(1, 1, 1, 0.06))
g.set_color(1, Color(1, 1, 1, 0))
_glow = GradientTexture2D.new()
_glow.gradient = g
_glow.fill = GradientTexture2D.FILL_RADIAL
_glow.fill_from = Vector2(0.35, 0.3)
_glow.fill_to = Vector2(0.8, 0.3)
_glow.width = 256
_glow.height = 256
resized.connect(queue_redraw)
func _draw() -> void:
var r := Rect2(Vector2.ZERO, size)
draw_rect(r, Style.FELT)
draw_texture_rect(_glow, r, false)
draw_texture_rect(_dots, r, true)
func world_mouse() -> Vector2:
return world.get_local_mouse_position()
func screen_to_world(p: Vector2) -> Vector2:
return (p - world.position) / zoom
func set_zoom(z: float, around: Vector2 = Vector2(-1, -1)) -> void:
if around.x < 0.0:
around = size / 2.0
var anchor := screen_to_world(around)
zoom = clampf(snappedf(z, 0.01), MIN_ZOOM, MAX_ZOOM)
world.scale = Vector2(zoom, zoom)
world.position = around - anchor * zoom
## Zooms to show every table, centred horizontally.
func fit() -> void:
var first := true
var bounds := Rect2()
for c in cards.get_children():
var card := c as Control
var r := Rect2(card.position, card.size)
bounds = r if first else bounds.merge(r)
first = false
if first:
return
bounds = bounds.grow(20.0)
zoom = clampf(minf(1.0, minf((size.x - 16.0) / bounds.size.x, (size.y - 16.0) / bounds.size.y)), MIN_ZOOM, MAX_ZOOM)
world.scale = Vector2(zoom, zoom)
world.position = Vector2(maxf(8.0, (size.x - bounds.size.x * zoom) / 2.0), 8.0) - bounds.position * zoom
func _gui_input(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb:
match mb.button_index:
MOUSE_BUTTON_LEFT, MOUSE_BUTTON_MIDDLE:
if mb.pressed:
if mb.button_index == MOUSE_BUTTON_LEFT and main.mode == Main.Mode.BUILD:
var key := pipes.pipe_at(world_mouse(), 11.0)
if key != "":
main.remove_pipe(key)
accept_event()
return
_panning = true
mouse_default_cursor_shape = CURSOR_DRAG
else:
_panning = false
mouse_default_cursor_shape = CURSOR_ARROW
accept_event()
MOUSE_BUTTON_WHEEL_UP, MOUSE_BUTTON_WHEEL_DOWN:
if mb.pressed:
var up := mb.button_index == MOUSE_BUTTON_WHEEL_UP
if mb.ctrl_pressed:
set_zoom(zoom * (1.1 if up else 1.0 / 1.1), mb.position)
elif mb.shift_pressed:
world.position.x += 60.0 if up else -60.0
else:
world.position.y += 60.0 if up else -60.0
accept_event()
MOUSE_BUTTON_WHEEL_LEFT, MOUSE_BUTTON_WHEEL_RIGHT:
if mb.pressed:
world.position.x += 60.0 if mb.button_index == MOUSE_BUTTON_WHEEL_LEFT else -60.0
accept_event()
return
var mm := ev as InputEventMouseMotion
if mm:
if _panning:
world.position += mm.relative
elif main.mode == Main.Mode.BUILD:
pipes.hover_key = pipes.pipe_at(world_mouse(), 11.0)
mouse_default_cursor_shape = CURSOR_POINTING_HAND if pipes.hover_key != "" else CURSOR_ARROW
return
var pan := ev as InputEventPanGesture
if pan:
world.position -= pan.delta * 16.0
accept_event()
return
var mag := ev as InputEventMagnifyGesture
if mag:
set_zoom(zoom * mag.factor, mag.position)
accept_event()
func _notification(what: int) -> void:
if what == NOTIFICATION_MOUSE_EXIT and pipes:
pipes.hover_key = ""