TableTower/ui/card_frame.gd
RobinFiredrake f9aa66d74d Beaded card frames and per-card / per-thread colours
CardFrame draws the user's frame_corner/frame_border art around each card,
tiling the bead strip so it fits any size; a small shader recolours the art
to the theme's trim (or the card's own colour). Themes choose the border
(Tarot: beaded, Felt: plain). Cards (RollTable.color) and threads
(TableEntry.pipe_color, JSON "color") can have their own colour from the
inspector's Colour row (presets, Theme, or any colour).

tools/run_tests.sh imports first and prints at most 8 lines per failing suite.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:20:46 -06:00

111 lines
4.1 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name CardFrame
extends Control
## The ornamental border around a card: four corner pieces (frame_corner.svg,
## mirrored) joined by beaded strips (frame_border.svg, tiled along each edge so
## it scales to any card size). A small shader recolours the art: its amber
## becomes the trim colour (the card's own colour, else the theme's TRIM) and
## its near-black strip becomes BG_DEEP.
##
## It's the card's first child and draws around the card's whole rectangle,
## a little outside it (OUTSET), whatever box the container gives it.
const CORNER: Texture2D = preload("res://assets/ui/frame_corner.svg")
const BORDER: Texture2D = preload("res://assets/ui/frame_border.svg")
## Drawn size relative to the art's source units (corner art is 200 × 194).
const S := 0.28
## How far the frame reaches outside the card.
const OUTSET := 9.0
## Extra padding inside a framed card, so the corners clear its contents.
const PAD := 5.0
# Source-unit geometry (see the comments in the SVGs).
const CORNER_W := 200.0
const CORNER_H := 194.0
const TOP_MID := 33.0 ## The top tab's centre line (y) in the corner art.
const SIDE_MID := 29.0 ## The side tab's centre line (x) in the corner art.
const TILE_W := 44.0
const TILE_H := 50.0
const TILE_MID := 22.0
const SHADER := "shader_type canvas_item;
uniform vec4 dark : source_color;
uniform vec4 trim : source_color;
void fragment() {
vec4 t = texture(TEXTURE, UV);
float l = dot(t.rgb, vec3(0.299, 0.587, 0.114));
COLOR = vec4(mix(dark.rgb, trim.rgb, smoothstep(0.05, 0.30, l)), t.a * COLOR.a);
}"
static var _shader: Shader
static var _materials: Dictionary = {}
func _init() -> void:
mouse_filter = MOUSE_FILTER_IGNORE
texture_filter = TEXTURE_FILTER_LINEAR_WITH_MIPMAPS
set_trim(Color(0, 0, 0, 0))
## Recolours the frame; a transparent colour means the theme's TRIM.
func set_trim(c: Color) -> void:
material = material_for(Style.TRIM if c.a == 0.0 else c)
queue_redraw()
## Shared materials, one per trim colour.
static func material_for(trim: Color) -> ShaderMaterial:
var key := trim.to_html() + Style.BG_DEEP.to_html()
if not _materials.has(key):
if _shader == null:
_shader = Shader.new()
_shader.code = SHADER
var mat := ShaderMaterial.new()
mat.shader = _shader
mat.set_shader_parameter("dark", Style.BG_DEEP)
mat.set_shader_parameter("trim", trim)
_materials[key] = mat
return _materials[key]
func _draw() -> void:
var host := get_parent() as Control
if host == null:
return
# Work in the host card's coordinates, grown by OUTSET.
var r := Rect2(-position, host.size).grow(OUTSET)
var cw := CORNER_W * S
var ch := CORNER_H * S
var corner := Vector2(cw, ch)
# Corners: top-left as drawn, the others mirrored.
for flip: Vector2 in [Vector2(1, 1), Vector2(-1, 1), Vector2(1, -1), Vector2(-1, -1)]:
var at := Vector2(r.position.x if flip.x > 0 else r.end.x, r.position.y if flip.y > 0 else r.end.y)
draw_set_transform(at, 0.0, flip)
draw_texture_rect(CORNER, Rect2(Vector2.ZERO, corner), false)
# Sides: vertical strips between the corners' side tabs.
var side_len := r.size.y - 2.0 * ch
for flip: float in [1.0, -1.0]:
var x := (r.position.x if flip > 0 else r.end.x) + flip * SIDE_MID * S
_strip(Vector2(x, r.position.y + ch), side_len, 0.0)
# Top and bottom: the same strip turned on its side.
var top_len := r.size.x - 2.0 * cw
for flip: float in [1.0, -1.0]:
var y := (r.position.y if flip > 0 else r.end.y) + flip * TOP_MID * S
_strip(Vector2(r.position.x + cw, y), top_len, -PI / 2.0)
draw_set_transform(Vector2.ZERO)
## Tiles the bead strip along `length` from `start` (the strip's centre line),
## downwards (angle 0) or rightwards (angle −π/2). Tiles stretch slightly so a
## whole number of beads fits.
func _strip(start: Vector2, length: float, angle: float) -> void:
if length <= 1.0:
return
var n := maxi(1, roundi(length / (TILE_H * S)))
var tile := length / n
var w := TILE_W * S
if angle == 0.0:
draw_set_transform(start - Vector2(TILE_MID * S, 0), 0.0, Vector2.ONE)
else:
# Rotating by −90° maps the tile's +y (its length) onto +x.
draw_set_transform(start + Vector2(0, TILE_MID * S), angle, Vector2.ONE)
for i in n:
draw_texture_rect(BORDER, Rect2(0, i * tile, w, tile), false)