Skip to content

Commit

Permalink
A bunch of fixes! New styles, widgets, and more!
Browse files Browse the repository at this point in the history
  • Loading branch information
StarArawn committed Nov 6, 2022
1 parent 8cbde95 commit 98b4de4
Show file tree
Hide file tree
Showing 44 changed files with 1,474 additions and 692 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ kayak_ui_macros = { path = "./kayak_ui_macros" }
indexmap = "1.9"
log = "0.4"
bitflags = "1.3.2"
reorder = "2.1"

[dev-dependencies]
fastrand = "1.8"
Expand Down
Binary file added assets/lato-light - Copy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/lato-light.kayak_font

Large diffs are not rendered by default.

Binary file added assets/lato-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 4 additions & 14 deletions book/src/chapter_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ fn startup(
commands.spawn(UICameraBundle::new());
let mut widget_context = KayakRootContext::new();

let pre_existing_app_entity = widget_context.get_child_at(None);
let app_entity = if let Some(entity) = pre_existing_app_entity {
commands.get_or_spawn(entity).id()
} else {
commands.spawn_empty().id()
};

let app_entity = widget_context.spawn_widget(&mut commands, None);
// Create default app bundle
let mut app_bundle = KayakAppBundle {
..Default::default()
Expand All @@ -85,12 +79,7 @@ fn startup(
let mut children = KChildren::new();

// Create the text child
let pre_existing_text_entity = widget_context.get_child_at(Some(app_entity));
let text_entity = if let Some(entity) = pre_existing_text_entity {
commands.get_or_spawn(entity).id()
} else {
commands.spawn_empty().id()
};
let text_entity = widget_context.spawn_widget(&mut commands, Some(app_entity));
commands.entity(text_entity).insert(TextWidgetBundle {
text: TextProps {
content: "Hello World".into(),
Expand All @@ -113,10 +102,11 @@ fn startup(
}
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
.add_plugins(DefaultPlugins)
.add_plugin(KayakContextPlugin)
.add_plugin(KayakWidgets)
.add_startup_system(startup)
.run()
}

```
21 changes: 10 additions & 11 deletions examples/bevy_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,29 +227,28 @@ fn startup(
text={TextProps {
size: 13.0,
content: "You can check if the cursor is over the UI or on a focusable widget using the BevyContext resource.".to_string(),
user_styles: text_styles.clone(),
..Default::default()
}}
styles={text_styles.clone()}
/>
<KButtonBundle
button={KButton {
text: "Change Tile Color".into(),
..Default::default()
}}
on_event={handle_change_color}
styles={button_styles}
>
<TextWidgetBundle
text={TextProps {
size: 16.0,
content: "Change Tile Color".to_string(),
..Default::default()
}}
/>
</KButtonBundle>
/>
<TextWidgetBundle
text={TextProps {
size: 11.0,
content: "Go ahead and click the button! The tile won't move.".to_string(),
user_styles: KStyle {
top: Units::Pixels(10.0).into(),
..text_styles
},
..Default::default()
}}
styles={text_styles}
/>
</WindowBundle>
</KayakAppBundle>
Expand Down
143 changes: 143 additions & 0 deletions examples/conditional_widget.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
use bevy::prelude::*;
use kayak_ui::prelude::{widgets::*, *};

#[derive(Component, Default, PartialEq, Clone)]
struct MyWidget;

impl Widget for MyWidget {}

#[derive(Component, Default, PartialEq, Clone)]
struct MyWidgetState {
pub show_window: bool,
}

#[derive(Bundle)]
struct MyWidgetBundle {
count: MyWidget,
styles: KStyle,
widget_name: WidgetName,
}

impl Default for MyWidgetBundle {
fn default() -> Self {
Self {
count: MyWidget::default(),
styles: KStyle::default(),
widget_name: MyWidget::default().get_name(),
}
}
}

fn my_widget_render(
In((widget_context, entity)): In<(KayakWidgetContext, Entity)>,
mut commands: Commands,
query: Query<&MyWidgetState>,
) -> bool {
let state_entity = widget_context.use_state(&mut commands, entity, MyWidgetState::default());
if let Ok(state) = query.get(state_entity) {
let parent_id = Some(entity);
rsx! {
<ElementBundle>
<KButtonBundle
button={KButton {
text: "Show Window".into(),
user_styles: KStyle {
left: Units::Stretch(1.0).into(),
right: Units::Stretch(1.0).into(),
..Default::default()
}
}}
on_event={OnEvent::new(
move |In((event_dispatcher_context, _, mut event, _entity)): In<(EventDispatcherContext, WidgetState, Event, Entity)>,
mut query: Query<&mut MyWidgetState>| {
event.prevent_default();
event.stop_propagation();
match event.event_type {
EventType::Click(..) => {
if let Ok(mut state) = query.get_mut(state_entity) {
state.show_window = true;
}
}
_ => {}
}
(event_dispatcher_context, event)
},
)}
/>
{if state.show_window {
constructor! {
<WindowBundle
window={KWindow {
title: "Conditional widget rendering!".into(),
draggable: true,
initial_position: Vec2::new(10.0, 10.0),
size: Vec2::new(300.0, 250.0),
..KWindow::default()
}}
>
<KButtonBundle
button={KButton { text: "Hide Window".into(), ..Default::default() }}
on_event={OnEvent::new(
move |In((event_dispatcher_context, _, mut event, _entity)): In<(EventDispatcherContext, WidgetState, Event, Entity)>,
mut query: Query<&mut MyWidgetState>| {
match event.event_type {
EventType::Click(..) => {
event.prevent_default();
event.stop_propagation();
if let Ok(mut state) = query.get_mut(state_entity) {
state.show_window = false;
}
}
_ => {}
}
(event_dispatcher_context, event)
},
)}
/>
</WindowBundle>
}
}}
</ElementBundle>
}
}

true
}

fn startup(
mut commands: Commands,
mut font_mapping: ResMut<FontMapping>,
asset_server: Res<AssetServer>,
) {
font_mapping.set_default(asset_server.load("lato-light.kayak_font"));

// Camera 2D forces a clear pass in bevy.
// We do this because our scene is not rendering anything else.
commands.spawn(Camera2dBundle::default());
commands.spawn(UICameraBundle::new());

let mut widget_context = KayakRootContext::new();
let parent_id = None;
widget_context.add_widget_data::<MyWidget, MyWidgetState>();
widget_context.add_widget_system(
MyWidget::default().get_name(),
widget_update::<MyWidget, MyWidgetState>,
my_widget_render,
);
rsx! {
<KayakAppBundle>
<MyWidgetBundle />
</KayakAppBundle>
}
commands.insert_resource(widget_context);
}

fn main() {
App::new()
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.add_plugins(DefaultPlugins)
.add_plugin(KayakContextPlugin)
.add_plugin(KayakWidgets)
.add_startup_system(startup)
.run()
}
28 changes: 9 additions & 19 deletions examples/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,17 @@ fn update_theme_demo(
};

let parent_id = Some(entity);
let mut children = kayak_ui::prelude::KChildren::new();
rsx! {
<>
<ElementBundle>
<TextWidgetBundle
text={TextProps {
content: select_lbl,
size: 14.0,
line_height: Some(28.0),
..Default::default()
}}
styles={KStyle {
height: StyleProp::Value(Units::Pixels(28.0)),
user_styles: KStyle {
height: StyleProp::Value(Units::Pixels(28.0)),
..Default::default()
},
..Default::default()
}}
/>
Expand All @@ -284,21 +283,14 @@ fn update_theme_demo(
text={TextProps {
content: "Lorem ipsum dolor...".into(),
size: 12.0,
user_styles: text_styles.clone(),
..Default::default()
}}
styles={text_styles.clone()}
/>
<KButtonBundle
button={KButton { text: "BUTTON".into(), ..Default::default() }}
styles={btn_style.clone()}
>
<TextWidgetBundle
text={TextProps {
content: "BUTTON".into(),
size: 14.0,
..Default::default()
}}
/>
</KButtonBundle>
/>
{
if theme_demo.is_root {
widget_context.set_context_entity::<Theme>(
Expand All @@ -321,10 +313,8 @@ fn update_theme_demo(
}
}
</BackgroundBundle>
</>
</ElementBundle>
}

children.process(&widget_context, parent_id);
}
}
}
Expand Down
15 changes: 2 additions & 13 deletions examples/hello_world_no_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ fn startup(
commands.spawn(UICameraBundle::new());
let mut widget_context = KayakRootContext::new();

let pre_existing_app_entity = widget_context.get_child_at(None);
let app_entity = if let Some(entity) = pre_existing_app_entity {
commands.get_or_spawn(entity).id()
} else {
commands.spawn_empty().id()
};

let app_entity = widget_context.spawn_widget(&mut commands, None);
// Create default app bundle
let mut app_bundle = KayakAppBundle {
..Default::default()
Expand All @@ -25,12 +19,7 @@ fn startup(
let mut children = KChildren::new();

// Create the text child
let pre_existing_text_entity = widget_context.get_child_at(Some(app_entity));
let text_entity = if let Some(entity) = pre_existing_text_entity {
commands.get_or_spawn(entity).id()
} else {
commands.spawn_empty().id()
};
let text_entity = widget_context.spawn_widget(&mut commands, Some(app_entity));
commands.entity(text_entity).insert(TextWidgetBundle {
text: TextProps {
content: "Hello World".into(),
Expand Down
10 changes: 7 additions & 3 deletions examples/quads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct MyQuad {
pos: Vec2,
pub size: Vec2,
pub color: Color,
pub z_index: i32,
}

fn my_quad_update(
Expand All @@ -24,16 +25,19 @@ fn my_quad_update(
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);
style.z_index = StyleProp::Value(quad.z_index);
}

*on_event = OnEvent::new(
move |In((event_dispatcher_context, _, event, entity)): In<(
move |In((event_dispatcher_context, _, mut event, entity)): In<(
EventDispatcherContext,
WidgetState,
Event,
Entity,
)>,
mut query: Query<(&mut KStyle, &MyQuad)>| {
event.prevent_default();
event.stop_propagation();
match event.event_type {
EventType::MouseIn(..) => {
if let Ok((mut styles, _)) = query.get_mut(entity) {
Expand Down Expand Up @@ -99,7 +103,7 @@ fn startup(
rsx! {
<KayakAppBundle>
{
(0..1000).for_each(|_| {
(0..1000i32).for_each(|i| {
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,
Expand All @@ -113,7 +117,7 @@ fn startup(
);
constructor! {
<MyQuadBundle
my_quad={MyQuad { pos, size, color }}
my_quad={MyQuad { pos, size, color, z_index: i }}
/>
}
});
Expand Down
Loading

0 comments on commit 98b4de4

Please sign in to comment.