use std::rc::Rc;
use std::sync::atomic::{AtomicU32, Ordering};
use crate::id::ElementId;
use crate::paint::{PaintCommand, PaintList};
use crate::render_object::{BoxConstraints, LayoutContext, RenderObject};
use crate::{Color, Point, Rect, Size};
static NEXT_IMAGE_ID: AtomicU32 = AtomicU32::new(1);
pub fn next_image_id() -> u32 {
NEXT_IMAGE_ID.fetch_add(1, Ordering::Relaxed)
}
#[derive(Clone)]
pub struct ImageData {
pub rgba: Vec<u8>,
pub width: u32,
pub height: u32,
}
impl ImageData {
pub fn solid(width: u32, height: u32, color: Color) -> Self {
let mut rgba = Vec::with_capacity((width * height * 4) as usize);
for _ in 0..width * height {
rgba.extend_from_slice(&[
(color.r * 255.0) as u8,
(color.g * 255.0) as u8,
(color.b * 255.0) as u8,
(color.a * 255.0) as u8,
]);
}
Self { rgba, width, height }
}
pub fn pixel_count(&self) -> u32 {
self.width * self.height
}
}
impl std::fmt::Debug for ImageData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ImageData")
.field("width", &self.width)
.field("height", &self.height)
.field("bytes", &self.rgba.len())
.finish()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BoxFit {
#[default]
Fill,
Contain,
}
pub struct ImageRenderObject {
pub image: Rc<ImageData>,
pub texture_id: u32,
pub width: Option<f32>,
pub height: Option<f32>,
pub fit: BoxFit,
pub element_id: Option<ElementId>,
pub cached_size: Size,
pub dst_rect: Rect,
}
impl ImageRenderObject {
pub fn new(image: Rc<ImageData>, texture_id: u32, width: Option<f32>, height: Option<f32>, fit: BoxFit) -> Self {
Self {
image,
texture_id,
width,
height,
fit,
element_id: None,
cached_size: Size::ZERO,
dst_rect: Rect::new(0.0, 0.0, 0.0, 0.0),
}
}
}
impl RenderObject for ImageRenderObject {
fn layout(&mut self, constraints: BoxConstraints, _ctx: &mut LayoutContext) -> Size {
let natural_w = self.image.width as f32;
let natural_h = self.image.height as f32;
let want = Size::new(self.width.unwrap_or(natural_w), self.height.unwrap_or(natural_h));
self.cached_size = constraints.constrain(want);
self.dst_rect = match self.fit {
BoxFit::Fill => Rect::new(0.0, 0.0, self.cached_size.width, self.cached_size.height),
BoxFit::Contain => {
if natural_w <= 0.0
|| natural_h <= 0.0
|| self.cached_size.width <= 0.0
|| self.cached_size.height <= 0.0
{
Rect::new(0.0, 0.0, 0.0, 0.0)
} else {
let scale = (self.cached_size.width / natural_w).min(self.cached_size.height / natural_h);
let dw = natural_w * scale;
let dh = natural_h * scale;
Rect::new((self.cached_size.width - dw) / 2.0, (self.cached_size.height - dh) / 2.0, dw, dh)
}
}
};
self.cached_size
}
fn content_size(&self) -> Size {
self.cached_size
}
fn paint(&self, offset: Point, painter: &mut PaintList) {
let d = self.dst_rect;
if d.width <= 0.0 || d.height <= 0.0 {
return;
}
painter.push_texture(self.texture_id, Rc::clone(&self.image));
painter.push(PaintCommand::TexturedRect {
rect: Rect::new(offset.x + d.x, offset.y + d.y, d.width, d.height),
color: Color::WHITE,
uv: [0.0, 0.0, 1.0, 1.0],
texture_id: self.texture_id,
mode: crate::paint::TextureMode::Color,
});
}
fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
let rect = Rect::new(
offset.x + self.dst_rect.x,
offset.y + self.dst_rect.y,
self.dst_rect.width,
self.dst_rect.height,
);
if rect.contains(position) {
self.element_id
} else {
None
}
}
fn set_element_id(&mut self, id: ElementId) {
self.element_id = Some(id);
}
fn element_id(&self) -> Option<ElementId> {
self.element_id
}
fn children_count(&self) -> usize {
0
}
}