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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
};
use kayak_ui::prelude::{widgets::*, *};
#[derive(Component, Default, Clone, PartialEq)]
pub struct MyQuad {
transition: TransitionProps,
}
fn my_quad_update(
In(entity): In<Entity>,
widget_context: Res<KayakWidgetContext>,
mut commands: Commands,
mut query: Query<&MyQuad>,
) -> bool {
if let Ok(quad) = query.get_mut(entity) {
create_transition(
&widget_context,
&mut commands,
entity,
&Transition::new(&quad.transition),
);
}
true
}
impl Widget for MyQuad {}
#[derive(Bundle)]
pub struct MyQuadBundle {
my_quad: MyQuad,
computed_styles: ComputedStyles,
on_event: OnEvent,
widget_name: WidgetName,
}
impl Default for MyQuadBundle {
fn default() -> Self {
Self {
my_quad: Default::default(),
on_event: OnEvent::default(),
computed_styles: ComputedStyles::default(),
widget_name: MyQuad::default().get_name(),
}
}
}
fn startup(
mut commands: Commands,
mut font_mapping: ResMut<FontMapping>,
asset_server: Res<AssetServer>,
) {
let camera_entity = commands
.spawn((Camera2dBundle::default(), CameraUIKayak))
.id();
font_mapping.set_default(asset_server.load("roboto.kayak_font"));
let mut widget_context = KayakRootContext::new(camera_entity);
widget_context.add_plugin(KayakWidgetsContextPlugin);
widget_context.add_widget_system(
MyQuad::default().get_name(),
widget_update::<MyQuad, EmptyState>,
my_quad_update,
);
let parent_id = None;
// Some default styles for our transition examples
let quad_styles = KStyle {
left: Units::Pixels(50.0).into(),
top: Units::Pixels(50.0).into(),
width: Units::Pixels(100.0).into(),
height: Units::Pixels(100.0).into(),
..Default::default()
};
rsx! {
<KayakAppBundle>
// Move a quad back and forth.
<TransitionBundle
transition={TransitionProps {
easing: TransitionEasing::QuadraticInOut,
timeout: 500.0,
looping: true,
style_a: KStyle {
..quad_styles.clone()
},
style_b: KStyle {
left: Units::Pixels(500.0).into(),
..quad_styles.clone()
},
..Default::default()
}}
>
<BackgroundBundle
styles={KStyle {
background_color: Color::rgba(1.0, 0.0, 0.0, 1.0).into(),
..Default::default()
}}
/>
</TransitionBundle>
// Example of two transitions. One element that moves, and the other that changes the color of the quad!
<TransitionBundle
transition={TransitionProps {
easing: TransitionEasing::CubicInOut,
timeout: 5000.0,
looping: true,
style_a: KStyle {
left: Units::Pixels(50.0).into(),
..quad_styles.clone()
},
style_b: KStyle {
left: Units::Pixels(500.0).into(),
..quad_styles.clone()
},
..Default::default()
}}
>
// We can have a nested transition!
<MyQuadBundle
my_quad={MyQuad {
transition: TransitionProps {
easing: TransitionEasing::CircularInOut,
timeout: 5000.0,
looping: true,
style_a: KStyle {
render_command: RenderCommand::Quad.into(),
background_color: Color::rgba(1.0, 0.0, 0.0, 1.0).into(),
..Default::default()
},
style_b: KStyle {
render_command: RenderCommand::Quad.into(),
background_color: Color::rgba(0.0, 0.0, 1.0, 1.0).into(),
..Default::default()
},
..Default::default()
}}
}
/>
</TransitionBundle>
// Transitions work with clipping as well!
<TransitionBundle
transition={TransitionProps {
easing: TransitionEasing::CubicInOut,
timeout: 1000.0,
looping: true,
style_a: KStyle {
width: Units::Pixels(0.0).into(),
..quad_styles.clone()
},
style_b: KStyle {
width: Units::Pixels(100.0).into(),
..quad_styles
},
..Default::default()
}}
>
<ClipBundle>
<BackgroundBundle
styles={KStyle {
background_color: Color::rgba(1.0, 0.0, 0.0, 1.0).into(),
..Default::default()
}}
/>
</ClipBundle>
</TransitionBundle>
</KayakAppBundle>
};
commands.spawn((widget_context, EventDispatcher::default()));
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins((
KayakContextPlugin,
KayakWidgets,
LogDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin,
))
.add_systems(Startup, startup)
.run()
}