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 },
109 Wash {
112 bounds: Bounds,
113 radius: u8,
115 color: Rgb,
116 alpha: u8,
118 border: Option<Rgb>,
119 },
120 Hairline { bounds: Bounds, color: Rgb },
122 Text {
123 x: i32,
124 y: i32,
126 max_width: i32,
127 text: String,
128 color: Rgb,
129 font: Font,
130 align_right: bool,
132 centred: bool,
134 underline: bool,
136 },
137 Icon {
138 bounds: Bounds,
139 icon: Icon,
140 color: Rgb,
141 },
142 Corners {
145 bounds: Bounds,
146 radius: u8,
148 color: Rgb,
149 which: u8,
150 },
151 Strips { bounds: Bounds, strips: Vec<Strip> },
153 Image {
155 bounds: Bounds,
156 clip: Bounds,
158 bitmap: Bitmap,
159 color: Rgb,
160 },
161}
162
163const ROW_REACH: i32 = 4 * UNIT;
166
167fn beyond_the_row(bounds: Bounds, top: i32) -> Bounds {
170 let (low, high) = (top - ROW_REACH, top + UNIT + ROW_REACH);
171 let y = bounds.y.clamp(low, high);
172 let bottom = (bounds.y + bounds.h).clamp(low, high);
173 Bounds {
174 y,
175 h: bottom - y,
176 ..bounds
177 }
178}
179
180impl Paint {
181 pub fn reach(&self) -> (i32, i32) {
184 match self {
185 Self::Panel { bounds, shadow, .. } => {
186 let shade = if *shadow { 2 * UNIT } else { 0 };
187 (bounds.x - shade, bounds.x + bounds.w + shade)
188 }
189 Self::Hairline { bounds, .. }
190 | Self::Wash { bounds, .. }
191 | Self::Icon { bounds, .. }
192 | Self::Strips { bounds, .. }
193 | Self::Corners { bounds, .. } => (bounds.x, bounds.x + bounds.w.max(1)),
194 Self::Image { bounds, clip, .. } => (
195 bounds.x.max(clip.x),
196 (bounds.x + bounds.w.max(1)).min(clip.x + clip.w),
197 ),
198 Self::Text {
199 x,
200 max_width,
201 text,
202 align_right,
203 centred,
204 ..
205 } => {
206 let wide = (text.chars().count() as i32 + 1).saturating_mul(4 * UNIT);
208 if *align_right {
209 (x.saturating_sub(wide), *x)
210 } else if *centred {
211 (x.saturating_sub(wide), x.saturating_add(wide))
212 } else {
213 (*x, x.saturating_add(*max_width))
214 }
215 }
216 }
217 }
218
219 pub fn on_row(&self, top: i32) -> Self {
222 let mut paint = self.clone();
223 match &mut paint {
224 Self::Panel {
228 bounds,
229 dashed: false,
230 ..
231 }
232 | Self::Wash { bounds, .. }
233 | Self::Hairline { bounds, .. }
234 | Self::Corners { bounds, .. } => {
235 *bounds = beyond_the_row(*bounds, top);
236 bounds.y -= top;
237 }
238 Self::Panel { bounds, .. }
239 | Self::Icon { bounds, .. }
240 | Self::Strips { bounds, .. } => bounds.y -= top,
241 Self::Image { bounds, clip, .. } => {
242 let bottom = (clip.y + clip.h).min(top + UNIT);
244 clip.y = clip.y.max(top);
245 clip.h = bottom - clip.y;
246 clip.y -= top;
247 bounds.y -= top;
248 }
249 Self::Text { y, .. } => *y -= top,
250 }
251 paint
252 }
253
254 pub(crate) fn span(&self) -> (i32, i32) {
256 match self {
257 Self::Panel { bounds, shadow, .. } => {
258 let reach = if *shadow { UNIT } else { 0 };
259 (bounds.y - reach, bounds.y + bounds.h + reach)
260 }
261 Self::Hairline { bounds, .. }
262 | Self::Wash { bounds, .. }
263 | Self::Icon { bounds, .. }
264 | Self::Strips { bounds, .. }
265 | Self::Corners { bounds, .. } => (bounds.y, bounds.y + bounds.h.max(1)),
266 Self::Image { bounds, clip, .. } => (
267 bounds.y.max(clip.y),
268 (bounds.y + bounds.h.max(1)).min(clip.y + clip.h),
269 ),
270 Self::Text { y, font, .. } if font.size > 100 => (*y - UNIT, *y + 2 * UNIT),
272 Self::Text { y, .. } => (*y, *y + UNIT),
273 }
274 }
275}