use std::hash::{Hash, Hasher};
use std::sync::Arc;
use crate::grid::{Rect, Rgb};
use crate::icon::Icon;
use crate::measure::Font;
pub const UNIT: i32 = 64;
pub fn units(cells: f32) -> i32 {
(cells * UNIT as f32).round() as i32
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Bounds {
pub x: i32,
pub y: i32,
pub w: i32,
pub h: i32,
}
impl Bounds {
pub fn cells(x: f32, y: f32, w: f32, h: f32) -> Self {
Self {
x: units(x),
y: units(y),
w: units(w),
h: units(h),
}
}
pub fn inset(rect: Rect, x: f32, y: f32) -> Self {
Self::cells(
rect.x as f32 + x,
rect.y as f32 + y,
rect.w as f32 - x * 2.0,
rect.h as f32 - y * 2.0,
)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Strip {
pub x: u8,
pub y: u8,
pub w: u8,
pub color: Rgb,
}
pub const TOP_LEFT: u8 = 1;
pub const TOP_RIGHT: u8 = 2;
pub const BOTTOM_RIGHT: u8 = 4;
pub const BOTTOM_LEFT: u8 = 8;
pub const ALL_CORNERS: u8 = 15;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Pixels {
Coverage,
Rgba,
}
#[derive(Clone, Debug)]
pub struct Bitmap {
pub id: u64,
pub width: u32,
pub height: u32,
pub kind: Pixels,
pub pixels: Arc<Vec<u8>>,
}
impl PartialEq for Bitmap {
fn eq(&self, other: &Self) -> bool {
(self.id, self.width, self.height) == (other.id, other.width, other.height)
}
}
impl Eq for Bitmap {}
impl Hash for Bitmap {
fn hash<H: Hasher>(&self, state: &mut H) {
(self.id, self.width, self.height).hash(state);
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Paint {
Panel {
bounds: Bounds,
radius: u8,
fill: Option<Rgb>,
border: Option<Rgb>,
shadow: bool,
dashed: bool,
},
Wash {
bounds: Bounds,
radius: u8,
color: Rgb,
alpha: u8,
border: Option<Rgb>,
},
Hairline { bounds: Bounds, color: Rgb },
Text {
x: i32,
y: i32,
max_width: i32,
text: String,
color: Rgb,
font: Font,
align_right: bool,
centred: bool,
underline: bool,
},
Icon {
bounds: Bounds,
icon: Icon,
color: Rgb,
},
Corners {
bounds: Bounds,
radius: u8,
color: Rgb,
which: u8,
},
Strips { bounds: Bounds, strips: Vec<Strip> },
Image {
bounds: Bounds,
clip: Bounds,
bitmap: Bitmap,
color: Rgb,
},
}
impl Paint {
pub fn reach(&self) -> (i32, i32) {
match self {
Self::Panel { bounds, shadow, .. } => {
let shade = if *shadow { 2 * UNIT } else { 0 };
(bounds.x - shade, bounds.x + bounds.w + shade)
}
Self::Hairline { bounds, .. }
| Self::Wash { bounds, .. }
| Self::Icon { bounds, .. }
| Self::Strips { bounds, .. }
| Self::Corners { bounds, .. } => (bounds.x, bounds.x + bounds.w.max(1)),
Self::Image { bounds, clip, .. } => (
bounds.x.max(clip.x),
(bounds.x + bounds.w.max(1)).min(clip.x + clip.w),
),
Self::Text {
x,
max_width,
text,
align_right,
centred,
..
} => {
let wide = (text.chars().count() as i32 + 1).saturating_mul(4 * UNIT);
if *align_right {
(x.saturating_sub(wide), *x)
} else if *centred {
(x.saturating_sub(wide), x.saturating_add(wide))
} else {
(*x, x.saturating_add(*max_width))
}
}
}
}
pub fn on_row(&self, top: i32) -> Self {
let mut paint = self.clone();
match &mut paint {
Self::Panel { bounds, .. }
| Self::Wash { bounds, .. }
| Self::Hairline { bounds, .. }
| Self::Icon { bounds, .. }
| Self::Strips { bounds, .. }
| Self::Corners { bounds, .. } => bounds.y -= top,
Self::Image { bounds, clip, .. } => {
let bottom = (clip.y + clip.h).min(top + UNIT);
clip.y = clip.y.max(top);
clip.h = bottom - clip.y;
clip.y -= top;
bounds.y -= top;
}
Self::Text { y, .. } => *y -= top,
}
paint
}
pub(crate) fn span(&self) -> (i32, i32) {
match self {
Self::Panel { bounds, shadow, .. } => {
let reach = if *shadow { UNIT } else { 0 };
(bounds.y - reach, bounds.y + bounds.h + reach)
}
Self::Hairline { bounds, .. }
| Self::Wash { bounds, .. }
| Self::Icon { bounds, .. }
| Self::Strips { bounds, .. }
| Self::Corners { bounds, .. } => (bounds.y, bounds.y + bounds.h.max(1)),
Self::Image { bounds, clip, .. } => (
bounds.y.max(clip.y),
(bounds.y + bounds.h.max(1)).min(clip.y + clip.h),
),
Self::Text { y, font, .. } if font.size > 100 => (*y - UNIT, *y + 2 * UNIT),
Self::Text { y, .. } => (*y, *y + UNIT),
}
}
}