use std::any::Any;
use crate::core::Component;
use super::sprite::Color;
#[derive(Debug, Clone, PartialEq)]
pub struct Text {
pub content: String, pub font_path: String, pub font_size: f32, pub color: Color, pub visible: bool, pub alignment: TextAlignment, pub line_spacing: f32, pub layer: i32, }
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TextAlignment {
Left, Center, Right, }
impl TextAlignment {
pub fn default() -> Self {
TextAlignment::Left
}
}
impl Text {
pub fn new(content: &str) -> Self {
Self {
content: content.to_string(),
font_path: String::new(), font_size: 16.0,
color: Color::white(),
visible: true,
alignment: TextAlignment::default(),
line_spacing: 1.0,
layer: 0,
}
}
pub fn default() -> Self {
Self::new("")
}
pub fn with_size(content: &str, font_size: f32) -> Self {
assert!(font_size > 0.0, "Font size must be positive, got: {}", font_size);
Self {
content: content.to_string(),
font_path: String::new(),
font_size,
color: Color::white(),
visible: true,
alignment: TextAlignment::default(),
line_spacing: 1.0,
layer: 0,
}
}
pub fn with_color(content: &str, font_size: f32, color: Color) -> Self {
assert!(font_size > 0.0, "Font size must be positive, got: {}", font_size);
Self {
content: content.to_string(),
font_path: String::new(),
font_size,
color,
visible: true,
alignment: TextAlignment::default(),
line_spacing: 1.0,
layer: 0,
}
}
pub fn set_content(&mut self, content: &str) {
self.content = content.to_string();
}
pub fn append_content(&mut self, content: &str) {
self.content.push_str(content);
}
pub fn clear_content(&mut self) {
self.content.clear();
}
pub fn set_font(&mut self, font_path: &str) {
self.font_path = font_path.to_string();
}
pub fn use_default_font(&mut self) {
self.font_path.clear();
}
pub fn set_font_size(&mut self, size: f32) {
assert!(size > 0.0, "Font size must be positive, got: {}", size);
self.font_size = size;
}
pub fn set_color(&mut self, color: Color) {
self.color = color;
}
pub fn set_color_rgb(&mut self, r: f32, g: f32, b: f32) {
self.color = Color::rgb(r, g, b);
}
pub fn set_alpha(&mut self, alpha: f32) {
assert!((0.0..=1.0).contains(&alpha), "Alpha must be in range 0.0-1.0, got: {}", alpha);
self.color.a = alpha;
}
pub fn set_visible(&mut self, visible: bool) {
self.visible = visible;
}
pub fn toggle_visible(&mut self) {
self.visible = !self.visible;
}
pub fn set_alignment(&mut self, alignment: TextAlignment) {
self.alignment = alignment;
}
pub fn set_line_spacing(&mut self, spacing: f32) {
assert!(spacing > 0.0, "Line spacing must be positive, got: {}", spacing);
self.line_spacing = spacing;
}
pub fn set_layer(&mut self, layer: i32) {
self.layer = layer;
}
pub fn is_visible(&self) -> bool {
self.visible && self.color.a > 0.0 && !self.content.is_empty()
}
pub fn has_custom_font(&self) -> bool {
!self.font_path.is_empty()
}
pub fn is_empty(&self) -> bool {
self.content.is_empty()
}
pub fn length(&self) -> usize {
self.content.len()
}
pub fn line_count(&self) -> usize {
if self.content.is_empty() {
0
} else {
self.content.lines().count()
}
}
}
impl Component for Text {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn clone_box(&self) -> Box<dyn Component> {
Box::new(self.clone())
}
}