TableTower/ui/mascot_art.gd
RobinFiredrake 61a548902f Theme tiers 1-2: theme folders, mascot recolour, background scene and tile, frame sets, picture threads, sounds; nine new built-in themes
- ThemeAssets loads a theme's files; FrameSet + GradientMap (3-stop) generalise the card frame;
  ThreadStrip draws threads from a picture; BoardBackdrop draws scene and tile; MascotArt
  recolours Morgan by role (logo SVG now uses the Keep File importer); SoundBoard plays
  roll / card turn / thread / click / ambient with View > Sound levels.
- Vaporwave, Synthwave, Cozy Autumn, Rustic, Woodsy, Sky Blue, Frutiger Aero, Gothic, Terminal:
  generated art (tools/theme_art), sounds (tools/theme_sounds), settings (tools/theme_defs).
- Ten OFL fonts; --theme=<id> flag; docs/THEMES.md format spec; test_theme_assets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:05:40 -06:00

85 lines
3.3 KiB
GDScript

class_name MascotArt
extends RefCounted
## The theme's mascot as a picture, recoloured by colour role. A mascot SVG
## names its parts by role (`<path id="cloth" …>`); the theme's mascot_colors
## give each role a colour, and roles it leaves out keep the colour in the file.
## Morgan Fortuna is the default mascot. See docs/THEMES.md, "Mascot colour roles".
## Morgan's roles, in the order the theme editor lists them.
const ROLES := ["outline", "cloth", "trim", "eye", "eye_glint", "inner_line"]
const ROLE_LABELS := {"outline": "Outline", "cloth": "Robe", "trim": "Trim", "eye": "Eye",
"eye_glint": "Eye shine", "inner_line": "Inner lines"}
## Imported with the Keep File importer, so its text is in the export.
const MORGAN := "res://assets/art/fortune_teller_logo.svg"
static var _cache: Dictionary = {}
## The SVG file a theme's logo uses ("" for none or the tower mark).
static func source(theme_set: Dictionary) -> String:
match str(theme_set.get("logo", "none")):
"fortune_teller":
return MORGAN
"mascot":
return ThemeAssets.resolve(theme_set, str(theme_set.get("mascot", "")))
return ""
## The mascot drawn `pixels` wide, or null when the theme has none.
static func texture(theme_set: Dictionary, pixels: int) -> Texture2D:
var path := source(theme_set)
if path == "":
return null
var colors: Dictionary = theme_set.get("mascot_colors", {})
var key := "%s|%s|%d" % [path, JSON.stringify(colors), pixels]
if not _cache.has(key):
_cache[key] = _render(path, colors, pixels)
return _cache[key]
## The colours a file uses for each role it has (for the theme editor).
static func file_colors(path: String) -> Dictionary:
var out := {}
var svg := ThemeAssets.text(path)
var finder := RegEx.create_from_string("<(?:path|g)\\b[^>]*>")
for found in finder.search_all(svg):
var tag := found.get_string()
var id := _attribute(tag, "id")
if id in ROLES:
out[id] = _attribute(tag, "fill")
return out
## The SVG text with each role's fill replaced.
static func recolour(svg: String, colors: Dictionary) -> String:
var finder := RegEx.create_from_string("<(?:path|g)\\b[^>]*>")
var out := ""
var from := 0
for found in finder.search_all(svg):
var tag := found.get_string()
var id := _attribute(tag, "id")
if not colors.has(id):
continue
var fill := RegEx.create_from_string("\\bfill=\"[^\"]*\"")
var new_tag := fill.sub(tag, "fill=\"%s\"" % str(colors[id])) if fill.search(tag) else tag.replace("<path", "<path fill=\"%s\"" % str(colors[id])).replace("<g", "<g fill=\"%s\"" % str(colors[id]))
out += svg.substr(from, found.get_start() - from) + new_tag
from = found.get_end()
return out + svg.substr(from)
static func _attribute(tag: String, name: String) -> String:
var found := RegEx.create_from_string("\\b%s=\"([^\"]*)\"" % name).search(tag)
return found.get_string(1) if found else ""
static func _render(path: String, colors: Dictionary, pixels: int) -> Texture2D:
var svg := ThemeAssets.text(path)
if svg == "":
return null
var width_found := RegEx.create_from_string("<svg\\b[^>]*\\bwidth=\"([\\d.]+)").search(svg)
var width := float(width_found.get_string(1)) if width_found else 2048.0
var image := Image.new()
if image.load_svg_from_string(recolour(svg, colors), float(pixels) / width) != OK:
return null
image.generate_mipmaps()
return ImageTexture.create_from_image(image)