Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish 0.11 TODOs #253

Merged
merged 7 commits into from
Feb 16, 2023
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Template support!
Templates are loaded automatically when they are encountered, and are treated as intermediate
objects. As such, `ResourceCache` has now methods for both getting and inserting them (#170).
- Text object support (#230).
- VFS support (#199).
- Text object support (#230).
- `cache_mut` loader property (#207).

### Changed
- `LayerType` variants have been stripped from the `Layer` suffix (#203).
- `TileData::tile_type` has been renamed to `TileData::user_type`. (#253)
- `Orientation`'s `FromStr` impl now returns `OrientationParseError` as the error type. (#253)
- `ResourceCache::get_or_try_insert_tileset_with` has been replaced by `ResourceCache::insert_tileset`.
- `DefaultResourceCache`'s members have been made public.

### Removed
- `ObjectData::obj_type`, `ObjectData::width`, `ObjectData::height`. (#253)
- `TileData::tile_type` (which has been renamed to `TileData::user_type`) (#253)

## [0.10.3]
### Added
- Support for Wang sets.
Expand Down
2 changes: 1 addition & 1 deletion src/layers/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<'map> ObjectLayer<'map> {
/// _ => None,
/// })
/// .flat_map(|layer| layer.objects())
/// .filter(|object| object.obj_type == "spawn")
/// .filter(|object| object.user_type == "spawn")
/// .collect();
///
/// dbg!(spawnpoints);
Expand Down
20 changes: 19 additions & 1 deletion src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,25 @@ use crate::{DefaultResourceCache, Map, ResourceCache, Result, Tileset};
/// This trait should be implemented if you wish to load data from a virtual filesystem.
///
/// ## Example
/// TODO: ResourceReader example
/// ```
/// use std::io::Cursor;
///
/// /// Basic example reader impl that just keeps a few resources in memory
/// struct MemoryReader;
///
/// impl tiled::ResourceReader for MemoryReader {
/// type Resource = Cursor<&'static [u8]>;
/// type Error = std::io::Error;
///
/// fn read_from(&mut self, path: &std::path::Path) -> std::result::Result<Self::Resource, Self::Error> {
/// if path == std::path::Path::new("my_map.tmx") {
/// Ok(Cursor::new(include_bytes!("../assets/tiled_xml.tmx")))
/// } else {
/// Err(std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"))
/// }
/// }
/// }
/// ```
pub trait ResourceReader {
/// The type of the resource that the reader provides. For example, for
/// [`FilesystemResourceReader`], this is defined as [`File`].
Expand Down
23 changes: 20 additions & 3 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,34 @@ pub enum Orientation {
Hexagonal,
}

#[derive(Debug)]
/// An error arising from trying to parse an [`Orientation`] that is not valid.
pub struct OrientationParseError {
/// The invalid string found.
pub str_found: String,
}

impl std::fmt::Display for OrientationParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("failed to parse orientation, valid options are `orthogonal`, `isometric`, `staggered` \
and `hexagonal` but got `{}` instead", self.str_found))
}
}

impl std::error::Error for OrientationParseError {}

impl FromStr for Orientation {
// TODO(0.11): Change error type to OrientationParseErr or similar
type Err = ();
type Err = OrientationParseError;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"orthogonal" => Ok(Orientation::Orthogonal),
"isometric" => Ok(Orientation::Isometric),
"staggered" => Ok(Orientation::Staggered),
"hexagonal" => Ok(Orientation::Hexagonal),
_ => Err(()),
_ => Err(OrientationParseError {
str_found: s.to_owned(),
}),
}
}
}
Expand Down
23 changes: 1 addition & 22 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,6 @@ pub struct ObjectData {
pub name: String,
/// The type of the object, which is arbitrary and set by the user.
pub user_type: String,
/// This property has been renamed to `user_type`.
#[deprecated(since = "0.10.3", note = "Use [`ObjectData::user_type`] instead")]
pub obj_type: String,
/// The width of the object, if applicable. This refers to the attribute in `object`.
/// Since it is duplicate or irrelevant information in all cases, use the equivalent
/// member in [`ObjectShape`] instead.
#[deprecated(since = "0.10.0", note = "Use [`ObjectShape`] members instead")]
pub width: f32,
/// The height of the object, if applicable. This refers to the attribute in `object`.
/// Since it is duplicate or irrelevant information in all cases, use the equivalent
/// member in [`ObjectShape`] instead.
#[deprecated(since = "0.10.0", note = "Use [`ObjectShape`] members instead")]
pub height: f32,
/// The X coordinate of this object in pixels.
pub x: f32,
/// The Y coordinate of this object in pixels.
Expand Down Expand Up @@ -238,7 +225,7 @@ impl ObjectData {
reader: &mut impl ResourceReader,
cache: &mut impl ResourceCache,
) -> Result<ObjectData> {
let (id, tile, mut n, mut t, c, mut w, mut h, mut v, mut r, template, x, y) = get_attrs!(
let (id, tile, mut n, mut t, c, w, h, mut v, mut r, template, x, y) = get_attrs!(
for v in attrs {
Some("id") => id ?= v.parse(),
Some("gid") => tile ?= v.parse::<u32>(),
Expand Down Expand Up @@ -278,10 +265,6 @@ impl ObjectData {
// The template sets the default values for the object
let obj = &template.object;
v.get_or_insert(obj.visible);
#[allow(deprecated)]
w.get_or_insert(obj.width);
#[allow(deprecated)]
h.get_or_insert(obj.height);
r.get_or_insert(obj.rotation);
n.get_or_insert_with(|| obj.name.clone());
t.get_or_insert_with(|| obj.user_type.clone());
Expand Down Expand Up @@ -346,15 +329,11 @@ impl ObjectData {

let shape = shape.unwrap_or(ObjectShape::Rect { width, height });

#[allow(deprecated)]
Ok(ObjectData {
id,
tile,
name,
obj_type: user_type.clone(),
user_type,
width,
height,
x,
y,
rotation,
Expand Down
7 changes: 2 additions & 5 deletions src/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ pub struct TileData {
/// The animation frames of this tile.
pub animation: Option<Vec<Frame>>,
/// The type of this tile.
// TODO(0.11.0): Rename to `user_type` (https://github.com/mapeditor/rs-tiled/pull/238, postponed because of
// breaking change)
pub tile_type: Option<String>,
pub user_type: Option<String>,
/// The probability of this tile.
pub probability: f32,
}
Expand Down Expand Up @@ -105,13 +103,12 @@ impl TileData {
});
Ok((
id,
#[allow(deprecated)]
TileData {
image,
properties,
collision: objectgroup,
animation,
tile_type: user_type,
user_type,
probability: probability.unwrap_or(1.0),
},
))
Expand Down