moltrun 1.7.2

High-performance game engine library with AI capabilities, built on wgpu for modern 3D graphics and physics simulation
Documentation
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,        // 줄 간격 (1.0이 기본)
    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;
    }
    
    /// 색상을 RGB로 설정
    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())
    }
}