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
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::{
App as BevyApp, AssetServer, Bundle, Changed, Color, Commands, Component, Entity, In,
Query, Res, ResMut, Vec2,
},
DefaultPlugins,
};
use kayak_ui::prelude::{widgets::*, KStyle, *};
use morphorm::{PositionType, Units};
#[derive(Component, Default)]
pub struct MyQuad {
pos: Vec2,
pub size: Vec2,
pub color: Color,
}
fn my_quad_update(
In((_widget_context, entity)): In<(WidgetContext, Entity)>,
mut query: Query<(&MyQuad, &mut KStyle), Changed<MyQuad>>,
) -> bool {
if let Ok((quad, mut style)) = query.get_mut(entity) {
style.render_command = StyleProp::Value(RenderCommand::Quad);
style.position_type = StyleProp::Value(PositionType::SelfDirected);
style.left = StyleProp::Value(Units::Pixels(quad.pos.x));
style.top = StyleProp::Value(Units::Pixels(quad.pos.y));
style.width = StyleProp::Value(Units::Pixels(quad.size.x));
style.height = StyleProp::Value(Units::Pixels(quad.size.y));
style.background_color = StyleProp::Value(quad.color);
return true;
}
false
}
impl Widget for MyQuad {}
#[derive(Bundle)]
pub struct MyQuadBundle {
my_quad: MyQuad,
styles: KStyle,
widget_name: WidgetName,
}
impl Default for MyQuadBundle {
fn default() -> Self {
Self {
my_quad: Default::default(),
styles: KStyle::default(),
widget_name: MyQuad::default().get_name(),
}
}
}
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();
widget_context.add_widget_system(MyQuad::default().get_name(), my_quad_update);
let parent_id = None;
rsx! {
<KayakAppBundle>
{
(0..1000).for_each(|_| {
let pos = Vec2::new(fastrand::i32(0..1280) as f32, fastrand::i32(0..720) as f32);
let size = Vec2::new(
fastrand::i32(32..64) as f32,
fastrand::i32(32..64) as f32,
);
let color = Color::rgba(
fastrand::f32(),
fastrand::f32(),
fastrand::f32(),
1.0,
);
constructor! {
<MyQuadBundle
my_quad={MyQuad { pos, size, color }}
/>
}
});
}
</KayakAppBundle>
}
commands.insert_resource(widget_context);
}
fn main() {
BevyApp::new()
.add_plugins(DefaultPlugins)
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(ContextPlugin)
.add_plugin(KayakWidgets)
.add_startup_system(startup)
.run()
}