aboutsummaryrefslogtreecommitdiff
path: root/server/tools/src/diagram_svg.rs
blob: d006ce782a8d0e13be95d9bbf322958976c91f2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
    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 mut dst = node_edge_connect_pos(dst_node, src_node);

        let dir = (src - dst).normalize_or_zero();
        let tip0 = dst;
        let tip1 = dst + (dir + dir.perp() * 0.5) * 10.;
        let tip2 = dst + (dir + dir.perp() * -0.5) * 10.;
        dst += dir * 5.; // prevent miter line cap from peeking out

        // line path
        writeln!(
            out,
            r#"<path fill="none" stroke="black" stroke-width="2" d="M{} {} L{} {}" />"#,
            src.x, src.y, dst.x, dst.y,
        )?;
        // tip path
        writeln!(
            out,
            r#"<path fill="black" stroke="none" d="M{} {} L{} {} L{} {} Z" />"#,
            tip0.x, tip0.y, tip1.x, tip1.y, tip2.x, tip2.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)
    }
}

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",
    }
}

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(())
}