use std::any::Any;
use std::cell::RefCell;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use serge_style::Color;
#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub struct Point {
pub x: f32,
pub y: f32,
}
impl Point {
pub const fn new(x: f32, y: f32) -> Point {
Point { x, y }
}
}
#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub struct Rect {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl Rect {
pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Rect {
Rect {
x,
y,
width,
height,
}
}
pub fn right(&self) -> f32 {
self.x + self.width
}
pub fn bottom(&self) -> f32 {
self.y + self.height
}
pub fn contains(&self, p: Point) -> bool {
p.x >= self.x && p.x < self.right() && p.y >= self.y && p.y < self.bottom()
}
pub fn offset(&self, dx: f32, dy: f32) -> Rect {
Rect::new(self.x + dx, self.y + dy, self.width, self.height)
}
pub fn inset(&self, d: f32) -> Rect {
Rect::new(
self.x + d,
self.y + d,
(self.width - 2.0 * d).max(0.0),
(self.height - 2.0 * d).max(0.0),
)
}
pub fn center(&self) -> Point {
Point::new(self.x + self.width / 2.0, self.y + self.height / 2.0)
}
}
#[derive(Clone, PartialEq, Debug)]
pub enum PaintCmd {
FillRect {
rect: Rect,
color: Color,
radius: f32,
},
StrokeRect {
rect: Rect,
color: Color,
width: f32,
},
Polyline {
points: Vec<Point>,
color: Color,
width: f32,
},
FillPolygon {
points: Vec<Point>,
color: Color,
},
Text {
origin: Point,
text: String,
size: f32,
color: Color,
},
}
#[derive(Debug, Default)]
pub struct PaintCtx {
width: f32,
height: f32,
commands: Vec<PaintCmd>,
}
impl PaintCtx {
pub fn new(width: f32, height: f32) -> PaintCtx {
PaintCtx {
width,
height,
commands: Vec::new(),
}
}
pub fn bounds(&self) -> Rect {
Rect::new(0.0, 0.0, self.width, self.height)
}
pub fn fill_rect(&mut self, rect: Rect, color: Color) {
self.commands.push(PaintCmd::FillRect {
rect,
color,
radius: 0.0,
});
}
pub fn fill_rounded_rect(&mut self, rect: Rect, radius: f32, color: Color) {
self.commands.push(PaintCmd::FillRect {
rect,
color,
radius,
});
}
pub fn stroke_rect(&mut self, rect: Rect, width: f32, color: Color) {
self.commands
.push(PaintCmd::StrokeRect { rect, color, width });
}
pub fn line(&mut self, a: Point, b: Point, width: f32, color: Color) {
self.polyline(&[a, b], width, color);
}
pub fn polyline(&mut self, points: &[Point], width: f32, color: Color) {
if points.len() >= 2 {
self.commands.push(PaintCmd::Polyline {
points: points.to_vec(),
color,
width,
});
}
}
pub fn fill_polygon(&mut self, points: &[Point], color: Color) {
if points.len() >= 3 {
self.commands.push(PaintCmd::FillPolygon {
points: points.to_vec(),
color,
});
}
}
pub fn text(&mut self, origin: Point, text: impl Into<String>, size: f32, color: Color) {
self.commands.push(PaintCmd::Text {
origin,
text: text.into(),
size,
color,
});
}
pub fn commands(&self) -> &[PaintCmd] {
&self.commands
}
pub fn into_commands(self) -> Vec<PaintCmd> {
self.commands
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Glyph {
pub id: u32,
pub x: f32,
pub y: f32,
}
#[derive(Clone)]
pub struct FontHandle(pub Arc<dyn Any + Send + Sync>);
impl std::fmt::Debug for FontHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("FontHandle(..)")
}
}
impl PartialEq for FontHandle {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}
#[derive(Clone, PartialEq, Debug)]
pub enum ScenePrimitive {
ExternalTexture {
key: crate::viewport::TextureKey,
rect: Rect,
},
Quad {
rect: Rect,
color: Color,
radius: f32,
},
Border {
rect: Rect,
color: Color,
width: f32,
radius: f32,
},
Line {
points: Vec<Point>,
color: Color,
width: f32,
},
Polygon {
points: Vec<Point>,
color: Color,
},
Glyphs {
origin: Point,
font: FontHandle,
size: f32,
color: Color,
glyphs: Vec<Glyph>,
},
PushClip(Rect),
PopClip,
}
#[derive(Clone, Default)]
pub struct FrameSource {
local: Arc<AtomicU64>,
external: Option<Arc<dyn Fn() -> u64 + Send + Sync>>,
}
impl std::fmt::Debug for FrameSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FrameSource")
.field("version", &self.version())
.finish()
}
}
impl FrameSource {
pub fn new() -> FrameSource {
FrameSource::default()
}
pub fn with_revision(read: impl Fn() -> u64 + Send + Sync + 'static) -> Self {
Self {
external: Some(Arc::new(read)),
..Self::default()
}
}
pub fn bump(&self) {
self.local.fetch_add(1, Ordering::Release);
}
pub fn version(&self) -> u64 {
self.local
.load(Ordering::Acquire)
.wrapping_add(self.external.as_ref().map_or(0, |read| read()))
}
pub fn track(&self) {
TRACKED.with(|t| {
if let Some(list) = t.borrow_mut().as_mut() {
list.push((self.clone(), self.version()));
}
});
}
}
thread_local! {
static TRACKED: RefCell<Option<Vec<(FrameSource, u64)>>> = const { RefCell::new(None) };
}
pub fn track_frame_sources<R>(f: impl FnOnce() -> R) -> (R, Vec<(FrameSource, u64)>) {
let saved = TRACKED.with(|t| t.borrow_mut().replace(Vec::new()));
let r = f();
let list = TRACKED.with(|t| std::mem::replace(&mut *t.borrow_mut(), saved));
(r, list.unwrap_or_default())
}
pub fn sources_changed(sources: &[(FrameSource, u64)]) -> bool {
sources.iter().any(|(s, seen)| s.version() != *seen)
}