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 std::{any::Any, fmt::Debug};
6
7/// Storage Rust Constructor resource for page-specific data and state management.
8///
9/// 用于指定页面的数据和状态管理的Rust Constructor存储资源。
10///
11/// This resource provides metadata for page transitions and update cycles.
12///
13/// 该资源为页面转换和更新周期提供元数据。
14#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
15pub struct PageData {
16    /// Forces the page to refresh every frame regardless of changes.
17    ///
18    /// 强制页面刷新每一帧,无论更改。
19    pub forced_update: bool,
20
21    /// Indicates if resources needed for the initial page transition have been loaded.
22    ///
23    /// 指示是否已加载初始化页面转换所需的资源。
24    pub change_page_updated: bool,
25
26    /// Indicates if resources needed for entering the page have been loaded.
27    ///
28    /// 指示是否已加载进入该页所需的资源。
29    pub enter_page_updated: bool,
30
31    /// Key-value pairs for categorization and metadata storage.
32    ///
33    /// 用于分类和元数据存储的键值对。
34    pub tags: Vec<[String; 2]>,
35}
36
37impl RustConstructorResource for PageData {
38    fn as_any(&self) -> &dyn Any {
39        self
40    }
41
42    fn as_any_mut(&mut self) -> &mut dyn Any {
43        self
44    }
45
46    fn display_display_info(&self) -> Option<DisplayInfo> {
47        None
48    }
49
50    fn modify_display_info(&mut self, _display_info: DisplayInfo) {}
51
52    fn display_tags(&self) -> Vec<[String; 2]> {
53        self.tags.clone()
54    }
55
56    fn modify_tags(&mut self, tags: &[[String; 2]], replace: bool) {
57        if replace {
58            self.tags = tags.to_owned();
59        } else {
60            for tag in tags {
61                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
62                    self.tags.remove(index);
63                };
64            }
65            self.tags.extend(tags.iter().cloned());
66        };
67    }
68}
69
70impl Default for PageData {
71    fn default() -> Self {
72        PageData {
73            forced_update: true,
74            change_page_updated: false,
75            enter_page_updated: false,
76            tags: Vec::new(),
77        }
78    }
79}
80
81impl PageData {
82    #[inline]
83    pub fn forced_update(mut self, forced_update: bool) -> Self {
84        self.forced_update = forced_update;
85        self
86    }
87
88    #[inline]
89    pub fn tags(mut self, tags: &[[String; 2]], replace: bool) -> Self {
90        if replace {
91            self.tags = tags.to_owned();
92        } else {
93            for tag in tags {
94                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
95                    self.tags.remove(index);
96                };
97            }
98            self.tags.extend(tags.iter().cloned());
99        };
100        self
101    }
102}
103
104/// Generic variable resource for storing any type of data with metadata.
105///
106/// 用于存储任意类型数据及元数据的通用变量资源。
107#[derive(Debug, Clone, PartialEq, PartialOrd)]
108pub struct Variable<T> {
109    /// The stored value of the variable.
110    ///
111    /// 变量的存储值。
112    pub value: Option<T>,
113
114    /// Key-value pairs for categorization and metadata.
115    ///
116    /// 用于分类和元数据的键值对标签。
117    pub tags: Vec<[String; 2]>,
118}
119
120impl<T: Debug + 'static> RustConstructorResource for Variable<T> {
121    fn as_any(&self) -> &dyn Any {
122        self
123    }
124
125    fn as_any_mut(&mut self) -> &mut dyn Any {
126        self
127    }
128
129    fn display_display_info(&self) -> Option<DisplayInfo> {
130        None
131    }
132
133    fn modify_display_info(&mut self, _display_info: DisplayInfo) {}
134
135    fn display_tags(&self) -> Vec<[String; 2]> {
136        self.tags.clone()
137    }
138
139    fn modify_tags(&mut self, tags: &[[String; 2]], replace: bool) {
140        if replace {
141            self.tags = tags.to_owned();
142        } else {
143            for tag in tags {
144                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
145                    self.tags.remove(index);
146                };
147            }
148            self.tags.extend(tags.iter().cloned());
149        };
150    }
151}
152
153impl<T> Default for Variable<T> {
154    fn default() -> Self {
155        Variable {
156            value: None,
157            tags: Vec::new(),
158        }
159    }
160}
161
162impl<T> Variable<T> {
163    #[inline]
164    pub fn value(mut self, value: Option<T>) -> Self {
165        self.value = value;
166        self
167    }
168
169    #[inline]
170    pub fn tags(mut self, tags: &[[String; 2]], replace: bool) -> Self {
171        if replace {
172            self.tags = tags.to_owned();
173        } else {
174            for tag in tags {
175                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
176                    self.tags.remove(index);
177                };
178            }
179            self.tags.extend(tags.iter().cloned());
180        };
181        self
182    }
183}
184
185/// Time segmentation resource for tracking and managing timing information.
186///
187/// 时间分段资源,用于跟踪和管理时间信息。
188///
189/// This resource allows for precise timing control by storing both page-specific
190/// and total application runtime, enabling coordinated animations.
191///
192/// 该资源通过存储页面特定运行时间和应用程序总运行时间实现精确的时间控制,支持协调动画。
193#[derive(Debug, Clone, PartialEq, PartialOrd)]
194pub struct SplitTime {
195    /// Timing values: [page_runtime, total_runtime] in seconds.
196    ///
197    /// 时间点:[页面运行时间, 总运行时间],单位为秒。
198    pub time: [f32; 2],
199
200    /// Key-value pairs for categorization and metadata storage.
201    ///
202    /// 用于分类和元数据存储的键值对标签。
203    pub tags: Vec<[String; 2]>,
204}
205
206impl RustConstructorResource for SplitTime {
207    fn as_any(&self) -> &dyn Any {
208        self
209    }
210
211    fn as_any_mut(&mut self) -> &mut dyn Any {
212        self
213    }
214
215    fn display_display_info(&self) -> Option<DisplayInfo> {
216        None
217    }
218
219    fn modify_display_info(&mut self, _display_info: DisplayInfo) {}
220
221    fn display_tags(&self) -> Vec<[String; 2]> {
222        self.tags.clone()
223    }
224
225    fn modify_tags(&mut self, tags: &[[String; 2]], replace: bool) {
226        if replace {
227            self.tags = tags.to_owned();
228        } else {
229            for tag in tags {
230                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
231                    self.tags.remove(index);
232                };
233            }
234            self.tags.extend(tags.iter().cloned());
235        };
236    }
237}
238
239impl Default for SplitTime {
240    fn default() -> Self {
241        Self {
242            time: [0_f32, 0_f32],
243            tags: Vec::new(),
244        }
245    }
246}
247
248impl SplitTime {
249    #[inline]
250    pub fn tags(mut self, tags: &[[String; 2]], replace: bool) -> Self {
251        if replace {
252            self.tags = tags.to_owned();
253        } else {
254            for tag in tags {
255                if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
256                    self.tags.remove(index);
257                };
258            }
259            self.tags.extend(tags.iter().cloned());
260        };
261        self
262    }
263}