-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic_builder_inject.rs
More file actions
60 lines (52 loc) · 1.69 KB
/
basic_builder_inject.rs
File metadata and controls
60 lines (52 loc) · 1.69 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
//! A simple increasing timer injecting the entity builder into an existing entity, showcasing a
//! less invasive way to start using jonmo signals in existing Bevy apps.
mod utils;
use utils::*;
use bevy::prelude::*;
use jonmo::prelude::*;
fn main() {
let mut app = App::new();
app.add_plugins(examples_plugin)
.add_systems(Startup, (ui, camera))
.add_systems(Update, incr_value)
.insert_resource(ValueTicker(Timer::from_seconds(1., TimerMode::Repeating)))
.run();
}
#[derive(Resource, Deref, DerefMut)]
struct ValueTicker(Timer);
#[derive(Component, Clone, Default, PartialEq, Deref)]
struct Value(i32);
fn ui(world: &mut World) {
let text = world
.spawn((Node::default(), TextFont::from_font_size(100.), Value(0)))
.id();
let mut ui_root = world.spawn(Node {
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
height: Val::Percent(100.),
width: Val::Percent(100.),
..default()
});
ui_root.add_child(text);
jonmo::Builder::new()
.component_signal(
signal::from_component_changed::<Value>(text)
.map_in(deref_copied)
.map_in_ref(ToString::to_string)
.map_in(Text)
.map_in(Some),
)
.spawn_on_entity(world, text)
.unwrap();
}
fn incr_value(mut ticker: ResMut<ValueTicker>, time: Res<Time>, mut values: Query<&mut Value>) {
if ticker.tick(time.delta()).is_finished() {
for mut value in values.iter_mut() {
value.0 = value.0.wrapping_add(1);
}
ticker.reset();
}
}
fn camera(mut commands: Commands) {
commands.spawn(Camera2d);
}