Files
stickman/scripts/editors/prop/prop_inspector_icons.gd
T

96 lines
4.5 KiB
GDScript

class_name PropInspectorIcons
extends RefCounted
## Small flat 2D glyphs for the Prop Editor inspector.
## SVG is rasterized at runtime so colors can follow the editor and material tokens.
const ICON_SIZE := 24
static func material(preset: int, tint: Color) -> Texture2D:
if preset == 0:
return _texture(_svg('<circle cx="24" cy="24" r="15" fill="#252B38" stroke="#C4D1E5" stroke-width="4"/>'))
return _texture(_svg('<circle cx="24" cy="24" r="15" fill="%s" stroke="#FFFFFF" stroke-opacity="0.35" stroke-width="3"/>' % _hex(tint)))
static func shape(shape_id: String, tint: Color = Color("#22C6FF")) -> Texture2D:
var color := _hex(tint)
var content := ""
match shape_id:
"box":
content = '<rect x="8" y="8" width="32" height="32" rx="2" fill="%s"/>' % color
"plank":
content = '<rect x="5" y="16" width="38" height="16" rx="2" fill="%s"/>' % color
"triangle":
content = '<polygon points="24,6 44,41 4,41" fill="%s"/>' % color
"line":
content = '<path d="M8 38 L40 10" fill="none" stroke="%s" stroke-width="6" stroke-linecap="round"/>' % color
"circle":
content = '<circle cx="24" cy="24" r="16" fill="%s"/>' % color
_:
content = '<rect x="8" y="8" width="32" height="32" rx="2" fill="%s"/>' % color
return _texture(_svg(content))
static func section(section_id: String) -> Texture2D:
var content := ""
match section_id:
"material":
content = '<rect x="7" y="9" width="14" height="14" rx="3" fill="#F3B65D"/><rect x="27" y="9" width="14" height="14" rx="3" fill="#E76D66"/><rect x="7" y="27" width="14" height="14" rx="3" fill="#66C681"/><rect x="27" y="27" width="14" height="14" rx="3" fill="#70BFE8"/>'
"selected_shape":
content = '<path d="M9 32 L16 11 L38 15 L35 37 Z" fill="#DDE7F5" stroke="#22C6FF" stroke-width="3" stroke-linejoin="round"/><circle cx="9" cy="32" r="3" fill="#FFFFFF"/><circle cx="16" cy="11" r="3" fill="#FFFFFF"/><circle cx="38" cy="15" r="3" fill="#FFFFFF"/><circle cx="35" cy="37" r="3" fill="#FFFFFF"/>'
"add_shape":
content = '<rect x="7" y="7" width="34" height="34" fill="none" stroke="#22C6FF" stroke-width="5"/><path d="M24 15 V33 M15 24 H33" stroke="#22C6FF" stroke-width="4" stroke-linecap="round"/>'
"tags":
content = '<path d="M7 17 L17 7 H34 L42 15 V32 L25 42 L7 24 Z" fill="#FFB84D"/><circle cx="31" cy="16" r="3.5" fill="#252B38"/>'
return _texture(_svg(content))
static func tag(tag_id: String) -> Texture2D:
var color := "#66C681"
var body := ""
match tag_id:
"nature":
color = "#66C681"
body = '<path d="M9 35 C9 18 21 8 39 8 C39 26 29 39 13 39 Z" fill="%s"/><path d="M12 38 L33 15" stroke="#E8FFF0" stroke-width="3" stroke-linecap="round"/>' % color
"buildings":
color = "#F3A653"
body = '<path d="M10 40 V10 H29 V18 H38 V40 Z" fill="%s"/><path d="M16 16 H21 M16 23 H21 M16 30 H21 M31 24 H35 M31 31 H35" stroke="#3C2A22" stroke-width="3"/>' % color
"walls":
color = "#F36B5B"
body = '<path d="M5 10 H21 V19 H5 Z M24 10 H42 V19 H24 Z M14 22 H31 V31 H14 Z M4 34 H20 V42 H4 Z M23 34 H42 V42 H23 Z" fill="%s"/><path d="M22 10 V19 M13 22 V31 M31 34 V42" stroke="#733A37" stroke-width="2"/>' % color
"roads":
color = "#9BAEC9"
body = '<path d="M16 7 H32 L42 41 H6 Z" fill="%s"/><path d="M24 12 V19 M24 25 V31 M24 36 V40" stroke="#283247" stroke-width="3" stroke-linecap="round"/>' % color
"objects":
color = "#E8A96D"
body = '<rect x="7" y="11" width="34" height="26" fill="none" stroke="%s" stroke-width="5"/>' % color
return _texture(_svg(body))
static func popup_item(texture: Texture2D, size: int = 44) -> Texture2D:
if texture == null:
return null
var source: Image = texture.get_image()
var canvas := Image.create(size, size, false, Image.FORMAT_RGBA8)
var draw_size: Vector2i = Vector2i(mini(source.get_width(), size), mini(source.get_height(), size))
var source_rect := Rect2i(Vector2i.ZERO, draw_size)
var destination := Vector2i((size - draw_size.x) / 2, (size - draw_size.y) / 2)
canvas.blit_rect(source, source_rect, destination)
return ImageTexture.create_from_image(canvas)
static func _svg(content: String) -> String:
return '<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d" viewBox="0 0 48 48">%s</svg>' % [ICON_SIZE, ICON_SIZE, content]
static func _texture(svg: String) -> Texture2D:
var image := Image.new()
var error: Error = image.load_svg_from_string(svg)
if error != OK:
push_warning("Prop Inspector icon could not be rasterized (%s)." % error_string(error))
return null
return ImageTexture.create_from_image(image)
static func _hex(color: Color) -> String:
return "#" + color.to_html(false)