blit-gui 0.1.0

2d drawing for blit
Documentation
use core::hash::{Hash, Hasher};

use blit::{FrameSlice, HasFrameStore, LogicalRect, Paint, Ui, Widget, state};

use crate::{
    color::Color,
    display_list::{Compare, DisplayList},
    style::{Border, BorderRadius, ConicGradient, Fill, GradientStop, LinearGradient},
};

blit::builder! {
    #[derive(Clone, Copy, Debug, PartialEq)]
    pub struct Rectangle<'a> {
        new(),
        #[into]
        background: Fill<'a> = Fill::Solid(Color::TRANSPARENT),
        border: Border<'a> = Border::default(),
        radius: BorderRadius = BorderRadius::new(),
        opacity: f32 = 1.0,
    }
}

impl<C> Widget<C, state::Insert> for Rectangle<'_>
where
    C: HasFrameStore + Paint<FrameRectangle, Scalar = f32>,
{
    type Response = ();

    fn build(self, mut ui: Ui<'_, C, state::Insert>) {
        if self.background == Fill::Solid(Color::TRANSPARENT) && self.border.width <= 0.0 {
            return;
        }
        let background = FrameFill::new(self.background, ui.context_mut());
        let border_fill = FrameFill::new(self.border.fill, ui.context_mut());
        let rectangle = FrameRectangle {
            background,
            border_fill,
            border_width: self.border.width,
            radius: self.radius,
            opacity: self.opacity,
        };
        ui.paint(move |cx, area| cx.context_mut().paint(area, &rectangle));
    }
}

impl Rectangle<'_> {
    /// stores borrowed fills in the context and paints the result
    pub fn paint<C: HasFrameStore + Paint<FrameRectangle, Scalar = f32>>(self, area: LogicalRect, context: &mut C) {
        let rectangle = FrameRectangle {
            background: FrameFill::new(self.background, context),
            border_fill: FrameFill::new(self.border.fill, context),
            border_width: self.border.width,
            radius: self.radius,
            opacity: self.opacity,
        };
        context.paint(area, &rectangle);
    }
}

blit::builder! {
    /// rectangle appearance retained in the frame
    #[derive(Clone, Copy, Debug, PartialEq)]
    pub struct FrameRectangle {
        new(),
        #[into]
        background: FrameFill = FrameFill::Solid(Color::TRANSPARENT),
        #[into]
        border_fill: FrameFill = FrameFill::Solid(Color::TRANSPARENT),
        border_width: f32 = 0.0,
        radius: BorderRadius = BorderRadius::new(),
        opacity: f32 = 1.0,
    }
}

impl From<Color> for FrameFill {
    fn from(color: Color) -> Self {
        Self::Solid(color)
    }
}

impl Hash for Rectangle<'_> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        [
            self.radius.top_left,
            self.radius.top_right,
            self.radius.bottom_right,
            self.radius.bottom_left,
            self.opacity,
            self.border.width,
        ]
        .map(|value| if value == 0.0 { 0 } else { value.to_bits() })
        .hash(state);
        self.background.hash(state);
        self.border.fill.hash(state);
    }
}

impl FrameRectangle {
    #[inline]
    pub fn view<'a>(&self, list: &'a DisplayList) -> Rectangle<'a> {
        Rectangle {
            background: self.background.view(list),
            border: Border {
                width: self.border_width,
                fill: self.border_fill.view(list),
            },
            radius: self.radius,
            opacity: self.opacity,
        }
    }
}

impl Compare for FrameRectangle {
    #[inline]
    fn equal(a: &Self, left: &DisplayList, b: &Self, right: &DisplayList) -> bool {
        a.view(left) == b.view(right)
    }

    fn hash(value: &Self, list: &DisplayList, state: &mut impl Hasher) {
        value.view(list).hash(state);
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum FrameFill {
    Solid(Color),
    LinearGradient {
        stops: FrameSlice<GradientStop>,
        angle: f32,
    },
    ConicGradient {
        stops: FrameSlice<GradientStop>,
        center: [f32; 2],
        angle: f32,
    },
}

impl FrameFill {
    #[inline]
    pub(crate) fn new(fill: Fill<'_>, store: &mut impl HasFrameStore) -> Self {
        match fill {
            Fill::Solid(color) => Self::Solid(color),
            Fill::LinearGradient(gradient) => Self::LinearGradient {
                stops: store.store_mut().slice_from(gradient.stops),
                angle: gradient.angle,
            },
            Fill::ConicGradient(gradient) => Self::ConicGradient {
                stops: store.store_mut().slice_from(gradient.stops),
                center: gradient.center,
                angle: gradient.angle,
            },
        }
    }

    #[inline]
    pub(crate) fn view<'a>(&self, list: &'a DisplayList) -> Fill<'a> {
        match *self {
            Self::Solid(color) => Fill::Solid(color),
            Self::LinearGradient { stops, angle } => Fill::LinearGradient(LinearGradient {
                stops: list.store().view(stops),
                angle,
            }),
            Self::ConicGradient { stops, center, angle } => Fill::ConicGradient(ConicGradient {
                stops: list.store().view(stops),
                center,
                angle,
            }),
        }
    }
}