/*
Hurry Curry! - a game about cooking
Copyright (C) 2025 Hurry Curry! Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
use anyhow::Result;
use hurrycurry_protocol::{
Gamedata, Message,
book::{Diagram, DiagramEdge, DiagramNode, EdgeStyle, NodeStyle},
glam::Vec2,
};
use std::fmt::Write;
const SIZE: f32 = 64.;
const HSIZE: f32 = SIZE / 2.;
pub fn diagram_svg(data: &Gamedata, diagram: &Diagram) -> Result {
let mut out = String::new();
let (vb_min, vb_max) = diagram
.nodes
.iter()
.map(|n| (n.position, n.position))
.reduce(|(xa, xb), (ya, yb)| (xa.min(ya), xb.max(yb)))
.unwrap();
writeln!(
out,
r#"")?;
Ok(out)
}
fn node_edge_connect_pos(src: &DiagramNode, dst: &DiagramNode) -> Vec2 {
let dir = (dst.position - src.position).normalize_or_zero();
if matches!(
src.style,
NodeStyle::FinalProduct | NodeStyle::IntermediateProduct
) {
src.position + dir * HSIZE // circle
} else {
src.position + dir / dir.y.abs() * HSIZE // square (only +Y and -Y sides)
}
}
pub(crate) fn node_color(node: &DiagramNode) -> &'static str {
match node.style {
NodeStyle::FinalProduct => "#555",
NodeStyle::IntermediateProduct => "#333",
NodeStyle::ProcessActive => "#47c42b",
NodeStyle::ProcessPassive => "#c4a32b",
NodeStyle::ProcessInstant => "#5452d8",
}
}
pub(crate) fn edge_color(edge: &DiagramEdge) -> &'static str {
match edge.style {
EdgeStyle::Regular => "#000000",
EdgeStyle::ProcessPassive => "#c4a32b",
}
}
fn image_node(out: &mut String, node: &DiagramNode, path: String) -> Result<()> {
if matches!(
node.style,
NodeStyle::FinalProduct | NodeStyle::IntermediateProduct
) {
writeln!(
out,
r#""#,
node.position.x,
node.position.y,
HSIZE,
node_color(node)
)?;
} else {
writeln!(
out,
r#""#,
node.position.x - HSIZE,
node.position.y - HSIZE,
node_color(node)
)?;
}
writeln!(
out,
r#""#,
node.position.x - HSIZE,
node.position.y - HSIZE,
)?;
Ok(())
}