forked from PPakalns/bevy_immediate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget_native.rs
More file actions
79 lines (67 loc) · 2.5 KB
/
widget_native.rs
File metadata and controls
79 lines (67 loc) · 2.5 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
use bevy::ecs::{
component::Component,
system::{Query, SystemParam},
};
use bevy::ui::{AlignItems, FlexDirection, Node, UiRect};
use bevy_immediate::{
Imm,
attach::{BevyImmediateAttachPlugin, ImmediateAttach},
ui::{CapsUi, clicked::ImmUiClicked, text::ImmUiText},
};
use crate::styles;
pub struct WidgetNativeExamplePlugin;
impl bevy::app::Plugin for WidgetNativeExamplePlugin {
fn build(&self, app: &mut bevy::app::App) {
app.add_plugins(BevyImmediateAttachPlugin::<CapsUi, NativeWidgetComp>::new());
}
}
#[derive(Component)]
pub struct NativeWidgetComp {
pub title: String,
pub counter: usize,
}
#[derive(SystemParam)]
pub struct Params<'w, 's> {
query: Query<'w, 's, &'static mut NativeWidgetComp>,
}
impl ImmediateAttach<CapsUi> for NativeWidgetComp {
type Params = Params<'static, 'static>;
fn construct(ui: &mut Imm<CapsUi>, params: &mut Params) {
let entity = ui.current_entity().unwrap();
// Value can be stored inside component
let mut value = params.query.get_mut(entity).unwrap();
ui.ch()
.on_spawn_insert(|| Node {
flex_direction: FlexDirection::Row,
align_items: AlignItems::Center,
padding: UiRect::ZERO,
..styles::node_container()
})
.add(|ui| {
let change_detector = ui.change_detector();
ui.ch()
.on_spawn_insert(styles::text_style)
// Change detection can be used to
// update bevy only when something has changed.
.on_change_text_fn(change_detector.has_changed(&value), || {
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);
}
});
}
}