1use std::hash::{Hash, Hasher};
6use std::sync::Arc;
7
8use crate::grid::{Rect, Rgb};
9use crate::icon::Icon;
10use crate::measure::Font;
11
12pub const UNIT: i32 = 64;
14
15pub fn units(cells: f32) -> i32 {
17 (cells * UNIT as f32).round() as i32
18}
19
20#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
21pub struct Bounds {
22 pub x: i32,
23 pub y: i32,
24 pub w: i32,
25 pub h: i32,
26}
27
28impl Bounds {
29 pub fn cells(x: f32, y: f32, w: f32, h: f32) -> Self {
30 Self {
31 x: units(x),
32 y: units(y),
33 w: units(w),
34 h: units(h),
35 }
36 }
37
38 pub fn inset(rect: Rect, x: f32, y: f32) -> Self {
40 Self::cells(
41 rect.x as f32 + x,
42 rect.y as f32 + y,
43 rect.w as f32 - x * 2.0,
44 rect.h as f32 - y * 2.0,
45 )
46 }
47}
48
49#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
51pub struct Strip {
52 pub x: u8,
53 pub y: u8,
54 pub w: u8,
55 pub color: Rgb,
56}
57
58pub const TOP_LEFT: u8 = 1;
59pub const TOP_RIGHT: u8 = 2;
60pub const BOTTOM_RIGHT: u8 = 4;
61pub const BOTTOM_LEFT: u8 = 8;
62pub const ALL_CORNERS: u8 = 15;
63
64#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
66pub enum Pixels {
67 Coverage,
69 Rgba,
71}
72
73#[derive(Clone, Debug)]
75pub struct Bitmap {
76 pub id: u64,
77 pub width: u32,
78 pub height: u32,
79 pub kind: Pixels,
80 pub pixels: Arc<Vec<u8>>,
81}
82
83impl PartialEq for Bitmap {
84 fn eq(&self, other: &Self) -> bool {
85 (self.id, self.width, self.height) == (other.id, other.width, other.height)
86 }
87}
88
89impl Eq for Bitmap {}
90
91impl Hash for Bitmap {
92 fn hash<H: Hasher>(&self, state: &mut H) {
93 (self.id, self.width, self.height).hash(state);
94 }
95}
96
97#[derive(Clone, Debug, PartialEq, Eq, Hash)]
98pub enum Paint {
99 Panel {
100 bounds: Bounds,
101 radius: u8,
103 fill: Option<Rgb>,
104 border: Option<Rgb>,
105 shadow: bool,
106 dashed: bool,
108 inset: bool,
112 },
113 Wash {
116 bounds: Bounds,
117 radius: u8,
119 color: Rgb,
120 alpha: u8,
122 border: Option<Rgb>,
123 },
124 Hairline { bounds: Bounds, color: Rgb },
126 Text {
127 x: i32,
128 y: i32,
130 max_width: i32,
131 text: String,
132 color: Rgb,
133 font: Font,
134 align_right: bool,
136 centred: bool,
138 underline: bool,
140 },
141 Icon {
142 bounds: Bounds,
143 icon: Icon,
144 color: Rgb,
145 },
146 Spinner {
149 bounds: Bounds,
150 color: Rgb,
151 step: Option<u8>,
152 },
153 Corners {
156 bounds: Bounds,
157 radius: u8,
159 color: Rgb,
160 which: u8,
161 },
162 Strips { bounds: Bounds, strips: Vec<Strip> },
164 Image {
166 bounds: Bounds,
167 clip: Bounds,
169 bitmap: Bitmap,
170 color: Rgb,
171 },
172}
173
174const ROW_REACH: i32 = 4 * UNIT;
177
178fn beyond_the_row(bounds: Bounds, top: i32) -> Bounds {
181 let (low, high) = (top - ROW_REACH, top + UNIT + ROW_REACH);
182 let y = bounds.y.clamp(low, high);
183 let bottom = (bounds.y + bounds.h).clamp(low, high);
184 Bounds {
185 y,
186 h: bottom - y,
187 ..bounds
188 }
189}
190
191impl Paint {
192 pub fn top(&self) -> i32 {
194 match self {
195 Self::Text { y, .. } => *y,
196 Self::Panel { bounds, .. }
197 | Self::Wash { bounds, .. }
198 | Self::Hairline { bounds, .. }
199 | Self::Icon { bounds, .. }
200 | Self::Spinner { bounds, .. }
201 | Self::Corners { bounds, .. }
202 | Self::Strips { bounds, .. }
203 | Self::Image { bounds, .. } => bounds.y,
204 }
205 }
206
207 pub fn lift(&mut self, by: i32) {
209 match self {
210 Self::Text { y, .. } => *y -= by,
211 Self::Panel { bounds, .. }
212 | Self::Wash { bounds, .. }
213 | Self::Hairline { bounds, .. }
214 | Self::Icon { bounds, .. }
215 | Self::Spinner { bounds, .. }
216 | Self::Corners { bounds, .. }
217 | Self::Strips { bounds, .. } => bounds.y -= by,
218 Self::Image { bounds, clip, .. } => {
219 bounds.y -= by;
220 clip.y -= by;
221 }
222 }
223 }
224
225 pub fn reach(&self) -> (i32, i32) {
228 match self {
229 Self::Panel { bounds, shadow, .. } => {
230 let shade = if *shadow { 2 * UNIT } else { 0 };
231 (bounds.x - shade, bounds.x + bounds.w + shade)
232 }
233 Self::Hairline { bounds, .. }
234 | Self::Wash { bounds, .. }
235 | Self::Icon { bounds, .. }
236 | Self::Spinner { bounds, .. }
237 | Self::Strips { bounds, .. }
238 | Self::Corners { bounds, .. } => (bounds.x, bounds.x + bounds.w.max(1)),
239 Self::Image { bounds, clip, .. } => (
240 bounds.x.max(clip.x),
241 (bounds.x + bounds.w.max(1)).min(clip.x + clip.w),
242 ),
243 Self::Text {
244 x,
245 max_width,
246 text,
247 align_right,
248 centred,
249 ..
250 } => {
251 let wide = (text.chars().count() as i32 + 1).saturating_mul(4 * UNIT);
253 if *align_right {
254 (x.saturating_sub(wide), *x)
255 } else if *centred {
256 (x.saturating_sub(wide), x.saturating_add(wide))
257 } else {
258 (*x, x.saturating_add(*max_width))
259 }
260 }
261 }
262 }
263
264 pub fn on_row(&self, top: i32) -> Self {
267 let mut paint = self.clone();
268 match &mut paint {
269 Self::Panel {
273 bounds,
274 dashed: false,
275 ..
276 }
277 | Self::Wash { bounds, .. }
278 | Self::Hairline { bounds, .. }
279 | Self::Corners { bounds, .. } => {
280 *bounds = beyond_the_row(*bounds, top);
281 bounds.y -= top;
282 }
283 Self::Panel { bounds, .. }
284 | Self::Icon { bounds, .. }
285 | Self::Spinner { bounds, .. }
286 | Self::Strips { bounds, .. } => bounds.y -= top,
287 Self::Image { bounds, clip, .. } => {
288 let bottom = (clip.y + clip.h).min(top + UNIT);
290 clip.y = clip.y.max(top);
291 clip.h = bottom - clip.y;
292 clip.y -= top;
293 bounds.y -= top;
294 }
295 Self::Text { y, .. } => *y -= top,
296 }
297 paint
298 }
299
300 pub(crate) fn span(&self) -> (i32, i32) {
302 match self {
303 Self::Panel { bounds, shadow, .. } => {
304 let reach = if *shadow { UNIT } else { 0 };
305 (bounds.y - reach, bounds.y + bounds.h + reach)
306 }
307 Self::Hairline { bounds, .. }
308 | Self::Wash { bounds, .. }
309 | Self::Icon { bounds, .. }
310 | Self::Spinner { bounds, .. }
311 | Self::Strips { bounds, .. }
312 | Self::Corners { bounds, .. } => (bounds.y, bounds.y + bounds.h.max(1)),
313 Self::Image { bounds, clip, .. } => (
314 bounds.y.max(clip.y),
315 (bounds.y + bounds.h.max(1)).min(clip.y + clip.h),
316 ),
317 Self::Text { y, font, .. } if font.size > 100 => (*y - UNIT, *y + 2 * UNIT),
319 Self::Text { y, .. } => (*y, *y + UNIT),
320 }
321 }
322}