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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use crate::local::{Position, RoomCoordinate, RoomName};
use js_sys::JsString;
use serde::Serialize;

use crate::objects::impls::room_visual::{CircleStyle, LineStyle, PolyStyle, RectStyle, TextStyle};

#[derive(Clone, Serialize)]
pub struct MapCircleData {
    x: RoomCoordinate,
    y: RoomCoordinate,
    n: RoomName,
    #[serde(rename = "s", skip_serializing_if = "Option::is_none")]
    style: Option<CircleStyle>,
}

#[derive(Clone, Serialize)]
pub struct MapLineData {
    x1: RoomCoordinate,
    y1: RoomCoordinate,
    n1: RoomName,
    x2: RoomCoordinate,
    y2: RoomCoordinate,
    n2: RoomName,
    #[serde(rename = "s", skip_serializing_if = "Option::is_none")]
    style: Option<LineStyle>,
}

#[derive(Clone, Serialize)]
pub struct MapRectData {
    x: RoomCoordinate,
    y: RoomCoordinate,
    n: RoomName,
    #[serde(rename = "w")]
    width: u32,
    #[serde(rename = "h")]
    height: u32,
    #[serde(rename = "s", skip_serializing_if = "Option::is_none")]
    style: Option<RectStyle>,
}

#[derive(Clone, Serialize)]
pub struct MapPolyPoint {
    x: RoomCoordinate,
    y: RoomCoordinate,
    n: RoomName,
}

impl From<&Position> for MapPolyPoint {
    fn from(pos: &Position) -> MapPolyPoint {
        MapPolyPoint {
            x: pos.x(),
            y: pos.y(),
            n: pos.room_name(),
        }
    }
}

#[derive(Clone, Serialize)]
pub struct MapPolyData {
    points: Vec<MapPolyPoint>,
    #[serde(rename = "s", skip_serializing_if = "Option::is_none")]
    style: Option<PolyStyle>,
}

#[derive(Clone, Serialize)]
pub struct MapTextData {
    text: String,
    x: RoomCoordinate,
    y: RoomCoordinate,
    n: RoomName,
    #[serde(rename = "s", skip_serializing_if = "Option::is_none")]
    style: Option<TextStyle>,
}

#[derive(Clone, Serialize)]
#[serde(tag = "t")]
pub enum MapVisualShape {
    #[serde(rename = "c")]
    Circle(MapCircleData),
    #[serde(rename = "l")]
    Line(MapLineData),
    #[serde(rename = "r")]
    Rect(MapRectData),
    #[serde(rename = "p")]
    Poly(MapPolyData),
    #[serde(rename = "t")]
    Text(MapTextData),
}

impl MapVisualShape {
    pub fn circle(center: Position, style: Option<CircleStyle>) -> MapVisualShape {
        MapVisualShape::Circle(MapCircleData {
            x: center.x(),
            y: center.y(),
            n: center.room_name(),
            style,
        })
    }

    pub fn line(from: Position, to: Position, style: Option<LineStyle>) -> MapVisualShape {
        MapVisualShape::Line(MapLineData {
            x1: from.x(),
            y1: from.y(),
            n1: from.room_name(),
            x2: to.x(),
            y2: to.y(),
            n2: to.room_name(),
            style,
        })
    }

    pub fn rect(
        top_left: Position,
        width: u32,
        height: u32,
        style: Option<RectStyle>,
    ) -> MapVisualShape {
        MapVisualShape::Rect(MapRectData {
            x: top_left.x(),
            y: top_left.y(),
            n: top_left.room_name(),
            width,
            height,
            style,
        })
    }

    pub fn poly(points: Vec<MapPolyPoint>, style: Option<PolyStyle>) -> MapVisualShape {
        MapVisualShape::Poly(MapPolyData { points, style })
    }

    pub fn text(pos: Position, text: String, style: Option<TextStyle>) -> MapVisualShape {
        MapVisualShape::Text(MapTextData {
            x: pos.x(),
            y: pos.y(),
            n: pos.room_name(),
            text,
            style,
        })
    }
}

pub struct MapVisual {}

impl MapVisual {
    pub fn draw(visual: &MapVisualShape) {
        let val = serde_wasm_bindgen::to_value(visual).expect("expect convert visual to value");

        crate::console::add_visual(Some(&JsString::from("map")), &val);
    }

    pub fn draw_multi(visuals: &[MapVisualShape]) {
        for visual in visuals {
            let val = serde_wasm_bindgen::to_value(visual).expect("expect convert visual to value");

            crate::console::add_visual(Some(&JsString::from("map")), &val);
        }
    }

    pub fn circle(pos: Position, style: Option<CircleStyle>) {
        Self::draw(&MapVisualShape::circle(pos, style));
    }

    pub fn line(from: Position, to: Position, style: Option<LineStyle>) {
        Self::draw(&MapVisualShape::line(from, to, style));
    }

    pub fn rect(top_left: Position, width: u32, height: u32, style: Option<RectStyle>) {
        Self::draw(&MapVisualShape::rect(top_left, width, height, style));
    }

    pub fn poly(points: Vec<Position>, style: Option<PolyStyle>) {
        let points = points.iter().map(Into::into).collect();
        Self::draw(&MapVisualShape::poly(points, style));
    }

    pub fn text(pos: Position, text: String, style: Option<TextStyle>) {
        Self::draw(&MapVisualShape::text(pos, text, style));
    }
}