Skip to content

Commit

Permalink
Merging GID changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Anti-Alias committed Feb 10, 2022
1 parent 2fd9532 commit 884eb7d
Show file tree
Hide file tree
Showing 18 changed files with 844 additions and 915 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- `Tileset::source` for obtaining where the tileset actually came from.
- `Tileset::columns`.
- `Color::alpha`.
- `Layer::id`, `Layer::width`, `Layer::height`, `Layer::parallax_x` and `Layer::parallax_y`.
- Support for 'object'-type properties.
- Support for multiline string properties.
Expand All @@ -18,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- MIT license file.

### Changed
- **Set the minimum Tiled TMX version to 0.13.**
- `Tileset::tilecount` is no longer optional.
- `Layer` has been renamed to `TileLayer`, and the original `Layer` structure is now used
for common data from all layer types.
- `Map` now has a single `layers` member which contains layers of all types in order.
Expand All @@ -28,7 +31,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `parse_with_path` -> `Map::parse_reader`.
- `parse_tileset` -> `Tileset::parse`.
- All mentions of `Colour` have been changed to `Color` for consistency with the Tiled dataformat.
- `Map::get_tileset_by_gid` -> `Map::tileset_by_gid`.
- `Layer::tiles` changed from `Vec<Vec<LayerTile>>` to `Vec<LayerTile>`.
- Tile now has `image` instead of `images`. ([Issue comment](https://github.com/mapeditor/rs-tiled/issues/103#issuecomment-940773123))
- Tileset now has `image` instead of `images`.
Expand All @@ -39,6 +41,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bumped `zstd` to `0.9`.
- Fixed markdown formatting in the `CONTRIBUTORS` file.

### Fixed
- `Color` parsing.


## [0.9.5] - 2021-05-02
### Added
- Support for file properties.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ tiled = "0.9.5"

to the dependencies section of your Cargo.toml.

The minimum supported TMX version is 0.13.

### Example

```rust
Expand Down
4 changes: 2 additions & 2 deletions assets/tiled_image_layers.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<tileset firstgid="1" name="tilesheet" tilewidth="32" tileheight="32" tilecount="84" columns="14">
<image source="tilesheet.png" width="448" height="192"/>
</tileset>
<imagelayer id="1" name="Image Layer 1"/>
<imagelayer id="2" name="Image Layer 2">
<imagelayer id="1" name="Image Layer 1" tintcolor="#12345678"/>
<imagelayer id="2" name="Image Layer 2" tintcolor="123456">
<image source="tilesheet.png" width="448" height="192"/>
</imagelayer>
</map>
5 changes: 4 additions & 1 deletion assets/tiled_object_groups.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.4" tiledversion="1.4.0" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="32" tileheight="32" infinite="0" nextlayerid="4" nextobjectid="1">
<map version="1.5" tiledversion="1.7.0" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="32" tileheight="32" infinite="0" nextlayerid="4" nextobjectid="1">
<layer id="1" name="Tile Layer 1" width="10" height="10">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,
Expand All @@ -15,6 +15,9 @@
</data>
</layer>
<group id="2" name="group">
<properties>
<property name="key" value="value"/>
</properties>
<objectgroup id="3" name="sub_layer">
<properties>
<property name="an object group property" type="bool" value="true"/>
Expand Down
58 changes: 54 additions & 4 deletions examples/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
use tiled::Map;
use std::path::PathBuf;

use tiled::{FilesystemResourceCache, Map};

fn main() {
let map = Map::parse_file("assets/tiled_base64_zlib.tmx").unwrap();
println!("{:?}", map);
println!("{:?}", map.tileset_by_gid(22));
// Create a new resource cache. This is a structure that holds references to loaded
// assets such as tilesets so that they only get loaded once.
// [`FilesystemResourceCache`] is a implementation of [`tiled::ResourceCache`] that
// identifies resources by their path in the filesystem.
let mut cache = FilesystemResourceCache::new();

let map_path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("assets/tiled_base64_zlib.tmx");
let map = Map::parse_file(map_path, &mut cache).unwrap();

for layer in map.layers() {
print!("Layer \"{}\":\n\t", layer.data().name);

match layer.layer_type() {
tiled::LayerType::TileLayer(layer) => match layer.data() {
tiled::TileLayerData::Finite(data) => println!(
"Finite tile layer with width = {} and height = {}; ID of tile @ (0,0): {}",
data.width(),
data.height(),
layer.get_tile(0, 0).unwrap().id
),
tiled::TileLayerData::Infinite(data) => {
// This is prone to change! Infinite layers will be refactored before 0.10.0
// releases.
println!("Infinite tile layer with {} chunks", data.chunks.len())
}
},

tiled::LayerType::ObjectLayer(layer) => {
println!("Object layer with {} objects", layer.data().objects.len())
},

tiled::LayerType::ImageLayer(layer) => {
println!(
"Image layer with {}",
match &layer.data().image {
Some(img) =>
format!("an image with source = {}", img.source.to_string_lossy()),
None => "no image".to_owned(),
}
)
},

tiled::LayerType::GroupLayer(layer) => {
println!(
"Group layer with {} sublayers",
layer.layers().len()
)
}
}
}
}
53 changes: 37 additions & 16 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::{fmt, path::PathBuf};

#[derive(Debug, Copy, Clone)]
pub enum ParseTileError {
Expand All @@ -23,38 +23,59 @@ pub enum TiledError {
SourceRequired {
object_to_parse: String,
},
/// The path given is invalid because it isn't contained in any folder.
PathIsNotFile,
CouldNotOpenFile {
path: PathBuf,
err: std::io::Error,
},
/// There was an invalid tile in the map parsed.
InvalidTileFound,
Other(String),
}

impl fmt::Display for TiledError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match *self {
TiledError::MalformedAttributes(ref s) => write!(fmt, "{}", s),
TiledError::DecompressingError(ref e) => write!(fmt, "{}", e),
TiledError::Base64DecodingError(ref e) => write!(fmt, "{}", e),
TiledError::XmlDecodingError(ref e) => write!(fmt, "{}", e),
TiledError::PrematureEnd(ref e) => write!(fmt, "{}", e),
match self {
TiledError::MalformedAttributes(s) => write!(fmt, "{}", s),
TiledError::DecompressingError(e) => write!(fmt, "{}", e),
TiledError::Base64DecodingError(e) => write!(fmt, "{}", e),
TiledError::XmlDecodingError(e) => write!(fmt, "{}", e),
TiledError::PrematureEnd(e) => write!(fmt, "{}", e),
TiledError::SourceRequired {
ref object_to_parse,
} => {
write!(fmt, "Tried to parse external {} without a file location, e.g. by using Map::parse_reader.", object_to_parse)
}
TiledError::Other(ref s) => write!(fmt, "{}", s),
TiledError::PathIsNotFile => {
write!(
fmt,
"The path given is invalid because it isn't contained in any folder."
)
}
TiledError::CouldNotOpenFile { path, err } => {
write!(
fmt,
"Could not open '{}'. Error: {}",
path.to_string_lossy(),
err
)
}
TiledError::InvalidTileFound => write!(fmt, "Invalid tile found in map being parsed"),
TiledError::Other(s) => write!(fmt, "{}", s),
}
}
}

// This is a skeleton implementation, which should probably be extended in the future.
impl std::error::Error for TiledError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
TiledError::MalformedAttributes(_) => None,
TiledError::DecompressingError(ref e) => Some(e as &dyn std::error::Error),
TiledError::Base64DecodingError(ref e) => Some(e as &dyn std::error::Error),
TiledError::XmlDecodingError(ref e) => Some(e as &dyn std::error::Error),
TiledError::PrematureEnd(_) => None,
TiledError::SourceRequired { .. } => None,
TiledError::Other(_) => None,
match self {
TiledError::DecompressingError(e) => Some(e as &dyn std::error::Error),
TiledError::Base64DecodingError(e) => Some(e as &dyn std::error::Error),
TiledError::XmlDecodingError(e) => Some(e as &dyn std::error::Error),
TiledError::CouldNotOpenFile { err, .. } => Some(err as &dyn std::error::Error),
_ => None,
}
}
}
11 changes: 4 additions & 7 deletions src/image.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{
io::Read,
path::{Path, PathBuf},
};
use std::path::{Path, PathBuf};

use xml::{attribute::OwnedAttribute, EventReader};
use xml::attribute::OwnedAttribute;

use crate::{error::TiledError, properties::Color, util::*};

Expand All @@ -24,8 +21,8 @@ pub struct Image {
}

impl Image {
pub(crate) fn new<R: Read>(
parser: &mut EventReader<R>,
pub(crate) fn new(
parser: &mut impl Iterator<Item = XmlEventResult>,
attrs: Vec<OwnedAttribute>,
path_relative_to: impl AsRef<Path>,
) -> Result<Image, TiledError> {
Expand Down
Loading

0 comments on commit 884eb7d

Please sign in to comment.