Skip to content

Commit

Permalink
feat: example_plugin_text
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 3, 2024
1 parent a568552 commit 8900058
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 50 deletions.
1 change: 1 addition & 0 deletions crates/beet_examples/examples/basics/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
14 changes: 14 additions & 0 deletions crates/beet_examples/examples/basics/scenes/hello_net.rs
Original file line number Diff line number Diff line change
@@ -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),
));
});
}
20 changes: 20 additions & 0 deletions crates/beet_examples/examples/basics/scenes/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
));
});
}
3 changes: 3 additions & 0 deletions crates/beet_examples/examples/basics/scenes/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
131 changes: 91 additions & 40 deletions crates/beet_examples/src/components/log_to_ui.rs
Original file line number Diff line number Diff line change
@@ -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<Entity, With<LogToUi>>,
actions: Query<&LogOnRun, Added<Running>>,
mut query: Query<&mut Text, With<LogToUi>>,
) {
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<WindowResized>,
parents: Query<&Node>,
mut list: Query<(&mut Style, &Node, &Parent), With<LogToUi>>,
) {
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<LogToUi>, Changed<Children>),
>,
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(),
// ),
// );
// }
// });
});
}
2 changes: 1 addition & 1 deletion crates/beet_examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
}
4 changes: 0 additions & 4 deletions crates/beet_examples/src/plugins/example_plugin.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
8 changes: 7 additions & 1 deletion crates/beet_examples/src/plugins/example_plugin_text.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::prelude::*;
use bevy::prelude::*;
use bevy::ui::UiSystem;

pub struct ExamplePluginText {}

Expand All @@ -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),
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/beet_examples/src/serde_utils/save_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/beet_examples/src/wasm/postmessage_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>A Cool Site</title>
<meta name="description" content="An amazing website">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
}

canvas {
width: 100dvw;
height: 100dvh;
}
</style>
</head>

<body>
<script type="module">
import init from './target/static/wasm/basics.js'
init()
.catch((error) => {
if (!error.message.startsWith("Using exceptions for control flow,"))
throw error
}).then(() => {
window.parent?.postMessage("wasm_loaded", "*")
})
</script>
<canvas id="beet-canvas"></canvas>
</body>

</html>
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8900058

Please sign in to comment.