Skip to main content

rust_constructor/
background.rs

1//! This file contains backend resources, which can store some key data and be called upon when needed.
2//!
3//! 此文件包含后端资源,后端资源可以存储一些关键数据并在有需要时调用。
4use crate::{DisplayInfo, RustConstructorResource};
5use egui::FontDefinitions;
6use std::{any::Any, fmt::Debug};
7
8/// Storage Rust Constructor resource for page-specific data and state management.
9///
10/// 用于指定页面的数据和状态管理的Rust Constructor存储资源。
11///
12/// This resource provides metadata for page transitions and update cycles.
13///
14/// 该资源为页面转换和更新周期提供元数据。
15#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
16pub struct PageData {
17    /// Forces the page to refresh every frame regardless of changes.
18    ///
19    /// 强制页面刷新每一帧,无论更改。
20    pub forced_update: bool,
21
22    /// Indicates if resources needed for the initial page transition have been loaded.
23    ///
24    /// 指示是否已加载初始化页面转换所需的资源。
25    pub change_page_updated: bool,
26
27    /// Indicates if resources needed for entering the page have been loaded.
28    ///
29    /// 指示是否已加载进入该页所需的资源。
30    pub enter_page_updated: bool,
31
32    /// Key-value pairs for categorization and metadata storage.
33    ///
34    /// 用于分类和元数据存储的键值对。
35    pub tags: Vec<[String; 2]>,
36}
37
38impl RustConstructorResource for PageData {
39    fn as_any(&self) -> &dyn Any {
40        self
41    }
42
43    fn as_any_mut(&mut self) -> &mut dyn Any {
44        self
45    }
46
47    fn display_display_info(&self) -> Option<DisplayInfo> {
48        None
49    }
50
51    fn modify_display_info(&mut self, _display_info: DisplayInfo) {}
52
53    fn display_tags(&self) -> Vec<[String; 2]> {
54        self.tags.clone()
55    }
56
57    fn modify_tags(&mut self, tags: &[[String; 2]], replace: bool) {
58        if replace {
59            self.tags = tags.to_owned();
60        } else {
61            for tag in tags {
62                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
63                    self.tags.remove(index);
64                };
65            }
66            self.tags.extend(tags.iter().cloned());
67        };
68    }
69}
70
71impl Default for PageData {
72    fn default() -> Self {
73        PageData {
74            forced_update: true,
75            change_page_updated: false,
76            enter_page_updated: false,
77            tags: Vec::new(),
78        }
79    }
80}
81
82impl PageData {
83    #[inline]
84    pub fn forced_update(mut self, forced_update: bool) -> Self {
85        self.forced_update = forced_update;
86        self
87    }
88
89    #[inline]
90    pub fn tags(mut self, tags: &[[String; 2]], replace: bool) -> Self {
91        if replace {
92            self.tags = tags.to_owned();
93        } else {
94            for tag in tags {
95                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
96                    self.tags.remove(index);
97                };
98            }
99            self.tags.extend(tags.iter().cloned());
100        };
101        self
102    }
103}
104
105/// Generic variable resource for storing any type of data with metadata.
106///
107/// 用于存储任意类型数据及元数据的通用变量资源。
108#[derive(Debug, Clone, PartialEq, PartialOrd)]
109pub struct Variable<T> {
110    /// The stored value of the variable.
111    ///
112    /// 变量的存储值。
113    pub value: Option<T>,
114
115    /// Key-value pairs for categorization and metadata.
116    ///
117    /// 用于分类和元数据的键值对标签。
118    pub tags: Vec<[String; 2]>,
119}
120
121impl<T: Debug + 'static> RustConstructorResource for Variable<T> {
122    fn as_any(&self) -> &dyn Any {
123        self
124    }
125
126    fn as_any_mut(&mut self) -> &mut dyn Any {
127        self
128    }
129
130    fn display_display_info(&self) -> Option<DisplayInfo> {
131        None
132    }
133
134    fn modify_display_info(&mut self, _display_info: DisplayInfo) {}
135
136    fn display_tags(&self) -> Vec<[String; 2]> {
137        self.tags.clone()
138    }
139
140    fn modify_tags(&mut self, tags: &[[String; 2]], replace: bool) {
141        if replace {
142            self.tags = tags.to_owned();
143        } else {
144            for tag in tags {
145                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
146                    self.tags.remove(index);
147                };
148            }
149            self.tags.extend(tags.iter().cloned());
150        };
151    }
152}
153
154impl<T> Default for Variable<T> {
155    fn default() -> Self {
156        Variable {
157            value: None,
158            tags: Vec::new(),
159        }
160    }
161}
162
163impl<T> Variable<T> {
164    #[inline]
165    pub fn value(mut self, value: Option<T>) -> Self {
166        self.value = value;
167        self
168    }
169
170    #[inline]
171    pub fn tags(mut self, tags: &[[String; 2]], replace: bool) -> Self {
172        if replace {
173            self.tags = tags.to_owned();
174        } else {
175            for tag in tags {
176                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
177                    self.tags.remove(index);
178                };
179            }
180            self.tags.extend(tags.iter().cloned());
181        };
182        self
183    }
184}
185
186/// Font resource for text rendering.
187///
188/// 用于文本渲染的字体资源。
189#[derive(Clone, Debug, Default, PartialEq)]
190pub struct Font {
191    /// Font definitions containing glyphs and styles.
192    ///
193    /// 包含字形和样式的字体定义。
194    pub font_definitions: FontDefinitions,
195
196    /// Path to the font file.
197    ///
198    /// 字体文件路径。
199    pub path: String,
200
201    /// Key-value pairs for categorization and metadata.
202    ///
203    /// 用于分类和元数据的键值对标签。
204    pub tags: Vec<[String; 2]>,
205}
206
207impl RustConstructorResource for Font {
208    fn as_any(&self) -> &dyn Any {
209        self
210    }
211
212    fn as_any_mut(&mut self) -> &mut dyn Any {
213        self
214    }
215
216    fn display_display_info(&self) -> Option<DisplayInfo> {
217        None
218    }
219
220    fn modify_display_info(&mut self, _display_info: DisplayInfo) {}
221
222    fn display_tags(&self) -> Vec<[String; 2]> {
223        self.tags.clone()
224    }
225
226    fn modify_tags(&mut self, tags: &[[String; 2]], replace: bool) {
227        if replace {
228            self.tags = tags.to_owned();
229        } else {
230            for tag in tags {
231                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
232                    self.tags.remove(index);
233                };
234            }
235            self.tags.extend(tags.iter().cloned());
236        };
237    }
238}
239
240impl Font {
241    #[inline]
242    pub fn path(mut self, path: &str) -> Self {
243        self.path = path.to_string();
244        self
245    }
246
247    #[inline]
248    pub fn tags(mut self, tags: &[[String; 2]], replace: bool) -> Self {
249        if replace {
250            self.tags = tags.to_owned();
251        } else {
252            for tag in tags {
253                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
254                    self.tags.remove(index);
255                };
256            }
257            self.tags.extend(tags.iter().cloned());
258        };
259        self
260    }
261}
262
263/// Time segmentation resource for tracking and managing timing information.
264///
265/// 时间分段资源,用于跟踪和管理时间信息。
266///
267/// This resource allows for precise timing control by storing both page-specific
268/// and total application runtime, enabling coordinated animations.
269///
270/// 该资源通过存储页面特定运行时间和应用程序总运行时间实现精确的时间控制,支持协调动画。
271#[derive(Debug, Clone, PartialEq, PartialOrd)]
272pub struct SplitTime {
273    /// Timing values: [page_runtime, total_runtime] in seconds.
274    ///
275    /// 时间点:[页面运行时间, 总运行时间],单位为秒。
276    pub time: [f32; 2],
277
278    /// Key-value pairs for categorization and metadata storage.
279    ///
280    /// 用于分类和元数据存储的键值对标签。
281    pub tags: Vec<[String; 2]>,
282}
283
284impl RustConstructorResource for SplitTime {
285    fn as_any(&self) -> &dyn Any {
286        self
287    }
288
289    fn as_any_mut(&mut self) -> &mut dyn Any {
290        self
291    }
292
293    fn display_display_info(&self) -> Option<DisplayInfo> {
294        None
295    }
296
297    fn modify_display_info(&mut self, _display_info: DisplayInfo) {}
298
299    fn display_tags(&self) -> Vec<[String; 2]> {
300        self.tags.clone()
301    }
302
303    fn modify_tags(&mut self, tags: &[[String; 2]], replace: bool) {
304        if replace {
305            self.tags = tags.to_owned();
306        } else {
307            for tag in tags {
308                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
309                    self.tags.remove(index);
310                };
311            }
312            self.tags.extend(tags.iter().cloned());
313        };
314    }
315}
316
317impl Default for SplitTime {
318    fn default() -> Self {
319        Self {
320            time: [0_f32, 0_f32],
321            tags: Vec::new(),
322        }
323    }
324}
325
326impl SplitTime {
327    #[inline]
328    pub fn tags(mut self, tags: &[[String; 2]], replace: bool) -> Self {
329        if replace {
330            self.tags = tags.to_owned();
331        } else {
332            for tag in tags {
333                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
334                    self.tags.remove(index);
335                };
336            }
337            self.tags.extend(tags.iter().cloned());
338        };
339        self
340    }
341}