diff --git a/.vscode/tasks.json b/.vscode/tasks.json index c2785c9..5139072 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -14,7 +14,7 @@ { "type": "cargo", "command": "run", - "args": ["--bin", "minimal"], + "args": ["--bin", "mesh2d"], "problemMatcher": [ "$rustc" ], diff --git a/crates/bevy_lunex/src/systems.rs b/crates/bevy_lunex/src/systems.rs index c2271f9..3687cba 100644 --- a/crates/bevy_lunex/src/systems.rs +++ b/crates/bevy_lunex/src/systems.rs @@ -1,5 +1,5 @@ use crate::*; -use bevy::{math::Vec3A, render::primitives::Aabb, text::TextLayoutInfo, window::PrimaryWindow}; +use bevy::{math::Vec3A, render::primitives::Aabb, sprite::Mesh2dHandle, text::TextLayoutInfo, window::PrimaryWindow}; use lunex_engine::*; @@ -331,7 +331,7 @@ pub fn fetch_dimension_from_node( if let Some(container) = node.obtain_data() { if dimension.as_ref().size != container.rectangle.size { #[cfg(feature = "verbose")] - info!("{} {} - Linked {} fetched Dimension data from node", "<-".bright_green(), link.path.yellow().bold(), "ENTITY".blue()); + info!("{} {} - Linked {} fetched Dimension data from node: {:?}", "<-".bright_green(), link.path.yellow().bold(), "ENTITY".blue(), container.rectangle.size); dimension.size = container.rectangle.size; } } @@ -405,24 +405,36 @@ pub fn element_image_size_from_dimension( /// * Generic `(T)` - Marker component grouping entities into one widget type pub fn element_reconstruct_mesh( mut msh: ResMut>, - mut query: Query<(&Dimension, &mut Handle, &mut Aabb), (With>, With, Changed)>, + mut query: Query<(&Dimension, Option<&mut Handle>, Option<&mut Mesh2dHandle>, Option<&mut Aabb>), (With>, With, Or<(Changed, Added)>)>, ) { - for (dimension, mut mesh, mut aabb) in &mut query { + for (dimension, mut mesh_option, mut mesh2d_option, mut aabb_option) in &mut query { #[cfg(feature = "verbose")] info!("{} {} - Reconstructed mesh size", "--".yellow(), "ELEMENT".red()); - // Unload old mesh - let _ = msh.remove(mesh.id()); + if let Some(aabb) = aabb_option.as_mut() { + // Create new culling boundary + **aabb = Aabb { + center: Vec3A::ZERO, + half_extents: Vec3A::new(dimension.size.x/2.0, dimension.size.y/2.0, 1.0), + }; + } + + if let Some(mesh) = mesh_option.as_mut() { + // Unload old mesh + let _ = msh.remove(mesh.id()); - // Create new culling boundary - *aabb = Aabb { - center: Vec3A::ZERO, - half_extents: Vec3A::new(dimension.size.x/2.0, dimension.size.y/2.0, 1.0), - }; + // Create new mesh + **mesh = msh.add(Rectangle {half_size: dimension.size / 2.0}); + } - // Create new mesh - *mesh = msh.add(Rectangle {half_size: dimension.size / 2.0}) + if let Some(mesh2d) = mesh2d_option.as_mut() { + // Unload old mesh + let _ = msh.remove(mesh2d.0.id()); + + // Create new mesh + **mesh2d = Mesh2dHandle(msh.add(Rectangle {half_size: dimension.size / 2.0})); + } } } diff --git a/examples/mesh2d/Cargo.toml b/examples/mesh2d/Cargo.toml new file mode 100644 index 0000000..da05b83 --- /dev/null +++ b/examples/mesh2d/Cargo.toml @@ -0,0 +1,28 @@ +[package] + name = "mesh2d" + authors.workspace = true + version.workspace = true + edition.workspace = true + publish = false + +[dependencies] + bevy = { version = "0.14.0-rc.2", default_features = false, features = [ + "bevy_asset", + "bevy_gilrs", + "bevy_winit", + "bevy_core_pipeline", + "bevy_pbr", + "bevy_render", + "bevy_sprite", + "bevy_text", + "multi_threaded", + "png", + "hdr", + "x11", + "bevy_gizmos", + "tonemapping_luts", + "default_font", + + "dynamic_linking", + ] } + bevy_lunex = { workspace = true, features=["debug"] } diff --git a/examples/mesh2d/assets/background.png b/examples/mesh2d/assets/background.png new file mode 100644 index 0000000..9c9ad3e Binary files /dev/null and b/examples/mesh2d/assets/background.png differ diff --git a/examples/mesh2d/src/main.rs b/examples/mesh2d/src/main.rs new file mode 100644 index 0000000..9a664cb --- /dev/null +++ b/examples/mesh2d/src/main.rs @@ -0,0 +1,50 @@ +use bevy::{prelude::*, sprite::{MaterialMesh2dBundle, Mesh2dHandle}}; +use bevy_lunex::prelude::*; + + +fn main() { + App::new() + .add_plugins((DefaultPlugins, UiPlugin)) + .add_systems(Startup, setup) + .run(); +} + +fn setup(mut cmd: Commands, mut meshes: ResMut>, mut materials: ResMut>) { + + cmd.spawn(( + MainUi, + Camera2dBundle { + transform: Transform::from_xyz(0.0, 0.0, 1000.0), + camera: Camera::default(), + ..default() + } + )); + + cmd.spawn(( + UiTreeBundle:: { + tree: UiTree::new("MyUiSystem"), + ..default() + }, + MovableByCamera, + )).with_children(|ui| { + + ui.spawn(( + UiLink::::path("Root"), + UiLayout::boundary().pos1(Ab(20.0)).pos2(Rl(100.0) - Ab(20.0)).pack::(), + )); + + ui.spawn(( + UiLink::::path("Root/Rectangle"), + UiLayout::solid().size((Ab(1920.0), Ab(1080.0))).pack::(), + Element, + Dimension::default(), + MaterialMesh2dBundle { + mesh: Mesh2dHandle(meshes.add(Rectangle { half_size: Vec2::splat(50.0) })), + material: materials.add(Color::srgb(1.0, 0.5, 0.5)), + ..default() + } + )); + + }); + +} \ No newline at end of file