1//! The cache is a store for intermediate data produced while computing state, notably layout
2//! results. The main type here is CachedData, usually accessed via `cx.cache`.
34use crate::prelude::*;
5use skia_safe::{Matrix, Path};
6use vizia_storage::SparseSet;
78/// Stores data which can be cached between system runs.
9///
10/// When an event occurs or style data is changed systems run to determine the new state of the UI.
11/// The output of these systems can be cached so that not all of the systems need to run again.
12#[derive(Default)]
13pub struct CachedData {
14pub(crate) bounds: SparseSet<BoundingBox>,
15pub(crate) draw_bounds: SparseSet<BoundingBox>,
16pub(crate) relative_bounds: SparseSet<BoundingBox>,
17pub(crate) geo_changed: SparseSet<GeoChanged>,
18pub(crate) transform: SparseSet<Matrix>,
19pub(crate) clip_path: SparseSet<BoundingBox>,
20pub(crate) path: SparseSet<Path>,
21}
2223impl CachedData {
24pub(crate) fn add(&mut self, entity: Entity) {
25self.bounds.insert(entity, Default::default());
26self.relative_bounds.insert(entity, Default::default());
27self.geo_changed.insert(entity, GeoChanged::empty());
28self.transform.insert(entity, Matrix::new_identity());
29 }
3031pub(crate) fn remove(&mut self, entity: Entity) {
32self.bounds.remove(entity);
33self.relative_bounds.remove(entity);
34self.draw_bounds.remove(entity);
35self.geo_changed.remove(entity);
36self.transform.remove(entity);
37self.clip_path.remove(entity);
38self.path.remove(entity);
39 }
4041/// Returns the bounding box of the entity, determined by the layout system.
42pub fn get_bounds(&self, entity: Entity) -> BoundingBox {
43self.bounds.get(entity).cloned().unwrap()
44 }
4546/// Returns the x position of the entity.
47pub fn get_posx(&self, entity: Entity) -> f32 {
48self.bounds.get(entity).map_or(0.0, |b| b.x)
49 }
5051/// Returns the y position of the entity.
52pub fn get_posy(&self, entity: Entity) -> f32 {
53self.bounds.get(entity).map_or(0.0, |b| b.y)
54 }
5556/// Returns the width of the entity.
57pub fn get_width(&self, entity: Entity) -> f32 {
58self.bounds.get(entity).map_or(0.0, |b| b.w)
59 }
6061/// Returns the height of the entity.
62pub fn get_height(&self, entity: Entity) -> f32 {
63self.bounds.get(entity).map_or(0.0, |b| b.h)
64 }
6566pub fn set_bounds(&mut self, entity: Entity, bounds: BoundingBox) {
67if let Some(b) = self.bounds.get_mut(entity) {
68*b = bounds;
69 }
70 }
7172/// Sets the x position of the entity.
73pub fn set_posx(&mut self, entity: Entity, val: f32) {
74if let Some(bounds) = self.bounds.get_mut(entity) {
75 bounds.x = val;
76 }
77 }
7879/// Sets the y position of the entity.
80pub fn set_posy(&mut self, entity: Entity, val: f32) {
81if let Some(bounds) = self.bounds.get_mut(entity) {
82 bounds.y = val;
83 }
84 }
8586/// Sets the width of the entity.
87pub fn set_width(&mut self, entity: Entity, val: f32) {
88if let Some(bounds) = self.bounds.get_mut(entity) {
89 bounds.w = val;
90 }
91 }
9293/// Sets the height of the entity.
94pub fn set_height(&mut self, entity: Entity, val: f32) {
95if let Some(bounds) = self.bounds.get_mut(entity) {
96 bounds.h = val;
97 }
98 }
99}