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 eframe::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/// A structure used to wrap font definitions.
187///
188/// 用于包裹字体定义的结构体。
189#[derive(Clone, Default, PartialEq)]
190pub struct WrapDefinitions {
191    /// Font definitions containing glyphs and styles.
192    ///
193    /// 包含字形和样式的字体定义。
194    pub font_definitions: FontDefinitions,
195}
196
197impl Debug for WrapDefinitions {
198    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
199        f.pad("WrapDefinitions { .. }")
200    }
201}
202
203/// Font resource for text rendering.
204///
205/// 用于文本渲染的字体资源。
206#[derive(Clone, Debug, Default, PartialEq)]
207pub struct Font {
208    /// Font definitions containing glyphs and styles.
209    ///
210    /// 包含字形和样式的字体定义。
211    pub font_definitions: WrapDefinitions,
212
213    /// Path to the font file.
214    ///
215    /// 字体文件路径。
216    pub path: String,
217
218    /// Key-value pairs for categorization and metadata.
219    ///
220    /// 用于分类和元数据的键值对标签。
221    pub tags: Vec<[String; 2]>,
222}
223
224impl RustConstructorResource for Font {
225    fn as_any(&self) -> &dyn Any {
226        self
227    }
228
229    fn as_any_mut(&mut self) -> &mut dyn Any {
230        self
231    }
232
233    fn display_display_info(&self) -> Option<DisplayInfo> {
234        None
235    }
236
237    fn modify_display_info(&mut self, _display_info: DisplayInfo) {}
238
239    fn display_tags(&self) -> Vec<[String; 2]> {
240        self.tags.clone()
241    }
242
243    fn modify_tags(&mut self, tags: &[[String; 2]], replace: bool) {
244        if replace {
245            self.tags = tags.to_owned();
246        } else {
247            for tag in tags {
248                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
249                    self.tags.remove(index);
250                };
251            }
252            self.tags.extend(tags.iter().cloned());
253        };
254    }
255}
256
257impl Font {
258    #[inline]
259    pub fn path(mut self, path: &str) -> Self {
260        self.path = path.to_string();
261        self
262    }
263
264    #[inline]
265    pub fn tags(mut self, tags: &[[String; 2]], replace: bool) -> Self {
266        if replace {
267            self.tags = tags.to_owned();
268        } else {
269            for tag in tags {
270                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
271                    self.tags.remove(index);
272                };
273            }
274            self.tags.extend(tags.iter().cloned());
275        };
276        self
277    }
278}
279
280/// Time segmentation resource for tracking and managing timing information.
281///
282/// 时间分段资源,用于跟踪和管理时间信息。
283///
284/// This resource allows for precise timing control by storing both page-specific
285/// and total application runtime, enabling coordinated animations.
286///
287/// 该资源通过存储页面特定运行时间和应用程序总运行时间实现精确的时间控制,支持协调动画。
288#[derive(Debug, Clone, PartialEq, PartialOrd)]
289pub struct SplitTime {
290    /// Timing values: [page_runtime, total_runtime] in seconds.
291    ///
292    /// 时间点:[页面运行时间, 总运行时间],单位为秒。
293    pub time: [f32; 2],
294
295    /// Key-value pairs for categorization and metadata storage.
296    ///
297    /// 用于分类和元数据存储的键值对标签。
298    pub tags: Vec<[String; 2]>,
299}
300
301impl RustConstructorResource for SplitTime {
302    fn as_any(&self) -> &dyn Any {
303        self
304    }
305
306    fn as_any_mut(&mut self) -> &mut dyn Any {
307        self
308    }
309
310    fn display_display_info(&self) -> Option<DisplayInfo> {
311        None
312    }
313
314    fn modify_display_info(&mut self, _display_info: DisplayInfo) {}
315
316    fn display_tags(&self) -> Vec<[String; 2]> {
317        self.tags.clone()
318    }
319
320    fn modify_tags(&mut self, tags: &[[String; 2]], replace: bool) {
321        if replace {
322            self.tags = tags.to_owned();
323        } else {
324            for tag in tags {
325                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
326                    self.tags.remove(index);
327                };
328            }
329            self.tags.extend(tags.iter().cloned());
330        };
331    }
332}
333
334impl Default for SplitTime {
335    fn default() -> Self {
336        Self {
337            time: [0_f32, 0_f32],
338            tags: Vec::new(),
339        }
340    }
341}
342
343impl SplitTime {
344    #[inline]
345    pub fn tags(mut self, tags: &[[String; 2]], replace: bool) -> Self {
346        if replace {
347            self.tags = tags.to_owned();
348        } else {
349            for tag in tags {
350                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
351                    self.tags.remove(index);
352                };
353            }
354            self.tags.extend(tags.iter().cloned());
355        };
356        self
357    }
358}