TableTower/ui/card_layer.gd
RobinFiredrake ff7c2023c2 Undo and redo for every Build edit
EditHistory (ui/edit_history.gd) keeps an undo and a redo stack of
whole-board snapshots per collection. Each table and group is its own
JSON string, shared with the previous snapshot when unchanged, so a step
costs about the size of the cards it changed. Collapsed flags are kept
apart: collapsing isn't a step and undo leaves it as it is. Labels come
from diffing the snapshots ("Delete table Loot", "Add pipe Loot → Gems",
"Move 3 cards", "Group Careers"), or from the caller.

Main.mark_dirty records steps: typing commits when it pauses (0.8 s) and
merges until focus moves, other edits commit at the end of the frame (a
drag is one step), and quick edits to the same card or pipe merge.
Actions in main.gd bracket their changes with _begin_edit/_end_edit.
Undo patches only the changed tables and groups in place and rebuilds
their cards, keeping view, mode, search and lit results, then flashes
what changed. Ctrl+Z, Ctrl+Shift+Z / Ctrl+Y outside text fields (Play
too), Undo/Redo buttons with step tooltips in the Build bar, and the
toast's Undo now goes through the history.

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

162 lines
5.2 KiB
GDScript

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)