Skip to content

Commit

Permalink
cleanup docs, add texture_size to tileset
Browse files Browse the repository at this point in the history
  • Loading branch information
B-Reif committed Jul 20, 2021
1 parent 1d3b918 commit 0f36139
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
11 changes: 3 additions & 8 deletions examples/tilemap/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,9 @@ pub fn check_loading_sprites(mut state: ResMut<State<AppState>>, aseloader: Res<
}

fn layer_settings_from(map_size: UVec2, chunk_size: UVec2, tileset: &Tileset) -> LayerSettings {
let Tileset {
tile_count,
tile_size,
..
} = tileset;
let tile_size = Vec2::new((*tile_size.width()).into(), (*tile_size.height()).into());
let tile_count = *tile_count as f32;
let texture_size = Vec2::new(tile_size.x, tile_size.y * tile_count);
let Tileset { tile_size, .. } = tileset;
let tile_size = Vec2::new(tile_size.width.into(), tile_size.height.into());
let texture_size = tileset.texture_size();
LayerSettings::new(map_size, chunk_size, tile_size, texture_size)
}

Expand Down
2 changes: 1 addition & 1 deletion src/ase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fmt, path::PathBuf};
use asefile::AsepriteFile;
use bevy::utils::HashMap;

/// AseId uniquely identifies an Aseprite file during runtime.
/// Unique identifier for an Aseprite file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AseId(u32);
impl AseId {
Expand Down
5 changes: 2 additions & 3 deletions src/slice.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::ase::AseId;
use std::fmt::Display;

/// SliceId identifies a [Slice] within a given Aseprite file.
/// Used with [SliceAseKey] to uniquely identify slices.
/// Identifier for a [Slice] within an Aseprite file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SliceId(u32);
impl SliceId {
Expand All @@ -19,7 +18,7 @@ impl Display for SliceId {
}
}

/// SliceAseKey uniquely identifies a Slice with an [AseId] and a [SliceId].
/// Unique identifier for a [Slice] with an [AseId] and a [SliceId].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SliceAseKey {
ase_id: AseId,
Expand Down
39 changes: 27 additions & 12 deletions src/tileset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ fn texture_from(ase: &AsepriteFile, tileset: &asefile::Tileset) -> TilesetResult
))
}

/// Identifier for a [Tileset] within an Aseprite file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TilesetId(u32);
impl TilesetId {
/// Creates a new [TilesetId] from an inner u32 value.
pub fn new(inner: u32) -> Self {
Self(inner)
}
/// Returns a reference to the id's inner u32 value.
pub fn inner(&self) -> &u32 {
&self.0
}
pub fn into_inner(self) -> u32 {
self.0
}
}
impl From<&asefile::TilesetId> for TilesetId {
fn from(ase_id: &asefile::TilesetId) -> Self {
Expand All @@ -81,18 +81,22 @@ impl fmt::Display for TilesetId {
}
}

/// Unique identifier for a [Tileset] with an [AseId] and a [TilesetId].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TilesetAseKey {
ase_id: AseId,
tileset_id: TilesetId,
}
impl TilesetAseKey {
/// Creates a new [TilesetAseKey] from an [AseId] and a [TilesetId].
pub fn new(ase_id: AseId, tileset_id: TilesetId) -> Self {
Self { ase_id, tileset_id }
}
/// Returns a reference to the key's [AseId].
pub fn ase_id(&self) -> &AseId {
&self.ase_id
}
/// Returns a reference to the key's [TilesetId].
pub fn tileset_id(&self) -> &TilesetId {
&self.tileset_id
}
Expand All @@ -103,10 +107,11 @@ impl fmt::Display for TilesetAseKey {
}
}

/// Width and height of a tile in pixels.
#[derive(Debug)]
pub struct TileSize {
width: u16,
height: u16,
pub width: u16,
pub height: u16,
}
impl TileSize {
fn from_ase(ase_size: &asefile::TileSize) -> Self {
Expand All @@ -115,24 +120,34 @@ impl TileSize {
height: *ase_size.height(),
}
}
pub fn width(&self) -> &u16 {
&self.width
}
pub fn height(&self) -> &u16 {
&self.height
}
}

/// A Sprite-based tileset.
/// Data and texture from an Aseprite tileset.
#[derive(Debug, TypeUuid)]
#[uuid = "0e2dbd05-dbad-46c9-a943-395f83dfa4ba"]
pub struct Tileset {
/// A unique identifier for this tileset.
pub key: TilesetAseKey,
/// Number of tiles in this tilset.
pub tile_count: u32,
/// Pixel size of this tileset's tiles.
pub tile_size: TileSize,
/// Name of this tileset.
pub name: String,
/// A handle to the tileset's texture. See also the [`Self::texture_size()`] method.
pub texture: Handle<Texture>,
}
impl Tileset {
/// Returns the size of the [Tileset]'s texture.
/// This has width = tile_size.width and height = tile_size.height * tile_count
/// (e.g. all tiles are stored in a vertical strip).
pub fn texture_size(&self) -> Vec2 {
let TileSize { width, height } = self.tile_size;
let tile_size = Vec2::new(width.into(), height.into());
let tile_count = self.tile_count as f32;
Vec2::new(tile_size.x, tile_size.y * tile_count)
}
}

#[derive(Debug)]
pub(crate) struct TilesetData<T> {
Expand Down

0 comments on commit 0f36139

Please sign in to comment.