godoru 0.1.0

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

use super::super::{CursorIcon, Image, ImageFit, NoAction, Style, Ui, UiNode};

pub struct ImageBuilder<'a, A = NoAction> {
    ui: &'a mut Ui<A>,
    image: Option<Image<A>>,
}

impl<'a, A: Clone> ImageBuilder<'a, A> {
    pub(crate) fn new(ui: &'a mut Ui<A>, image: ImageHandle) -> Self {
        Self {
            ui,
            image: Some(Image::new(image)),
        }
    }

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

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

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

    pub fn fit(mut self, fit: ImageFit) -> Self {
        if let Some(image) = self.image.as_mut() {
            image.fit = fit;
        }
        self
    }

    pub fn customMinimumSize(mut self, width: u32, height: u32) -> Self {
        if let Some(image) = self.image.as_mut() {
            image.customMinimumSize = Some((width, height));
        }
        self
    }

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

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

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

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

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

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

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

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

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

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

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