TableTower/ui/theme_assets.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

120 lines
4 KiB
GDScript

class_name ThemeAssets
extends RefCounted
## Finds and loads the files a theme names: pictures, sounds and mascot SVGs.
## A path is relative to the theme's folder (built in: res://themes/<id>/;
## the user's own: user://themes/<id>/) or a full res:// or user:// path.
## Each file loads once; ThemeStore.apply clears the cache. See docs/THEMES.md.
static var _cache: Dictionary = {}
## The folder a theme's relative paths start from.
static func folder(theme_set: Dictionary) -> String:
var id := str(theme_set.get("id", ""))
if bool(theme_set.get("builtin", false)):
return "res://themes/%s/" % id
return ThemeStore.DIR + id + "/"
## The full path for a file named in a theme ("" stays "").
static func resolve(theme_set: Dictionary, file: String) -> String:
if file == "":
return ""
if file.begins_with("res://") or file.begins_with("user://"):
return file
return folder(theme_set) + file
static func clear_cache() -> void:
_cache.clear()
## A picture (SVG, PNG, JPG or WebP), or null when the file is missing.
static func texture(theme_set: Dictionary, file: String) -> Texture2D:
var path := resolve(theme_set, file)
if path == "":
return null
if not _cache.has(path):
_cache[path] = _load_texture(path)
return _cache[path]
## A sound (OGG, WAV or MP3), or null when the file is missing.
static func sound(theme_set: Dictionary, file: String) -> AudioStream:
var path := resolve(theme_set, file)
if path == "":
return null
if not _cache.has(path):
_cache[path] = _load_sound(path)
return _cache[path]
## A file's text ("" when missing). Built-in files read this way must use the
## Keep File importer so they are in the export.
static func text(path: String) -> String:
if path == "" or not FileAccess.file_exists(path):
return ""
return FileAccess.get_file_as_string(path)
## A size pair from a theme ([w, h]), or `fallback`.
static func vec2(value: Variant, fallback: Vector2) -> Vector2:
if value is Array and (value as Array).size() == 2:
return Vector2(float(value[0]), float(value[1]))
return fallback
static func _load_texture(path: String) -> Texture2D:
if path.begins_with("res://") and ResourceLoader.exists(path):
return load(path) as Texture2D
if not FileAccess.file_exists(path):
return null
var image := Image.new()
var err: int
if path.get_extension().to_lower() == "svg":
err = image.load_svg_from_string(FileAccess.get_file_as_string(path))
else:
err = image.load(path)
if err != OK:
return null
image.generate_mipmaps()
return ImageTexture.create_from_image(image)
static func _load_sound(path: String) -> AudioStream:
if path.begins_with("res://") and ResourceLoader.exists(path):
return load(path) as AudioStream
if not FileAccess.file_exists(path):
return null
match path.get_extension().to_lower():
"ogg":
return AudioStreamOggVorbis.load_from_file(path)
"wav":
return AudioStreamWAV.load_from_file(path)
"mp3":
return AudioStreamMP3.load_from_file(path)
return null
## Every key that names a file, as [key] or [key, subkey].
const FILE_KEYS := [["mascot"], ["background_scene", "file"], ["background_tile", "file"],
["frame", "corner"], ["frame", "corner_fixed"], ["frame", "edge"], ["thread", "strip"], ["thread", "pin"]]
## A copy of a theme whose relative file names are made full paths from
## `base`, so the files are still found after the copy moves (a user copy of a
## built-in theme keeps using the built-in's folder).
static func pin_paths(theme_set: Dictionary, base: String) -> Dictionary:
var out := theme_set.duplicate(true)
var full := func(file: String) -> String:
return file if file == "" or file.begins_with("res://") or file.begins_with("user://") else base + file
for key: Array in FILE_KEYS:
if key.size() == 1:
if out.has(key[0]):
out[key[0]] = full.call(str(out[key[0]]))
elif out.get(key[0]) is Dictionary and (out[key[0]] as Dictionary).has(key[1]):
out[key[0]][key[1]] = full.call(str(out[key[0]][key[1]]))
if out.get("sounds") is Dictionary:
for event: String in out["sounds"]:
out["sounds"][event] = full.call(str(out["sounds"][event]))
return out