Newer
Older
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
use bevy::prelude::*;
use kayak_ui::prelude::{widgets::*, *};
#[derive(Debug, Component, Default, Clone, PartialEq, Eq)]
pub struct MyWidget {
pub foo: u32,
}
fn my_widget_1_render(
In(entity): In<Entity>,
mut _commands: Commands,
query: Query<&MyWidget>,
) -> bool {
if let Ok(my_widget) = query.get(entity) {
dbg!(my_widget.foo);
}
true
}
impl Widget for MyWidget {}
fn startup(mut commands: Commands) {
let camera_entity = commands
.spawn((Camera2dBundle::default(), CameraUIKayak))
.id();
let mut context = KayakRootContext::new(camera_entity);
context.add_plugin(KayakWidgetsContextPlugin);
context.add_widget_system(
MyWidget::default().get_name(),
widget_update::<MyWidget, EmptyState>,
my_widget_1_render,
);
context.add_widget_data::<MyWidget, EmptyState>();
let app_entity = commands.spawn_empty().id();
let mut children = KChildren::default();
let entity = commands
.spawn((
MyWidget { foo: 0 },
kayak_ui::prelude::KStyle::default(),
MyWidget::default().get_name(),
))
.id();
children.add(entity);
commands.entity(app_entity).insert(KayakAppBundle {
children,
..KayakAppBundle::default()
});
context.add_widget(None, app_entity);
commands.spawn((context, EventDispatcher::default()));
}
// Note this example shows prop changing not state changing which is quite different.
// For state changes please see simple_state example.
fn update_resource(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut query: Query<&mut MyWidget, Without<PreviousWidget>>,
) {
if keyboard_input.just_pressed(KeyCode::Space) {
for mut my_widget in query.iter_mut() {
my_widget.foo += 1;
}
}
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins((KayakContextPlugin, KayakWidgets))
.add_systems(Startup, startup)
.add_systems(Update, update_resource)
.run()
}