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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//! This example demonstrates how to use the provider/consumer pattern for passing props down
//! to multiple descendants.
//!
//! The problem we'll be solving here is adding support for theming.
//!
//! One reason the provider/consumer pattern might be favored over a global state is that it allows
//! for better specificity and makes local contexts much easier to manage. In the case of theming,
//! this allows us to have multiple active themes, even if they are nested within each other!
use bevy::prelude::*;
use kayak_ui::prelude::{widgets::*, KStyle, *};
/// The color theme struct we will be using across our demo widgets
#[derive(Component, Debug, Default, Clone, PartialEq)]
struct Theme {
name: String,
primary: Color,
secondary: Color,
background: Color,
}
impl Theme {
fn vampire() -> Self {
Self {
name: "Vampire".to_string(),
primary: Color::rgba(1.0, 0.475, 0.776, 1.0),
secondary: Color::rgba(0.641, 0.476, 0.876, 1.0),
background: Color::rgba(0.157, 0.165, 0.212, 1.0),
}
}
fn solar() -> Self {
Self {
name: "Solar".to_string(),
primary: Color::rgba(0.514, 0.580, 0.588, 1.0),
secondary: Color::rgba(0.149, 0.545, 0.824, 1.0),
background: Color::rgba(0.026, 0.212, 0.259, 1.0),
}
}
fn vector() -> Self {
Self {
name: "Vector".to_string(),
primary: Color::rgba(0.533, 1.0, 0.533, 1.0),
secondary: Color::rgba(0.098, 0.451, 0.098, 1.0),
background: Color::rgba(0.004, 0.059, 0.004, 1.0),
}
}
}
#[derive(Component, Debug, Default, Clone, PartialEq)]
struct ThemeButton {
pub theme: Theme,
}
impl Widget for ThemeButton {}
#[derive(Bundle)]
pub struct ThemeButtonBundle {
theme_button: ThemeButton,
styles: KStyle,
widget_name: WidgetName,
}
impl Default for ThemeButtonBundle {
fn default() -> Self {
Self {
theme_button: Default::default(),
styles: KStyle::default(),
widget_name: ThemeButton::default().get_name(),
}
}
}
fn update_theme_button(
In(theme_button_entity): In<Entity>,
widget_context: Res<KayakWidgetContext>,
mut commands: Commands,
query: Query<&ThemeButton>,
mut context_query: Query<&mut Theme>,
) -> bool {
if let Ok(theme_button) = query.get(theme_button_entity) {
if let Some(theme_context_entity) =
widget_context.get_context_entity::<Theme>(theme_button_entity)
{
if let Ok(theme) = context_query.get_mut(theme_context_entity) {
let mut box_style = KStyle {
width: StyleProp::Value(Units::Pixels(30.0)),
height: StyleProp::Value(Units::Pixels(30.0)),
background_color: StyleProp::Value(theme_button.theme.primary),
..Default::default()
};
if theme_button.theme.name == theme.name {
box_style.top = StyleProp::Value(Units::Pixels(3.0));
box_style.left = StyleProp::Value(Units::Pixels(3.0));
box_style.bottom = StyleProp::Value(Units::Pixels(3.0));
box_style.right = StyleProp::Value(Units::Pixels(3.0));
box_style.width = StyleProp::Value(Units::Pixels(24.0));
box_style.height = StyleProp::Value(Units::Pixels(24.0));
}
let parent_id = Some(theme_button_entity);
rsx! {
<BackgroundBundle
styles={box_style}
on_event={OnEvent::new(
move |In(_entity): In<Entity>,
event: ResMut<KEvent>,
query: Query<&ThemeButton>,
mut context_query: Query<&mut Theme>,
| {
if let EventType::Click(..) = event.event_type {
if let Ok(button) = query.get(theme_button_entity) {
if let Ok(mut context_theme) = context_query.get_mut(theme_context_entity) {
*context_theme = button.theme.clone();
}
}
}
},
)}
/>
};
}
}
}
true
}
#[derive(Component, Debug, Default, Clone, PartialEq)]
struct ThemeSelector;
impl Widget for ThemeSelector {}
#[derive(Bundle)]
pub struct ThemeSelectorBundle {
theme_selector: ThemeSelector,
styles: KStyle,
widget_name: WidgetName,
}
impl Default for ThemeSelectorBundle {
fn default() -> Self {
Self {
theme_selector: Default::default(),
styles: KStyle {
height: StyleProp::Value(Units::Auto),
padding_bottom: Units::Pixels(40.0).into(),
..Default::default()
},
widget_name: ThemeSelector.get_name(),
}
}
}
fn update_theme_selector(
In(entity): In<Entity>,
widget_context: Res<KayakWidgetContext>,
mut commands: Commands,
query: Query<&ThemeSelector>,
) -> bool {
if query.get(entity).is_ok() {
let button_container_style = KStyle {
layout_type: StyleProp::Value(LayoutType::Row),
width: StyleProp::Value(Units::Stretch(1.0)),
height: StyleProp::Value(Units::Auto),
top: StyleProp::Value(Units::Pixels(5.0)),
..Default::default()
};
let vampire_theme = Theme::vampire();
let solar_theme = Theme::solar();
let vector_theme = Theme::vector();
let parent_id = Some(entity);
rsx! {
<ElementBundle styles={button_container_style}>
<ThemeButtonBundle theme_button={ThemeButton { theme: vampire_theme }} />
<ThemeButtonBundle theme_button={ThemeButton { theme: solar_theme }} />
<ThemeButtonBundle theme_button={ThemeButton { theme: vector_theme }} />
</ElementBundle>
};
}
true
}
#[derive(Component, Debug, Default, Clone, PartialEq, Eq)]
pub struct ThemeDemo {
is_root: bool,
context_entity: Option<Entity>,
}
impl Widget for ThemeDemo {}
#[derive(Bundle)]
pub struct ThemeDemoBundle {
theme_demo: ThemeDemo,
styles: KStyle,
widget_name: WidgetName,
}
impl Default for ThemeDemoBundle {
fn default() -> Self {
Self {
theme_demo: Default::default(),
styles: KStyle::default(),
widget_name: ThemeDemo::default().get_name(),
}
}
}
fn update_theme_demo(
In(entity): In<Entity>,
widget_context: Res<KayakWidgetContext>,
mut commands: Commands,
mut query_set: Query<&mut ThemeDemo>,
theme_context: Query<&Theme>,
) -> bool {
if let Ok(mut theme_demo) = query_set.get_mut(entity) {
if let Some(theme_context_entity) = widget_context.get_context_entity::<Theme>(entity) {
if let Ok(theme) = theme_context.get(theme_context_entity) {
let select_lbl = if theme_demo.is_root {
format!("Select Theme (Current: {})", theme.name)
} else {
format!("Select A Different Theme (Current: {})", theme.name)
};
if theme_demo.is_root && theme_demo.context_entity.is_none() {
let theme_entity = commands.spawn(Theme::vector()).id();
theme_demo.context_entity = Some(theme_entity);
}
let context_entity = if let Some(entity) = theme_demo.context_entity {
entity
} else {
Entity::from_raw(1000000)
};
let text_styles = KStyle {
color: StyleProp::Value(theme.primary),
height: StyleProp::Value(Units::Pixels(28.0)),
..Default::default()
};
let btn_style = KStyle {
background_color: StyleProp::Value(theme.secondary),
width: StyleProp::Value(Units::Stretch(0.75)),
height: StyleProp::Value(Units::Pixels(32.0)),
top: StyleProp::Value(Units::Pixels(5.0)),
left: StyleProp::Value(Units::Stretch(1.0)),
right: StyleProp::Value(Units::Stretch(1.0)),
..Default::default()
};
let parent_id = Some(entity);
rsx! {
<ElementBundle>
<TextWidgetBundle
styles={KStyle {
height: StyleProp::Value(Units::Pixels(28.0)),
..Default::default()
}}
text={TextProps {
content: select_lbl,
size: 14.0,
line_height: Some(28.0),
..Default::default()
}}
/>
<ThemeSelectorBundle />
<BackgroundBundle
styles={KStyle {
background_color: StyleProp::Value(theme.background),
top: StyleProp::Value(Units::Pixels(15.0)),
width: StyleProp::Value(Units::Stretch(1.0)),
height: StyleProp::Value(Units::Stretch(1.0)),
..Default::default()
}}
>
<TextWidgetBundle
styles={text_styles}
text={TextProps {
content: "Lorem ipsum dolor...".into(),
size: 12.0,
..Default::default()
}}
/>
<KButtonBundle
button={KButton { text: "BUTTON".into() }}
styles={btn_style}
/>
{
if theme_demo.is_root {
widget_context.set_context_entity::<Theme>(
parent_id,
context_entity,
);
constructor! {
<ElementBundle
styles={KStyle {
top: StyleProp::Value(Units::Pixels(10.0)),
left: StyleProp::Value(Units::Pixels(10.0)),
bottom: StyleProp::Value(Units::Pixels(10.0)),
right: StyleProp::Value(Units::Pixels(10.0)),
..Default::default()
}}
>
<ThemeDemoBundle />
</ElementBundle>
}
}
}
</BackgroundBundle>
</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::<ThemeDemo, EmptyState>();
widget_context.add_widget_data::<ThemeButton, EmptyState>();
widget_context.add_widget_data::<ThemeSelector, EmptyState>();
widget_context.add_widget_system(
ThemeDemo::default().get_name(),
widget_update_with_context::<ThemeDemo, EmptyState, Theme>,
update_theme_demo,
);
widget_context.add_widget_system(
ThemeButton::default().get_name(),
widget_update_with_context::<ThemeButton, EmptyState, Theme>,
update_theme_button,
);
widget_context.add_widget_system(
ThemeSelector.get_name(),
widget_update_with_context::<ThemeSelector, EmptyState, Theme>,
update_theme_selector,
);
let parent_id = None;
rsx! {
<KayakAppBundle>
{
let theme_entity = commands.spawn(Theme::vampire()).id();
widget_context.set_context_entity::<Theme>(parent_id, theme_entity);
}
<WindowBundle
window={KWindow {
title: "Context Example".into(),
draggable: true,
initial_position: Vec2::ZERO,
size: Vec2::new(350.0, 400.0),
..Default::default()
}}
>
<ThemeDemoBundle
theme_demo={ThemeDemo {
is_root: true,
context_entity: None,
}}
/>
</WindowBundle>
</KayakAppBundle>
};
commands.spawn((widget_context, EventDispatcher::default()));
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins((KayakContextPlugin, KayakWidgets))
.add_systems(Startup, startup)
.run()
}