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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
pub use *;
// TODO: everything below will be removed once scene macros are implemented
/*
use bevy::prelude::*;
use bevy_cobweb::prelude::*;
//use crate::load_embedded_scene_file;
use crate::prelude::*;
use crate::sickle::*;
//-------------------------------------------------------------------------------------------------------------------
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
enum Indicator
{
#[default]
None,
Prime,
Reversed,
}
//-------------------------------------------------------------------------------------------------------------------
/// Coordinates toggling of radio buttons.
///
/// See [`RadioButtonBuilder::build`].
#[derive(Component, Default, Debug)]
pub struct RadioButtonManager
{
selected: Option<Entity>,
}
impl RadioButtonManager
{
/// Inserts a new manager onto the builder entity.
///
/// Returns the entity where the manager is stored.
pub fn insert(node: &mut UiBuilder<Entity>) -> Entity
{
node.insert(Self::default());
node.id()
}
/// Deselects the previous entity and saves the next selected.
///
/// Does not *select* the next entity, which is assumed to already be selected.
pub fn swap_selected(&mut self, c: &mut Commands, next: Entity)
{
if let Some(prev) = self.selected {
c.react().entity_event(prev, Deselect);
}
self.selected = Some(next);
}
}
//-------------------------------------------------------------------------------------------------------------------
#[derive(TypeName)]
pub struct RadioButton;
#[derive(TypeName)]
pub struct RadioButtonIndicator;
#[derive(TypeName)]
pub struct RadioButtonIndicatorDot;
#[derive(TypeName)]
pub struct RadioButtonContent;
//-------------------------------------------------------------------------------------------------------------------
enum RadioButtonType
{
Default
{
text: Option<String>,
},
DefaultInBox
{
text: Option<String>,
},
Custom(SceneRef),
CustomWithText
{
loadable: SceneRef,
text: Option<String>,
},
}
impl RadioButtonType
{
fn get_scene(&self) -> SceneRef
{
match self {
Self::Default { .. } => SceneRef::new("builtin.widgets.radio_button", "radio_button_default"),
Self::DefaultInBox { .. } => {
SceneRef::new("builtin.widgets.radio_button", "radio_button_default_in_vertical_box")
}
Self::Custom(loadable) => loadable.clone(),
Self::CustomWithText { loadable, .. } => loadable.clone(),
}
}
fn take_text(self) -> Option<String>
{
match self {
Self::Default { text } | Self::DefaultInBox { text } | Self::CustomWithText { text, .. } => text,
Self::Custom(..) => None,
}
}
}
//-------------------------------------------------------------------------------------------------------------------
/// Builds a [`RadioButton`] widget into an entity.
pub struct RadioButtonBuilder
{
button_type: RadioButtonType,
indicator: Indicator,
localized: bool,
}
impl RadioButtonBuilder
{
pub fn default() -> Self
{
Self {
button_type: RadioButtonType::Default { text: None },
indicator: Indicator::Prime, // Included by default
localized: false,
}
}
pub fn default_in_box() -> Self
{
Self {
button_type: RadioButtonType::DefaultInBox { text: None },
indicator: Indicator::Prime, // Included by default
localized: false,
}
}
/// Builds from a custom scene.
///
/// Does NOT include an indicator. Use [`Self::with_indicator`].
pub fn custom(scene: SceneRef) -> Self
{
Self {
button_type: RadioButtonType::Custom(scene),
indicator: Indicator::None,
localized: false,
}
}
/// Builds from a custom scene with text.
///
/// Does NOT include an indicator. Use [`Self::with_indicator`].
pub fn custom_with_text(scene: SceneRef, text: impl Into<String>) -> Self
{
Self {
button_type: RadioButtonType::CustomWithText { loadable: scene, text: Some(text.into()) },
indicator: Indicator::None,
localized: false,
}
}
pub fn new(text: impl Into<String>) -> Self
{
Self {
button_type: RadioButtonType::Default { text: Some(text.into()) },
indicator: Indicator::Prime, // Included by default
localized: false,
}
}
pub fn new_in_box(text: impl Into<String>) -> Self
{
Self {
button_type: RadioButtonType::DefaultInBox { text: Some(text.into()) },
indicator: Indicator::Prime, // Included by default
localized: false,
}
}
/// Include an indicator dot in the button to the left/top of the content.
///
/// Use [`Self::with_indicator_rev`] if you want the button to the right/bottom of the content.
pub fn with_indicator(mut self) -> Self
{
self.indicator = Indicator::Prime;
self
}
/// Include an indicator dot in the button to the right/bottom of the content.
///
/// Use [`Self::with_indicator`] if you want the button to the left/top of the content.
pub fn with_indicator_rev(mut self) -> Self
{
self.indicator = Indicator::Reversed;
self
}
/// Cause the text to be localized.
///
/// Mainly useful for default-themed radio buttons, since custom buttons can include [`LocalizedText`]
/// components directly.
pub fn localized(mut self) -> Self
{
self.localized = true;
self
}
/// Builds the button as a child of the builder entity.
///
/// The `manager_entity` should have a [`RadioButtonManager`] component.
///
/// If you want to add children to the content entity with [`Animated`] or [`Responsive`], then
/// use [`Self::build_with_themed_content`] instead.
pub fn build<'a>(self, manager_entity: Entity, node: &'a mut UiBuilder<Entity>) -> UiBuilder<'a, Entity>
{
self.build_with_themed_content(manager_entity, node, |_| {})
}
/// Builds the button as a child of the builder entity, with custom themed content.
///
/// The `manager_entity` should have a [`RadioButtonManager`] component.
///
/// Load your content sub-entities with `.load_with_subtheme::<RadioButton, YourSubtheme>()`.
/// Otherwise your sub-entities won't respond properly to interactions on the base button.
pub fn build_with_themed_content<'a>(
self,
manager_entity: Entity,
node: &'a mut UiBuilder<Entity>,
content_builder: impl FnOnce(&mut UiBuilder<Entity>),
) -> UiBuilder<'a, Entity>
{
let scene = self.button_type.get_scene();
let mut base_entity = Entity::PLACEHOLDER;
node.load(scene + "base", |base, path| {
base_entity = base.id();
// Setup behavior.
base
// Select this button.
// TODO: this callback could be moved to an EntityWorldReactor, with the manager entity as entity
// data.
.on_pressed(move |mut c: Commands, states: PseudoStateParam| {
states.try_select(base_entity, &mut c);
})
// Save the newly-selected button and deselect the previously selected.
.on_select(move |mut c: Commands, mut managers: Query<&mut RadioButtonManager>| {
let Ok(mut manager) = managers.get_mut(manager_entity) else { return };
manager.swap_selected(&mut c, base_entity);
});
// Add a dot if requested. This dot will be before the content (to the left/top).
if self.indicator == Indicator::Prime {
Self::add_indicator(base, &path);
}
// Add the content.
base.load(&path + "content", |content, _| {
// Localize if necessary.
if self.localized {
content.insert(LocalizedText::default());
}
// Add text if necessary.
if let Some(text) = self.button_type.take_text() {
// Note: The text needs to be updated on load otherwise it may be overwritten.
content.update_on((), |id| {
move |mut e: TextEditor| {
e.write(id, |t| write!(t, "{}", text.as_str()));
}
});
}
// Build contents.
(content_builder)(content);
});
// Add a dot if requested. This dot will be after the content (to the right/bottom).
if self.indicator == Indicator::Reversed {
Self::add_indicator(base, &path);
}
});
// Return UiBuilder for root of button where interactions will be detected.
node.commands().ui_builder(base_entity)
}
fn add_indicator(node: &mut UiBuilder<Entity>, path: &SceneRef)
{
node.load(path + "indicator", |outline, path| {
outline.load(path + "indicator_dot", |_, _| {});
});
}
}
//-------------------------------------------------------------------------------------------------------------------
*/