use crate::arena::StrRef;
use crate::display::SpriteFormat;
use crate::pod::FrameScope;
use crate::sdf::SurfacePainter;
use crate::style::Color;
use super::{Element, IntoElement};
impl<'a> IntoElement<'a> for Img<'a> {
#[inline(always)]
fn into_element(self, _cx: &'a FrameScope) -> Element<'a> {
Element::Img(self)
}
}
#[derive(Copy, Clone)]
pub enum SpriteSource<'a> {
Bitmap {
bytes: &'a [u8],
format: SpriteFormat,
tint: Color,
},
Shader {
state_hash: u32,
painter: &'a dyn Fn(&mut dyn SurfacePainter),
},
Svg {
source: StrRef<'a>,
is_path_only: bool,
tint: Color,
stroke_width_x10: u8,
},
Arc {
thickness: u8,
start_deg10: u16,
sweep_deg10: u16,
fill_sweep_deg10: u16,
track_color: Color,
fill_color: Color,
},
}
impl<'a> PartialEq for SpriteSource<'a> {
fn eq(&self, other: &Self) -> bool {
match (*self, *other) {
(
Self::Bitmap {
bytes: b1,
format: f1,
tint: t1,
},
Self::Bitmap {
bytes: b2,
format: f2,
tint: t2,
},
) => f1 == f2 && t1 == t2 && b1 == b2,
(
Self::Shader {
state_hash: h1, ..
},
Self::Shader {
state_hash: h2, ..
},
) => h1 == h2,
(
Self::Svg {
source: s1,
is_path_only: p1,
tint: t1,
stroke_width_x10: sw1,
},
Self::Svg {
source: s2,
is_path_only: p2,
tint: t2,
stroke_width_x10: sw2,
},
) => p1 == p2 && t1 == t2 && sw1 == sw2 && s1 == s2,
(
Self::Arc {
thickness: t1,
start_deg10: s1,
sweep_deg10: sw1,
fill_sweep_deg10: f1,
track_color: tc1,
fill_color: fc1,
},
Self::Arc {
thickness: t2,
start_deg10: s2,
sweep_deg10: sw2,
fill_sweep_deg10: f2,
track_color: tc2,
fill_color: fc2,
},
) => t1 == t2 && s1 == s2 && sw1 == sw2 && f1 == f2 && tc1 == tc2 && fc1 == fc2,
_ => false,
}
}
}
impl<'a> Eq for SpriteSource<'a> {}
impl<'a> core::fmt::Debug for SpriteSource<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match *self {
Self::Bitmap {
bytes,
format,
tint,
} => f
.debug_struct("Bitmap")
.field("byte_len", &bytes.len())
.field("format", &format)
.field("tint", &tint)
.finish(),
Self::Shader { state_hash, .. } => f
.debug_struct("Shader")
.field("state_hash", &state_hash)
.finish(),
Self::Svg {
source,
is_path_only,
tint,
stroke_width_x10,
} => f
.debug_struct("Svg")
.field("len", &source.as_str().len())
.field("is_path_only", &is_path_only)
.field("tint", &tint)
.field("stroke_width_x10", &stroke_width_x10)
.finish(),
Self::Arc {
thickness,
start_deg10,
sweep_deg10,
fill_sweep_deg10,
track_color,
fill_color,
} => f
.debug_struct("Arc")
.field("thickness", &thickness)
.field("start_deg10", &start_deg10)
.field("sweep_deg10", &sweep_deg10)
.field("fill_sweep_deg10", &fill_sweep_deg10)
.field("track_color", &track_color)
.field("fill_color", &fill_color)
.finish(),
}
}
}
#[derive(Copy, Clone)]
pub struct Img<'a> {
pub width: i16,
pub height: i16,
pub source: SpriteSource<'a>,
}
#[inline(always)]
pub const fn img<'a>(
width: i16,
height: i16,
bytes: &'a [u8],
format: SpriteFormat,
) -> Img<'a> {
Img {
width,
height,
source: SpriteSource::Bitmap {
bytes,
format,
tint: Color::WHITE,
},
}
}
#[inline(always)]
pub const fn svg<'a>(width: i16, height: i16, alpha_mask: &'a [u8]) -> Img<'a> {
img(width, height, alpha_mask, SpriteFormat::A8)
}
#[inline(always)]
pub const fn svg_a4<'a>(width: i16, height: i16, alpha_mask_a4: &'a [u8]) -> Img<'a> {
img(width, height, alpha_mask_a4, SpriteFormat::A4)
}
#[inline(always)]
pub fn svg_str<'a>(width: i16, height: i16, xml: impl Into<StrRef<'a>>) -> Img<'a> {
Img {
width,
height,
source: SpriteSource::Svg {
source: xml.into(),
is_path_only: false,
tint: Color::WHITE,
stroke_width_x10: 0,
},
}
}
#[inline(always)]
pub fn svg_path<'a>(width: i16, height: i16, d: impl Into<StrRef<'a>>) -> Img<'a> {
Img {
width,
height,
source: SpriteSource::Svg {
source: d.into(),
is_path_only: true,
tint: Color::WHITE,
stroke_width_x10: 0,
},
}
}
#[inline(always)]
pub fn svg_icon<'a>(width: i16, height: i16, d: impl Into<StrRef<'a>>) -> Img<'a> {
Img {
width,
height,
source: SpriteSource::Svg {
source: d.into(),
is_path_only: true,
tint: Color::WHITE,
stroke_width_x10: 20,
},
}
}
pub fn img_rotated_rgb565<'a>(
cx: &'a FrameScope,
width: i16,
height: i16,
src_w: i16,
src_h: i16,
rgb565_le_bytes: &'a [u8],
angle_deg10: u16,
scale_q8: u16,
) -> Img<'a> {
let state_hash = (rgb565_le_bytes.as_ptr() as usize as u32)
^ ((angle_deg10 as u32) << 16)
^ (scale_q8 as u32);
img_shader(cx, width, height, state_hash, move |surface| {
let b = surface.bounds();
let c = surface.clip();
if c.is_empty() || src_w <= 0 || src_h <= 0 || scale_q8 == 0 {
return;
}
let (sin_q10, cos_q10) = crate::sdf::sin_cos_deg10((angle_deg10 % 3600) as i32);
let dst_cx = b.x as i32 + (b.w as i32) / 2;
let dst_cy = b.y as i32 + (b.h as i32) / 2;
let src_cx_q8 = (src_w as i32) * 128;
let src_cy_q8 = (src_h as i32) * 128;
let inv_scale_q8 = (65536 / (scale_q8 as i32)).max(1);
for py in c.y..c.bottom() {
let dy = (py as i32) - dst_cy;
for px in c.x..c.right() {
let dx = (px as i32) - dst_cx;
let rx_q10 = (dx * cos_q10 + dy * sin_q10) as i64;
let ry_q10 = (-dx * sin_q10 + dy * cos_q10) as i64;
let sx = ((((rx_q10 * inv_scale_q8 as i64) >> 10) + src_cx_q8 as i64) >> 8) as i32;
let sy = ((((ry_q10 * inv_scale_q8 as i64) >> 10) + src_cy_q8 as i64) >> 8) as i32;
if sx >= 0 && sx < src_w as i32 && sy >= 0 && sy < src_h as i32 {
let idx = ((sy as usize) * (src_w as usize) + (sx as usize)) * 2;
if let Some(pair) = rgb565_le_bytes.get(idx..idx + 2) {
let raw = u16::from_le_bytes([pair[0], pair[1]]);
if raw != 0 {
let col = <crate::display::Rgb565 as crate::display::PixelFormat>::to_color(
crate::display::Rgb565(raw),
);
surface.blend_pixel(px, py, col, 255);
}
}
}
}
}
})
}
#[inline(always)]
pub const fn img_arc(
width: i16,
height: i16,
thickness: u8,
start_deg10: u16,
sweep_deg10: u16,
fill_sweep_deg10: u16,
track_color: Color,
fill_color: Color,
) -> Img<'static> {
Img {
width,
height,
source: SpriteSource::Arc {
thickness,
start_deg10,
sweep_deg10,
fill_sweep_deg10,
track_color,
fill_color,
},
}
}
#[inline(always)]
pub fn img_shader<'a, F>(
cx: &'a FrameScope,
width: i16,
height: i16,
state_hash: u32,
painter: F,
) -> Img<'a>
where
F: Fn(&mut dyn SurfacePainter) + 'a,
{
Img {
width,
height,
source: SpriteSource::Shader {
state_hash,
painter: cx.arena().alloc(painter),
},
}
}
impl<'a> Img<'a> {
#[inline(always)]
pub const fn tint(mut self, color: Color) -> Self {
match self.source {
SpriteSource::Bitmap {
bytes, format, ..
} => {
self.source = SpriteSource::Bitmap {
bytes,
format,
tint: color,
};
}
SpriteSource::Svg {
source,
is_path_only,
stroke_width_x10,
..
} => {
self.source = SpriteSource::Svg {
source,
is_path_only,
tint: color,
stroke_width_x10,
};
}
_ => {}
}
self
}
#[inline(always)]
pub const fn stroke_width(mut self, width_x10: u8) -> Self {
if let SpriteSource::Svg {
source,
is_path_only,
tint,
..
} = self.source
{
self.source = SpriteSource::Svg {
source,
is_path_only,
tint,
stroke_width_x10: width_x10,
};
}
self
}
}