Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ zstd = "0.13.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
ordermap = "1.0.0"
itertools = "0.14.0"

[dev-dependencies]
rstest = "0.26.0"
Expand Down
103 changes: 69 additions & 34 deletions src/map/map_info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::style::{CSSClass, get_class_names};
use super::style::{CSSClass, ROOM_COLORS, get_class_names, get_style};
use super::{RotationAngle, ViewBox, calc_point, decompress_base64_data};

use super::points::{Point, points_to_svg_path};
Expand Down Expand Up @@ -45,6 +45,14 @@ impl TryFrom<&str> for MapInfoType {
#[derive(Debug)]
struct MapInfoTypeEntry(MapInfoType, Vec<MapInfoTypeDataEntry>);

#[derive(Debug)]
struct MapInfoLayer {
map_info_type: MapInfoType,
css: Vec<CSSClass>,
force_connected: bool,
colorize: bool,
}

impl<'de> Deserialize<'de> for MapInfoTypeEntry {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down Expand Up @@ -88,8 +96,8 @@ impl MapInfo {
let mut svg_elements: Vec<Box<dyn svg::node::Node>> = Vec::with_capacity(order.len());
let mut used_styles = OrderSet::new();

for (map_info_type, css, force_connected) in order {
if let Some(entries) = self.data.get(&map_info_type) {
for layer in order {
if let Some(entries) = self.data.get(&layer.map_info_type) {
if entries.is_empty() {
continue;
}
Expand All @@ -114,53 +122,80 @@ impl MapInfo {
})
.collect();

let mut group = Group::new().set("class", get_class_names(&css));
for entry in &entries {
let mut group = Group::new().set("class", get_class_names(&layer.css));
for (index, entry) in entries.iter().enumerate() {
if let Some(path) =
points_to_svg_path(&entry.points, entry.close_path, force_connected)
points_to_svg_path(&entry.points, entry.close_path, layer.force_connected)
{
let path = if layer.colorize {
let color_class = ROOM_COLORS[index % ROOM_COLORS.len()];
used_styles.insert(color_class);
path.set("class", get_style(&color_class).class_name)
} else {
path
};
group = group.add(path);
}
}
svg_elements.push(Box::new(group));
used_styles.insert(css);
if map_info_type == MapInfoType::Outline {
used_styles.extend(layer.css);
if layer.map_info_type == MapInfoType::Outline {
viewbox = calc_viewbox(&entries);
}
}
}

Some((
svg_elements,
viewbox?,
used_styles.into_iter().flatten().collect(),
))
Some((svg_elements, viewbox?, used_styles))
}

fn get_order(&self) -> [(MapInfoType, Vec<CSSClass>, bool); 3] {
fn get_order(&self) -> Vec<MapInfoLayer> {
if self.data.contains_key(&MapInfoType::BlockLine) {
[
(MapInfoType::Room, vec![CSSClass::RoomUnreachable], false),
(MapInfoType::BlockLine, vec![CSSClass::RoomReachable], false),
(
MapInfoType::Outline,
vec![CSSClass::FillNone, CSSClass::OutlineStroke],
false,
),
vec![
MapInfoLayer {
map_info_type: MapInfoType::Room,
css: vec![],
force_connected: false,
colorize: true,
},
MapInfoLayer {
map_info_type: MapInfoType::Room,
css: vec![CSSClass::RoomUnreachable],
force_connected: false,
colorize: false,
},
MapInfoLayer {
map_info_type: MapInfoType::BlockLine,
css: vec![],
force_connected: false,
colorize: true,
},
MapInfoLayer {
map_info_type: MapInfoType::Outline,
css: vec![CSSClass::FillNone, CSSClass::OutlineStroke],
force_connected: false,
colorize: false,
},
]
} else {
[
(MapInfoType::Outline, vec![CSSClass::RoomUnreachable], true),
(
MapInfoType::Room,
vec![CSSClass::RoomReachable, CSSClass::OutlineStroke],
false,
),
(
MapInfoType::Outline,
vec![CSSClass::FillNone, CSSClass::OutlineStroke],
false,
),
vec![
MapInfoLayer {
map_info_type: MapInfoType::Outline,
css: vec![CSSClass::RoomUnknown],
force_connected: true,
colorize: false,
},
MapInfoLayer {
map_info_type: MapInfoType::Room,
css: vec![CSSClass::OutlineStroke],
force_connected: false,
colorize: true,
},
MapInfoLayer {
map_info_type: MapInfoType::Outline,
css: vec![CSSClass::FillNone, CSSClass::OutlineStroke],
force_connected: false,
colorize: false,
},
]
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use common::round;
use map_info::MapInfo;
use ordermap::OrderSet;
use points::{Point, TracePoints, points_to_svg_path};
use style::{CSSClass, get_style};
use style::{CSSClass, get_style, get_used_definitions};

use super::util::decompress_base64_data;
use log::debug;
Expand Down Expand Up @@ -208,7 +208,7 @@ impl MapData {
positions: Vec<Position>,
rotation: RotationAngle,
) -> PyResult<Option<String>> {
let defs = Definitions::new()
let mut defs = Definitions::new()
.add(
// Gradient used by Bot icon
RadialGradient::new()
Expand Down Expand Up @@ -263,18 +263,14 @@ impl MapData {
let mut styles = OrderSet::new();
styles.insert(CSSClass::Path);

let mut document = Document::new().add(defs);
let mut document = Document::new();

// Create map from MapInfo, if exists, or generate background image
let viewbox = match self.map_info.borrow(py).generate(rotation) {
Some((map_elements, viewbox, info_styles)) => {
// Append all map background elements to document
map_elements
.into_iter()
.for_each(|element| document.append(element));
info_styles.into_iter().for_each(|e| {
styles.insert(e);
});
map_elements.into_iter().for_each(|e| document.append(e));
styles.extend(info_styles);
viewbox
}
_ => {
Expand All @@ -299,7 +295,12 @@ impl MapData {
}
};

document = document.set("viewBox", viewbox.to_svg_viewbox());
// Add required definitions based on used CSS classes
get_used_definitions(&styles)
.into_iter()
.for_each(|def| defs.append(def));

document = document.add(defs).set("viewBox", viewbox.to_svg_viewbox());

if !subsets.is_empty() {
let group_css = CSSClass::WallBase;
Expand Down
Loading