419 lines
14 KiB
GDScript
419 lines
14 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,
|
|
## wheel or pinch to zoom (Shift+wheel pans sideways).
|
|
|
|
## Low enough that Fit shows a whole board of several hundred cards.
|
|
const MIN_ZOOM := 0.08
|
|
const MAX_ZOOM := 1.5
|
|
## Below this zoom text can't be read, so cards draw as plain cards (their
|
|
## contents aren't rendered) and group names show large (CardLayer).
|
|
const FAR_ZOOM := 0.22
|
|
## Below this, cards and collapsed groups only drag, right-click and (in Play)
|
|
## roll on a click; their names show as tooltips (CardShield).
|
|
const COMPACT_ZOOM := 0.45
|
|
const WORLD_SIZE := 40000.0
|
|
## How far (screen pixels) from a thread's line a click or hover still picks it.
|
|
const PIPE_HIT_RADIUS := 12.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
|
|
## The overview in the bottom-right corner (hidden unless turned on).
|
|
var minimap: Minimap
|
|
## Bumped whenever something the layers draw from changes (see _signature):
|
|
## cards or groups moved, resized, rebuilt or scrolled, lit results, edits.
|
|
## The pipe, port and card layers redraw only when it changes.
|
|
var geometry_rev := 0
|
|
var _sig_f := PackedFloat32Array()
|
|
var _sig_i := PackedInt64Array()
|
|
var _view_sig := PackedFloat32Array()
|
|
var _view_tween: Tween
|
|
var _settle := 0
|
|
|
|
var _culled_rev := -1
|
|
|
|
var _panning := false
|
|
var _banding := false
|
|
## Zoomed out past COMPACT_ZOOM (see set_compact).
|
|
var compact := false
|
|
## The selection box adds to the selection (Ctrl or Shift held).
|
|
var _band_adds := false
|
|
var _band_from := Vector2.ZERO
|
|
var _band_to := Vector2.ZERO
|
|
var _press_at := Vector2(-1, -1)
|
|
## The background behind the cards (gradient, scene picture, tile).
|
|
var backdrop: BoardBackdrop
|
|
|
|
|
|
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()
|
|
cards.main = main
|
|
world.add_child(cards)
|
|
world.add_child(cards.overlay)
|
|
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)
|
|
minimap = Minimap.new()
|
|
minimap.setup(self)
|
|
add_child(minimap)
|
|
|
|
backdrop = BoardBackdrop.new(ThemeStore.current)
|
|
resized.connect(queue_redraw)
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
if main == null or main.data == null:
|
|
return
|
|
_signature()
|
|
var v := PackedFloat32Array([world.position.x, world.position.y, zoom, size.x, size.y])
|
|
if v != _view_sig or _culled_rev != geometry_rev:
|
|
var far := zoom < FAR_ZOOM
|
|
if far != cards.far or (far and (_view_sig.is_empty() or v[2] != _view_sig[2])):
|
|
cards.far = far
|
|
cards.overlay.queue_redraw()
|
|
if (zoom < COMPACT_ZOOM) != compact:
|
|
set_compact(zoom < COMPACT_ZOOM)
|
|
_view_sig = v
|
|
_cull()
|
|
minimap.refresh()
|
|
|
|
|
|
## Stops rendering cards and group frames far outside the view (half a
|
|
## screen of margin), so a big expanded board only draws what's near, and far
|
|
## out (FAR_ZOOM) stops rendering their text (CardLayer sketches them). This
|
|
## is the renderer's own visibility, not `visible`: input, layout and the
|
|
## pipe geometry are untouched. Runs when the view or the geometry changed.
|
|
func _cull() -> void:
|
|
_culled_rev = geometry_rev
|
|
var view := view_rect()
|
|
view = view.grow(maxf(view.size.x, view.size.y) * 0.5)
|
|
for layer: Control in [cards, groups]:
|
|
for c in layer.get_children():
|
|
var n := c as Control
|
|
if n == null or not n.visible:
|
|
continue
|
|
RenderingServer.canvas_item_set_visible(n.get_canvas_item(), view.intersects(Rect2(n.position, n.size).grow(40.0)))
|
|
var content := _content_of(n)
|
|
if content:
|
|
RenderingServer.canvas_item_set_visible(content.get_canvas_item(), content.visible and not cards.far)
|
|
|
|
|
|
## Collects what the layers draw from into two packed arrays and bumps
|
|
## geometry_rev when they differ from last frame. Hidden table cards don't
|
|
## exist (Main builds them lazily), so this walks only what's on the board.
|
|
func _signature() -> void:
|
|
var f := PackedFloat32Array()
|
|
var ids := PackedInt64Array([main.view_rev, main.mode])
|
|
for layer: Control in [cards, groups]:
|
|
for c in layer.get_children():
|
|
var n := c as Control
|
|
if n == null or not n.visible:
|
|
continue
|
|
f.append(n.position.x)
|
|
f.append(n.position.y)
|
|
f.append(n.size.x)
|
|
f.append(n.size.y)
|
|
ids.append(n.get_child(0).get_instance_id() if n.get_child_count() > 0 else 0)
|
|
var tc := n as TableCard
|
|
if tc and tc.list_scroll != null:
|
|
f.append(tc.list_scroll.scroll_vertical)
|
|
var changed := f != _sig_f or ids != _sig_i
|
|
if changed:
|
|
_sig_f = f
|
|
_sig_i = ids
|
|
# Containers inside a rebuilt card can settle a frame or two later
|
|
# without the card's own rect changing, so redraw a little longer.
|
|
_settle = 2
|
|
elif _settle > 0:
|
|
_settle -= 1
|
|
changed = true
|
|
if changed:
|
|
geometry_rev += 1
|
|
cards.queue_redraw()
|
|
if cards.far:
|
|
cards.overlay.queue_redraw()
|
|
minimap.refresh()
|
|
|
|
|
|
## The part of a card or group card that holds its text (hidden far out).
|
|
func _content_of(n: Control) -> CanvasItem:
|
|
if n.get_child_count() == 0:
|
|
return null
|
|
var first := n.get_child(0)
|
|
var gc := n as GroupCard
|
|
if gc and gc.group and gc.group.collapsed:
|
|
return first.get_child(0) as CanvasItem if first.get_child_count() > 0 else null
|
|
return first as CanvasItem
|
|
|
|
|
|
func _draw() -> void:
|
|
backdrop.draw(self, Rect2(Vector2.ZERO, size))
|
|
|
|
|
|
## The built-in dots / stars tile (BoardBackdrop.pattern_texture).
|
|
static func pattern_texture(kind: String) -> ImageTexture:
|
|
return BoardBackdrop.pattern_texture(kind)
|
|
|
|
|
|
## Drops a half-made selection box or pan (a second finger landed: TouchInput).
|
|
func cancel_drag() -> void:
|
|
_banding = false
|
|
_panning = false
|
|
_press_at = Vector2(-1, -1)
|
|
overlay.queue_redraw()
|
|
|
|
|
|
## Shows or hides every card's and collapsed group's CardShield.
|
|
func set_compact(on: bool) -> void:
|
|
compact = on
|
|
for layer: Control in [cards, groups]:
|
|
for c in layer.get_children():
|
|
var sh: Variant = c.get("shield")
|
|
if sh is CardShield:
|
|
(sh as CardShield).visible = on
|
|
|
|
|
|
func world_mouse() -> Vector2:
|
|
return world.get_local_mouse_position()
|
|
|
|
|
|
## The pipe under the mouse, within PIPE_HIT_RADIUS screen pixels of its line.
|
|
func pipe_under_mouse() -> String:
|
|
return pipes.pipe_at(world_mouse(), PIPE_HIT_RADIUS / zoom)
|
|
|
|
|
|
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
|
|
stop_animation()
|
|
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
|
|
|
|
|
|
## Makes a layer cover the whole world. A Control's canvas item is clipped to
|
|
## its rect, and the world reaches past it (cards can sit at negative
|
|
## coordinates), so the clip is switched off: nothing a layer draws is lost.
|
|
static func cover_world(layer: Control) -> void:
|
|
layer.size = Vector2(WORLD_SIZE, WORLD_SIZE)
|
|
# Control sets its clip rect on entering the tree and on every resize, so
|
|
# switch it off again whenever the layer draws.
|
|
layer.draw.connect(func() -> void: RenderingServer.canvas_item_set_custom_rect(layer.get_canvas_item(), false))
|
|
|
|
|
|
## A control's rect in world coordinates. (Control.get_global_rect ignores the
|
|
## world's zoom for the size, so it is wrong whenever the board is not at 100%.)
|
|
func world_rect_of(control: Control) -> Rect2:
|
|
return (world.get_global_transform().affine_inverse() * control.get_global_transform()) * Rect2(Vector2.ZERO, control.size)
|
|
|
|
|
|
## The part of the world the board shows right now.
|
|
func view_rect() -> Rect2:
|
|
return Rect2(screen_to_world(Vector2.ZERO), size / zoom)
|
|
|
|
|
|
## Pans so a world point sits in the middle of the board.
|
|
func center_on(p: Vector2) -> void:
|
|
stop_animation()
|
|
world.position = size / 2.0 - p * zoom
|
|
|
|
|
|
## Glides to `z` with the world point `p` in the middle (duration 0 jumps).
|
|
func animate_to(p: Vector2, z: float, duration: float = 0.3) -> void:
|
|
stop_animation()
|
|
z = clampf(snappedf(z, 0.01), MIN_ZOOM, MAX_ZOOM)
|
|
if duration <= 0.0 or not is_inside_tree():
|
|
_apply_view(p, z)
|
|
return
|
|
var from_p := screen_to_world(size / 2.0)
|
|
var from_z := zoom
|
|
_view_tween = create_tween().set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_OUT)
|
|
_view_tween.tween_method(func(t: float) -> void:
|
|
_apply_view(from_p.lerp(p, t), lerpf(from_z, z, t)), 0.0, 1.0, duration)
|
|
|
|
|
|
func is_animating() -> bool:
|
|
return _view_tween != null and _view_tween.is_valid() and _view_tween.is_running()
|
|
|
|
|
|
func stop_animation() -> void:
|
|
if _view_tween and _view_tween.is_valid():
|
|
_view_tween.kill()
|
|
_view_tween = null
|
|
|
|
|
|
func _apply_view(p: Vector2, z: float) -> void:
|
|
zoom = z
|
|
world.scale = Vector2(zoom, zoom)
|
|
world.position = size / 2.0 - p * 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:
|
|
# Middle-drag or Space+drag pans; a left-drag on empty space draws a
|
|
# selection box, in Play and Build alike.
|
|
var pan_drag := mb.button_index == MOUSE_BUTTON_MIDDLE or Input.is_key_pressed(KEY_SPACE)
|
|
var build_left := mb.button_index == MOUSE_BUTTON_LEFT and not pan_drag
|
|
if mb.pressed:
|
|
# Clicking the felt leaves a text field, so Ctrl+Z undoes on the board.
|
|
var f := get_viewport().gui_get_focus_owner()
|
|
if f is LineEdit or f is TextEdit:
|
|
f.release_focus()
|
|
if build_left:
|
|
var key := pipe_under_mouse() if main.mode == Main.Mode.BUILD else ""
|
|
if key != "":
|
|
# The pipe opens in the inspector (Main.open_pipe_options).
|
|
main.open_pipe_options(key, mb.global_position)
|
|
accept_event()
|
|
return
|
|
# A drag on empty space draws a selection box; Ctrl or Shift
|
|
# adds to the selection instead of replacing it.
|
|
_press_at = mb.position
|
|
_band_adds = mb.ctrl_pressed or mb.meta_pressed or mb.shift_pressed
|
|
_banding = true
|
|
_band_from = mb.position
|
|
_band_to = mb.position
|
|
accept_event()
|
|
return
|
|
_panning = true
|
|
stop_animation()
|
|
mouse_default_cursor_shape = CURSOR_DRAG
|
|
else:
|
|
if _banding and mb.button_index == MOUSE_BUTTON_LEFT:
|
|
_banding = false
|
|
overlay.queue_redraw()
|
|
if mb.position.distance_to(_press_at) < 4.0:
|
|
# A plain click on the felt (no drag) clears the selection.
|
|
if not _band_adds:
|
|
main.clear_selection()
|
|
main.clear_inspector()
|
|
else:
|
|
if not _band_adds:
|
|
main.clear_selection()
|
|
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()))
|
|
_press_at = Vector2(-1, -1)
|
|
accept_event()
|
|
return
|
|
_press_at = Vector2(-1, -1)
|
|
_panning = false
|
|
mouse_default_cursor_shape = CURSOR_ARROW
|
|
accept_event()
|
|
MOUSE_BUTTON_RIGHT:
|
|
if mb.pressed:
|
|
# A thread under the mouse, else the empty spread.
|
|
var key := pipe_under_mouse()
|
|
var list := Actions.thread(main, key) if key != "" else Actions.background(main, world_mouse())
|
|
if not list.is_empty():
|
|
Actions.popup_at(main, list, mb.global_position)
|
|
accept_event()
|
|
MOUSE_BUTTON_WHEEL_UP, MOUSE_BUTTON_WHEEL_DOWN:
|
|
if mb.pressed:
|
|
var up := mb.button_index == MOUSE_BUTTON_WHEEL_UP
|
|
if mb.shift_pressed:
|
|
world.position.x += 60.0 if up else -60.0
|
|
else:
|
|
set_zoom(zoom * (1.1 if up else 1.0 / 1.1), mb.position)
|
|
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 = pipe_under_mouse()
|
|
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.ACCENT, 0.12))
|
|
overlay.draw_rect(r, Style.ACCENT, false, 1.5)
|
|
|
|
|
|
func _notification(what: int) -> void:
|
|
if what == NOTIFICATION_MOUSE_EXIT and pipes:
|
|
pipes.hover_key = ""
|