Galactica/crates/packer/src/lib.rs

62 lines
1.5 KiB
Rust
Raw Normal View History

2024-01-03 19:48:46 -08:00
#![warn(missing_docs)]
//! This crate creates texture atlases from an asset tree.
//! The main interface for this crate is ... TODO
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use serde::{Deserialize, Serialize};
/// The location of a single image in a sprite atlas
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SpriteAtlasImage {
/// The index of the atlas this image is in
pub atlas: usize,
/// x-position of this image
/// (between 0 and 1, using wgpu texture coordinates)
pub x: f32,
/// y-position of this image
/// (between 0 and 1, using wgpu texture coordinates)
pub y: f32,
/// Width of this image
/// (between 0 and 1, using wgpu texture coordinates)
pub w: f32,
/// Height of this image
/// (between 0 and 1, using wgpu texture coordinates)
pub h: f32,
}
/// A map between file paths (relative to the root asset dir)
/// and [`AtlasTexture`]s.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SpriteAtlasIndex {
pub(crate) index: HashMap<PathBuf, SpriteAtlasImage>,
}
impl SpriteAtlasIndex {
/// Make an empty [`SpriteAtlasIndex`]
pub fn new() -> Self {
Self {
index: HashMap::new(),
}
}
/// Make an empty [`SpriteAtlasIndex`]
pub fn insert(&mut self, path: PathBuf, atlasimage: SpriteAtlasImage) {
self.index.insert(path, atlasimage);
}
/// Get an [`AtlasImage`] for a file `p`.
/// Paths must be relative to the root of the asset directory.
pub fn get(&self, p: &Path) -> Option<&SpriteAtlasImage> {
self.index.get(p)
}
}