Skip to content

Commit

Permalink
Merge pull request #40 from bytestring-net/dev
Browse files Browse the repository at this point in the history
Wasm fixes + Layout states
  • Loading branch information
IDEDARY authored Jun 12, 2024
2 parents 3602ca3 + 9ec4a74 commit b84dead
Show file tree
Hide file tree
Showing 20 changed files with 574 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
"bevy_gizmos",
] }

bevy_mod_picking = { version = "0.18.2", default_features = false, features = [] }
bevy_mod_picking = { version = "0.19.0", default_features = false, features = [] }
4 changes: 2 additions & 2 deletions crates/bevy_lunex/src/events/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bevy::prelude::*;
#[cfg(feature = "debug")]
use colored::Colorize;
use lunex_engine::UiLayout;
//use lunex_engine::Layout;

use crate::Cursor2d;
use crate::{Cursor2d, UiLayout};


// #==============#
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_lunex/src/interaction/cursor2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Cursor2d {
hidden: false,
}
}
/// A toggle if this cursor should native
/// A toggle if this cursor should be native
pub fn native_cursor(mut self, enable: bool) -> Self {
self.native_cursor = enable;
self
Expand All @@ -59,7 +59,7 @@ pub fn cursor_update( mut windows: Query<&mut Window, With<PrimaryWindow>>, mut
for (cursor, mut transform, mut visibility) in &mut query {

window.cursor.visible = if cursor.native_cursor { !cursor.hidden } else { false };
window.cursor.icon = cursor.cursor_request;
if window.cursor.visible { window.cursor.icon = cursor.cursor_request; }

match window.cursor_position() {
Some(position) => {
Expand Down
24 changes: 19 additions & 5 deletions crates/bevy_lunex/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#![doc = include_str!("../README.md")]

use bevy::prelude::*;
// #==============================#
// #=== IMPORTS FOR THIS CRATE ===#

pub (crate) use std::{borrow::Borrow, marker::PhantomData};
pub (crate) use bevy::prelude::*;
pub (crate) use lunex_engine::prelude::*;

#[cfg(feature = "debug")]
pub (crate) use colored::Colorize;

#[cfg(feature = "picking")]
pub (crate) use bevy_mod_picking::prelude::*;


// #======================#
Expand All @@ -24,13 +35,16 @@ impl Plugin for UiGeneralPlugin {
// #======================#
// #=== PRELUDE EXPORT ===#

pub mod events;
pub use events::*;
//pub mod events;
//pub use events::*;

pub mod interaction;
pub use interaction::*;

pub mod macros;
pub mod logic;
pub use logic::*;

//pub mod macros;

#[cfg(feature = "picking")]
pub mod picking;
Expand All @@ -47,7 +61,7 @@ pub use systems::*;
pub mod prelude {

pub use super::Cursor2d;
pub use super::events::{SetColor, SetUiLayout};
pub use super::{SetColor, SetUiLayout};

// BEVY-LUNEX SPECIFIC
pub use super::UiGeneralPlugin;
Expand Down
223 changes: 223 additions & 0 deletions crates/bevy_lunex/src/logic/actions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
use bevy::window::{EnabledButtons, PresentMode, PrimaryWindow, WindowMode};

use crate::*;


// #=====================#
// #=== ACTION EVENTS ===#
// These events behave like [`AppExit`] event.
// When you call them something will happen.

// #=== WINDOW ===#

/// This event will change the presentation mode (VSYNC)
#[derive(Event, Debug, Clone, PartialEq)]
pub struct SetWindowPresentModeAction (pub PresentMode);

/// This event will change the window mode (FULLSCREEN)
#[derive(Event, Debug, Clone, PartialEq)]
pub struct SetWindowModeAction (pub WindowMode);

/// This event will change the window position
#[derive(Event, Debug, Clone, PartialEq)]
pub struct SetWindowPositionAction (pub WindowPosition);

/// This event will change the window title
#[derive(Event, Debug, Clone, PartialEq)]
pub struct SetWindowTitleAction (pub String);

/// This event will change the window size
#[derive(Event, Debug, Clone, PartialEq)]
pub struct SetWindowResolutionAction (pub Vec2);

/// This event will change the window resize contstrains
#[derive(Event, Debug, Clone, PartialEq)]
pub struct SetWindowResizeConstrainsAction (pub WindowResizeConstraints);

/// This event will change if window is resizable
#[derive(Event, Debug, Clone, PartialEq)]
pub struct SetWindowResizableAction (pub bool);

/// This event will change the enabled buttons for the window
#[derive(Event, Debug, Clone, PartialEq)]
pub struct SetWindowEnabledButtonsAction (pub EnabledButtons);

/// This event will change if window decorations are available
#[derive(Event, Debug, Clone, PartialEq)]
pub struct SetWindowDecorationsAction (pub bool);

/// This event will focus OS on the window
#[derive(Event, Debug, Clone, PartialEq)]
pub struct SetWindowFocusAction (pub bool);







fn set_window_present_mode_action(mut events: EventReader<SetWindowPresentModeAction>, mut query: Query<&mut Window, With<PrimaryWindow>>) {
for event in events.read() {
if let Ok(window) = &mut query.get_single_mut() {
#[cfg(feature = "debug")]
info!("{} - Changed present mode to: {:?}", "ACTION".red().bold(), event.0);
window.present_mode = event.0;
}
}
}

fn set_window_mode_action(mut events: EventReader<SetWindowModeAction>, mut query: Query<&mut Window, With<PrimaryWindow>>) {
for event in events.read() {
if let Ok(window) = &mut query.get_single_mut() {
#[cfg(feature = "debug")]
info!("{} - Changed window mode to: {:?}", "ACTION".red().bold(), event.0);
window.mode = event.0;
}
}
}

fn set_window_position_action(mut events: EventReader<SetWindowPositionAction>, mut query: Query<&mut Window, With<PrimaryWindow>>) {
for event in events.read() {
if let Ok(window) = &mut query.get_single_mut() {
#[cfg(feature = "debug")]
info!("{} - Changed window position to: {:?}", "ACTION".red().bold(), event.0);
window.position = event.0;
}
}
}

fn set_window_title_action(mut events: EventReader<SetWindowTitleAction>, mut query: Query<&mut Window, With<PrimaryWindow>>) {
for event in events.read() {
if let Ok(window) = &mut query.get_single_mut() {
#[cfg(feature = "debug")]
info!("{} - Changed window title to: {:?}", "ACTION".red().bold(), event.0);
window.title = event.0.clone();
}
}
}

fn set_window_resolution_action(mut events: EventReader<SetWindowResolutionAction>, mut query: Query<&mut Window, With<PrimaryWindow>>) {
for event in events.read() {
if let Ok(window) = &mut query.get_single_mut() {
#[cfg(feature = "debug")]
info!("{} - Changed window resolution to: {:?}", "ACTION".red().bold(), event.0);
window.resolution.set(event.0.x, event.0.y);
}
}
}

fn set_window_resize_constrains_action(mut events: EventReader<SetWindowResizeConstrainsAction>, mut query: Query<&mut Window, With<PrimaryWindow>>) {
for event in events.read() {
if let Ok(window) = &mut query.get_single_mut() {
#[cfg(feature = "debug")]
info!("{} - Changed window resize constrains to: {:?}", "ACTION".red().bold(), event.0);
window.resize_constraints = event.0;
}
}
}










/// This event will override layout of targetted entity
#[derive(Event, PartialEq, Clone, Copy)]
pub struct HideCursor2d (pub bool);
fn apply_event_hide_cursor_2d(mut events: EventReader<HideCursor2d>, mut query: Query<&mut Cursor2d>) {
for event in events.read() {
for mut cursor in &mut query {
#[cfg(feature = "debug")]
info!("{} - Set cursor to hidden: {}", "EVENT".purple().bold(), event.0);
cursor.hidden = event.0;
}
}
}

/// This event will override layout of targetted entity
#[derive(Event, PartialEq, Clone, Copy)]
pub struct SetUiLayout {
pub target: Entity,
pub layout: UiLayout,
}
fn apply_event_set_ui_layout(mut events: EventReader<SetUiLayout>, mut query: Query<&mut UiLayout>) {
for event in events.read() {
if let Ok(mut layout) = query.get_mut(event.target) {
if layout.clone() != event.layout{
*layout = event.layout;
}
}
}
}

/// This event will override sprite/text color of targetted entity
#[derive(Event, PartialEq, Clone, Copy)]
pub struct SetColor {
pub target: Entity,
pub color: Color,
}
fn apply_event_set_color(mut events: EventReader<SetColor>, mut query: Query<(Option<&mut Sprite>, Option<&mut Text>)>) {
for event in events.read() {
if let Ok((sprite_option, text_option)) = query.get_mut(event.target) {
if let Some(mut sprite) = sprite_option {
sprite.color = event.color;
}
if let Some(mut text) = text_option {
for section in &mut text.sections {
section.style.color = event.color;
}
}
}
}
}


// #==============#
// #=== PLUGIN ===#

pub struct UiEventPlugin;
impl Plugin for UiEventPlugin {
fn build(&self, app: &mut App) {
app

.add_event::<SetWindowPresentModeAction>()
.add_systems(Update, set_window_present_mode_action.run_if(on_event::<SetWindowPresentModeAction>()))

.add_event::<SetWindowModeAction>()
.add_systems(Update, set_window_mode_action.run_if(on_event::<SetWindowModeAction>()))

.add_event::<SetWindowPositionAction>()
.add_systems(Update, set_window_position_action.run_if(on_event::<SetWindowPositionAction>()))

.add_event::<SetWindowTitleAction>()
.add_systems(Update, set_window_title_action.run_if(on_event::<SetWindowTitleAction>()))

.add_event::<SetWindowResolutionAction>()
.add_systems(Update, set_window_resolution_action.run_if(on_event::<SetWindowResolutionAction>()))

.add_event::<SetWindowResizeConstrainsAction>()
.add_systems(Update, set_window_resize_constrains_action.run_if(on_event::<SetWindowResizeConstrainsAction>()))










.add_event::<HideCursor2d>()
.add_systems(Update, apply_event_hide_cursor_2d.run_if(on_event::<HideCursor2d>()))

.add_event::<SetUiLayout>()
.add_systems(Update, apply_event_set_ui_layout.run_if(on_event::<SetUiLayout>()))

.add_event::<SetColor>()
.add_systems(Update, apply_event_set_color.run_if(on_event::<SetColor>()));
}
}
76 changes: 76 additions & 0 deletions crates/bevy_lunex/src/logic/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::*;


// #==============#
// #=== EVENTS ===#

/// This is an event you can listen to which broadcasts the entity the pointer clicked on.
/// [`UiClickEmitter`] is the component creating these events.
#[derive(Event, Debug, Clone, PartialEq, Eq)]
pub struct UiClickEvent {
/// The targetted entity that was clicked on
pub target: Entity,
}

/// This is an event you can listen to which broadcasts the entity that changed its value.
/// This event is for example created when you change values in text-input field, spinbox,
/// radio button, etc.
#[derive(Event, Debug, Clone, PartialEq, Eq)]
pub struct UiChangeEvent {
/// The targetted entity that changed its value
pub target: Entity,
/// The new value
pub value: String,
}


// #=================#
// #=== LISTENERS ===#

/// When clicked on this entity, it will create [`UiClickEvent`] event for the specified entity.
/// This component is commonly used in abstraction, where you want to listen to pointer events
/// from another entity that is not the parent and send that data over.
#[derive(Component, Debug, Clone, PartialEq, Eq)]
pub struct UiClickEmitter {
pub trigger: Option<Entity>,
}
impl UiClickEmitter {
/// The entity will create the event for itself and not other entities.
pub const SELF: UiClickEmitter = UiClickEmitter { trigger: None };
/// Specify the entity you want to create events for.
pub fn new(entity: Entity) -> Self {
UiClickEmitter {
trigger: Some(entity)
}
}
}

/// System that triggers when a pointer clicks a node and emmits an event
fn ui_click_listener_system(mut events: EventReader<Pointer<Down>>, mut write: EventWriter<UiClickEvent>, query: Query<(&UiClickEmitter, Entity)>) {
for event in events.read() {
if let Ok((emitter, entity)) = query.get(event.target) {
write.send(UiClickEvent {
target: if let Some(e) = emitter.trigger { e } else { entity },
});
}
}
}


// #====================#
// #=== HOVER PLUGIN ===#

/// Plugin adding all our logic
pub struct CorePlugin;
impl Plugin for CorePlugin {
fn build(&self, app: &mut App) {
app
// Add our events

.add_event::<UiClickEvent>()
.add_systems(Update, ui_click_listener_system.run_if(on_event::<Pointer<Down>>()))

.add_event::<UiChangeEvent>()
;
}
}
Loading

0 comments on commit b84dead

Please sign in to comment.