diff options
author | metamuffin <metamuffin@disroot.org> | 2025-10-05 23:42:35 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-10-05 23:42:39 +0200 |
commit | 6d7e8fda46be9d531551670cd66de2161e5dbeb5 (patch) | |
tree | 89ca36fa656a63272b50a56308c4940f41b0405f | |
parent | 1ff014de21c6f37399c222ac16cd5ae9b4bce219 (diff) | |
download | hurrycurry-6d7e8fda46be9d531551670cd66de2161e5dbeb5.tar hurrycurry-6d7e8fda46be9d531551670cd66de2161e5dbeb5.tar.bz2 hurrycurry-6d7e8fda46be9d531551670cd66de2161e5dbeb5.tar.zst |
diagram native svg output
-rw-r--r-- | server/tools/src/book_html.rs | 12 | ||||
-rw-r--r-- | server/tools/src/diagram_dot.rs | 31 | ||||
-rw-r--r-- | server/tools/src/diagram_layout.rs | 2 | ||||
-rw-r--r-- | server/tools/src/diagram_svg.rs | 148 | ||||
-rw-r--r-- | server/tools/src/main.rs | 49 |
5 files changed, 218 insertions, 24 deletions
diff --git a/server/tools/src/book_html.rs b/server/tools/src/book_html.rs index a1dfaf9b..4a111538 100644 --- a/server/tools/src/book_html.rs +++ b/server/tools/src/book_html.rs @@ -21,6 +21,8 @@ use hurrycurry_protocol::{ book::{Book, BookPage, Diagram}, }; +use crate::diagram_svg::diagram_svg; + pub fn render_html_book(data: &Gamedata, book: &Book) -> String { BookR { book, data }.to_string() } @@ -78,14 +80,6 @@ markup::define! { } DiagramR<'a>(data: &'a Gamedata, diagram: &'a Diagram) { - div.diagram[style="position: absolute;"] { - @for node in &diagram.nodes { - div.node[style=format!("position: relative; left: {}px; top: {}px;", node.position.x, 350. - node.position.y / 2.)] { - @MessageR { data, message: &node.label } - } - } - // @for edge in &diagram.edges { - // } - } + @markup::raw(diagram_svg(data, diagram).unwrap()) } } diff --git a/server/tools/src/diagram_dot.rs b/server/tools/src/diagram_dot.rs index 2c953987..b3e54881 100644 --- a/server/tools/src/diagram_dot.rs +++ b/server/tools/src/diagram_dot.rs @@ -16,19 +16,42 @@ */ -use anyhow::Result; +use anyhow::{Result, bail}; use hurrycurry_protocol::{ Gamedata, Message, book::{Diagram, NodeStyle}, }; -use std::fmt::Write; +use std::{ + fmt::Write, + io::Write as W2, + process::{Command, Stdio}, +}; + +pub fn diagram_dot_svg(data: &Gamedata, diagram: &Diagram) -> Result<String> { + let mut child = Command::new("dot") + .arg("-Tsvg") + .arg("-Knop2") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn()?; -pub fn diagram_dot(data: &Gamedata, diagram: &Diagram) -> Result<String> { + let dot = diagram_dot(data, diagram, true)?; + child.stdin.as_mut().unwrap().write_all(dot.as_bytes())?; + let output = child.wait_with_output()?; + if !output.status.success() { + bail!("dot failed"); + } + Ok(String::from_utf8(output.stdout)?) +} + +pub fn diagram_dot(data: &Gamedata, diagram: &Diagram, use_position: bool) -> Result<String> { let mut out = String::new(); writeln!(out, "digraph {{")?; for (i, n) in diagram.nodes.iter().enumerate() { let mut attrs = Vec::new(); - + if use_position { + attrs.push(format!("pos=\"{},{}!\"", n.position.x, n.position.y)); + } node_style(&mut attrs, &n.style); match &n.label { Message::Text(text) => { diff --git a/server/tools/src/diagram_layout.rs b/server/tools/src/diagram_layout.rs index bfa0a269..0ea26a69 100644 --- a/server/tools/src/diagram_layout.rs +++ b/server/tools/src/diagram_layout.rs @@ -42,7 +42,7 @@ pub fn diagram_layout(diagram: &mut Diagram) -> Result<()> { let mut out = String::new(); writeln!(out, "digraph {{")?; for (i, _) in diagram.nodes.iter().enumerate() { - writeln!(out, "k{i} [width=2, height=2]")?; + writeln!(out, "k{i} [width=1, height=1]")?; } for edge in &diagram.edges { writeln!(out, "k{} -> k{}", edge.src, edge.dst)?; diff --git a/server/tools/src/diagram_svg.rs b/server/tools/src/diagram_svg.rs new file mode 100644 index 00000000..a5d09316 --- /dev/null +++ b/server/tools/src/diagram_svg.rs @@ -0,0 +1,148 @@ +/* + 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 <https://www.gnu.org/licenses/>. + +*/ + +use anyhow::Result; +use hurrycurry_protocol::{ + Gamedata, Message, + book::{Diagram, DiagramNode, 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<String> { + 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#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="{} {} {} {}" width="{}" height="{}">"#, + vb_min.x - HSIZE, + vb_min.y - HSIZE, + vb_max.x + HSIZE, + vb_max.y + HSIZE, + vb_max.x - vb_min.x + SIZE, + vb_max.y - vb_min.y + SIZE, + )?; + + for node in &diagram.nodes { + match node.label { + Message::Translation { .. } => {} + Message::Text(_) => {} + Message::Item(item_index) => { + image_node( + &mut out, + node, + format!("items/{}.png", data.item_name(item_index)), + )?; + } + Message::Tile(tile_index) => { + image_node( + &mut out, + node, + format!("tiles/{}.png", data.tile_name(tile_index)), + )?; + } + } + } + for edge in &diagram.edges { + let src_node = &diagram.nodes[edge.src]; + let dst_node = &diagram.nodes[edge.dst]; + let src = node_edge_connect_pos(src_node, dst_node); + let dst = node_edge_connect_pos(dst_node, src_node); + writeln!( + out, + r#"<path fill="none" stroke="black" stroke-width="2" d="M{} {} L{} {}" />"#, + src.x, src.y, dst.x, dst.y, + )?; + + // Array tip + let dir = (src - dst).normalize_or_zero(); + let c1 = dst + (dir + dir.perp() * 0.5) * 10.; + let c2 = dst + (dir + dir.perp() * -0.5) * 10.; + writeln!( + out, + r#"<path fill="black" stroke="none" d="M{} {} L{} {} L{} {} Z" />"#, + dst.x, dst.y, c1.x, c1.y, c2.x, c2.y + )?; + } + + writeln!(out, "</svg>")?; + 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) + } +} + +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", + } +} + +fn image_node(out: &mut String, node: &DiagramNode, path: String) -> Result<()> { + if matches!( + node.style, + NodeStyle::FinalProduct | NodeStyle::IntermediateProduct + ) { + writeln!( + out, + r#"<circle cx="{}" cy="{}" r="{}" stroke="none" fill="{}" />"#, + node.position.x, + node.position.y, + HSIZE, + node_color(node) + )?; + } else { + writeln!( + out, + r#"<rect x="{}" y="{}" width="{SIZE}" height="{SIZE}" stroke="none" fill="{}" />"#, + node.position.x - HSIZE, + node.position.y - HSIZE, + node_color(node) + )?; + } + writeln!( + out, + r#"<image href="{path}" x="{}" y="{}" width="{SIZE}" height="{SIZE}" />"#, + node.position.x - HSIZE, + node.position.y - HSIZE, + )?; + Ok(()) +} diff --git a/server/tools/src/main.rs b/server/tools/src/main.rs index c121d5a1..cb5fda13 100644 --- a/server/tools/src/main.rs +++ b/server/tools/src/main.rs @@ -20,6 +20,7 @@ pub mod book; pub mod book_html; pub mod diagram_dot; pub mod diagram_layout; +pub mod diagram_svg; pub mod graph; pub mod graph_summary; pub mod map_linter; @@ -28,7 +29,9 @@ pub mod recipe_diagram; use crate::{ book::{book, print_book}, book_html::render_html_book, - diagram_dot::diagram_dot, + diagram_dot::{diagram_dot, diagram_dot_svg}, + diagram_layout::diagram_layout, + diagram_svg::diagram_svg, graph::graph, graph_summary::graph_summary, map_linter::check_map, @@ -42,13 +45,27 @@ use hurrycurry_server::data::DataIndex; enum Action { Graph, GraphSummary, - GraphSingle { out: String }, + GraphSingle { + out: String, + #[arg(short)] + custom_svg: bool, + #[arg(short)] + dot_out: bool, + }, Book, BookHtml, - MapDemands { map: String }, - MapItems { map: String }, - MapTiles { map: String }, - CheckMap { map: String }, + MapDemands { + map: String, + }, + MapItems { + map: String, + }, + MapTiles { + map: String, + }, + CheckMap { + map: String, + }, } fn main() -> Result<()> { @@ -57,13 +74,25 @@ fn main() -> Result<()> { match action { Action::Graph => graph()?, Action::GraphSummary => graph_summary()?, - Action::GraphSingle { out } => { + Action::GraphSingle { + out, + custom_svg, + dot_out, + } => { let mut index = DataIndex::default(); index.reload()?; let (data, serverdata, _) = index.generate("5star")?; - let diagram = recipe_diagram(&data, &serverdata, &[&out])?; - let dot = diagram_dot(&data, &diagram)?; - println!("{dot}"); + let mut diagram = recipe_diagram(&data, &serverdata, &[&out])?; + let out = if dot_out { + diagram_dot(&data, &diagram, false)? + } else if custom_svg { + diagram_layout(&mut diagram)?; + diagram_svg(&data, &diagram)? + } else { + diagram_layout(&mut diagram)?; + diagram_dot_svg(&data, &diagram)? + }; + println!("{out}"); } Action::Book => { let mut index = DataIndex::default(); |