summaryrefslogtreecommitdiff
path: root/src/lowlevel/map.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lowlevel/map.rs')
-rw-r--r--src/lowlevel/map.rs70
1 files changed, 63 insertions, 7 deletions
diff --git a/src/lowlevel/map.rs b/src/lowlevel/map.rs
index 63cc9e1..9d0b715 100644
--- a/src/lowlevel/map.rs
+++ b/src/lowlevel/map.rs
@@ -1,10 +1,15 @@
+use crate::{ensure_element, ensure_tag_name, get_attribute};
use anyhow::anyhow;
use rgb::RGBA;
+use super::property::Properties;
+use super::tileset::TileSetElement;
+
// https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#map
/// Map orientation
/// Tiled supports “orthogonal”, “isometric”, “staggered” and “hexagonal” (since 0.11).
+#[derive(Debug)]
pub enum MapOrientation {
Orthogonal,
Isometric,
@@ -85,6 +90,7 @@ impl MapOrientation {
/// The order in which tiles on tile layers are rendered.
/// In all cases, the map is drawn row-by-row. (only supported for orthogonal maps at the moment)
+#[derive(Debug)]
pub enum MapRenderOrder {
/// RightDown - The default
RightDown,
@@ -121,6 +127,7 @@ impl MapRenderOrder {
}
/// For staggered and hexagonal maps, determines which axis (“x” or “y”) is staggered. (since 0.11)
+#[derive(Debug)]
pub enum StaggerAxis {
X,
Y,
@@ -144,6 +151,7 @@ impl StaggerAxis {
}
/// For staggered and hexagonal maps, determines whether the “even” or “odd” indexes along the staggered axis are shifted. (since 0.11)
+#[derive(Debug)]
pub enum StaggerIndex {
Even,
Odd,
@@ -166,13 +174,11 @@ impl StaggerIndex {
}
}
+#[derive(Debug)]
pub struct Map {
/// The TMX format version. Was “1.0” so far, and will be incremented to match minor Tiled releases.
pub version: String,
- /// The Tiled version used to save the file (since Tiled 1.0.1). May be a date (for snapshot builds). (optional)
- pub tiled_version: Option<String>,
-
/// Map orientation. Tiled supports “orthogonal”, “isometric”, “staggered” and “hexagonal” (since 0.11).
pub orientation: MapOrientation,
@@ -180,7 +186,7 @@ pub struct Map {
pub render_order: MapRenderOrder,
/// The compression level to use for tile layer data (defaults to -1, which means to use the algorithm default).
- pub compression_level: Option<isize>, // TODO seems optional, please validate
+ pub compression_level: isize,
// The map width in tiles.
pub width: usize,
@@ -214,10 +220,60 @@ pub struct Map {
impl Map {
pub fn from_xml(doc: roxmltree::Document) -> anyhow::Result<Map> {
- doc.root_element();
+ let node = doc.root_element();
+
+ ensure_element!(node);
+ ensure_tag_name!(node, "map");
+
+ let version = get_attribute!(node, "version")?.to_owned();
+
+ let orientation = MapOrientation::from_xml_map_node(&node)?;
+
+ let render_order = MapRenderOrder::from_string(get_attribute!(node, "renderorder")?)?;
- assert!(doc.root_element().has_tag_name("map"));
+ let compression_level = if let Ok(clevel) = get_attribute!(node, "compressionlevel") {
+ clevel.parse()?
+ } else {
+ -1
+ };
+
+ let infinite = if let Ok(inf) = get_attribute!(node, "infinite") {
+ match inf {
+ "1" => true,
+ "0" | _ => false,
+ }
+ } else {
+ false
+ };
+
+ let properties: Properties = match node.children().find(|&c| c.has_tag_name("properties")) {
+ Some(child_node) => Properties::from_xml(child_node)?,
+ None => Properties::default(),
+ };
+
+ let mut tilesets = Vec::new();
+
+ for tileset_node in node.children().filter(|n| n.has_tag_name("tileset")) {
+ println!("->{:?}", &tileset_node);
+ tilesets.push(TileSetElement::from_xml(tileset_node)?);
+ }
- Err(anyhow!("not implemented"))
+ Ok(Map {
+ version,
+ orientation,
+ render_order,
+ compression_level,
+ width: get_attribute!(node, "width")?.parse()?,
+ height: get_attribute!(node, "height")?.parse()?,
+ tile_width: get_attribute!(node, "tilewidth")?.parse()?,
+ tile_height: get_attribute!(node, "tileheight")?.parse()?,
+ background_color: None, // TODO
+ next_layer_id: 4242, // TODO
+ next_object_id: 4242, // TODO
+ infinite,
+ properties,
+ tilesets,
+ layer: vec![], // TODO
+ })
}
}