use crate::widget::Widget;
use image::Rgba;
use image::RgbaImage;
use rusttype::{Font, Scale};
use std::any::Any;
pub struct Text {
pub id: Option<String>,
pub content: String,
pub position: (i32, i32),
pub color: Rgba<u8>,
pub scale: Scale,
pub font: Font<'static>,
}
impl Text {
pub fn new(content: impl Into<String>, font: Font<'static>) -> Self {
Self {
id: None,
content: content.into(),
position: (0, 0),
color: Rgba([0, 0, 0, 255]),
scale: Scale { x: 24.0, y: 24.0 },
font,
}
}
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = Some(id.into());
self
}
pub fn with_size(mut self, size: f32) -> Self {
self.scale = Scale { x: size, y: size };
self
}
}
impl Widget for Text {
fn set_position(&mut self, x: i32, y: i32) {
self.position = (x, y);
}
fn draw(&mut self, framebuffer: &mut RgbaImage) {
imageproc::drawing::draw_text_mut(
framebuffer,
self.color,
self.position.0,
self.position.1,
self.scale,
&self.font,
&self.content,
);
}
fn size(&self) -> (i32, i32) {
((self.content.len() as i32 * (self.scale.x * 0.5) as i32), self.scale.y as i32)
}
fn id(&self) -> Option<&str> {
self.id.as_deref()
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}