- SVG direkt aus dem Modell mit Rink, Section-Nummern und Legende - PNG über eine eigene Export-Szene in 2560 x 1600 - Export der Gesamtkür oder des aktiven Abschnitts - Export-Dialog im Datei-Menü, Ablage unter user://exports - Tests auf 417 Assertions erweitert
259 lines
6.9 KiB
GDScript
259 lines
6.9 KiB
GDScript
class_name RoutineSvgExporter
|
|
extends RefCounted
|
|
|
|
## Builds a printable SVG sheet directly from the routine model. No viewport
|
|
## screenshot is involved, so the output stays vector-based.
|
|
|
|
const PAGE_WIDTH := 1600.0
|
|
const PAGE_HEIGHT := 1000.0
|
|
const MARGIN := 48.0
|
|
const HEADER_HEIGHT := 118.0
|
|
const FOOTER_HEIGHT := 72.0
|
|
const CURVE_SAMPLE_STEP_M := 0.2
|
|
const MAX_CURVE_SAMPLES := 3000
|
|
|
|
const ICE_COLOR := "#eef6fb"
|
|
const BOARDS_COLOR := "#1e293b"
|
|
const OUTSIDE_COLOR := "#DC2626"
|
|
const INSIDE_COLOR := "#2563EB"
|
|
const TEXT_COLOR := "#0f172a"
|
|
const MUTED_TEXT_COLOR := "#64748b"
|
|
|
|
|
|
static func build(routine: Routine, section_id: String = "") -> String:
|
|
if routine == null:
|
|
return ""
|
|
|
|
var rink: RinkDimensions = routine.rink
|
|
if rink == null or not rink.is_valid():
|
|
rink = RinkDimensions.new()
|
|
|
|
var sections: Array[RoutineSection] = []
|
|
for section: RoutineSection in routine.sections:
|
|
if section == null:
|
|
continue
|
|
if section_id.is_empty() or section.section_id == section_id:
|
|
sections.append(section)
|
|
|
|
var title := routine.title.strip_edges()
|
|
if title.is_empty():
|
|
title = "Unbenannte Kür"
|
|
|
|
var subtitle := _build_subtitle(routine, sections, section_id)
|
|
var area := Rect2(
|
|
MARGIN,
|
|
HEADER_HEIGHT,
|
|
PAGE_WIDTH - 2.0 * MARGIN,
|
|
PAGE_HEIGHT - HEADER_HEIGHT - FOOTER_HEIGHT
|
|
)
|
|
var scale := minf(
|
|
area.size.x / maxf(rink.length_m, 0.001),
|
|
area.size.y / maxf(rink.width_m, 0.001)
|
|
)
|
|
var center := area.position + area.size * 0.5
|
|
|
|
var lines := PackedStringArray()
|
|
lines.append('<?xml version="1.0" encoding="UTF-8"?>')
|
|
lines.append(
|
|
'<svg xmlns="http://www.w3.org/2000/svg" width="%s" height="%s" viewBox="0 0 %s %s">'
|
|
% [_n(PAGE_WIDTH), _n(PAGE_HEIGHT), _n(PAGE_WIDTH), _n(PAGE_HEIGHT)]
|
|
)
|
|
lines.append(
|
|
'<rect x="0" y="0" width="%s" height="%s" fill="#ffffff"/>'
|
|
% [_n(PAGE_WIDTH), _n(PAGE_HEIGHT)]
|
|
)
|
|
lines.append(
|
|
'<text x="%s" y="%s" font-family="sans-serif" font-size="34" font-weight="bold" fill="%s">%s</text>'
|
|
% [_n(MARGIN), _n(58.0), TEXT_COLOR, _escape(title)]
|
|
)
|
|
lines.append(
|
|
'<text x="%s" y="%s" font-family="sans-serif" font-size="16" fill="%s">%s</text>'
|
|
% [_n(MARGIN), _n(88.0), MUTED_TEXT_COLOR, _escape(subtitle)]
|
|
)
|
|
|
|
var outline := _transform_points(rink.get_outline_points(16), scale, center)
|
|
if outline.size() >= 3:
|
|
lines.append(
|
|
'<polygon points="%s" fill="%s" stroke="%s" stroke-width="%s" stroke-linejoin="round"/>'
|
|
% [
|
|
_points(outline),
|
|
ICE_COLOR,
|
|
BOARDS_COLOR,
|
|
_n(maxf(2.0, 0.14 * scale)),
|
|
]
|
|
)
|
|
|
|
var half_width := rink.get_half_width()
|
|
lines.append(
|
|
'<line x1="%s" y1="%s" x2="%s" y2="%s" stroke="%s" stroke-opacity="0.35" stroke-width="%s"/>'
|
|
% [
|
|
_n(center.x),
|
|
_n(center.y - half_width * scale),
|
|
_n(center.x),
|
|
_n(center.y + half_width * scale),
|
|
OUTSIDE_COLOR,
|
|
_n(maxf(1.0, 0.09 * scale)),
|
|
]
|
|
)
|
|
lines.append(
|
|
'<circle cx="%s" cy="%s" r="%s" fill="none" stroke="#94a3b8" stroke-width="%s"/>'
|
|
% [
|
|
_n(center.x),
|
|
_n(center.y),
|
|
_n(4.5 * scale),
|
|
_n(maxf(1.0, 0.05 * scale)),
|
|
]
|
|
)
|
|
|
|
for section: RoutineSection in sections:
|
|
for segment: PathSegment in section.segments:
|
|
if segment == null or segment.curve == null:
|
|
continue
|
|
var points := _transform_points(
|
|
_sample_curve(segment.curve),
|
|
scale,
|
|
center
|
|
)
|
|
if points.size() < 2:
|
|
continue
|
|
|
|
var color := (
|
|
OUTSIDE_COLOR
|
|
if segment.edge == PathSegment.Edge.OUTSIDE
|
|
else INSIDE_COLOR
|
|
)
|
|
var dash := ""
|
|
if segment.direction == PathSegment.Direction.BACKWARD:
|
|
dash = ' stroke-dasharray="%s %s"' % [
|
|
_n(maxf(8.0, 0.35 * scale)),
|
|
_n(maxf(6.0, 0.2 * scale)),
|
|
]
|
|
|
|
lines.append(
|
|
'<polyline points="%s" fill="none" stroke="%s" stroke-width="%s" stroke-linecap="round" stroke-linejoin="round"%s/>'
|
|
% [
|
|
_points(points),
|
|
color,
|
|
_n(maxf(2.0, 0.12 * scale)),
|
|
dash,
|
|
]
|
|
)
|
|
|
|
_add_section_badges(lines, sections, scale, center)
|
|
|
|
lines.append(
|
|
'<text x="%s" y="%s" font-family="sans-serif" font-size="14" fill="%s">Vorwärts: durchgezogen · Rückwärts: gestrichelt · Außen: rot · Innen: blau</text>'
|
|
% [_n(MARGIN), _n(PAGE_HEIGHT - 34.0), MUTED_TEXT_COLOR]
|
|
)
|
|
lines.append(
|
|
'<text x="%s" y="%s" font-family="sans-serif" font-size="14" fill="%s" text-anchor="end">Rinkplanner</text>'
|
|
% [
|
|
_n(PAGE_WIDTH - MARGIN),
|
|
_n(PAGE_HEIGHT - 34.0),
|
|
MUTED_TEXT_COLOR,
|
|
]
|
|
)
|
|
lines.append("</svg>")
|
|
return "\n".join(lines)
|
|
|
|
|
|
static func _build_subtitle(
|
|
routine: Routine,
|
|
sections: Array[RoutineSection],
|
|
section_id: String
|
|
) -> String:
|
|
if not section_id.is_empty():
|
|
var section := routine.get_section_by_id(section_id)
|
|
if section != null:
|
|
return "%s · %s s" % [
|
|
section.title,
|
|
_format_seconds(section.get_duration_seconds()),
|
|
]
|
|
return "Abschnitt"
|
|
|
|
return "%d Abschnitte · %s s" % [
|
|
sections.size(),
|
|
_format_seconds(routine.get_duration_seconds()),
|
|
]
|
|
|
|
|
|
static func _add_section_badges(
|
|
lines: PackedStringArray,
|
|
sections: Array[RoutineSection],
|
|
scale: float,
|
|
center: Vector2
|
|
) -> void:
|
|
for index: int in range(sections.size()):
|
|
var section := sections[index]
|
|
var first_point := Vector2.INF
|
|
for segment: PathSegment in section.segments:
|
|
if segment != null and segment.curve != null and segment.curve.point_count > 0:
|
|
first_point = segment.curve.get_point_position(0)
|
|
break
|
|
if first_point == Vector2.INF:
|
|
continue
|
|
var point := center + first_point * scale
|
|
lines.append(
|
|
'<circle cx="%s" cy="%s" r="12" fill="%s"/>'
|
|
% [_n(point.x), _n(point.y), TEXT_COLOR]
|
|
)
|
|
lines.append(
|
|
'<text x="%s" y="%s" font-family="sans-serif" font-size="13" font-weight="bold" fill="#ffffff" text-anchor="middle" dominant-baseline="central">%d</text>'
|
|
% [_n(point.x), _n(point.y), index + 1]
|
|
)
|
|
|
|
|
|
static func _sample_curve(curve: Curve2D) -> PackedVector2Array:
|
|
var length := curve.get_baked_length()
|
|
if length <= 0.0:
|
|
if curve.point_count == 0:
|
|
return PackedVector2Array()
|
|
return PackedVector2Array([curve.get_point_position(0)])
|
|
|
|
var steps: int = clampi(
|
|
ceili(length / CURVE_SAMPLE_STEP_M),
|
|
2,
|
|
MAX_CURVE_SAMPLES
|
|
)
|
|
var points := PackedVector2Array()
|
|
points.resize(steps + 1)
|
|
for index: int in range(steps + 1):
|
|
points[index] = curve.sample_baked(length * float(index) / float(steps))
|
|
return points
|
|
|
|
|
|
static func _transform_points(
|
|
points: PackedVector2Array,
|
|
scale: float,
|
|
center: Vector2
|
|
) -> PackedVector2Array:
|
|
var transformed := PackedVector2Array()
|
|
transformed.resize(points.size())
|
|
for index: int in range(points.size()):
|
|
transformed[index] = center + points[index] * scale
|
|
return transformed
|
|
|
|
|
|
static func _points(points: PackedVector2Array) -> String:
|
|
var parts := PackedStringArray()
|
|
for point: Vector2 in points:
|
|
parts.append("%s,%s" % [_n(point.x), _n(point.y)])
|
|
return " ".join(parts)
|
|
|
|
|
|
static func _n(value: float) -> String:
|
|
return "%.2f" % value
|
|
|
|
|
|
static func _format_seconds(seconds: float) -> String:
|
|
return ("%.1f" % seconds).replace(".", ",")
|
|
|
|
|
|
static func _escape(text: String) -> String:
|
|
return (
|
|
text
|
|
.replace("&", "&")
|
|
.replace("<", "<")
|
|
.replace(">", ">")
|
|
.replace('"', """)
|
|
)
|