From a9fa1ded5570a8559f0279011d831f58c7018a7e Mon Sep 17 00:00:00 2001 From: StarToaster <startoaster23@gmail.com> Date: Sun, 23 Oct 2022 23:46:08 -0400 Subject: [PATCH] Renamed some things, use state for window widget. --- examples/context.rs | 2 +- examples/simple_state.rs | 2 +- examples/tabs/tabs.rs | 2 +- examples/text_box.rs | 2 +- examples/todo/todo.rs | 5 +- src/widgets/background.rs | 2 +- src/widgets/button.rs | 2 +- src/widgets/clip.rs | 2 +- src/widgets/element.rs | 2 +- src/widgets/image.rs | 2 +- src/widgets/mod.rs | 54 ++++---- src/widgets/nine_patch.rs | 2 +- src/widgets/scroll/scroll_bar.rs | 2 +- src/widgets/scroll/scroll_box.rs | 2 +- src/widgets/scroll/scroll_content.rs | 2 +- src/widgets/scroll/scroll_context.rs | 2 +- src/widgets/text_box.rs | 2 +- src/widgets/texture_atlas.rs | 2 +- src/widgets/window.rs | 197 +++++++++++++++------------ 19 files changed, 155 insertions(+), 133 deletions(-) diff --git a/examples/context.rs b/examples/context.rs index f82b831..3c921c7 100644 --- a/examples/context.rs +++ b/examples/context.rs @@ -380,7 +380,7 @@ fn startup( window={KWindow { title: "Context Example".into(), draggable: true, - position: Vec2::ZERO, + initial_position: Vec2::ZERO, size: Vec2::new(350.0, 400.0), ..Default::default() }} diff --git a/examples/simple_state.rs b/examples/simple_state.rs index 6fa8f47..3e90b32 100644 --- a/examples/simple_state.rs +++ b/examples/simple_state.rs @@ -86,7 +86,7 @@ fn startup( window={KWindow { title: "State Example Window".into(), draggable: true, - position: Vec2::new(10.0, 10.0), + initial_position: Vec2::new(10.0, 10.0), size: Vec2::new(300.0, 250.0), ..KWindow::default() }} diff --git a/examples/tabs/tabs.rs b/examples/tabs/tabs.rs index ad1b104..64f4096 100644 --- a/examples/tabs/tabs.rs +++ b/examples/tabs/tabs.rs @@ -46,7 +46,7 @@ fn startup( window={KWindow { title: "Tabs".into(), draggable: true, - position: Vec2::new(10.0, 10.0), + initial_position: Vec2::new(10.0, 10.0), size: Vec2::new(300.0, 250.0), ..KWindow::default() }} diff --git a/examples/text_box.rs b/examples/text_box.rs index ba5a728..987311b 100644 --- a/examples/text_box.rs +++ b/examples/text_box.rs @@ -114,7 +114,7 @@ fn startup( window={KWindow { title: "Hello text box".into(), draggable: true, - position: Vec2::new(10.0, 10.0), + initial_position: Vec2::new(10.0, 10.0), size: Vec2::new(300.0, 250.0), ..KWindow::default() }} diff --git a/examples/todo/todo.rs b/examples/todo/todo.rs index a6b05f9..442c42b 100644 --- a/examples/todo/todo.rs +++ b/examples/todo/todo.rs @@ -71,7 +71,10 @@ fn startup( window={KWindow { title: "Todo App".into(), 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), ..Default::default() }} diff --git a/src/widgets/background.rs b/src/widgets/background.rs index 3b500d9..0bcbaaa 100644 --- a/src/widgets/background.rs +++ b/src/widgets/background.rs @@ -36,7 +36,7 @@ impl Default for BackgroundBundle { } } -pub fn update_background( +pub fn background_render( In((widget_context, entity)): In<(WidgetContext, Entity)>, _: Commands, mut query: Query<(&mut KStyle, &KChildren)>, diff --git a/src/widgets/button.rs b/src/widgets/button.rs index a8cc4e2..d882860 100644 --- a/src/widgets/button.rs +++ b/src/widgets/button.rs @@ -38,7 +38,7 @@ impl Default for KButtonBundle { impl Widget for KButton {} impl WidgetProps for KButton {} -pub fn button_update( +pub fn button_render( In((widget_context, entity)): In<(WidgetContext, Entity)>, _: Commands, mut query: Query<(&mut KStyle, &KChildren)>, diff --git a/src/widgets/clip.rs b/src/widgets/clip.rs index 3eff77c..349c52b 100644 --- a/src/widgets/clip.rs +++ b/src/widgets/clip.rs @@ -38,7 +38,7 @@ impl Default for ClipBundle { } } -pub fn update_clip( +pub fn clip_render( In((widget_context, entity)): In<(WidgetContext, Entity)>, _: Commands, mut query: Query<(&KStyle, &KChildren)>, diff --git a/src/widgets/element.rs b/src/widgets/element.rs index 72ef9d3..e55646e 100644 --- a/src/widgets/element.rs +++ b/src/widgets/element.rs @@ -36,7 +36,7 @@ impl Default for ElementBundle { } } -pub fn update_element( +pub fn element_render( In((mut widget_context, entity)): In<(WidgetContext, Entity)>, _: Commands, mut query: Query<(&mut KStyle, &KChildren)>, diff --git a/src/widgets/image.rs b/src/widgets/image.rs index c2e0d2c..7d7a97b 100644 --- a/src/widgets/image.rs +++ b/src/widgets/image.rs @@ -30,7 +30,7 @@ impl Default for ImageBundle { } } -pub fn update_image( +pub fn image_render( In((_widget_context, entity)): In<(WidgetContext, Entity)>, mut query: Query<(&mut KStyle, &Image), Or<((Changed<Image>, Changed<KStyle>), With<Mounted>)>>, ) -> bool { diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index 69967c7..036829c 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -34,26 +34,28 @@ pub use texture_atlas::{TextureAtlas, TextureAtlasBundle}; pub use window::{KWindow, WindowBundle}; use app::{app_render, app_update}; -use background::update_background; -use button::button_update; -use clip::update_clip; -use element::update_element; -use image::update_image; -use nine_patch::update_nine_patch; +use background::background_render; +use button::button_render; +use clip::clip_render; +use element::element_render; +use image::image_render; +use nine_patch::nine_patch_render; use scroll::{ - scroll_bar::update_scroll_bar, scroll_box::update_scroll_box, - scroll_content::update_scroll_content, scroll_context::update_scroll_context, + scroll_bar::scroll_bar_render, scroll_box::scroll_box_render, + scroll_content::scroll_content_render, scroll_context::scroll_context_render, }; use text::text_render; -use text_box::update_text_box; -use texture_atlas::update_texture_atlas; -use window::window_update; +use text_box::text_box_render; +use texture_atlas::texture_atlas_render; +use window::window_render; use crate::{ context::Context, widget::{widget_update, widget_update_with_context, EmptyState, Widget}, }; +use self::window::KWindowState; + pub struct KayakWidgets; impl Plugin for KayakWidgets { @@ -66,7 +68,7 @@ fn add_widget_systems(mut context: ResMut<Context>) { context.add_widget_data::<KayakApp, EmptyState>(); context.add_widget_data::<KButton, 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::<Clip, EmptyState>(); context.add_widget_data::<Image, EmptyState>(); @@ -83,7 +85,7 @@ fn add_widget_systems(mut context: ResMut<Context>) { context.add_widget_system( KButton::default().get_name(), widget_update::<KButton, EmptyState>, - button_update, + button_render, ); context.add_widget_system( TextProps::default().get_name(), @@ -92,62 +94,62 @@ fn add_widget_systems(mut context: ResMut<Context>) { ); context.add_widget_system( KWindow::default().get_name(), - widget_update::<KWindow, EmptyState>, - window_update, + widget_update::<KWindow, KWindowState>, + window_render, ); context.add_widget_system( Background::default().get_name(), widget_update::<Background, EmptyState>, - update_background, + background_render, ); context.add_widget_system( Clip::default().get_name(), widget_update::<Clip, EmptyState>, - update_clip, + clip_render, ); context.add_widget_system( Image::default().get_name(), widget_update::<Image, EmptyState>, - update_image, + image_render, ); context.add_widget_system( TextureAtlas::default().get_name(), widget_update::<TextureAtlas, EmptyState>, - update_texture_atlas, + texture_atlas_render, ); context.add_widget_system( NinePatch::default().get_name(), widget_update::<NinePatch, EmptyState>, - update_nine_patch, + nine_patch_render, ); context.add_widget_system( Element::default().get_name(), widget_update::<Element, EmptyState>, - update_element, + element_render, ); context.add_widget_system( ScrollBarProps::default().get_name(), widget_update_with_context::<ScrollBarProps, EmptyState, ScrollContext>, - update_scroll_bar, + scroll_bar_render, ); context.add_widget_system( ScrollContentProps::default().get_name(), widget_update_with_context::<ScrollContentProps, EmptyState, ScrollContext>, - update_scroll_content, + scroll_content_render, ); context.add_widget_system( ScrollBoxProps::default().get_name(), widget_update_with_context::<ScrollBoxProps, EmptyState, ScrollContext>, - update_scroll_box, + scroll_box_render, ); context.add_widget_system( ScrollContextProvider::default().get_name(), widget_update::<ScrollContextProvider, EmptyState>, - update_scroll_context, + scroll_context_render, ); context.add_widget_system( TextBoxProps::default().get_name(), widget_update::<TextBoxProps, TextBoxState>, - update_text_box, + text_box_render, ); } diff --git a/src/widgets/nine_patch.rs b/src/widgets/nine_patch.rs index f13f994..65b02d9 100644 --- a/src/widgets/nine_patch.rs +++ b/src/widgets/nine_patch.rs @@ -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)>, _: Commands, mut query: Query<(&mut KStyle, &NinePatch, &KChildren)>, diff --git a/src/widgets/scroll/scroll_bar.rs b/src/widgets/scroll/scroll_bar.rs index ba56e72..37f856a 100644 --- a/src/widgets/scroll/scroll_bar.rs +++ b/src/widgets/scroll/scroll_bar.rs @@ -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)>, mut commands: Commands, mut query: Query<(&ScrollBarProps, &mut KStyle)>, diff --git a/src/widgets/scroll/scroll_box.rs b/src/widgets/scroll/scroll_box.rs index 8615a79..1721d65 100644 --- a/src/widgets/scroll/scroll_box.rs +++ b/src/widgets/scroll/scroll_box.rs @@ -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)>, mut commands: Commands, mut query: Query<(&ScrollBoxProps, &mut KStyle, &KChildren, &mut OnLayout)>, diff --git a/src/widgets/scroll/scroll_content.rs b/src/widgets/scroll/scroll_content.rs index 7ea79ca..6898e87 100644 --- a/src/widgets/scroll/scroll_content.rs +++ b/src/widgets/scroll/scroll_content.rs @@ -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)>, mut query: Query<(&mut KStyle, &KChildren, &mut OnLayout), With<ScrollContentProps>>, context_query: Query<&ScrollContext>, diff --git a/src/widgets/scroll/scroll_context.rs b/src/widgets/scroll/scroll_context.rs index 413c124..2f19470 100644 --- a/src/widgets/scroll/scroll_context.rs +++ b/src/widgets/scroll/scroll_context.rs @@ -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)>, mut commands: Commands, mut query: Query<(&ScrollContextProvider, &KChildren)>, diff --git a/src/widgets/text_box.rs b/src/widgets/text_box.rs index 817b4f9..d101027 100644 --- a/src/widgets/text_box.rs +++ b/src/widgets/text_box.rs @@ -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)>, mut commands: Commands, mut query: Query<(&mut KStyle, &TextBoxProps, &mut OnEvent, &OnChange)>, diff --git a/src/widgets/texture_atlas.rs b/src/widgets/texture_atlas.rs index 555cb62..c9f7cb2 100644 --- a/src/widgets/texture_atlas.rs +++ b/src/widgets/texture_atlas.rs @@ -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)>, mut query: Query< (&mut KStyle, &TextureAtlas), diff --git a/src/widgets/window.rs b/src/widgets/window.rs index 776c4ff..77244e4 100644 --- a/src/widgets/window.rs +++ b/src/widgets/window.rs @@ -27,15 +27,19 @@ use super::{ pub struct KWindow { /// If true, allows the window to be draggable by its title bar pub draggable: bool, - /// The position at which to display the window in pixels - pub position: Vec2, + /// The initial position at which to display the window in pixels + pub initial_position: Vec2, /// The size of the window in pixels pub size: Vec2, /// The text to display in the window's title bar pub title: String, +} +#[derive(Component, PartialEq, Clone, Debug, Default)] +pub struct KWindowState { pub is_dragging: bool, pub offset: Vec2, + pub position: Vec2, } impl Widget for KWindow {} @@ -60,113 +64,126 @@ impl Default for WindowBundle { } } -pub fn window_update( +pub fn window_render( In((widget_context, window_entity)): In<(WidgetContext, Entity)>, mut commands: Commands, mut query: Query<(&KStyle, &KChildren, &KWindow)>, + state_query: Query<&KWindowState>, ) -> bool { if let Ok((window_style, window_children, window)) = query.get_mut(window_entity) { let title = window.title.clone(); - let parent_id = Some(window_entity); - rsx! { - <ElementBundle - styles={KStyle { - background_color: StyleProp::Value(Color::rgba(0.125, 0.125, 0.125, 1.0)), - border_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)), - render_command: StyleProp::Value(RenderCommand::Quad), - position_type: StyleProp::Value(PositionType::SelfDirected), - left: StyleProp::Value(Units::Pixels(window.position.x)), - top: StyleProp::Value(Units::Pixels(window.position.y)), - width: StyleProp::Value(Units::Pixels(window.size.x)), - height: StyleProp::Value(Units::Pixels(window.size.y)), - 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"} + let state_entity = widget_context.use_state( + &mut commands, + window_entity, + KWindowState { + position: window.initial_position, + offset: Vec2::ZERO, + is_dragging: false, + }, + ); + + if let Ok(state) = state_query.get(state_entity) { + let parent_id = Some(window_entity); + rsx! { + <ElementBundle styles={KStyle { - 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)), + background_color: StyleProp::Value(Color::rgba(0.125, 0.125, 0.125, 1.0)), + border_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)), - 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() + render_command: StyleProp::Value(RenderCommand::Quad), + position_type: StyleProp::Value(PositionType::SelfDirected), + left: StyleProp::Value(Units::Pixels(state.position.x)), + top: StyleProp::Value(Units::Pixels(state.position.y)), + width: StyleProp::Value(Units::Pixels(window.size.x)), + height: StyleProp::Value(Units::Pixels(window.size.y)), + min_width: StyleProp::Value(Units::Pixels(window.size.x)), + min_height: StyleProp::Value(Units::Pixels(window.size.y)), + ..window_style.clone() }} > - <TextWidgetBundle - text={TextProps { - content: title.clone(), - size: 14.0, - line_height: Some(25.0), - ..Default::default() - }} + <BackgroundBundle + id={"title_bar_entity"} 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() }} - /> - </BackgroundBundle> - { - if window.draggable { - commands - .entity(title_bar_entity) - .insert(OnEvent::new( - move |In((mut event_dispatcher_context, _, event, entity)): In<( - EventDispatcherContext, - WidgetState, - Event, - Entity, - )>, - mut query: Query<&mut KWindow>| { - if let Ok(mut window) = query.get_mut(window_entity) { - match event.event_type { - EventType::MouseDown(data) => { - event_dispatcher_context.capture_cursor(entity); - window.is_dragging = true; - window.offset = Vec2::new( - 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, + > + <TextWidgetBundle + text={TextProps { + content: title.clone(), + size: 14.0, + line_height: Some(25.0), + ..Default::default() + }} + styles={KStyle { + height: StyleProp::Value(Units::Pixels(25.0)), + ..KStyle::default() + }} + /> + </BackgroundBundle> + { + if window.draggable { + commands + .entity(title_bar_entity) + .insert(OnEvent::new( + move |In((mut event_dispatcher_context, _, event, entity)): In<( + EventDispatcherContext, + WidgetState, + Event, + Entity, + )>, + mut query: Query<&mut KWindowState>| { + if let Ok(mut window) = query.get_mut(state_entity) { + match event.event_type { + EventType::MouseDown(data) => { + event_dispatcher_context.capture_cursor(entity); + window.is_dragging = true; + window.offset = Vec2::new( + 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 - styles={KStyle { - padding: StyleProp::Value(Edge::all(Units::Pixels(10.0))), - ..Default::default() - }} - children={window_children.clone()} - /> - </ElementBundle> + <ClipBundle + styles={KStyle { + padding: StyleProp::Value(Edge::all(Units::Pixels(10.0))), + ..Default::default() + }} + children={window_children.clone()} + /> + </ElementBundle> + } } } -- GitLab