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
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
use bevy::{
prelude::{
App as BevyApp, AssetServer, Bundle, Changed, Commands, Component, Entity, In, Query, Res,
ResMut, Vec2,
},
DefaultPlugins,
};
use kayak_ui::prelude::{widgets::*, *};
#[derive(Component, Default)]
struct CurrentCount(pub u32);
impl Widget for CurrentCount {}
#[derive(Bundle)]
struct CurrentCountBundle {
count: CurrentCount,
styles: KStyle,
widget_name: WidgetName,
}
impl Default for CurrentCountBundle {
fn default() -> Self {
Self {
count: CurrentCount::default(),
styles: KStyle::default(),
widget_name: CurrentCount::default().get_name(),
}
}
}
fn current_count_update(
In((widget_context, entity)): In<(WidgetContext, Entity)>,
mut commands: Commands,
query: Query<&CurrentCount, Changed<CurrentCount>>,
) -> bool {
if let Ok(current_count) = query.get(entity) {
let parent_id = Some(entity);
rsx! {
<TextWidgetBundle
text={
TextProps {
content: format!("Current Count: {}", current_count.0).into(),
size: 16.0,
line_height: Some(40.0),
..Default::default()
}
}
/>
}
return true;
}
false
}
fn startup(
mut commands: Commands,
mut font_mapping: ResMut<FontMapping>,
asset_server: Res<AssetServer>,
) {
font_mapping.set_default(asset_server.load("roboto.kayak_font"));
commands.spawn(UICameraBundle::new());
let mut widget_context = Context::new();
let parent_id = None;
widget_context.add_widget_system(CurrentCount::default().get_name(), current_count_update);
rsx! {
<KayakAppBundle>
<WindowBundle
window={KWindow {
title: "State Example Window".into(),
draggable: true,
position: Vec2::new(10.0, 10.0),
size: Vec2::new(300.0, 250.0),
..KWindow::default()
}}
>
<CurrentCountBundle id={"current_count_entity"} />
<KButtonBundle
on_event={OnEvent::new(
move |In((event_dispatcher_context, event, _entity)): In<(EventDispatcherContext, Event, Entity)>,
mut query: Query<&mut CurrentCount>| {
match event.event_type {
EventType::Click(..) => {
if let Ok(mut current_count) =
query.get_mut(current_count_entity)
{
current_count.0 += 1;
}
}
_ => {}
}
(event_dispatcher_context, event)
},
)}
>
<TextWidgetBundle
text={TextProps {
content: "Click me!".into(),
size: 16.0,
alignment: Alignment::Start,
..Default::default()
}}
/>
</KButtonBundle>
</WindowBundle>
</KayakAppBundle>
}
commands.insert_resource(widget_context);
}
fn main() {
BevyApp::new()
.add_plugins(DefaultPlugins)
.add_plugin(ContextPlugin)
.add_plugin(KayakWidgets)
.add_startup_system(startup)
.run()
}