rootvg_quad/primitive/
solid.rs1use 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#[derive(Default, Debug, Clone, Copy, PartialEq)]
11pub struct SolidQuad {
12 pub bounds: Rect,
14 pub bg_color: PackedSrgb,
16 pub border: Border,
18 }
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 }
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 pub fn build(self) -> SolidQuad {
114 self.quad
115 }
116}
117
118#[repr(C)]
121#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
122pub struct SolidQuadPrimitive {
123 pub color: PackedSrgb,
125
126 pub position: [f32; 2],
128
129 pub size: [f32; 2],
131
132 pub border_color: PackedSrgb,
134
135 pub border_radius: [f32; 4],
137
138 pub border_width: f32,
140 }
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 }
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}