use bevy_ecs::prelude::*;
use super::color::Color;
use super::font;
use super::widget::Widget;
use crate::scene::SceneEntity;
#[derive(Component, Clone, Debug)]
pub struct TextLine {
pub text: String,
pub x: f32,
pub y: f32,
pub pixel_size: f32,
pub color: Color,
}
impl TextLine {
pub fn width(&self) -> f32 {
font::text_width(&self.text, self.pixel_size)
}
}
pub struct Text {
line: TextLine,
}
impl Text {
pub const DEFAULT_PIXEL_SIZE: f32 = 2.0;
pub fn at(x: f32, y: f32, text: impl Into<String>) -> Self {
Self {
line: TextLine {
text: text.into(),
x,
y,
pixel_size: Self::DEFAULT_PIXEL_SIZE,
color: Color::WHITE,
},
}
}
pub fn pixel_size(mut self, pixel_size: f32) -> Self {
self.line.pixel_size = pixel_size;
self
}
pub fn color(mut self, color: Color) -> Self {
self.line.color = color;
self
}
}
impl Widget for Text {
type Output = Entity;
fn spawn(self, world: &mut World, _screen_width: f32, _screen_height: f32) -> Entity {
world.spawn((self.line, SceneEntity)).id()
}
}