use crate::{ color::Srgba, dpi::{LogicalPoint, LogicalQuad, LogicalRect, LogicalVector, NormalizedQuad, NormalizedRect}, fonts::Layout, math::Point, renderer::textures::TextureId, }; use ordered_float::OrderedFloat; use std::{ hash::{Hash, Hasher}, sync::Arc, }; #[derive(Clone)] pub struct ClippedPrimitive { pub clip_rect: LogicalRect, pub primitive: DrawPrimitive, } impl Hash for ClippedPrimitive { fn hash(&self, state: &mut H) { hash_pos(&self.clip_rect.min, state); hash_pos(&self.clip_rect.max, state); self.primitive.hash(state); } } #[derive(Clone, Hash)] pub enum DrawPrimitive { Rect(RectPrimitive), Quad(QuadPrimitive), Text(TextPrimitive), } impl DrawPrimitive { pub fn texture_id(&self) -> TextureId { match self { Self::Rect(rect_primitive) => rect_primitive .texture .map_or_else(TextureId::default, |tex| tex.texture_id), Self::Quad(quad_primitive) => quad_primitive .texture .map_or_else(TextureId::default, |tex| tex.texture_id), Self::Text(_) => TextureId::default(), } } } #[derive(Clone, Copy)] pub struct RectPrimitive { pub rect: LogicalRect, pub color: Srgba, pub texture: Option, } impl RectPrimitive { pub fn new(rect: LogicalRect, color: Srgba, texture: Option) -> Self { Self { rect, color, texture, } } pub fn translate(&mut self, direction: LogicalVector) { self.rect = self.rect.translate(direction); } } impl Hash for RectPrimitive { fn hash(&self, state: &mut H) { hash_pos(&self.rect.min, state); hash_pos(&self.rect.max, state); self.color.hash(state); self.texture.hash(state); } } #[derive(Clone, Copy)] pub struct RectTexture { pub texture_id: TextureId, pub uv: NormalizedRect, pub layer_idx: u32, } impl RectTexture { pub fn new(texture_id: TextureId, uv: NormalizedRect, layer_idx: u32) -> Self { Self { texture_id, uv, layer_idx, } } } impl Hash for RectTexture { fn hash(&self, state: &mut H) { self.texture_id.hash(state); hash_pos(&self.uv.min, state); hash_pos(&self.uv.max, state); self.layer_idx.hash(state); } } #[derive(Clone, Copy)] pub struct QuadPrimitive { pub quad: LogicalQuad, pub color: Srgba, pub texture: Option, } impl QuadPrimitive { pub fn new(quad: LogicalQuad, color: Srgba, texture: Option) -> Self { Self { quad, color, texture, } } pub fn translate(&mut self, direction: LogicalVector) { self.quad = self.quad.translate(direction); } } impl Hash for QuadPrimitive { fn hash(&self, state: &mut H) { hash_pos(&self.quad.p0, state); hash_pos(&self.quad.p1, state); hash_pos(&self.quad.p2, state); hash_pos(&self.quad.p3, state); self.color.hash(state); self.texture.hash(state); } } #[derive(Clone, Copy)] pub struct QuadTexture { pub texture_id: TextureId, pub uv: NormalizedQuad, pub layer_idx: u32, } impl QuadTexture { pub fn new(texture_id: TextureId, uv: NormalizedQuad, layer_idx: u32) -> Self { Self { texture_id, uv, layer_idx, } } } impl Hash for QuadTexture { fn hash(&self, state: &mut H) { self.texture_id.hash(state); hash_pos(&self.uv.p0, state); hash_pos(&self.uv.p1, state); hash_pos(&self.uv.p2, state); hash_pos(&self.uv.p3, state); self.layer_idx.hash(state); } } #[derive(Clone)] pub struct TextPrimitive { pub pos: LogicalPoint, pub layout: Arc, } impl TextPrimitive { pub fn new(pos: LogicalPoint, layout: Arc) -> Self { Self { pos, layout } } #[inline(always)] pub fn translate(&mut self, direction: LogicalVector) { self.pos += direction; } } impl Hash for TextPrimitive { fn hash(&self, state: &mut H) { hash_pos(&self.pos, state); self.layout.hash(state); } } fn hash_pos(pos: &Point, state: &mut H) { OrderedFloat(pos.x).hash(state); OrderedFloat(pos.y).hash(state); }