godoru 0.1.0

UI Framework for Rust using Godot
use crate::FontHandle;

use super::super::{Color, CursorIcon, FontWeight, NoAction, Style, Text, Ui, UiNode};

pub struct TextBuilder<'a, A = NoAction> {
    ui: &'a mut Ui<A>,
    text: Option<Text<A>>,
}

impl<'a, A: Clone> TextBuilder<'a, A> {
    pub(crate) fn new(ui: &'a mut Ui<A>, value: impl Into<String>) -> Self {
        Self {
            ui,
            text: Some(Text::new(value)),
        }
    }

    pub fn id(mut self, id: impl Into<String>) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.id = Some(id.into());
        }
        self
    }

    pub fn key(mut self, key: impl Into<String>) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.key = Some(key.into());
        }
        self
    }

    pub fn class(mut self, class: impl Into<String>) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.class = Some(class.into());
        }
        self
    }

    pub fn color(mut self, color: Color) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.style.color = Some(color);
        }
        self
    }

    pub fn size(mut self, size: u32) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.style.fontSize = Some(size);
        }
        self
    }

    pub fn style(mut self, configure: impl FnOnce(&mut Style)) -> Self {
        if let Some(text) = self.text.as_mut() {
            configure(&mut text.style);
        }
        self
    }

    pub fn font(mut self, font: FontHandle) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.style.font = Some(font);
        }
        self
    }

    pub fn weight(mut self, weight: FontWeight) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.style.fontWeight = Some(weight);
        }
        self
    }

    pub fn onClick(mut self, action: A) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.interactions.click = Some(action);
        }
        self
    }

    pub fn onHover(mut self, action: A) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.interactions.hover = Some(action);
        }
        self
    }

    pub fn onUnhover(mut self, action: A) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.interactions.unhover = Some(action);
        }
        self
    }

    pub fn onFocus(mut self, action: A) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.interactions.focus = Some(action);
        }
        self
    }

    pub fn onBlur(mut self, action: A) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.interactions.blur = Some(action);
        }
        self
    }

    pub fn onPointerDown(mut self, action: A) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.interactions.pointerDown = Some(action);
        }
        self
    }

    pub fn onPointerUp(mut self, action: A) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.interactions.pointerUp = Some(action);
        }
        self
    }

    pub fn cursor(mut self, cursor: CursorIcon) -> Self {
        if let Some(text) = self.text.as_mut() {
            text.cursor = Some(cursor);
        }
        self
    }
}

impl<A> Drop for TextBuilder<'_, A> {
    fn drop(&mut self) {
        if let Some(text) = self.text.take() {
            self.ui.nodes.push(UiNode::Text(text));
        }
    }
}