use crate::core::{CanDraw, ColChar, Modifier, Vec2D};
use super::TextAlign;
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct Text {
pub pos: Vec2D,
pub content: String,
pub align: TextAlign,
pub modifier: Modifier,
}
impl Text {
#[must_use]
pub fn new(pos: Vec2D, content: &str, modifier: Modifier) -> Self {
assert!(
!content.contains('\n'),
"Text was created with a content string containing a \n character"
);
Self {
pos,
content: String::from(content),
align: TextAlign::Begin,
modifier,
}
}
#[must_use]
pub const fn with_align(mut self, align: TextAlign) -> Self {
self.align = align;
self
}
}
impl CanDraw for Text {
fn draw_to(&self, canvas: &mut impl crate::core::Canvas) {
let mut pos = self.pos;
pos.x = self.align.apply_to(pos.x, self.content.len() as i64);
for (x, text_char) in (0..).zip(self.content.chars()) {
if text_char != ' ' {
canvas.plot(
pos + Vec2D::new(x, 0),
ColChar::new(text_char, self.modifier),
);
}
}
}
}