Skip to content

Commit

Permalink
Merge pull request #60 from bytestring-net/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
IDEDARY authored Jul 20, 2024
2 parents 88cf92a + 7f4839e commit 2b810af
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 17 deletions.
74 changes: 69 additions & 5 deletions crates/bevy_lunex/src/logic/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use pointer::{InputMove, InputPress, Location};

/// Component for easy cursor control.
/// Read more about it in the [docs](https://bytestring-net.github.io/bevy_lunex/advanced/3_cursor.html)
#[derive(Component, Default)]
#[derive(Component, Debug, Clone, PartialEq)]
pub struct Cursor2d {
/// Indicates which cursor is being requested.
cursor_request: CursorIcon,
Expand Down Expand Up @@ -48,7 +48,18 @@ impl Cursor2d {
self
}
}

impl Default for Cursor2d {
fn default() -> Self {
Self {
cursor_request: Default::default(),
cursor_request_priority: Default::default(),
cursor_atlas_map: Default::default(),
location: Default::default(),
confined: Default::default(),
visible: true,
}
}
}

/// This will make the [`Cursor2d`] controllable by specific gamepad.
#[derive(Component, Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -84,6 +95,56 @@ pub enum GamepadCursorMode {
}


// #======================#
// #=== CURSOR BUNDLES ===#

/// Use this bundle to spawn native cursor
#[derive(Bundle)]
pub struct CursorBundle {
/// Main cursor component
pub cursor: Cursor2d,
/// The virtual pointer that the cursor controls
pub pointer: PointerBundle,
/// Required for Cursor to exist
pub spatial: SpatialBundle,
}
impl Default for CursorBundle {
fn default() -> Self {
Self {
cursor: default(),
pointer: PointerBundle::new(PointerId::Custom(pointer::Uuid::new_v4())),
spatial: default(),
}
}
}

/// Use this bundle to spawn styled custom cursor
#[derive(Bundle)]
pub struct StyledCursorBundle {
/// Main cursor component
pub cursor: Cursor2d,
/// The virtual pointer that the cursor controls
pub pointer: PointerBundle,
/// Sprite atlas for the cursor
pub atlas: TextureAtlas,
/// Sprite cursor
pub sprite: SpriteBundle,
/// Required to be [`Pickable::IGNORE`]
pub pickable: Pickable,
}
impl Default for StyledCursorBundle {
fn default() -> Self {
Self {
cursor: default(),
pointer: PointerBundle::new(PointerId::Custom(pointer::Uuid::new_v4())),
atlas: default(),
sprite: default(),
pickable: Pickable::IGNORE,
}
}
}


// #========================#
// #=== CURSOR FUNCTIONS ===#

Expand Down Expand Up @@ -158,13 +219,16 @@ fn gamepad_move_cursor(
fn mouse_move_cursor(
windows: Query<&Window, With<PrimaryWindow>>,
cameras: Query<&OrthographicProjection>,
mut query: Query<(&mut Cursor2d, &Parent), Without<GamepadCursor>>
mut query: Query<(&mut Cursor2d, Option<&Parent>), Without<GamepadCursor>>
) {
if let Ok(window) = windows.get_single() {
for (mut cursor, parent) in &mut query {
for (mut cursor, parent_option) in &mut query {
if let Some(position) = window.cursor_position() {
// Get projection scale to account for zoomed cameras
let scale = if let Ok(projection) = cameras.get(**parent) { projection.scale } else { 1.0 };
let scale = if let Some(parent) = parent_option {
if let Ok(projection) = cameras.get(**parent) { projection.scale } else { 1.0 }
} else { 1.0 };


// Move the cursor
cursor.location.x = (position.x - window.width()*0.5) * scale;
Expand Down
27 changes: 26 additions & 1 deletion crates/bevy_lunex/src/structs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::*;
use bevy::{render::primitives::Aabb, sprite::{Anchor, SpriteSource}, text::{Text2dBounds, TextLayoutInfo}};
use bevy::{render::primitives::Aabb, sprite::{Anchor, Material2d, Mesh2dHandle, SpriteSource}, text::{Text2dBounds, TextLayoutInfo}};


// #=====================#
Expand Down Expand Up @@ -488,6 +488,31 @@ impl UiMaterial3dBundle {
}


/// Additional bundle for `UiNode` entity.
/// Provides functionality to bind mesh in 2D to a `UiNode`.
#[derive(Bundle, Default, Clone)]
pub struct UiMaterial2dBundle<M: Material2d> {
/// The mesh
pub mesh: Mesh2dHandle,
/// Material of the mesh
pub material: Handle<M>,
/// Marks this as node element.
pub element: Element,
/// Contains the ui node size.
pub dimension: Dimension,
/// The visibility of the entity.
pub visibility: Visibility,
/// The inherited visibility of the entity.
pub inherited_visibility: InheritedVisibility,
/// The view visibility of the entity.
pub view_visibility: ViewVisibility,
/// The transform of the entity.
pub transform: Transform,
/// The global transform of the entity.
pub global_transform: GlobalTransform,
}


/// Additional bundle for `UiNode` entity.
/// Provides functionality to bind sprite to `UiNode`.
#[derive(Bundle, Clone, Debug, Default)]
Expand Down
2 changes: 1 addition & 1 deletion docs/src/advanced/abstraction/1_components.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Begin by creating a new `.rs` file in the `components` folder. First, define a p

/// When this component is added, a UI system is built
#[derive(Component)]
pub struct CustomButtom {
pub struct CustomButton {
// Any fields we want to interact with should be here.
text: String,
}
Expand Down
1 change: 1 addition & 0 deletions examples/bevypunk/goto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This example has it's own repository: https://github.com/IDEDARY/Calculator
1 change: 1 addition & 0 deletions examples/calculator/goto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This example has it's own repository: https://github.com/IDEDARY/Bevypunk
Binary file removed examples/mesh2d/assets/background.png
Binary file not shown.
15 changes: 7 additions & 8 deletions examples/mesh2d/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::{prelude::*, sprite::{MaterialMesh2dBundle, Mesh2dHandle}};
use bevy::prelude::*;
use bevy_lunex::prelude::*;


Expand All @@ -9,8 +9,9 @@ fn main() {
.run();
}

fn setup(mut cmd: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<ColorMaterial>>) {
fn setup(mut cmd: Commands, mut materials: ResMut<Assets<ColorMaterial>>) {

// Spawn camera
cmd.spawn((
MainUi,
Camera2dBundle {
Expand All @@ -20,6 +21,7 @@ fn setup(mut cmd: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: Res
}
));

// Spawn UiTree
cmd.spawn((
UiTreeBundle::<MainUi> {
tree: UiTree::new("MyUiSystem"),
Expand All @@ -28,23 +30,20 @@ fn setup(mut cmd: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: Res
MovableByCamera,
)).with_children(|ui| {

// Spawn boundary node
ui.spawn((
UiLink::<MainUi>::path("Root"),
UiLayout::boundary().pos1(Ab(20.0)).pos2(Rl(100.0) - Ab(20.0)).pack::<Base>(),
));

// Spawn a color filled node
ui.spawn((
UiLink::<MainUi>::path("Root/Rectangle"),
UiLayout::solid().size((Ab(1920.0), Ab(1080.0))).pack::<Base>(),
Element,
Dimension::default(),
MaterialMesh2dBundle {
mesh: Mesh2dHandle(meshes.add(Rectangle { half_size: Vec2::splat(50.0) })),
UiMaterial2dBundle {
material: materials.add(Color::srgb(1.0, 0.5, 0.5)),
..default()
}
));

});

}
6 changes: 4 additions & 2 deletions examples/minimal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn main() {

fn setup(mut cmd: Commands, assets: Res<AssetServer>) {

// Spawn camera
cmd.spawn((
MainUi,
Camera2dBundle {
Expand All @@ -20,6 +21,7 @@ fn setup(mut cmd: Commands, assets: Res<AssetServer>) {
}
));

// Spawn UiTree
cmd.spawn((
UiTreeBundle::<MainUi> {
tree: UiTree::new("MyUiSystem"),
Expand All @@ -28,17 +30,17 @@ fn setup(mut cmd: Commands, assets: Res<AssetServer>) {
MovableByCamera,
)).with_children(|ui| {

// Spawn boundary node
ui.spawn((
UiLink::<MainUi>::path("Root"),
UiLayout::boundary().pos1(Ab(20.0)).pos2(Rl(100.0) - Ab(20.0)).pack::<Base>(),
));

// Spawn image with a node
ui.spawn((
UiLink::<MainUi>::path("Root/Rectangle"),
UiLayout::solid().size((Ab(1920.0), Ab(1080.0))).pack::<Base>(),
UiImage2dBundle::from(assets.load("background.png")),
));

});

}

0 comments on commit 2b810af

Please sign in to comment.