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
use bevy::prelude::*;
use kayak_ui::prelude::{widgets::*, *};
#[derive(Component, Default, Clone, PartialEq)]
struct TextBoxExample;
#[derive(Component, Default, Clone, PartialEq)]
struct TextBoxExampleState {
pub value1: String,
pub value2: String,
}
impl Widget for TextBoxExample {}
#[derive(Bundle)]
struct TextBoxExampleBundle {
text_box_example: TextBoxExample,
styles: KStyle,
widget_name: WidgetName,
}
impl Default for TextBoxExampleBundle {
fn default() -> Self {
Self {
text_box_example: Default::default(),
styles: Default::default(),
widget_name: TextBoxExample.get_name(),
}
}
}
fn update_text_box_example(
In(entity): In<Entity>,
widget_context: Res<KayakWidgetContext>,
mut commands: Commands,
state_query: Query<&TextBoxExampleState>,
) -> bool {
let state_entity = widget_context.use_state::<TextBoxExampleState>(
&mut commands,
entity,
TextBoxExampleState {
value1: "Hello World".into(),
value2: "Hello World2".into(),
},
);
if let Ok(textbox_state) = state_query.get(state_entity) {
let on_change = OnChange::new(
move |In((_, value)): In<(Entity, String)>,
mut state_query: Query<&mut TextBoxExampleState>| {
if let Ok(mut state) = state_query.get_mut(state_entity) {
state.value1 = value;
}
},
);
let on_change2 = OnChange::new(
move |In((_, value)): In<(Entity, String)>,
mut state_query: Query<&mut TextBoxExampleState>| {
if let Ok(mut state) = state_query.get_mut(state_entity) {
state.value2 = value;
}
},
);
let parent_id = Some(entity);
rsx! {
<ElementBundle>
<TextBoxBundle
styles={KStyle {
bottom: StyleProp::Value(Units::Pixels(10.0)),
..Default::default()
}}
text_box={TextBoxProps { value: textbox_state.value1.clone(), ..Default::default()}}
on_change={on_change}
/>
<TextBoxBundle
text_box={TextBoxProps { value: textbox_state.value2.clone(), ..Default::default()}}
on_change={on_change2}
/>
</ElementBundle>
};
}
true
}
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_data::<TextBoxExample, TextBoxExampleState>();
widget_context.add_widget_system(
TextBoxExample.get_name(),
widget_update::<TextBoxExample, TextBoxExampleState>,
update_text_box_example,
);
let parent_id = None;
rsx! {
<KayakAppBundle>
<WindowBundle
window={KWindow {
title: "Hello text box".into(),
draggable: true,
initial_position: Vec2::new(10.0, 10.0),
size: Vec2::new(300.0, 250.0),
..KWindow::default()
}}
>
<TextBoxExampleBundle />
</WindowBundle>
</KayakAppBundle>
};
commands.spawn((widget_context, EventDispatcher::default()));
}
fn main() {
App::new()
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.add_plugins(DefaultPlugins)
.add_plugins((KayakContextPlugin, KayakWidgets))
.add_systems(Startup, startup)
.run()
}