-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement.rs
More file actions
204 lines (191 loc) · 6.38 KB
/
Copy pathelement.rs
File metadata and controls
204 lines (191 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use bevy::prelude::*;
use crate::{
core::container::{Container, WithObservers},
widgets::{
checkbox::EasyCheckboxBuilder,
containers::{
button::EasyButtonContainer,
horizontal_layout::EasyHorizontalLayoutContainer,
radio_group::EasyRadioGroupContainer, rich_text::EasyRichTextContainer,
slider::EasySliderContainer,
vertical_layout::EasyVerticalLayoutContainer,
},
image::EasyImageBuilder,
label::EasyLabelBuilder,
radio::EasyRadioButtonBuilder,
slider_thumb::EasySliderThumbBuilder,
span::EasySpanBuilder,
text::EasyTextBuilder,
text_input::EasyTextInputBuilder,
},
};
//>--------------------- ALL ELEMENTS ---------------------
pub enum EasyElement {
// Containers (i.e. elements that can have children):
ButtonContainer(EasyButtonContainer),
RichTextContainer(EasyRichTextContainer),
VerticalContainer(EasyVerticalLayoutContainer),
HorizontalContainer(EasyHorizontalLayoutContainer),
Slider(EasySliderContainer),
RadioGroup(EasyRadioGroupContainer),
// Non-containers (i.e. leaf nodes):
Image(EasyImageBuilder),
Text(EasyTextBuilder),
Label(EasyLabelBuilder),
Span(EasySpanBuilder),
TextInput(EasyTextInputBuilder),
Checkbox(EasyCheckboxBuilder),
SliderThumb(EasySliderThumbBuilder),
RadioButton(EasyRadioButtonBuilder),
}
//>--------------------- IMPLEMENTATIONS ---------------------
//> Impl for containers
impl From<EasyButtonContainer> for EasyElement {
fn from(b: EasyButtonContainer) -> Self {
EasyElement::ButtonContainer(b)
}
}
impl From<EasyRichTextContainer> for EasyElement {
fn from(t: EasyRichTextContainer) -> Self {
EasyElement::RichTextContainer(t)
}
}
impl From<EasyVerticalLayoutContainer> for EasyElement {
fn from(c: EasyVerticalLayoutContainer) -> Self {
EasyElement::VerticalContainer(c)
}
}
impl From<EasyHorizontalLayoutContainer> for EasyElement {
fn from(c: EasyHorizontalLayoutContainer) -> Self {
EasyElement::HorizontalContainer(c)
}
}
impl From<EasySliderContainer> for EasyElement {
fn from(s: EasySliderContainer) -> Self {
EasyElement::Slider(s)
}
}
impl From<EasyRadioGroupContainer> for EasyElement {
fn from(r: EasyRadioGroupContainer) -> Self {
EasyElement::RadioGroup(r)
}
}
//> Impl for non-containers
impl From<EasyImageBuilder> for EasyElement {
fn from(i: EasyImageBuilder) -> Self {
EasyElement::Image(i)
}
}
impl From<EasyTextBuilder> for EasyElement {
fn from(t: EasyTextBuilder) -> Self {
EasyElement::Text(t)
}
}
impl From<EasyLabelBuilder> for EasyElement {
fn from(l: EasyLabelBuilder) -> Self {
EasyElement::Label(l)
}
}
impl From<EasySpanBuilder> for EasyElement {
fn from(s: EasySpanBuilder) -> Self {
EasyElement::Span(s)
}
}
impl From<EasyTextInputBuilder> for EasyElement {
fn from(t: EasyTextInputBuilder) -> Self {
EasyElement::TextInput(t)
}
}
impl From<EasyCheckboxBuilder> for EasyElement {
fn from(c: EasyCheckboxBuilder) -> Self {
EasyElement::Checkbox(c)
}
}
impl From<EasySliderThumbBuilder> for EasyElement {
fn from(s: EasySliderThumbBuilder) -> Self {
EasyElement::SliderThumb(s)
}
}
impl From<EasyRadioButtonBuilder> for EasyElement {
fn from(r: EasyRadioButtonBuilder) -> Self {
EasyElement::RadioButton(r)
}
}
impl EasyElement {
/// Spawns this EasyElement in the world, as a child of the given parent.
/// This is done by matching on the type of the element (container vs non-container)
/// and calling the appropriate helper function to spawn it.
pub fn spawn_in(self, p: &mut ChildSpawnerCommands) {
match self {
// Containers
EasyElement::ButtonContainer(c) => spawn_container(c, p),
EasyElement::RichTextContainer(c) => spawn_rich_text(c, p),
EasyElement::VerticalContainer(c) => spawn_container(c, p),
EasyElement::HorizontalContainer(c) => spawn_container(c, p),
EasyElement::Slider(s) => spawn_slider(s, p),
EasyElement::RadioGroup(r) => spawn_container(r, p),
// Non-containers
EasyElement::Image(i) => spawn(i, p),
EasyElement::Text(t) => spawn(t, p),
EasyElement::Label(l) => spawn(l, p),
EasyElement::Span(s) => spawn(s, p),
EasyElement::TextInput(t) => spawn(t, p),
EasyElement::Checkbox(c) => spawn(c, p),
EasyElement::SliderThumb(s) => spawn(s, p),
EasyElement::RadioButton(r) => spawn(r, p),
}
}
}
/// **Helper function** to spawn an EasyElement that is a non-container (i.e. can't have children).
/// It spawns the element itself and then spawns its observers.
fn spawn(mut e: impl WithObservers, p: &mut ChildSpawnerCommands) {
let entity = p.spawn(e.take_bundle()).id();
for observer in e.take_observers() {
p.commands().spawn(observer.with_entity(entity));
}
}
/// **Helper function** to spawn an EasyElement that is a container (i.e. can have children).
/// It spawns the container itself, then recursively spawns its children, and finally spawns its observers.
fn spawn_container(
mut c: impl Container<EasyElement>,
p: &mut ChildSpawnerCommands,
) {
let entity = p.spawn(c.take_bundle()).id();
let kids = c.take_children();
p.commands().entity(entity).with_children(|sub| {
for child in kids {
child.spawn_in(sub);
}
});
for observer in c.take_observers() {
p.commands().spawn(observer.with_entity(entity));
}
}
/// **Helper function** to spawn an EasyRichTextContainer, which is a special case because it has a different bundle and children type
/// (EasySpanBuilder only instead of generic EasyElement).
fn spawn_rich_text(mut t: EasyRichTextContainer, p: &mut ChildSpawnerCommands) {
let entity = p.spawn(t.take_bundle()).id();
let kids = t.take_children();
for observer in t.take_observers() {
p.commands().spawn(observer.with_entity(entity));
}
p.commands().entity(entity).with_children(|sub| {
for child in kids {
spawn(child, sub);
}
});
}
/// **Helper function** to spawn an EasySliderContainer, which is a special case because it has a different bundle and children type
/// (EasySliderThumbBuilder only instead of generic EasyElement).
fn spawn_slider(mut s: EasySliderContainer, p: &mut ChildSpawnerCommands) {
let entity = p.spawn(s.take_bundle()).id();
let kids = s.take_children();
for observer in s.take_observers() {
p.commands().spawn(observer.with_entity(entity));
}
p.commands().entity(entity).with_children(|sub| {
for child in kids {
spawn(child, sub);
}
});
}