forked from PPakalns/bevy_immediate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget_functional.rs
More file actions
65 lines (55 loc) · 2.16 KB
/
widget_functional.rs
File metadata and controls
65 lines (55 loc) · 2.16 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
use bevy::ui::{AlignItems, FlexDirection, Node, UiRect};
use bevy_immediate::{
Imm,
ui::{CapsUi, ImplCapsUi, clicked::ImmUiClicked, text::ImmUiText},
};
use crate::styles;
pub struct WidgetFunctionalExamplePlugin;
impl bevy::app::Plugin for WidgetFunctionalExamplePlugin {
fn build(&self, _app: &mut bevy::app::App) {
// No need to even create a plugin in this case
}
}
pub struct WidgetParams<'a> {
pub title: &'a str,
pub counter: &'a mut usize,
}
/// You can implement your functional widget as a simple function with arbitrary parameters
pub fn my_functional_widget(ui: &mut Imm<CapsUi>, value: WidgetParams) {
// This example avoids duplicated implementation by calling generic one
my_functional_widget_generic(ui, value);
}
/// If you develop a library, use generic functional widget variant so that users can use your widget
/// with `Caps` in which they could have additional capability support
///
/// In this case we require for at least CapsUi capabilities to be implemented
pub fn my_functional_widget_generic<Caps: ImplCapsUi>(ui: &mut Imm<Caps>, value: WidgetParams) {
ui.ch()
.on_spawn_insert(|| Node {
flex_direction: FlexDirection::Row,
align_items: AlignItems::Center,
padding: UiRect::ZERO,
..styles::node_container()
})
.add(|ui| {
ui.ch()
.on_spawn_insert(styles::text_style)
.text(format!("{}: {}", value.title, value.counter));
let mut button = ui.ch().on_spawn_insert(styles::button_bundle).add(|ui| {
ui.ch()
.on_spawn_insert(styles::text_style)
.on_spawn_text("-");
});
if button.clicked() {
*value.counter = value.counter.saturating_sub(1);
}
let mut button = ui.ch().on_spawn_insert(styles::button_bundle).add(|ui| {
ui.ch()
.on_spawn_insert(styles::text_style)
.on_spawn_text("+");
});
if button.clicked() {
*value.counter = value.counter.saturating_add(1);
}
});
}