Skip to content
Snippets Groups Projects
Commit a9fa1ded authored by StarToaster's avatar StarToaster
Browse files

Renamed some things, use state for window widget.

parent 6cfb52bb
No related branches found
No related tags found
No related merge requests found
Showing
with 155 additions and 133 deletions
...@@ -380,7 +380,7 @@ fn startup( ...@@ -380,7 +380,7 @@ fn startup(
window={KWindow { window={KWindow {
title: "Context Example".into(), title: "Context Example".into(),
draggable: true, draggable: true,
position: Vec2::ZERO, initial_position: Vec2::ZERO,
size: Vec2::new(350.0, 400.0), size: Vec2::new(350.0, 400.0),
..Default::default() ..Default::default()
}} }}
......
...@@ -86,7 +86,7 @@ fn startup( ...@@ -86,7 +86,7 @@ fn startup(
window={KWindow { window={KWindow {
title: "State Example Window".into(), title: "State Example Window".into(),
draggable: true, draggable: true,
position: Vec2::new(10.0, 10.0), initial_position: Vec2::new(10.0, 10.0),
size: Vec2::new(300.0, 250.0), size: Vec2::new(300.0, 250.0),
..KWindow::default() ..KWindow::default()
}} }}
......
...@@ -46,7 +46,7 @@ fn startup( ...@@ -46,7 +46,7 @@ fn startup(
window={KWindow { window={KWindow {
title: "Tabs".into(), title: "Tabs".into(),
draggable: true, draggable: true,
position: Vec2::new(10.0, 10.0), initial_position: Vec2::new(10.0, 10.0),
size: Vec2::new(300.0, 250.0), size: Vec2::new(300.0, 250.0),
..KWindow::default() ..KWindow::default()
}} }}
......
...@@ -114,7 +114,7 @@ fn startup( ...@@ -114,7 +114,7 @@ fn startup(
window={KWindow { window={KWindow {
title: "Hello text box".into(), title: "Hello text box".into(),
draggable: true, draggable: true,
position: Vec2::new(10.0, 10.0), initial_position: Vec2::new(10.0, 10.0),
size: Vec2::new(300.0, 250.0), size: Vec2::new(300.0, 250.0),
..KWindow::default() ..KWindow::default()
}} }}
......
...@@ -71,7 +71,10 @@ fn startup( ...@@ -71,7 +71,10 @@ fn startup(
window={KWindow { window={KWindow {
title: "Todo App".into(), title: "Todo App".into(),
draggable: true, draggable: true,
position: Vec2::new((1280.0 / 2.0) - (350.0 / 2.0), (720.0 / 2.0) - (600.0 / 2.0)), initial_position: Vec2::new(
(1280.0 / 2.0) - (350.0 / 2.0),
(720.0 / 2.0) - (600.0 / 2.0)
),
size: Vec2::new(400.0, 600.0), size: Vec2::new(400.0, 600.0),
..Default::default() ..Default::default()
}} }}
......
...@@ -36,7 +36,7 @@ impl Default for BackgroundBundle { ...@@ -36,7 +36,7 @@ impl Default for BackgroundBundle {
} }
} }
pub fn update_background( pub fn background_render(
In((widget_context, entity)): In<(WidgetContext, Entity)>, In((widget_context, entity)): In<(WidgetContext, Entity)>,
_: Commands, _: Commands,
mut query: Query<(&mut KStyle, &KChildren)>, mut query: Query<(&mut KStyle, &KChildren)>,
......
...@@ -38,7 +38,7 @@ impl Default for KButtonBundle { ...@@ -38,7 +38,7 @@ impl Default for KButtonBundle {
impl Widget for KButton {} impl Widget for KButton {}
impl WidgetProps for KButton {} impl WidgetProps for KButton {}
pub fn button_update( pub fn button_render(
In((widget_context, entity)): In<(WidgetContext, Entity)>, In((widget_context, entity)): In<(WidgetContext, Entity)>,
_: Commands, _: Commands,
mut query: Query<(&mut KStyle, &KChildren)>, mut query: Query<(&mut KStyle, &KChildren)>,
......
...@@ -38,7 +38,7 @@ impl Default for ClipBundle { ...@@ -38,7 +38,7 @@ impl Default for ClipBundle {
} }
} }
pub fn update_clip( pub fn clip_render(
In((widget_context, entity)): In<(WidgetContext, Entity)>, In((widget_context, entity)): In<(WidgetContext, Entity)>,
_: Commands, _: Commands,
mut query: Query<(&KStyle, &KChildren)>, mut query: Query<(&KStyle, &KChildren)>,
......
...@@ -36,7 +36,7 @@ impl Default for ElementBundle { ...@@ -36,7 +36,7 @@ impl Default for ElementBundle {
} }
} }
pub fn update_element( pub fn element_render(
In((mut widget_context, entity)): In<(WidgetContext, Entity)>, In((mut widget_context, entity)): In<(WidgetContext, Entity)>,
_: Commands, _: Commands,
mut query: Query<(&mut KStyle, &KChildren)>, mut query: Query<(&mut KStyle, &KChildren)>,
......
...@@ -30,7 +30,7 @@ impl Default for ImageBundle { ...@@ -30,7 +30,7 @@ impl Default for ImageBundle {
} }
} }
pub fn update_image( pub fn image_render(
In((_widget_context, entity)): In<(WidgetContext, Entity)>, In((_widget_context, entity)): In<(WidgetContext, Entity)>,
mut query: Query<(&mut KStyle, &Image), Or<((Changed<Image>, Changed<KStyle>), With<Mounted>)>>, mut query: Query<(&mut KStyle, &Image), Or<((Changed<Image>, Changed<KStyle>), With<Mounted>)>>,
) -> bool { ) -> bool {
......
...@@ -34,26 +34,28 @@ pub use texture_atlas::{TextureAtlas, TextureAtlasBundle}; ...@@ -34,26 +34,28 @@ pub use texture_atlas::{TextureAtlas, TextureAtlasBundle};
pub use window::{KWindow, WindowBundle}; pub use window::{KWindow, WindowBundle};
use app::{app_render, app_update}; use app::{app_render, app_update};
use background::update_background; use background::background_render;
use button::button_update; use button::button_render;
use clip::update_clip; use clip::clip_render;
use element::update_element; use element::element_render;
use image::update_image; use image::image_render;
use nine_patch::update_nine_patch; use nine_patch::nine_patch_render;
use scroll::{ use scroll::{
scroll_bar::update_scroll_bar, scroll_box::update_scroll_box, scroll_bar::scroll_bar_render, scroll_box::scroll_box_render,
scroll_content::update_scroll_content, scroll_context::update_scroll_context, scroll_content::scroll_content_render, scroll_context::scroll_context_render,
}; };
use text::text_render; use text::text_render;
use text_box::update_text_box; use text_box::text_box_render;
use texture_atlas::update_texture_atlas; use texture_atlas::texture_atlas_render;
use window::window_update; use window::window_render;
use crate::{ use crate::{
context::Context, context::Context,
widget::{widget_update, widget_update_with_context, EmptyState, Widget}, widget::{widget_update, widget_update_with_context, EmptyState, Widget},
}; };
use self::window::KWindowState;
pub struct KayakWidgets; pub struct KayakWidgets;
impl Plugin for KayakWidgets { impl Plugin for KayakWidgets {
...@@ -66,7 +68,7 @@ fn add_widget_systems(mut context: ResMut<Context>) { ...@@ -66,7 +68,7 @@ fn add_widget_systems(mut context: ResMut<Context>) {
context.add_widget_data::<KayakApp, EmptyState>(); context.add_widget_data::<KayakApp, EmptyState>();
context.add_widget_data::<KButton, EmptyState>(); context.add_widget_data::<KButton, EmptyState>();
context.add_widget_data::<TextProps, EmptyState>(); context.add_widget_data::<TextProps, EmptyState>();
context.add_widget_data::<KWindow, EmptyState>(); context.add_widget_data::<KWindow, KWindowState>();
context.add_widget_data::<Background, EmptyState>(); context.add_widget_data::<Background, EmptyState>();
context.add_widget_data::<Clip, EmptyState>(); context.add_widget_data::<Clip, EmptyState>();
context.add_widget_data::<Image, EmptyState>(); context.add_widget_data::<Image, EmptyState>();
...@@ -83,7 +85,7 @@ fn add_widget_systems(mut context: ResMut<Context>) { ...@@ -83,7 +85,7 @@ fn add_widget_systems(mut context: ResMut<Context>) {
context.add_widget_system( context.add_widget_system(
KButton::default().get_name(), KButton::default().get_name(),
widget_update::<KButton, EmptyState>, widget_update::<KButton, EmptyState>,
button_update, button_render,
); );
context.add_widget_system( context.add_widget_system(
TextProps::default().get_name(), TextProps::default().get_name(),
...@@ -92,62 +94,62 @@ fn add_widget_systems(mut context: ResMut<Context>) { ...@@ -92,62 +94,62 @@ fn add_widget_systems(mut context: ResMut<Context>) {
); );
context.add_widget_system( context.add_widget_system(
KWindow::default().get_name(), KWindow::default().get_name(),
widget_update::<KWindow, EmptyState>, widget_update::<KWindow, KWindowState>,
window_update, window_render,
); );
context.add_widget_system( context.add_widget_system(
Background::default().get_name(), Background::default().get_name(),
widget_update::<Background, EmptyState>, widget_update::<Background, EmptyState>,
update_background, background_render,
); );
context.add_widget_system( context.add_widget_system(
Clip::default().get_name(), Clip::default().get_name(),
widget_update::<Clip, EmptyState>, widget_update::<Clip, EmptyState>,
update_clip, clip_render,
); );
context.add_widget_system( context.add_widget_system(
Image::default().get_name(), Image::default().get_name(),
widget_update::<Image, EmptyState>, widget_update::<Image, EmptyState>,
update_image, image_render,
); );
context.add_widget_system( context.add_widget_system(
TextureAtlas::default().get_name(), TextureAtlas::default().get_name(),
widget_update::<TextureAtlas, EmptyState>, widget_update::<TextureAtlas, EmptyState>,
update_texture_atlas, texture_atlas_render,
); );
context.add_widget_system( context.add_widget_system(
NinePatch::default().get_name(), NinePatch::default().get_name(),
widget_update::<NinePatch, EmptyState>, widget_update::<NinePatch, EmptyState>,
update_nine_patch, nine_patch_render,
); );
context.add_widget_system( context.add_widget_system(
Element::default().get_name(), Element::default().get_name(),
widget_update::<Element, EmptyState>, widget_update::<Element, EmptyState>,
update_element, element_render,
); );
context.add_widget_system( context.add_widget_system(
ScrollBarProps::default().get_name(), ScrollBarProps::default().get_name(),
widget_update_with_context::<ScrollBarProps, EmptyState, ScrollContext>, widget_update_with_context::<ScrollBarProps, EmptyState, ScrollContext>,
update_scroll_bar, scroll_bar_render,
); );
context.add_widget_system( context.add_widget_system(
ScrollContentProps::default().get_name(), ScrollContentProps::default().get_name(),
widget_update_with_context::<ScrollContentProps, EmptyState, ScrollContext>, widget_update_with_context::<ScrollContentProps, EmptyState, ScrollContext>,
update_scroll_content, scroll_content_render,
); );
context.add_widget_system( context.add_widget_system(
ScrollBoxProps::default().get_name(), ScrollBoxProps::default().get_name(),
widget_update_with_context::<ScrollBoxProps, EmptyState, ScrollContext>, widget_update_with_context::<ScrollBoxProps, EmptyState, ScrollContext>,
update_scroll_box, scroll_box_render,
); );
context.add_widget_system( context.add_widget_system(
ScrollContextProvider::default().get_name(), ScrollContextProvider::default().get_name(),
widget_update::<ScrollContextProvider, EmptyState>, widget_update::<ScrollContextProvider, EmptyState>,
update_scroll_context, scroll_context_render,
); );
context.add_widget_system( context.add_widget_system(
TextBoxProps::default().get_name(), TextBoxProps::default().get_name(),
widget_update::<TextBoxProps, TextBoxState>, widget_update::<TextBoxProps, TextBoxState>,
update_text_box, text_box_render,
); );
} }
...@@ -38,7 +38,7 @@ impl Default for NinePatchBundle { ...@@ -38,7 +38,7 @@ impl Default for NinePatchBundle {
} }
} }
pub fn update_nine_patch( pub fn nine_patch_render(
In((widget_context, entity)): In<(WidgetContext, Entity)>, In((widget_context, entity)): In<(WidgetContext, Entity)>,
_: Commands, _: Commands,
mut query: Query<(&mut KStyle, &NinePatch, &KChildren)>, mut query: Query<(&mut KStyle, &NinePatch, &KChildren)>,
......
...@@ -54,7 +54,7 @@ impl Default for ScrollBarBundle { ...@@ -54,7 +54,7 @@ impl Default for ScrollBarBundle {
} }
} }
pub fn update_scroll_bar( pub fn scroll_bar_render(
In((widget_context, entity)): In<(WidgetContext, Entity)>, In((widget_context, entity)): In<(WidgetContext, Entity)>,
mut commands: Commands, mut commands: Commands,
mut query: Query<(&ScrollBarProps, &mut KStyle)>, mut query: Query<(&ScrollBarProps, &mut KStyle)>,
......
...@@ -77,7 +77,7 @@ impl Default for ScrollBoxBundle { ...@@ -77,7 +77,7 @@ impl Default for ScrollBoxBundle {
} }
} }
pub fn update_scroll_box( pub fn scroll_box_render(
In((widget_context, entity)): In<(WidgetContext, Entity)>, In((widget_context, entity)): In<(WidgetContext, Entity)>,
mut commands: Commands, mut commands: Commands,
mut query: Query<(&ScrollBoxProps, &mut KStyle, &KChildren, &mut OnLayout)>, mut query: Query<(&ScrollBoxProps, &mut KStyle, &KChildren, &mut OnLayout)>,
......
...@@ -40,7 +40,7 @@ impl Default for ScrollContentBundle { ...@@ -40,7 +40,7 @@ impl Default for ScrollContentBundle {
} }
} }
pub fn update_scroll_content( pub fn scroll_content_render(
In((widget_context, entity)): In<(WidgetContext, Entity)>, In((widget_context, entity)): In<(WidgetContext, Entity)>,
mut query: Query<(&mut KStyle, &KChildren, &mut OnLayout), With<ScrollContentProps>>, mut query: Query<(&mut KStyle, &KChildren, &mut OnLayout), With<ScrollContentProps>>,
context_query: Query<&ScrollContext>, context_query: Query<&ScrollContext>,
......
...@@ -161,7 +161,7 @@ impl Default for ScrollContextProviderBundle { ...@@ -161,7 +161,7 @@ impl Default for ScrollContextProviderBundle {
} }
} }
pub fn update_scroll_context( pub fn scroll_context_render(
In((widget_context, entity)): In<(WidgetContext, Entity)>, In((widget_context, entity)): In<(WidgetContext, Entity)>,
mut commands: Commands, mut commands: Commands,
mut query: Query<(&ScrollContextProvider, &KChildren)>, mut query: Query<(&ScrollContextProvider, &KChildren)>,
......
...@@ -80,7 +80,7 @@ impl Default for TextBoxBundle { ...@@ -80,7 +80,7 @@ impl Default for TextBoxBundle {
} }
} }
pub fn update_text_box( pub fn text_box_render(
In((widget_context, entity)): In<(WidgetContext, Entity)>, In((widget_context, entity)): In<(WidgetContext, Entity)>,
mut commands: Commands, mut commands: Commands,
mut query: Query<(&mut KStyle, &TextBoxProps, &mut OnEvent, &OnChange)>, mut query: Query<(&mut KStyle, &TextBoxProps, &mut OnEvent, &OnChange)>,
......
...@@ -52,7 +52,7 @@ impl Default for TextureAtlasBundle { ...@@ -52,7 +52,7 @@ impl Default for TextureAtlasBundle {
} }
} }
pub fn update_texture_atlas( pub fn texture_atlas_render(
In((_widget_context, entity)): In<(WidgetContext, Entity)>, In((_widget_context, entity)): In<(WidgetContext, Entity)>,
mut query: Query< mut query: Query<
(&mut KStyle, &TextureAtlas), (&mut KStyle, &TextureAtlas),
......
...@@ -27,15 +27,19 @@ use super::{ ...@@ -27,15 +27,19 @@ use super::{
pub struct KWindow { pub struct KWindow {
/// If true, allows the window to be draggable by its title bar /// If true, allows the window to be draggable by its title bar
pub draggable: bool, pub draggable: bool,
/// The position at which to display the window in pixels /// The initial position at which to display the window in pixels
pub position: Vec2, pub initial_position: Vec2,
/// The size of the window in pixels /// The size of the window in pixels
pub size: Vec2, pub size: Vec2,
/// The text to display in the window's title bar /// The text to display in the window's title bar
pub title: String, pub title: String,
}
#[derive(Component, PartialEq, Clone, Debug, Default)]
pub struct KWindowState {
pub is_dragging: bool, pub is_dragging: bool,
pub offset: Vec2, pub offset: Vec2,
pub position: Vec2,
} }
impl Widget for KWindow {} impl Widget for KWindow {}
...@@ -60,113 +64,126 @@ impl Default for WindowBundle { ...@@ -60,113 +64,126 @@ impl Default for WindowBundle {
} }
} }
pub fn window_update( pub fn window_render(
In((widget_context, window_entity)): In<(WidgetContext, Entity)>, In((widget_context, window_entity)): In<(WidgetContext, Entity)>,
mut commands: Commands, mut commands: Commands,
mut query: Query<(&KStyle, &KChildren, &KWindow)>, mut query: Query<(&KStyle, &KChildren, &KWindow)>,
state_query: Query<&KWindowState>,
) -> bool { ) -> bool {
if let Ok((window_style, window_children, window)) = query.get_mut(window_entity) { if let Ok((window_style, window_children, window)) = query.get_mut(window_entity) {
let title = window.title.clone(); let title = window.title.clone();
let parent_id = Some(window_entity); let state_entity = widget_context.use_state(
rsx! { &mut commands,
<ElementBundle window_entity,
styles={KStyle { KWindowState {
background_color: StyleProp::Value(Color::rgba(0.125, 0.125, 0.125, 1.0)), position: window.initial_position,
border_color: StyleProp::Value(Color::rgba(0.0781, 0.0898, 0.101, 1.0)), offset: Vec2::ZERO,
border: StyleProp::Value(Edge::all(4.0)), is_dragging: false,
border_radius: StyleProp::Value(Corner::all(5.0)), },
render_command: StyleProp::Value(RenderCommand::Quad), );
position_type: StyleProp::Value(PositionType::SelfDirected),
left: StyleProp::Value(Units::Pixels(window.position.x)), if let Ok(state) = state_query.get(state_entity) {
top: StyleProp::Value(Units::Pixels(window.position.y)), let parent_id = Some(window_entity);
width: StyleProp::Value(Units::Pixels(window.size.x)), rsx! {
height: StyleProp::Value(Units::Pixels(window.size.y)), <ElementBundle
min_width: StyleProp::Value(Units::Pixels(window.size.x)),
min_height: StyleProp::Value(Units::Pixels(window.size.y)),
..window_style.clone()
}}
>
<BackgroundBundle
id={"title_bar_entity"}
styles={KStyle { styles={KStyle {
cursor: StyleProp::Value(KCursorIcon(CursorIcon::Hand)), background_color: StyleProp::Value(Color::rgba(0.125, 0.125, 0.125, 1.0)),
render_command: StyleProp::Value(RenderCommand::Quad), border_color: StyleProp::Value(Color::rgba(0.0781, 0.0898, 0.101, 1.0)),
background_color: StyleProp::Value(Color::rgba(0.0781, 0.0898, 0.101, 1.0)), border: StyleProp::Value(Edge::all(4.0)),
border_radius: StyleProp::Value(Corner::all(5.0)), border_radius: StyleProp::Value(Corner::all(5.0)),
height: StyleProp::Value(Units::Pixels(24.0)), render_command: StyleProp::Value(RenderCommand::Quad),
width: StyleProp::Value(Units::Stretch(1.0)), position_type: StyleProp::Value(PositionType::SelfDirected),
left: StyleProp::Value(Units::Pixels(0.0)), left: StyleProp::Value(Units::Pixels(state.position.x)),
right: StyleProp::Value(Units::Pixels(0.0)), top: StyleProp::Value(Units::Pixels(state.position.y)),
top: StyleProp::Value(Units::Pixels(0.0)), width: StyleProp::Value(Units::Pixels(window.size.x)),
bottom: StyleProp::Value(Units::Pixels(0.0)), height: StyleProp::Value(Units::Pixels(window.size.y)),
padding_left: StyleProp::Value(Units::Pixels(5.0)), min_width: StyleProp::Value(Units::Pixels(window.size.x)),
..KStyle::default() min_height: StyleProp::Value(Units::Pixels(window.size.y)),
..window_style.clone()
}} }}
> >
<TextWidgetBundle <BackgroundBundle
text={TextProps { id={"title_bar_entity"}
content: title.clone(),
size: 14.0,
line_height: Some(25.0),
..Default::default()
}}
styles={KStyle { styles={KStyle {
height: StyleProp::Value(Units::Pixels(25.0)), cursor: StyleProp::Value(KCursorIcon(CursorIcon::Hand)),
render_command: StyleProp::Value(RenderCommand::Quad),
background_color: StyleProp::Value(Color::rgba(0.0781, 0.0898, 0.101, 1.0)),
border_radius: StyleProp::Value(Corner::all(5.0)),
height: StyleProp::Value(Units::Pixels(24.0)),
width: StyleProp::Value(Units::Stretch(1.0)),
left: StyleProp::Value(Units::Pixels(0.0)),
right: StyleProp::Value(Units::Pixels(0.0)),
top: StyleProp::Value(Units::Pixels(0.0)),
bottom: StyleProp::Value(Units::Pixels(0.0)),
padding_left: StyleProp::Value(Units::Pixels(5.0)),
..KStyle::default() ..KStyle::default()
}} }}
/> >
</BackgroundBundle> <TextWidgetBundle
{ text={TextProps {
if window.draggable { content: title.clone(),
commands size: 14.0,
.entity(title_bar_entity) line_height: Some(25.0),
.insert(OnEvent::new( ..Default::default()
move |In((mut event_dispatcher_context, _, event, entity)): In<( }}
EventDispatcherContext, styles={KStyle {
WidgetState, height: StyleProp::Value(Units::Pixels(25.0)),
Event, ..KStyle::default()
Entity, }}
)>, />
mut query: Query<&mut KWindow>| { </BackgroundBundle>
if let Ok(mut window) = query.get_mut(window_entity) { {
match event.event_type { if window.draggable {
EventType::MouseDown(data) => { commands
event_dispatcher_context.capture_cursor(entity); .entity(title_bar_entity)
window.is_dragging = true; .insert(OnEvent::new(
window.offset = Vec2::new( move |In((mut event_dispatcher_context, _, event, entity)): In<(
window.position.x - data.position.0, EventDispatcherContext,
window.position.y - data.position.1, WidgetState,
); Event,
} Entity,
EventType::MouseUp(..) => { )>,
event_dispatcher_context.release_cursor(entity); mut query: Query<&mut KWindowState>| {
window.is_dragging = false; if let Ok(mut window) = query.get_mut(state_entity) {
} match event.event_type {
EventType::Hover(data) => { EventType::MouseDown(data) => {
if window.is_dragging { event_dispatcher_context.capture_cursor(entity);
window.position = Vec2::new( window.is_dragging = true;
window.offset.x + data.position.0, window.offset = Vec2::new(
window.offset.y + data.position.1, window.position.x - data.position.0,
window.position.y - data.position.1,
); );
} }
EventType::MouseUp(..) => {
event_dispatcher_context.release_cursor(entity);
window.is_dragging = false;
}
EventType::Hover(data) => {
if window.is_dragging {
window.position = Vec2::new(
window.offset.x + data.position.0,
window.offset.y + data.position.1,
);
}
}
_ => {}
} }
_ => {}
} }
} (event_dispatcher_context, event)
(event_dispatcher_context, event) },
}, ));
)); }
} }
} <ClipBundle
<ClipBundle styles={KStyle {
styles={KStyle { padding: StyleProp::Value(Edge::all(Units::Pixels(10.0))),
padding: StyleProp::Value(Edge::all(Units::Pixels(10.0))), ..Default::default()
..Default::default() }}
}} children={window_children.clone()}
children={window_children.clone()} />
/> </ElementBundle>
</ElementBundle> }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment