TableTower/ui/card_layer.gd

163 lines
5.2 KiB
GDScript3
Raw Permalink Normal View History

class_name CardLayer
extends Control
## Holds the table cards and paints their hard drop shadow underneath them.
## Redraws only when the board's geometry changes (BoardView calls
## queue_redraw), not every frame.
##
## Far out (BoardView.FAR_ZOOM) the cards' contents aren't rendered; the
## overlay sketches each card's title line (brass when it holds a lit result)
## and writes group names large enough to read.
##
## It also owns the focus flash (Main.focus_table): a brass ring around a card
## and, optionally, around one of its rows, fading out over FLASH_TIME. The
## flash is drawn in `overlay`, a layer BoardView puts above the cards.
const FLASH_TIME := 1.5
## Drawn above the cards (BoardView adds it to the world after this layer).
var overlay: Control
var main: Main
## True while the board is zoomed out past BoardView.FAR_ZOOM.
var far := false
var _shadow: StyleBoxFlat
var _ring: StyleBoxFlat
var _flash_node: Control = null
## More nodes ringed along with _flash_node (flash_all: an undo's changes).
var _flash_extra: Array[Control] = []
var _flash_row: Rect2 = Rect2()
var _flash_left := 0.0
func _init() -> void:
mouse_filter = MOUSE_FILTER_IGNORE
# Controls only draw inside their own rect, so cover the whole world.
size = Vector2(BoardView.WORLD_SIZE, BoardView.WORLD_SIZE)
_shadow = Style.box(Color(0, 0, 0, 0.22), 10)
_ring = Style.box(Color(0, 0, 0, 0), 14)
_ring.draw_center = false
Style.bordered(_ring, 4, Style.BRASS)
overlay = Control.new()
overlay.mouse_filter = MOUSE_FILTER_IGNORE
overlay.size = size
overlay.draw.connect(_draw_flash)
set_process(false)
func _draw() -> void:
for c in get_children():
var card := c as Control
if card and card.visible:
draw_style_box(_shadow, Rect2(card.position + Vector2(0, 5), card.size))
## Flashes a ring around `node` (a TableCard or GroupCard) for FLASH_TIME.
## `row` is a world rect inside it to ring as well (Rect2() for none).
func flash(node: Control, row: Rect2 = Rect2()) -> void:
_flash_extra.clear()
_flash_node = node
_flash_row = row
_flash_left = FLASH_TIME
set_process(true)
overlay.queue_redraw()
## Flashes rings around several nodes at once (none: nothing happens).
func flash_all(nodes: Array[Control]) -> void:
if nodes.is_empty():
return
flash(nodes[0])
_flash_extra = nodes.slice(1)
func is_flashing() -> bool:
return _flash_left > 0.0 and is_instance_valid(_flash_node)
## The world rect of the flashed card, or Rect2() when nothing flashes.
func flash_rect() -> Rect2:
if not is_flashing():
return Rect2()
return Rect2(_flash_node.position, _flash_node.size)
func flash_row_rect() -> Rect2:
return _flash_row if is_flashing() else Rect2()
func _process(delta: float) -> void:
_flash_left -= delta
overlay.queue_redraw()
if _flash_left <= 0.0:
_flash_node = null
_flash_extra.clear()
set_process(false)
func _draw_flash() -> void:
if far:
_draw_far()
if not is_flashing():
return
var t := clampf(_flash_left / FLASH_TIME, 0.0, 1.0)
# Two quick pulses, then a fade.
var pulse := 0.65 + 0.35 * cos((1.0 - t) * TAU * 2.0)
var a := minf(1.0, t * 2.5) * pulse
_ring.border_color = Color(Style.BRASS, a)
var glow := _glow_box(a)
for v: Variant in [_flash_node] + _flash_extra:
if not is_instance_valid(v):
continue
var n := v as Control
if n.visible:
var r := Rect2(n.position, n.size).grow(8.0)
overlay.draw_style_box(glow, r.grow(6.0))
overlay.draw_style_box(_ring, r)
if _flash_row.size != Vector2.ZERO:
overlay.draw_rect(_flash_row, Color(Style.BRASS, 0.28 * a))
overlay.draw_rect(_flash_row, Color(Style.BRASS_DARK, a), false, 2.5)
func _glow_box(a: float) -> StyleBoxFlat:
var g := Style.box(Color(0, 0, 0, 0), 20)
g.draw_center = false
Style.bordered(g, 8, Color(Style.BRASS, 0.25 * a))
return g
## The far-out sketch: a title line per card, group names in large type.
func _draw_far() -> void:
if main == null or main.data == null:
return
var lit := {}
for k: String in main.lit_entries:
lit[k.substr(0, k.rfind(":"))] = true
var ink := Color(Style.INK, 0.45)
for c in get_children():
var card := c as TableCard
if card == null or not card.visible or card.table == null:
continue
var p := card.position
var w := card.size.x
overlay.draw_rect(Rect2(p + Vector2(14, 16), Vector2(w * 0.6, 16)), ink)
if lit.has(card.table.id):
overlay.draw_rect(Rect2(p + Vector2(0, 46), Vector2(w, minf(40.0, card.size.y - 52.0))), Style.BRASS)
var z := main.board.zoom
var font := Style.display(true)
for g in main.data.groups:
var n := main.group_node(g.id)
if n == null:
continue
var r := Rect2(n.position, n.size)
var text := Formatter.strip_tags(g.name)
var fs := int(18.0 / z)
var tw := font.get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, fs).x
var room := maxf(r.size.x, 520.0) * 1.1
if tw > room:
fs = maxi(8, int(fs * room / tw))
tw = font.get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, fs).x
var at := Vector2(r.position.x + (r.size.x - tw) / 2.0, r.position.y + fs * 0.85)
if g.collapsed:
at.y = r.position.y + r.size.y / 2.0 + fs * 0.3
overlay.draw_string_outline(font, at, text, HORIZONTAL_ALIGNMENT_LEFT, -1, fs, maxi(2, int(4.0 / z)), Style.FELT_DEEP)
overlay.draw_string(font, at, text, HORIZONTAL_ALIGNMENT_LEFT, -1, fs, Style.ON_FELT if not g.collapsed else Style.BRASS)