86 lines
3.3 KiB
GDScript3
86 lines
3.3 KiB
GDScript3
|
|
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)
|