rootvg_quad/primitive/
solid.rs

1use bytemuck::{Pod, Zeroable};
2
3use rootvg_core::color::PackedSrgb;
4use rootvg_core::math::{Point, Rect, Size};
5
6use crate::border::Border;
7use crate::Radius;
8
9/// A quad primitive with a solid background.
10#[derive(Default, Debug, Clone, Copy, PartialEq)]
11pub struct SolidQuad {
12    /// The bounds of the quad in logical points.
13    pub bounds: Rect,
14    /// The background color of the quad
15    pub bg_color: PackedSrgb,
16    /// The [`Border`] of the quad
17    pub border: Border,
18    /*
19    /// The shadow of the quad
20    pub shadow: Shadow,
21    */
22}
23
24impl SolidQuad {
25    pub fn packed(&self) -> SolidQuadPrimitive {
26        SolidQuadPrimitive {
27            color: self.bg_color,
28            position: self.bounds.origin.into(),
29            size: self.bounds.size.into(),
30            border_color: self.border.color,
31            border_radius: self.border.radius.into(),
32            border_width: self.border.width,
33            //shadow_color: self.shadow.color,
34            //shadow_offset: self.shadow.offset.into(),
35            //shadow_blur_radius: self.shadow.blur_radius,
36        }
37    }
38
39    pub fn builder(size: Size) -> SolidQuadBuilder {
40        SolidQuadBuilder::new(size)
41    }
42}
43
44pub struct SolidQuadBuilder {
45    quad: SolidQuad,
46}
47
48impl SolidQuadBuilder {
49    pub fn new(size: Size) -> Self {
50        Self {
51            quad: SolidQuad {
52                bounds: Rect {
53                    origin: Point::new(0.0, 0.0),
54                    size,
55                },
56                ..Default::default()
57            },
58        }
59    }
60
61    pub fn position(mut self, position: Point) -> Self {
62        self.quad.bounds.origin = position;
63        self
64    }
65
66    pub fn bg_color(mut self, color: impl Into<PackedSrgb>) -> Self {
67        self.quad.bg_color = color.into();
68        self
69    }
70
71    pub fn border_color(mut self, color: impl Into<PackedSrgb>) -> Self {
72        self.quad.border.color = color.into();
73        self
74    }
75
76    pub fn border_width(mut self, width: f32) -> Self {
77        self.quad.border.width = width;
78        self
79    }
80
81    pub fn border_radius(mut self, radius: impl Into<Radius>) -> Self {
82        self.quad.border.radius = radius.into();
83        self
84    }
85
86    pub fn border(mut self, border: Border) -> Self {
87        self.quad.border = border;
88        self
89    }
90
91    /*
92    pub fn shadow_color(mut self, color: impl Into<PackedSrgb>) -> Self {
93        self.quad.shadow.color = color.into();
94        self
95    }
96
97    pub fn shadow_offset(mut self, offset: Point) -> Self {
98        self.quad.shadow.offset = offset;
99        self
100    }
101
102    pub fn shadow_blur_radius(mut self, blur_radius: f32) -> Self {
103        self.quad.shadow.blur_radius = blur_radius;
104        self
105    }
106
107    pub fn shadow(mut self, shadow: Shadow) -> Self {
108        self.quad.shadow = shadow;
109        self
110    }
111    */
112
113    pub fn build(self) -> SolidQuad {
114        self.quad
115    }
116}
117
118/// A quad primitive with a solid background, packed into a format for
119/// use in rendering.
120#[repr(C)]
121#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
122pub struct SolidQuadPrimitive {
123    /// The background color data of the quad.
124    pub color: PackedSrgb,
125
126    /// The position of the [`Quad`] in logical points.
127    pub position: [f32; 2],
128
129    /// The size of the [`Quad`] in logical points.
130    pub size: [f32; 2],
131
132    /// The border color of the [`Quad`], in __linear RGB__.
133    pub border_color: PackedSrgb,
134
135    /// The border radii of the [`Quad`] in logical points.
136    pub border_radius: [f32; 4],
137
138    /// The border width of the [`Quad`] in logical points.
139    pub border_width: f32,
140    /*
141    /// The shadow color of the [`Quad`].
142    pub shadow_color: PackedSrgb,
143
144    /// The shadow offset of the [`Quad`] in logical points.
145    pub shadow_offset: [f32; 2],
146
147    /// The shadow blur radius of the [`Quad`] in logical points.
148    pub shadow_blur_radius: f32,
149    */
150}
151
152impl SolidQuadPrimitive {
153    pub fn new(quad: &SolidQuad) -> Self {
154        Self {
155            color: quad.bg_color,
156            position: quad.bounds.origin.into(),
157            size: quad.bounds.size.into(),
158            border_color: quad.border.color,
159            border_radius: quad.border.radius.into(),
160            border_width: quad.border.width,
161            //shadow_color: quad.shadow.color,
162            //shadow_offset: quad.shadow.offset.into(),
163            //shadow_blur_radius: quad.shadow.blur_radius,
164        }
165    }
166}
167
168impl From<SolidQuad> for SolidQuadPrimitive {
169    fn from(q: SolidQuad) -> SolidQuadPrimitive {
170        q.packed()
171    }
172}
173
174impl<'a> From<&'a SolidQuad> for SolidQuadPrimitive {
175    fn from(q: &'a SolidQuad) -> SolidQuadPrimitive {
176        q.packed()
177    }
178}
179
180impl From<SolidQuadBuilder> for SolidQuadPrimitive {
181    fn from(q: SolidQuadBuilder) -> SolidQuadPrimitive {
182        q.build().packed()
183    }
184}
185
186impl From<SolidQuadBuilder> for SolidQuad {
187    fn from(q: SolidQuadBuilder) -> SolidQuad {
188        q.build()
189    }
190}