gemini_engine/ascii/
sprite.rsuse super::{Text, TextAlign2D};
use crate::core::{CanDraw, Modifier, Vec2D};
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct Sprite {
pub pos: Vec2D,
pub texture: String,
pub modifier: Modifier,
pub align: TextAlign2D,
}
impl Sprite {
#[must_use]
pub fn new(pos: Vec2D, texture: &str, modifier: Modifier) -> Self {
Self {
pos,
texture: texture.trim_start_matches('\n').into(),
modifier,
align: TextAlign2D::default(),
}
}
#[must_use]
pub const fn with_align(mut self, align: TextAlign2D) -> Self {
self.align = align;
self
}
}
impl CanDraw for Sprite {
fn draw_to(&self, canvas: &mut impl crate::core::Canvas) {
let content_size = Vec2D::new(
self.texture.lines().map(str::len).max().unwrap_or(0) as i64,
self.texture.lines().count() as i64,
);
let pos = self.align.apply_to(self.pos, content_size);
let lines = self.texture.split('\n');
for (y, line) in (0..).zip(lines) {
Text::new(pos + Vec2D::new(0, y), line, self.modifier).draw_to(canvas);
}
}
}