use core::fmt;
use std::fmt::Debug;
use glam::{UVec2, Vec2};
use crate::{
color::{Color, WHITE},
handle::Handle,
types::Rect,
};
impl Debug for Sprite {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let size = self.size();
f.debug_struct("Sprite").field("size", &size).finish()
}
}
#[derive(Clone)]
pub struct Sprite {
pub texture: Handle,
pub src: Option<Rect>,
pub size: UVec2,
pub color: Color,
pub spacing: f32,
pub padding: f32,
pub flip_x: bool,
pub flip_y: bool,
pub anchor: Vec2,
}
impl Sprite {
pub fn new(texture: Handle, size: UVec2) -> Self {
Self {
texture,
size,
src: None,
color: WHITE,
spacing: 0.0,
padding: 0.0,
flip_x: false,
flip_y: false,
anchor: Vec2::splat(0.5),
}
}
pub fn with_anchor(mut self, anchor: Vec2) -> Self {
self.anchor = anchor;
self
}
pub fn with_tile(mut self, tile: u16, tile_size: Vec2) -> Self {
let cols =
((self.size.x as f32 - self.padding) / (tile_size.x + self.spacing)).ceil() as u32;
let row = tile as u32 / cols;
let col = tile as u32 % cols;
let src_pos = Vec2::new(
col as f32 * (tile_size.x + self.spacing) + self.padding,
row as f32 * (tile_size.y + self.spacing) + self.padding,
);
let src_size = Vec2::new(tile_size.x, tile_size.y);
let src = Rect {
min: src_pos,
max: src_pos + src_size,
};
self.src = Some(src);
self
}
pub fn size(&self) -> UVec2 {
self.size
}
}