diff --git a/Cargo.toml b/Cargo.toml index 529aef2f72437650dc4a2a5b948525766b46c35d..a4612d0f17c5ef54a8e88785e39c06c6532b69fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,19 +1,17 @@ [package] name = "advent_ui" -version = "0.1.0" +version = "0.2.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [features] default = ["serialise"] serialise = ["dep:serde"] [dependencies] -bevy = "0.10.0" -web_instant = "0.3.0" +bevy = "0.11.2" +instant = "0.1.12" num-traits = "0.2.15" -micro_musicbox = "0.6.2" +micro_musicbox = "0.7.0" serde = { version = "1.0.152", optional = true } #kayak_ui = { git = "https://github.com/StarArawn/kayak_ui", rev = "ca2048963dcfc3a87ad36f7686e1d8f9c81f4e36" } @@ -22,5 +20,8 @@ serde = { version = "1.0.152", optional = true } #kayak_ui = { path = "../../OSS/kayak_ui" } #kayak_font = { path = "../../OSS/kayak_ui/kayak_font" } -kayak_ui = { rev = "e68d154f8031c3cbec0ad774779f787ff339ee3b", git = "https://lab.lcr.gr/microhacks/bevy-forks/kayak-ui.git" } -kayak_font = { rev = "e68d154f8031c3cbec0ad774779f787ff339ee3b", git = "https://lab.lcr.gr/microhacks/bevy-forks/kayak-ui.git" } \ No newline at end of file +#kayak_ui = { rev = "e68d154f8031c3cbec0ad774779f787ff339ee3b", git = "https://lab.lcr.gr/microhacks/bevy-forks/kayak-ui.git" } +#kayak_font = { rev = "e68d154f8031c3cbec0ad774779f787ff339ee3b", git = "https://lab.lcr.gr/microhacks/bevy-forks/kayak-ui.git" } + +kayak_ui = { git = "https://github.com/NiseVoid/kayak_ui.git", rev = "e4c183e9ea17bbc71368111547153597eddb5648" } +kayak_font = { git = "https://github.com/NiseVoid/kayak_ui.git", rev = "e4c183e9ea17bbc71368111547153597eddb5648" } diff --git a/src/components/a_text_box.rs b/src/components/a_text_box.rs index 8344eeaff38619bd2b215e1d79ba7110ba606913..1d873228a486f016d7c2b71b598d00505f4052f3 100644 --- a/src/components/a_text_box.rs +++ b/src/components/a_text_box.rs @@ -1,9 +1,9 @@ +use ::kayak_font::{KayakFont, TextProperties}; use bevy::prelude::*; -use kayak_font::{KayakFont, TextProperties}; +use instant::Instant; use kayak_ui::prelude::*; use kayak_ui::widgets::{BackgroundBundle, ClipBundle, ElementBundle, TextProps, TextWidgetBundle}; use kayak_ui::DEFAULT_FONT; -use web_instant::Spot; use crate::theme::tokens::THEME_NINEPATCH_TEXTBOX_ACTIVE; use crate::theme::ThemeProvider; @@ -48,7 +48,7 @@ pub struct ATextBoxState { pub cursor_x: f32, pub cursor_position: usize, pub cursor_visible: bool, - pub cursor_last_update: Spot, + pub cursor_last_update: Instant, pub current_value: String, } @@ -60,7 +60,7 @@ impl Default for ATextBoxState { cursor_x: 0.0, cursor_position: Default::default(), cursor_visible: Default::default(), - cursor_last_update: Spot::now(), + cursor_last_update: Instant::now(), current_value: String::new(), } } @@ -102,7 +102,8 @@ impl Default for ATextBoxWidget { } pub fn render_text_box_widget( - In((widget_context, entity)): In<(KayakWidgetContext, Entity)>, + In(entity): In<Entity>, + widget_context: Res<KayakWidgetContext>, mut commands: Commands, mut query: Query<( &KStyle, @@ -210,13 +211,9 @@ pub fn render_text_box_widget( let cloned_on_change = on_change.clone(); *on_event = OnEvent::new( - move |In((event_dispatcher_context, _, mut event, _entity)): In<( - EventDispatcherContext, - WidgetState, - KEvent, - Entity, - )>, - font_assets: Res<Assets<KayakFont>>, + move |In(_), + mut event: ResMut<KEvent>, + fonts: Res<Assets<KayakFont>>, font_mapping: Res<FontMapping>, mut state_query: Query<&mut ATextBoxState>| { match event.event_type { @@ -228,7 +225,7 @@ pub fn render_text_box_widget( } set_new_cursor_position( &mut state, - &font_assets, + &fonts, &font_mapping, &style_font, font_size, @@ -242,7 +239,7 @@ pub fn render_text_box_widget( } set_new_cursor_position( &mut state, - &font_assets, + &fonts, &font_mapping, &style_font, font_size, @@ -254,7 +251,7 @@ pub fn render_text_box_widget( if let Ok(mut state) = state_query.get_mut(state_entity) { let cloned_on_change = cloned_on_change.clone(); if !state.focused { - return (event_dispatcher_context, event); + return; } let cursor_pos = state.cursor_position; if is_backspace_key(c) { @@ -286,11 +283,11 @@ pub fn render_text_box_widget( } // Update graphemes - set_graphemes(&mut state, &font_assets, &font_mapping, &style_font); + set_graphemes(&mut state, &fonts, &font_mapping, &style_font); set_new_cursor_position( &mut state, - &font_assets, + &fonts, &font_mapping, &style_font, font_size, @@ -303,13 +300,13 @@ pub fn render_text_box_widget( if let Ok(mut state) = state_query.get_mut(state_entity) { state.focused = true; // Update graphemes - set_graphemes(&mut state, &font_assets, &font_mapping, &style_font); + set_graphemes(&mut state, &fonts, &font_mapping, &style_font); state.cursor_position = state.graphemes.len(); set_new_cursor_position( &mut state, - &font_assets, + &fonts, &font_mapping, &style_font, font_size, @@ -323,7 +320,6 @@ pub fn render_text_box_widget( } _ => {} } - (event_dispatcher_context, event) }, ); @@ -535,7 +531,7 @@ pub fn cursor_animation_system( for state_entity in should_update.drain(..) { if let Ok(mut state) = state_query.p1().get_mut(state_entity) { - state.cursor_last_update = Spot::now(); + state.cursor_last_update = Instant::now(); state.cursor_visible = !state.cursor_visible; } } diff --git a/src/components/button.rs b/src/components/button.rs index b590abe3f6d698d779a3141c97ebee0e8611b05e..708bcd3d7c0fc4b929c1dcb847b140496547bde1 100644 --- a/src/components/button.rs +++ b/src/components/button.rs @@ -1,5 +1,5 @@ +use ::kayak_font::{KayakFont, TextProperties}; use bevy::prelude::*; -use kayak_font::{KayakFont, TextProperties}; use kayak_ui::prelude::*; use kayak_ui::widgets::{ElementBundle, NinePatchBundle, TextProps, TextWidgetBundle}; use micro_musicbox::prelude::MusicBox; @@ -76,7 +76,8 @@ pub struct ButtonWidgetState { } pub fn render_button_widget( - In((widget_context, entity)): In<(KayakWidgetContext, Entity)>, + In(entity): In<Entity>, + widget_context: Res<KayakWidgetContext>, mut commands: Commands, state_query: Query<&ButtonWidgetState>, mut query: Query<(&ButtonWidgetProps, &KChildren, &mut ComputedStyles, &KStyle)>, @@ -91,26 +92,20 @@ pub fn render_button_widget( if let Ok(state) = state_query.get(state_entity) { let events = OnEvent::new( - move |In((event_dispatcher_context, _, mut event, _)): In<( - EventDispatcherContext, - WidgetState, - KEvent, - Entity, - )>, - mut params: ParamSet<( - Query<&ButtonWidgetProps>, - Query<&mut ButtonWidgetState>, - MusicBox<ThemeMapping>, - )>| { - let widget_props = match params.p0().get(entity) { - Ok(p) => p.clone(), - Err(..) => return (event_dispatcher_context, event), + move |In(entity), + mut event: ResMut<KEvent>, + props: Query<&ButtonWidgetProps>, + mut state: Query<&mut ButtonWidgetState>, + mut musicbox: MusicBox<ThemeMapping>| { + let widget_props = match props.get(entity) { + Ok(v) => v, + Err(_) => return, }; let mut should_click = false; let mut should_proing = false; - if let Ok(mut state) = params.p1().get_mut(state_entity) { + if let Ok(mut state) = state.get_mut(state_entity) { match &event.event_type { EventType::Hover(..) => { if !widget_props.is_disabled { @@ -148,13 +143,11 @@ pub fn render_button_widget( } if should_click { - params.p2().play_ui_sfx(THEME_SOUND_BUTTON_CLICK); + musicbox.play_ui_sfx(THEME_SOUND_BUTTON_CLICK); } if should_proing { - params.p2().play_ui_sfx(THEME_SOUND_BUTTON_OVER); + musicbox.play_ui_sfx(THEME_SOUND_BUTTON_OVER); } - - (event_dispatcher_context, event) }, ); diff --git a/src/components/h_divider.rs b/src/components/h_divider.rs index 90c284d7202a968b93986a29a12885b0bbc7e046..21f8d01a355a54cf561761da6b7b7f5d8ab5c441 100644 --- a/src/components/h_divider.rs +++ b/src/components/h_divider.rs @@ -28,7 +28,8 @@ impl Widget for HDividerWidgetProps {} basic_widget!(HDividerWidgetProps => HDividerWidget); pub fn render_h_divider( - In((_, entity)): In<(KayakWidgetContext, Entity)>, + In(entity): In<Entity>, + _widget_context: Res<KayakWidgetContext>, _: Commands, mut query: Query<(&HDividerWidgetProps, &KStyle, &mut ComputedStyles)>, ) -> bool { diff --git a/src/components/image_button.rs b/src/components/image_button.rs deleted file mode 100644 index 52f71b69b21faf5c4c4961026d1bfd52f716524f..0000000000000000000000000000000000000000 --- a/src/components/image_button.rs +++ /dev/null @@ -1,232 +0,0 @@ -// use advent_loader::resources::AssetHandles; -// use bevy::prelude::*; -// use kayak_ui::prelude::*; -// use kayak_ui::widgets::{ -// KImage, KImageBundle, NinePatch, NinePatchBundle, TextureAtlasBundle, TextureAtlasProps, -// }; -// -// use crate::parent_widget; -// use crate::{px, stretch, value}; -// -// #[derive(Component, Clone, Eq, PartialEq, Default)] -// pub enum ImageButtonContent { -// Image(String), -// Atlas(String, usize), -// #[default] -// None, -// } -// -// #[derive(Component, Clone, Eq, PartialEq, Default)] -// pub struct ImageButtonWidgetProps { -// pub content: ImageButtonContent, -// pub is_disabled: bool, -// pub is_fixed: bool, -// } -// -// impl ImageButtonWidgetProps { -// pub fn image(name: impl ToString) -> Self { -// Self { -// content: ImageButtonContent::Image(name.to_string()), -// is_disabled: false, -// is_fixed: false, -// } -// } -// pub fn atlas(name: impl ToString, index: usize) -> Self { -// Self { -// content: ImageButtonContent::Atlas(name.to_string(), index), -// is_disabled: false, -// is_fixed: false, -// } -// } -// } -// -// impl Widget for ImageButtonWidgetProps {} -// -// parent_widget!(ImageButtonWidgetProps => ImageButtonWidget); -// -// #[derive(Component, PartialEq, Clone, Default)] -// pub struct ImageButtonWidgetState { -// pub is_pressed: bool, -// pub is_hovered: bool, -// } -// -// pub struct ButtonClickEvent { -// pub button: Entity, -// } -// -// pub fn render_image_button_widget( -// In((mut widget_context, entity)): In<(KayakWidgetContext, Entity)>, -// mut commands: Commands, -// state_query: Query<&ImageButtonWidgetState>, -// mut query: Query<( -// &ImageButtonWidgetProps, -// &KChildren, -// &mut ComputedStyles, -// &KStyle, -// )>, -// assets: Res<AssetHandles>, -// atlass: Res<Assets<TextureAtlas>>, -// ) -> bool { -// if let Ok((props, children, mut computed, style)) = query.get_mut(entity) { -// let state_entity = -// widget_context.use_state(&mut commands, entity, ImageButtonWidgetState::default()); -// -// let parent_id = Some(entity); -// -// if let Ok(state) = state_query.get(state_entity) { -// let events = OnEvent::new( -// move |In((event_dispatcher_context, _, mut event, _)): In<( -// EventDispatcherContext, -// WidgetState, -// KEvent, -// Entity, -// )>, -// mut params: ParamSet<( -// Query<&ImageButtonWidgetProps>, -// Query<&mut ImageButtonWidgetState>, -// )>| { -// let widget_props = match params.p0().get(entity) { -// Ok(p) => p.clone(), -// Err(..) => return (event_dispatcher_context, event), -// }; -// -// if let Ok(mut state) = params.p1().get_mut(state_entity) { -// match &event.event_type { -// EventType::Hover(..) | EventType::MouseIn(..) => { -// if !widget_props.is_disabled { -// state.is_hovered = true; -// } -// } -// EventType::MouseOut(..) => { -// state.is_hovered = false; -// state.is_pressed = false; -// } -// EventType::MouseDown(..) => { -// if !widget_props.is_disabled { -// state.is_pressed = true; -// } -// } -// EventType::MouseUp(..) => { -// state.is_pressed = false; -// } -// EventType::Click(..) => { -// if widget_props.is_disabled { -// event.prevent_default(); -// event.stop_propagation(); -// } -// } -// _ => {} -// } -// } -// -// (event_dispatcher_context, event) -// }, -// ); -// -// let nine_vals = if props.is_disabled { -// NinePatch { -// handle: assets.image("button_disabled"), -// border: Edge::all(3.0), -// } -// } else if state.is_pressed { -// NinePatch { -// handle: assets.image("button_down"), -// border: Edge::all(3.0), -// } -// } else if state.is_hovered { -// NinePatch { -// handle: assets.image("button_active"), -// border: Edge::all(3.0), -// } -// } else { -// NinePatch { -// handle: assets.image("button_idle"), -// border: Edge::all(3.0), -// } -// }; -// -// let padding = if state.is_pressed { -// StyleProp::Value(Edge::new( -// Units::Pixels(12.0), -// Units::Pixels(8.0), -// Units::Pixels(12.0), -// Units::Pixels(8.0), -// )) -// } else { -// StyleProp::Value(Edge::new( -// Units::Pixels(8.0), -// Units::Pixels(8.0), -// Units::Pixels(16.0), -// Units::Pixels(8.0), -// )) -// }; -// -// *computed = KStyle { -// render_command: value(RenderCommand::Layout), -// min_height: px(32.0), -// min_width: px(32.0), -// padding: value(Edge::all(Units::Stretch(0.0))), -// ..Default::default() -// } -// .with_style(style) -// .into(); -// -// let ninepatch_styles = KStyle { -// padding, -// ..Default::default() -// }; -// -// let image_styles = KStyle { -// background_color: Color::RED.into(), -// width: stretch(1.0), -// height: stretch(1.0), -// ..Default::default() -// }; -// -// rsx! { -// <NinePatchBundle -// on_event={events} -// nine_patch={nine_vals} -// styles={ninepatch_styles} -// > -// {match &props.content { -// ImageButtonContent::Image(name) => { -// let handle = assets.image(name); -// -// constructor! { -// <KImageBundle -// image={KImage(handle)} -// styles={image_styles} -// /> -// } -// }, -// ImageButtonContent::Atlas(name, index) => { -// let atlas_handle = assets.atlas(name); -// let image_handle = assets.image(name); -// if let Some(atlas) = atlass.get(&atlas_handle) { -// let rect = atlas.textures[*index]; -// let position = rect.min; -// let tile_size = rect.max - rect.min; -// -// constructor! { -// <TextureAtlasBundle -// atlas={TextureAtlasProps { -// tile_size, -// position, -// handle: image_handle, -// }} -// styles={image_styles} -// /> -// } -// -// } -// -// }, -// ImageButtonContent::None => {} -// } } -// </NinePatchBundle> -// }; -// } -// } -// true -// } diff --git a/src/components/inset_icon.rs b/src/components/inset_icon.rs index f2db8c10c4dda07a0eb174247ec1a4c67ae2214e..bc8045a45fcf32b6c7727badbf4aa1149d635716 100644 --- a/src/components/inset_icon.rs +++ b/src/components/inset_icon.rs @@ -27,7 +27,8 @@ impl Widget for InsetIconProps {} parent_widget!(InsetIconProps => InsetIconWidget); pub fn render_inset_icon_widget( - In((widget_context, entity)): In<(KayakWidgetContext, Entity)>, + In(entity): In<Entity>, + widget_context: Res<KayakWidgetContext>, mut commands: Commands, mut query: Query<(&InsetIconProps, &mut ComputedStyles, &KStyle)>, theme_provider: ThemeProvider, diff --git a/src/components/mod.rs b/src/components/mod.rs index 89fa54997f6374adaec7d894cd8a2fe3045d4864..6335b62e215ff85573d36ef8f171fb35a632509a 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -1,7 +1,6 @@ mod a_text_box; mod button; mod h_divider; -mod image_button; mod inset_icon; mod panel; mod v_divider; @@ -13,9 +12,6 @@ pub use self::button::{ button_props, render_button_widget, ButtonWidget, ButtonWidgetProps, ButtonWidgetState, }; pub use self::h_divider::{render_h_divider, HDividerWidget, HDividerWidgetProps}; -// pub use self::image_button::{ -// render_image_button_widget, ImageButtonWidget, ImageButtonWidgetProps, ImageButtonWidgetState, -// }; pub use self::inset_icon::{render_inset_icon_widget, InsetIconProps, InsetIconWidget}; pub use self::panel::{render_panel_widget, PanelProps, PanelVariant, PanelWidget}; pub use self::v_divider::{render_v_divider, VDividerWidget, VDividerWidgetProps}; diff --git a/src/components/panel.rs b/src/components/panel.rs index c2b9de30c4689f9624ddd85b6dbd26dd27f6d17d..1171d26b34bd4b710fe3d3b4116440a32e99d045 100644 --- a/src/components/panel.rs +++ b/src/components/panel.rs @@ -1,6 +1,5 @@ use bevy::prelude::*; use kayak_ui::prelude::*; -use kayak_ui::widgets::NinePatchBundle; use crate::parent_widget; use crate::theme::tokens::THEME_NINEPATCH_PANEL_DEFAULT; @@ -25,7 +24,8 @@ impl Widget for PanelProps {} parent_widget!(PanelProps => PanelWidget); pub fn render_panel_widget( - In((widget_context, entity)): In<(KayakWidgetContext, Entity)>, + In(entity): In<Entity>, + widget_context: Res<KayakWidgetContext>, mut commands: Commands, mut query: Query<(&PanelProps, &KChildren, &mut ComputedStyles, &KStyle)>, theme_provider: ThemeProvider, @@ -74,15 +74,7 @@ pub fn render_panel_widget( }, }; - children.process(&widget_context, Some(entity)); - - // rsx! { - // <NinePatchBundle - // nine_patch={patch.clone()} - // styles={inner_style} - // children={children.clone()} - // /> - // }; + children.process(&widget_context, &mut commands, Some(entity)); } true } diff --git a/src/components/v_divider.rs b/src/components/v_divider.rs index b38bbb977ecaa3a15069b116366f71567c419fb5..b38ba941411aa380e22dfdc48f289a5283d11f36 100644 --- a/src/components/v_divider.rs +++ b/src/components/v_divider.rs @@ -28,7 +28,8 @@ impl Widget for VDividerWidgetProps {} basic_widget!(VDividerWidgetProps => VDividerWidget); pub fn render_v_divider( - In((_, entity)): In<(KayakWidgetContext, Entity)>, + In(entity): In<Entity>, + _widget_context: Res<KayakWidgetContext>, _: Commands, mut query: Query<(&VDividerWidgetProps, &KStyle, &mut ComputedStyles)>, ) -> bool { diff --git a/src/utilities.rs b/src/utilities.rs index a97ac897b5d1ee6ab5d16ad82d564de3e21f47f1..f38511f08c85e6d305368849659f17a99be0b19f 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -4,7 +4,7 @@ use bevy::asset::Assets; use bevy::ecs::system::SystemParam; use bevy::prelude::{ Commands, Component, DespawnRecursiveExt, DetectChanges, Entity, In, ParamSet, Query, Res, - Resource, With, + ResMut, Resource, With, }; use kayak_font::{Alignment, KayakFont, TextProperties}; use kayak_ui::prelude::{ @@ -316,22 +316,20 @@ where State: HasHoveredState + HasPressedState + Component, { OnEvent::new( - move |In((event_dispatcher_context, _, mut event, _)): In<( - EventDispatcherContext, - WidgetState, - KEvent, - Entity, - )>, - mut params: ParamSet<(Query<&Props>, Query<&mut State>, MusicBox<ThemeMapping>)>| { - let widget_props = match params.p0().get(entity) { - Ok(p) => p.clone(), - Err(..) => return (event_dispatcher_context, event), + move |In(_), + mut event: ResMut<KEvent>, + props_query: Query<&Props>, + mut state_query: Query<&mut State>, + mut musicbox: MusicBox<ThemeMapping>| { + let widget_props = match props_query.get(entity) { + Ok(v) => v, + Err(_) => return, }; let mut should_click = false; let mut should_proing = false; - if let Ok(mut state) = params.p1().get_mut(state_entity) { + if let Ok(mut state) = state_query.get_mut(state_entity) { match &event.event_type { EventType::Hover(..) => { if !widget_props.is_disabled() { @@ -369,13 +367,11 @@ where } if should_click { - params.p2().play_ui_sfx(THEME_SOUND_BUTTON_CLICK); + musicbox.play_ui_sfx(THEME_SOUND_BUTTON_CLICK); } if should_proing { - params.p2().play_ui_sfx(THEME_SOUND_BUTTON_OVER); + musicbox.play_ui_sfx(THEME_SOUND_BUTTON_OVER); } - - (event_dispatcher_context, event) }, ) }