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.rs176
1 files changed, 176 insertions, 0 deletions
diff --git a/src/lowlevel/map.rs b/src/lowlevel/map.rs
new file mode 100644
index 0000000..c2a33cf
--- /dev/null
+++ b/src/lowlevel/map.rs
@@ -0,0 +1,176 @@
+use anyhow::anyhow;
+use rgb::RGBA;
+
+// https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#map
+
+/// Map orientation
+/// Tiled supports “orthogonal”, “isometric”, “staggered” and “hexagonal” (since 0.11).
+pub enum MapOrientation {
+ Orthogonal,
+ Isometric,
+ Staggered,
+ Hexagonal,
+}
+
+impl MapOrientation {
+ pub fn from_string(string: &str) -> anyhow::Result<MapOrientation> {
+ match string {
+ "orthogonal" => Ok(MapOrientation::Orthogonal),
+ "isometric" => Ok(MapOrientation::Isometric),
+ "staggered" => Ok(MapOrientation::Staggered),
+ "hexagonal" => Ok(MapOrientation::Hexagonal),
+ _ => Err(anyhow!("Unknown MapOrientation: {}", string)),
+ }
+ }
+
+ pub fn to_string(self) -> String {
+ match self {
+ MapOrientation::Orthogonal => "orthogonal".to_owned(),
+ MapOrientation::Isometric => "isometric".to_owned(),
+ MapOrientation::Staggered => "staggered".to_owned(),
+ MapOrientation::Hexagonal => "hexagonal".to_owned(),
+ }
+ }
+}
+
+/// 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)
+pub enum MapRenderOrder {
+ /// RightDown - The default
+ RightDown,
+ RightUp,
+ LeftDown,
+ LeftUp,
+}
+
+impl Default for MapRenderOrder {
+ fn default() -> Self {
+ MapRenderOrder::RightDown
+ }
+}
+
+impl MapRenderOrder {
+ pub fn from_string(string: &str) -> anyhow::Result<MapRenderOrder> {
+ match string {
+ "right-down" => Ok(MapRenderOrder::RightDown),
+ "right-up" => Ok(MapRenderOrder::RightUp),
+ "left-down" => Ok(MapRenderOrder::LeftDown),
+ "left-up" => Ok(MapRenderOrder::LeftUp),
+ _ => Err(anyhow!("Unknown MapRenderOrder: {}", string)),
+ }
+ }
+
+ pub fn to_string(self) -> String {
+ match self {
+ MapRenderOrder::RightDown => "right-down".to_owned(),
+ MapRenderOrder::RightUp => "right-up".to_owned(),
+ MapRenderOrder::LeftDown => "left-down".to_owned(),
+ MapRenderOrder::LeftUp => "left-up".to_owned(),
+ }
+ }
+}
+
+/// For staggered and hexagonal maps, determines which axis (“x” or “y”) is staggered. (since 0.11)
+pub enum StaggerAxis {
+ X,
+ Y,
+}
+
+impl StaggerAxis {
+ pub fn from_string(string: &str) -> anyhow::Result<StaggerAxis> {
+ match string {
+ "x" => Ok(StaggerAxis::X),
+ "y" => Ok(StaggerAxis::Y),
+ _ => Err(anyhow!("invalid StaggerAxis: {}", string)),
+ }
+ }
+
+ pub fn to_string(self) -> String {
+ match self {
+ StaggerAxis::X => "x".to_owned(),
+ StaggerAxis::Y => "y".to_owned(),
+ }
+ }
+}
+
+/// For staggered and hexagonal maps, determines whether the “even” or “odd” indexes along the staggered axis are shifted. (since 0.11)
+pub enum StaggerIndex {
+ Even,
+ Odd,
+}
+
+impl StaggerIndex {
+ pub fn from_string(string: &str) -> anyhow::Result<StaggerIndex> {
+ match string {
+ "even" => Ok(StaggerIndex::Even),
+ "odd" => Ok(StaggerIndex::Odd),
+ _ => Err(anyhow!("invalid StaggerIndex: {}", string)),
+ }
+ }
+
+ pub fn to_string(self) -> String {
+ match self {
+ StaggerIndex::Even => "even".to_owned(),
+ StaggerIndex::Odd => "odd".to_owned(),
+ }
+ }
+}
+
+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 tiledversion: 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,
+
+ /// 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
+
+ // The map width in tiles.
+ pub width: usize,
+ /// The map height in tiles.
+ pub height: usize,
+
+ /// The width of a tile.
+ pub tilewidth: 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>,
+
+ /// 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>>,
+
+ /// 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
+ /// 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,
+
+ /// 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)
+}
+
+impl Map {
+ pub fn from_xml(doc: roxmltree::Document) -> anyhow::Result<Map> {
+ doc.root_element();
+
+ assert!(doc.root_element().has_tag_name("map"));
+
+ Err(anyhow!("not implemented"))
+ }
+}