use super::{state::PaintState, CachedElement, Paint};
use crate::{
context::Id,
shapes::Mesh,
text::{TextSystem, DEFAULT_TAB},
Rect, Vec2,
};
use std::sync::Arc;
pub(super) enum Reuse {
Hidden,
Translated(Vec2),
Unchanged,
Rebuild,
}
impl PaintState {
pub(super) fn reuse(
&self,
id: Id,
paint: &[Paint],
clip: Rect,
paint_scale: f32,
scale: f32,
text: &mut TextSystem,
) -> Reuse {
let Some(cached) = self.cache.get(&id).filter(|c| c.scale == paint_scale) else {
return Reuse::Rebuild;
};
let moved_out = |delta: Vec2, margin: Vec2| {
cached.bounds.is_some_and(|bounds| {
let bounds = bounds.translate(delta);
Rect::from_min_max(bounds.min - margin, bounds.max + margin)
.intersect(clip)
.is_empty()
})
};
match translation(&cached.paint, paint, scale) {
Some(delta) if moved_out(delta, Vec2::ZERO) => Reuse::Hidden,
Some(delta)
if delta != Vec2::ZERO && !preserves_raster(text, &cached.paint, delta, scale) =>
{
Reuse::Rebuild
}
Some(delta) => Reuse::Translated(delta),
None if translation(&cached.paint, paint, 0.0)
.is_some_and(|delta| moved_out(delta, Vec2::splat(1.0 / scale))) =>
{
Reuse::Hidden
}
None if cached.paint == paint => Reuse::Unchanged,
None => Reuse::Rebuild,
}
}
pub(super) fn touch(&mut self, id: Id, frame: u64) {
if let Some(cached) = self.cache.get_mut(&id) {
cached.last_frame = frame;
}
}
pub(super) fn translate(&mut self, id: Id, delta: Vec2, paint: Vec<Paint>, frame: u64) {
let cached = self.cache.get_mut(&id).expect("translated paint is cached");
cached.last_frame = frame;
if delta == Vec2::ZERO {
return;
}
for vertex in &mut Arc::make_mut(&mut cached.mesh).vertices {
vertex.position[0] += delta.x;
vertex.position[1] += delta.y;
}
cached.bounds = cached.bounds.map(|bounds| bounds.translate(delta));
cached.paint = paint;
self.modified.insert(id);
}
pub(super) fn store(&mut self, id: Id, paint: Vec<Paint>, scale: f32, mesh: Mesh, frame: u64) {
let bounds = mesh_bounds(&mesh);
self.cache.insert(
id,
CachedElement {
paint,
scale,
mesh: Arc::new(mesh),
bounds,
last_frame: frame,
},
);
self.modified.insert(id);
}
}
fn mesh_bounds(mesh: &Mesh) -> Option<Rect> {
let first = mesh.vertices.first()?;
let mut min = Vec2::from_array(first.position);
let mut max = min;
for vertex in &mesh.vertices[1..] {
let p = Vec2::from_array(vertex.position);
min = min.min(p);
max = max.max(p);
}
Some(Rect::from_min_max(min, max))
}
fn translation(old: &[Paint], new: &[Paint], scale: f32) -> Option<Vec2> {
if old.len() != new.len() {
return None;
}
let delta = new.first()?.origin() - old.first()?.origin();
if !delta.is_finite() {
return None;
}
for (a, b) in old.iter().zip(new) {
if let (
Paint::Text {
text: at,
position: ap,
size: az,
weight: aweight,
wrap_width: aw,
color: ac,
},
Paint::Text {
text: bt,
position: bp,
size: bz,
weight: bweight,
wrap_width: bw,
color: bc,
},
) = (a, b)
{
let physical = delta * scale;
if scale > 0.0 && (physical.x != physical.x.round() || physical.y != physical.y.round())
{
return None;
}
if at != bt
|| *ap + delta != *bp
|| az != bz
|| aweight != bweight
|| aw != bw
|| ac != bc
{
return None;
}
} else {
let mut translated = a.clone();
translated.translate(delta);
if translated != *b {
return None;
}
}
}
Some(delta)
}
fn preserves_raster(text: &mut TextSystem, paint: &[Paint], delta: Vec2, scale: f32) -> bool {
paint.iter().all(|primitive| match primitive {
Paint::Text {
text: string,
position,
size,
weight,
wrap_width,
..
} => text.translation_preserves_raster(
string,
*position,
*size,
*weight,
*wrap_width,
DEFAULT_TAB,
delta,
scale,
),
Paint::Paragraph {
text: string,
position,
size,
font,
wrap_width,
tab,
..
} => text.translation_preserves_raster(
string,
*position,
*size,
*font,
*wrap_width,
*tab,
delta,
scale,
),
Paint::Rich {
text: string,
runs,
position,
size,
font,
wrap_width,
tab,
..
} => text.rich_translation_preserves_raster(
string,
runs,
*position,
*size,
*font,
*wrap_width,
*tab,
delta,
scale,
),
_ => true,
})
}