use bevy::{prelude::*, text::LineHeight};
use super::UiText;
#[derive(Component, Default)]
pub struct DynamicTextData {
pub id: usize,
}
pub struct DynamicTextBuilder {
data: DynamicTextData,
text: Text,
font: TextFont,
color: TextColor,
layout: TextLayout,
dynamic_text: String,
line_height: LineHeight,
}
impl Default for DynamicTextBuilder {
fn default() -> Self {
Self {
data: DynamicTextData::default(),
text: Text::new(""),
font: TextFont::from_font_size(super::DEFAULT_SIZE_MEDIUM),
color: TextColor(Color::WHITE),
layout: TextLayout::new_with_justify(Justify::Center),
dynamic_text: String::new(),
line_height: LineHeight::default(),
}
}
}
impl UiText for DynamicTextBuilder {
fn color(mut self, color: TextColor) -> Self {
self.color = color;
self
}
fn font(mut self, font: &Handle<Font>) -> Self {
self.font.font = font.clone();
self
}
fn size(mut self, size: f32) -> Self {
self.font.font_size = size;
self
}
fn text(mut self, text: Text) -> Self {
self.text = text;
self
}
fn line_height(mut self, line_height: LineHeight) -> Self {
self.line_height = line_height;
self
}
fn justify(mut self, justify: Justify) -> Self {
self.layout.justify = justify;
self
}
fn line_break(mut self, line_break: LineBreak) -> Self {
self.layout.linebreak = line_break;
self
}
}
impl DynamicTextBuilder {
pub fn id(mut self, id: usize) -> Self {
self.data.id = id;
self
}
pub fn initial_dynamic_text(mut self, text: &str) -> Self {
self.dynamic_text = text.to_string();
self
}
pub fn build(self) -> impl Bundle {
(
self.data,
self.text,
self.font.clone(),
self.color,
self.layout,
children![(self.font, self.color, TextSpan::new(self.dynamic_text))],
)
}
}