Skip to main content

lgui_core/core/scene/
static_layer.rs

1use std::hash::{Hash, Hasher};
2
3use super::{normalized_f32_bits, ImageFit};
4
5#[derive(Clone, Debug, PartialEq, Eq, Hash)]
6pub enum StaticLayerSource {
7    BakedAsset {
8        key: &'static str,
9        fit: ImageFit,
10    },
11    RuntimeGenerated,
12    Hybrid {
13        baked_base: Option<&'static str>,
14        fit: ImageFit,
15    },
16}
17
18impl StaticLayerSource {
19    pub fn baked(key: &'static str, fit: ImageFit) -> Self {
20        Self::BakedAsset { key, fit }
21    }
22
23    pub fn runtime() -> Self {
24        Self::RuntimeGenerated
25    }
26
27    pub fn hybrid(baked_base: Option<&'static str>, fit: ImageFit) -> Self {
28        Self::Hybrid { baked_base, fit }
29    }
30}
31
32#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
33pub enum RasterCachePolicy {
34    Disabled,
35    Memory {
36        retention: crate::memory::RetentionClass,
37        priority: crate::memory::CachePriority,
38    },
39}
40
41impl RasterCachePolicy {
42    pub const fn memory(
43        retention: crate::memory::RetentionClass,
44        priority: crate::memory::CachePriority,
45    ) -> Self {
46        Self::Memory {
47            retention,
48            priority,
49        }
50    }
51
52    pub const fn is_enabled(self) -> bool {
53        matches!(self, Self::Memory { .. })
54    }
55
56    pub const fn retention(self) -> Option<crate::memory::RetentionClass> {
57        match self {
58            Self::Disabled => None,
59            Self::Memory { retention, .. } => Some(retention),
60        }
61    }
62
63    pub const fn priority(self) -> Option<crate::memory::CachePriority> {
64        match self {
65            Self::Disabled => None,
66            Self::Memory { priority, .. } => Some(priority),
67        }
68    }
69}
70
71#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
72pub enum StaticLayerBackground {
73    Opaque,
74    Transparent,
75}
76
77#[derive(Clone, Debug, PartialEq)]
78pub struct StaticLayerSpec {
79    pub source: StaticLayerSource,
80    pub cache_policy: RasterCachePolicy,
81    pub revision: &'static str,
82    pub opacity: u8,
83    pub offset_x: f32,
84    pub offset_y: f32,
85    pub memory_budget_bytes: usize,
86    pub background: StaticLayerBackground,
87}
88
89impl StaticLayerSpec {
90    pub fn new(source: StaticLayerSource) -> Self {
91        Self {
92            source,
93            // Bitmap caching is opt-in. Static layers may contain dynamic children, so callers
94            // must explicitly choose memory caching only for stable reusable content.
95            cache_policy: RasterCachePolicy::Disabled,
96            revision: "v1",
97            opacity: 255,
98            offset_x: 0.0,
99            offset_y: 0.0,
100            memory_budget_bytes: usize::MAX,
101            background: StaticLayerBackground::Opaque,
102        }
103    }
104
105    pub fn cache_policy(mut self, policy: RasterCachePolicy) -> Self {
106        self.cache_policy = policy;
107        self
108    }
109
110    pub fn revision(mut self, revision: &'static str) -> Self {
111        self.revision = revision;
112        self
113    }
114
115    pub fn opacity(mut self, opacity: f32) -> Self {
116        self.opacity = (opacity.clamp(0.0, 1.0) * 255.0).round() as u8;
117        self
118    }
119
120    pub fn opacity_f32(&self) -> f32 {
121        self.opacity as f32 / 255.0
122    }
123
124    pub fn paint_offset(mut self, x: f32, y: f32) -> Self {
125        self.offset_x = x;
126        self.offset_y = y;
127        self
128    }
129
130    pub fn memory_budget_bytes(mut self, budget: usize) -> Self {
131        self.memory_budget_bytes = budget;
132        self
133    }
134
135    pub fn background(mut self, background: StaticLayerBackground) -> Self {
136        self.background = background;
137        self
138    }
139
140    pub fn transparent_background(mut self) -> Self {
141        self.background = StaticLayerBackground::Transparent;
142        self
143    }
144
145    pub fn cache_signature(&self) -> StaticLayerCacheSignature {
146        StaticLayerCacheSignature {
147            source: self.source.clone(),
148            cache_policy: self.cache_policy,
149            revision: self.revision,
150            background: self.background,
151        }
152    }
153}
154
155impl Eq for StaticLayerSpec {}
156
157impl Hash for StaticLayerSpec {
158    fn hash<H: Hasher>(&self, state: &mut H) {
159        self.source.hash(state);
160        self.cache_policy.hash(state);
161        self.revision.hash(state);
162        self.opacity.hash(state);
163        normalized_f32_bits(self.offset_x).hash(state);
164        normalized_f32_bits(self.offset_y).hash(state);
165        self.memory_budget_bytes.hash(state);
166        self.background.hash(state);
167    }
168}
169
170#[derive(Clone, Debug, PartialEq, Eq, Hash)]
171pub struct StaticLayerCacheSignature {
172    source: StaticLayerSource,
173    cache_policy: RasterCachePolicy,
174    revision: &'static str,
175    background: StaticLayerBackground,
176}