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.rs99
1 files changed, 73 insertions, 26 deletions
diff --git a/src/lowlevel/map.rs b/src/lowlevel/map.rs
index c2a33cf..63cc9e1 100644
--- a/src/lowlevel/map.rs
+++ b/src/lowlevel/map.rs
@@ -8,18 +8,68 @@ use rgb::RGBA;
pub enum MapOrientation {
Orthogonal,
Isometric,
- Staggered,
- Hexagonal,
+ Staggered {
+ /// For staggered and hexagonal maps, determines whether the “even” or “odd” indexes along the staggered axis are shifted. (since 0.11)
+ stagger_axis: StaggerAxis,
+ /// For staggered and hexagonal maps, determines which axis (“x” or “y”) is staggered. (since 0.11)
+ stagger_index: StaggerIndex,
+ },
+ Hexagonal {
+ /// Only for hexagonal maps. Determines the width or height (depending on the staggered axis) of the tile’s edge, in pixels.
+ hexside_length: i32,
+ /// For staggered and hexagonal maps, determines whether the “even” or “odd” indexes along the staggered axis are shifted. (since 0.11)
+ stagger_axis: StaggerAxis,
+ /// For staggered and hexagonal maps, determines which axis (“x” or “y”) is staggered. (since 0.11)
+ stagger_index: StaggerIndex,
+ },
+ // thanks https://docs.rs/tmx/0.3.1/tmx/map/enum.Orientation.html for this format idea.
}
impl MapOrientation {
- pub fn from_string(string: &str) -> anyhow::Result<MapOrientation> {
- match string {
+ pub fn from_xml_map_node(node: &roxmltree::Node) -> anyhow::Result<MapOrientation> {
+ let orientation = node
+ .attribute("orientation")
+ .ok_or(anyhow!("map orientation missing"))?;
+
+ match orientation {
"orthogonal" => Ok(MapOrientation::Orthogonal),
"isometric" => Ok(MapOrientation::Isometric),
- "staggered" => Ok(MapOrientation::Staggered),
- "hexagonal" => Ok(MapOrientation::Hexagonal),
- _ => Err(anyhow!("Unknown MapOrientation: {}", string)),
+ "staggered" => {
+ let stagger_axis = StaggerAxis::from_string(node.attribute("staggeraxis").ok_or(
+ anyhow!("map.staggeraxis missing, it is required for orientation=staggered"),
+ )?)?;
+ let stagger_index =
+ StaggerIndex::from_string(node.attribute("staggerindex").ok_or(anyhow!(
+ "map.staggerindex missing, it is required for orientation=staggered"
+ ))?)?;
+
+ Ok(MapOrientation::Staggered {
+ stagger_axis,
+ stagger_index,
+ })
+ }
+ "hexagonal" => {
+ let stagger_axis = StaggerAxis::from_string(node.attribute("staggeraxis").ok_or(
+ anyhow!("map.staggeraxis missing, it is required for orientation=hexagonal"),
+ )?)?;
+ let stagger_index =
+ StaggerIndex::from_string(node.attribute("staggerindex").ok_or(anyhow!(
+ "map.staggerindex missing, it is required for orientation=hexagonal"
+ ))?)?;
+ let hexside_length: i32 = node
+ .attribute("hexsidelength")
+ .ok_or(anyhow!(
+ "map.hexsidelength missing, it is required for orientation=hexagonal"
+ ))?
+ .parse()?;
+
+ Ok(MapOrientation::Hexagonal {
+ stagger_axis,
+ stagger_index,
+ hexside_length,
+ })
+ }
+ _ => Err(anyhow!("Unknown MapOrientation: {}", orientation)),
}
}
@@ -27,8 +77,8 @@ impl MapOrientation {
match self {
MapOrientation::Orthogonal => "orthogonal".to_owned(),
MapOrientation::Isometric => "isometric".to_owned(),
- MapOrientation::Staggered => "staggered".to_owned(),
- MapOrientation::Hexagonal => "hexagonal".to_owned(),
+ MapOrientation::Staggered { .. } => "staggered".to_owned(),
+ MapOrientation::Hexagonal { .. } => "hexagonal".to_owned(),
}
}
}
@@ -121,16 +171,16 @@ pub struct Map {
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 tiledversion: Option<String>,
+ pub tiled_version: Option<String>,
/// Map orientation. Tiled supports “orthogonal”, “isometric”, “staggered” and “hexagonal” (since 0.11).
pub orientation: MapOrientation,
/// The order in which tiles on tile layers are rendered.
- pub renderorder: MapRenderOrder,
+ pub render_order: MapRenderOrder,
/// The compression level to use for tile layer data (defaults to -1, which means to use the algorithm default).
- pub compressionlevel: Option<isize>, // TODO seems optional, please validate
+ pub compression_level: Option<isize>, // TODO seems optional, please validate
// The map width in tiles.
pub width: usize,
@@ -138,31 +188,28 @@ pub struct Map {
pub height: usize,
/// The width of a tile.
- pub tilewidth: usize,
+ pub tile_width: usize,
/// The height of a tile.
- pub tileheight: usize,
- /// Only for hexagonal maps. Determines the width or height (depending on the staggered axis) of the tile’s edge, in pixels.
- pub hexsidelength: Option<usize>,
-
- /// For staggered and hexagonal maps, determines which axis (“x” or “y”) is staggered. (since 0.11)
- pub staggeraxis: Option<StaggerAxis>,
- /// For staggered and hexagonal maps, determines whether the “even” or “odd” indexes along the staggered axis are shifted. (since 0.11)
- pub staggerindex: Option<StaggerIndex>,
+ pub tile_height: usize,
/// The background color of the map. (optional, may include alpha value since 0.15 in the form #AARRGGBB. Defaults to fully transparent.)
- pub backgroundcolor: Option<RGBA<u8>>,
+ pub background_color: Option<RGBA<u8>>,
/// Stores the next available ID for new layers. This number is stored to prevent reuse of the same ID after layers have been removed. (since 1.2) (defaults to the highest layer id in the file + 1)
- pub nextlayerid: usize, // TODO set default in funtions
+ pub next_layer_id: usize, // TODO set default in funtions
/// Stores the next available ID for new objects. This number is stored to prevent reuse of the same ID after objects have been removed. (since 0.11) (defaults to the highest object id in the file + 1)
- pub nextobjectid: usize,
+ pub next_object_id: usize,
/// Whether this map is infinite. An infinite map has no fixed size and can grow in all directions. Its layer data is stored in chunks. (0 for false, 1 for true, defaults to 0)
pub infinite: bool,
// xml child elements
- pub properties: Option<super::property::Properties>,
- // TODO Can contain any number: <tileset>, <layer>, <objectgroup>, <imagelayer>, <group> (since 1.0), <editorsettings> (since 1.3)
+ pub properties: super::property::Properties,
+
+ pub tilesets: Vec<super::tileset::TileSetElement>,
+
+ pub layer: Vec<super::layer::LayerData>,
+ // TODO Can contain any number: <objectgroup>, <imagelayer>, <group> (since 1.0), <editorsettings> (since 1.3)
}
impl Map {