Groups: an optional groups list on a collection (frame rect, collapsed,
members, details), saved by TowerBoard and kept by JsonIO. Expanded groups
draw a frame behind their cards with a title bar; collapsed ones become one
compact card that lists the members' lit results, with pipes re-routed to
its inlet and right edge. Build mode adds multi-select (Ctrl/Shift-click,
Ctrl-drag), Group (Ctrl+G), drop in/out of frames, rename, ungroup, delete
with undo and a details editor.
Templates: TemplateKit saves cards, groups or selections with relative
positions, internal pipes and outside pipes by name, detects {$name}
parameters and inserts with fresh ids. TemplateLibrary keeps user templates
in user://templates and lists the built-in ones in templates/.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
212 lines
6.3 KiB
GDScript
212 lines
6.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 groups: GroupLayer
|
|
var pipes: PipeLayer
|
|
var cards: CardLayer
|
|
var ports: PortLayer
|
|
var overlay: Control
|
|
var zoom: float = 1.0
|
|
|
|
var _panning := false
|
|
var _banding := false
|
|
var _band_from := Vector2.ZERO
|
|
var _band_to := Vector2.ZERO
|
|
var _press_at := Vector2(-1, -1)
|
|
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)
|
|
groups = GroupLayer.new()
|
|
groups.main = main
|
|
world.add_child(groups)
|
|
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)
|
|
# Drawn over the world: the rubber band of a Ctrl-drag selection.
|
|
overlay = Control.new()
|
|
overlay.mouse_filter = MOUSE_FILTER_IGNORE
|
|
overlay.set_anchors_and_offsets_preset(PRESET_FULL_RECT)
|
|
overlay.draw.connect(_draw_band)
|
|
add_child(overlay)
|
|
|
|
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. Cards hidden in collapsed
|
|
## groups don't count; the group cards and frames do.
|
|
func fit() -> void:
|
|
var bounds := content_bounds()
|
|
if bounds.size == Vector2.ZERO:
|
|
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
|
|
|
|
|
|
## The world rect of every visible card, group card and group frame (empty
|
|
## when there are none).
|
|
func content_bounds() -> Rect2:
|
|
var first := true
|
|
var bounds := Rect2()
|
|
for c in cards.get_children() + groups.get_children():
|
|
var card := c as Control
|
|
if card == null or not card.visible or card.is_queued_for_deletion():
|
|
continue
|
|
var r := Rect2(card.position, card.size)
|
|
bounds = r if first else bounds.merge(r)
|
|
first = false
|
|
return bounds
|
|
|
|
|
|
func _gui_input(ev: InputEvent) -> void:
|
|
var mb := ev as InputEventMouseButton
|
|
if mb:
|
|
match mb.button_index:
|
|
MOUSE_BUTTON_LEFT, MOUSE_BUTTON_MIDDLE:
|
|
var build_left := mb.button_index == MOUSE_BUTTON_LEFT and main.mode == Main.Mode.BUILD
|
|
if mb.pressed:
|
|
if build_left and (mb.ctrl_pressed or mb.meta_pressed):
|
|
# Ctrl-drag on the felt: rubber-band selection.
|
|
_banding = true
|
|
_band_from = mb.position
|
|
_band_to = mb.position
|
|
overlay.queue_redraw()
|
|
accept_event()
|
|
return
|
|
if build_left:
|
|
var key := pipes.pipe_at(world_mouse(), 11.0)
|
|
if key != "":
|
|
main.open_pipe_options(key, mb.global_position)
|
|
accept_event()
|
|
return
|
|
_press_at = mb.position
|
|
_panning = true
|
|
mouse_default_cursor_shape = CURSOR_DRAG
|
|
else:
|
|
if _banding and mb.button_index == MOUSE_BUTTON_LEFT:
|
|
_banding = false
|
|
overlay.queue_redraw()
|
|
var a := screen_to_world(_band_from)
|
|
var b := screen_to_world(_band_to)
|
|
main.select_in_rect(Rect2(a.min(b), (a - b).abs()))
|
|
accept_event()
|
|
return
|
|
# A plain click on the felt (no drag) clears the selection.
|
|
if build_left and _press_at.x >= 0.0 and mb.position.distance_to(_press_at) < 4.0:
|
|
main.clear_selection()
|
|
_press_at = Vector2(-1, -1)
|
|
_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 _banding:
|
|
_band_to = mm.position
|
|
overlay.queue_redraw()
|
|
elif _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 _draw_band() -> void:
|
|
if not _banding:
|
|
return
|
|
var r := Rect2(_band_from.min(_band_to), (_band_from - _band_to).abs())
|
|
overlay.draw_rect(r, Color(Style.BRASS, 0.12))
|
|
overlay.draw_rect(r, Style.BRASS, false, 1.5)
|
|
|
|
|
|
func _notification(what: int) -> void:
|
|
if what == NOTIFICATION_MOUSE_EXIT and pipes:
|
|
pipes.hover_key = ""
|