convenience method to clone single BitmapAtlas tiles as new bitmaps

This commit is contained in:
Gered 2023-05-26 11:58:30 -04:00
parent 4257985962
commit 73e64946f7

View file

@ -2,13 +2,16 @@ use std::ops::Index;
use thiserror::Error;
use crate::graphics::GeneralBitmap;
use crate::graphics::{GeneralBitmap, GeneralBlitMethod};
use crate::math::Rect;
#[derive(Error, Debug)]
pub enum BitmapAtlasError {
#[error("Region is out of bounds for the Bitmap used by the BitmapAtlas")]
OutOfBounds,
#[error("Tile index {0} is invalid / out of range")]
InvalidTileIndex(usize),
}
#[derive(Debug, Clone, Eq, PartialEq)]
@ -118,6 +121,16 @@ where
pub fn bitmap(&self) -> &BitmapType {
&self.bitmap
}
pub fn clone_tile(&self, index: usize) -> Result<BitmapType, BitmapAtlasError> {
if let Some(tile_rect) = self.get(index) {
let mut tile_bitmap = BitmapType::new(tile_rect.width, tile_rect.height).unwrap();
tile_bitmap.blit_region(GeneralBlitMethod::Solid, &self.bitmap, tile_rect, 0, 0);
Ok(tile_bitmap)
} else {
Err(BitmapAtlasError::InvalidTileIndex(index))
}
}
}
impl<BitmapType> Index<usize> for BitmapAtlas<BitmapType>