From 8900058fc4cf26b4d0caf26146706a7e49a9d91b Mon Sep 17 00:00:00 2001 From: Peter Hayman Date: Wed, 3 Jul 2024 13:12:54 +1000 Subject: [PATCH] feat: example_plugin_text --- crates/beet_examples/examples/basics/main.rs | 1 + .../examples/basics/scenes/hello_net.rs | 14 ++ .../examples/basics/scenes/hello_world.rs | 20 +++ .../examples/basics/scenes/mod.rs | 3 + .../beet_examples/src/components/log_to_ui.rs | 131 ++++++++++++------ crates/beet_examples/src/lib.rs | 2 +- .../src/plugins/example_plugin.rs | 4 - .../src/plugins/example_plugin_text.rs | 8 +- .../src/serde_utils/save_scene.rs | 2 +- .../src/wasm/postmessage_input.rs | 2 +- index.html | 35 +++++ justfile | 4 +- 12 files changed, 176 insertions(+), 50 deletions(-) create mode 100644 crates/beet_examples/examples/basics/scenes/hello_net.rs create mode 100644 index.html diff --git a/crates/beet_examples/examples/basics/main.rs b/crates/beet_examples/examples/basics/main.rs index 98d0ae31..b752f822 100644 --- a/crates/beet_examples/examples/basics/main.rs +++ b/crates/beet_examples/examples/basics/main.rs @@ -13,6 +13,7 @@ fn main() { DefaultBeetPlugins::default(), )) .add_systems(Startup, scenes::hello_world) + // .add_systems(Startup, scenes::hello_net) .add_systems(PostStartup, save_scene("target/scenes/hello_world.ron")) .run(); } diff --git a/crates/beet_examples/examples/basics/scenes/hello_net.rs b/crates/beet_examples/examples/basics/scenes/hello_net.rs new file mode 100644 index 00000000..38bcb8d6 --- /dev/null +++ b/crates/beet_examples/examples/basics/scenes/hello_net.rs @@ -0,0 +1,14 @@ +use beet_examples::prelude::*; +use beet::prelude::*; +use bevy::prelude::*; + +pub fn hello_net(mut commands: Commands) { + commands + .spawn((SequenceSelector::default(), Running)) + .with_children(|parent| { + parent.spawn(( + LogOnRun("Message Sent: AppLoaded".into()), + TriggerOnRun(AppLoaded), + )); + }); +} diff --git a/crates/beet_examples/examples/basics/scenes/hello_world.rs b/crates/beet_examples/examples/basics/scenes/hello_world.rs index 793e5640..6d35dcef 100644 --- a/crates/beet_examples/examples/basics/scenes/hello_world.rs +++ b/crates/beet_examples/examples/basics/scenes/hello_world.rs @@ -13,5 +13,25 @@ pub fn hello_world(mut commands: Commands) { LogOnRun("World".into()), InsertOnRun(RunResult::Success), )); + parent.spawn(( + LogOnRun("World 1".into()), + RunTimer::default(), + InsertInDuration::with_secs(RunResult::Success, 1), + )); + parent.spawn(( + LogOnRun("World 2".into()), + RunTimer::default(), + InsertInDuration::with_secs(RunResult::Success, 1), + )); + parent.spawn(( + LogOnRun("World 3".into()), + RunTimer::default(), + InsertInDuration::with_secs(RunResult::Success, 1), + )); + parent.spawn(( + LogOnRun("World 4".into()), + RunTimer::default(), + InsertInDuration::with_secs(RunResult::Success, 1), + )); }); } diff --git a/crates/beet_examples/examples/basics/scenes/mod.rs b/crates/beet_examples/examples/basics/scenes/mod.rs index 02d3d122..2ab07a43 100644 --- a/crates/beet_examples/examples/basics/scenes/mod.rs +++ b/crates/beet_examples/examples/basics/scenes/mod.rs @@ -1,3 +1,6 @@ +pub mod hello_net; +#[allow(unused_imports)] +pub use self::hello_net::*; pub mod hello_world; #[allow(unused_imports)] pub use self::hello_world::*; diff --git a/crates/beet_examples/src/components/log_to_ui.rs b/crates/beet_examples/src/components/log_to_ui.rs index 6ac48c5e..d99c921a 100644 --- a/crates/beet_examples/src/components/log_to_ui.rs +++ b/crates/beet_examples/src/components/log_to_ui.rs @@ -1,67 +1,118 @@ -use crate::prelude::DoNotSerialize; use beet::prelude::LogOnRun; use beet::prelude::Running; use bevy::prelude::*; +use bevy::window::WindowResized; #[derive(Debug, Default, Component)] pub struct LogToUi; - +fn style() -> TextStyle { + TextStyle { + font_size: 32., + ..Default::default() + } +} pub fn log_to_ui( + mut commands: Commands, + query: Query>, actions: Query<&LogOnRun, Added>, - mut query: Query<&mut Text, With>, ) { - for mut text in query.iter_mut() { + for entity in query.iter() { for log in actions.iter() { - text.sections[0].value.push_str(&log.0); - text.sections[0].value.push_str("\n"); + commands.entity(entity).with_children(|parent| { + parent.spawn( + // AccessibilityNode(NodeBuilder::new(Role::ListItem)), + TextBundle::from_section( + format!("> {}", log.0.clone()), + style(), + ), + ); + }); } } } -const TEST: &str = r#" -val 1 -val 2 -val 3 -val 4 -val 5 -val 6 -val 7 -val 8 -val 9 -val 10 -val 11 -"#; +fn get_top_pos(node: &Node, parent: &Node) -> f32 { + let items_height = node.size().y; + let container_height = parent.size().y; + let max_scroll = (items_height - container_height).max(0.); + log::info!("\nitems_height: {items_height}\ncontainer_height: {container_height}\nmax_scroll: {max_scroll}"); + return -max_scroll; +} +pub fn scroll_to_bottom_on_resize( + mut resize_reader: EventReader, + parents: Query<&Node>, + mut list: Query<(&mut Style, &Node, &Parent), With>, +) { + for _ev in resize_reader.read() { + for (mut style, node, parent) in list.iter_mut() { + if let Ok(parent) = parents.get(**parent) { + style.top = Val::Px(get_top_pos(node, parent)); + } + } + } +} + +pub fn scroll_to_bottom_on_append( + mut list: Query< + (&mut Style, &Node, &Parent), + (With, Changed), + >, + parents: Query<&Node>, +) { + for (mut style, node, parent) in list.iter_mut() { + if let Ok(parent) = parents.get(**parent) { + style.top = Val::Px(get_top_pos(node, parent)); + } + } +} pub fn spawn_log_to_ui(mut commands: Commands) { commands - .spawn(( - NodeBundle { - style: Style { - flex_direction: FlexDirection::ColumnReverse, - // align_items: AlignItems::Center, - ..default() - }, + // CONTAINER + .spawn(NodeBundle { + style: Style { + height: Val::Percent(100.), + width: Val::Percent(100.), + // align_self: AlignSelf::Stretch, + flex_direction: FlexDirection::Column, + overflow: Overflow::clip(), ..default() }, - // ScrollingList::default(), - // AccessibilityNode(NodeBuilder::new(Role::List)), - )) + // background_color: Color::srgb(0.10, 0.10, 0.10).into(), + ..default() + }) .with_children(|parent| { - parent.spawn(( - DoNotSerialize, - LogToUi, - TextBundle::from_sections([TextSection::new( - TEST, - TextStyle { - // This font is loaded and will be used instead of the default font. - // font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 60.0, + parent + // LIST + .spawn(( + LogToUi, + NodeBundle { + style: Style { + padding: UiRect::all(Val::Px(10.)), + flex_direction: FlexDirection::Column, + // align_items: AlignItems::Center, + ..default() + }, ..default() }, - )]), - )); + // ScrollingList::default(), + // AccessibilityNode(NodeBuilder::new(Role::List)), + )); + // )) + // .with_children(|parent| { + // // SCROLL TEST ITEMS + // for i in 0..30 { + // parent.spawn( + // // AccessibilityNode(NodeBuilder::new(Role::ListItem)), + // TextBundle::from_section( + // format!("Item {i}"), + // style(), + // ), + // ); + // } + // }); }); } diff --git a/crates/beet_examples/src/lib.rs b/crates/beet_examples/src/lib.rs index 93714d35..3c333cec 100644 --- a/crates/beet_examples/src/lib.rs +++ b/crates/beet_examples/src/lib.rs @@ -17,5 +17,5 @@ pub mod prelude { pub use crate::serde_utils::*; pub use crate::net::*; #[cfg(target_arch = "wasm32")] - pub use wasm::*; + pub use crate::wasm::*; } diff --git a/crates/beet_examples/src/plugins/example_plugin.rs b/crates/beet_examples/src/plugins/example_plugin.rs index 095ea604..10fe80e1 100644 --- a/crates/beet_examples/src/plugins/example_plugin.rs +++ b/crates/beet_examples/src/plugins/example_plugin.rs @@ -1,12 +1,8 @@ use crate::prelude::*; -use beet::prelude::*; use bevy::asset::AssetMetaCheck; use bevy::prelude::*; use forky_bevy::systems::close_on_esc; - - - #[derive(Default)] pub struct ExamplePlugin; diff --git a/crates/beet_examples/src/plugins/example_plugin_text.rs b/crates/beet_examples/src/plugins/example_plugin_text.rs index c993ed9a..9edc307b 100644 --- a/crates/beet_examples/src/plugins/example_plugin_text.rs +++ b/crates/beet_examples/src/plugins/example_plugin_text.rs @@ -1,5 +1,6 @@ use crate::prelude::*; use bevy::prelude::*; +use bevy::ui::UiSystem; pub struct ExamplePluginText {} @@ -14,7 +15,12 @@ impl Plugin for ExamplePluginText { fn build(&self, app: &mut App) { app.add_plugins(ExamplePlugin) .add_systems(Startup, (setup, spawn_log_to_ui)) - .add_systems(Update, log_to_ui); + .add_systems(Update, log_to_ui) + .add_systems( + PostUpdate, + (scroll_to_bottom_on_resize, scroll_to_bottom_on_append) + .after(UiSystem::Layout), + ); } } diff --git a/crates/beet_examples/src/serde_utils/save_scene.rs b/crates/beet_examples/src/serde_utils/save_scene.rs index 6e38fa97..7437b01b 100644 --- a/crates/beet_examples/src/serde_utils/save_scene.rs +++ b/crates/beet_examples/src/serde_utils/save_scene.rs @@ -32,7 +32,7 @@ pub fn save_scene(filename: &'static str) -> SystemConfigs { let serialized_scene = scene.serialize(&type_registry).unwrap(); // Showing the scene in the console - info!("{}", serialized_scene); + // info!("{}", serialized_scene); // Writing the scene to a new file. Using a task to avoid calling the filesystem APIs in a system // as they are blocking diff --git a/crates/beet_examples/src/wasm/postmessage_input.rs b/crates/beet_examples/src/wasm/postmessage_input.rs index 329b2c12..06ef46b9 100644 --- a/crates/beet_examples/src/wasm/postmessage_input.rs +++ b/crates/beet_examples/src/wasm/postmessage_input.rs @@ -8,7 +8,7 @@ use web_sys::window; use web_sys::MessageEvent; -#[deprecated("use beet_net instead")] +#[deprecated = "use beet_net instead"] pub struct PostmessageInputPlugin; impl Plugin for PostmessageInputPlugin { diff --git a/index.html b/index.html new file mode 100644 index 00000000..b8a6ffd8 --- /dev/null +++ b/index.html @@ -0,0 +1,35 @@ + + + + + + A Cool Site + + + + + + + + + + + \ No newline at end of file diff --git a/justfile b/justfile index 6aa3767b..8ea3c9fb 100644 --- a/justfile +++ b/justfile @@ -152,11 +152,11 @@ watch *command: build-wasm-release crate example *args: just _build-wasm release {{crate}} {{example}} --release {{args}} -build-wasm-debug crate example *args: +build-wasm crate example *args: just _build-wasm debug {{crate}} {{example}} {{args}} watch-wasm-release crate example *args: just _watch-wasm release {{crate}} {{example}} --release {{args}} -watch-wasm-debug crate example *args: +watch-wasm crate example *args: just _watch-wasm debug {{crate}} {{example}} {{args}} _build-wasm build_config crate example *args: