Skip to content

Commit

Permalink
Merge pull request #43 from bytestring-net/dev
Browse files Browse the repository at this point in the history
Update to 0.14.0-rc.3
  • Loading branch information
IDEDARY authored Jun 20, 2024
2 parents 484203f + 2f61e1a commit 6b469a5
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 53 deletions.
14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

[workspace.package]
authors = ["IDEDARY"]
version = "0.1.0"
version = "0.1.1"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/bytestring-net/bevy-lunex"
Expand All @@ -26,19 +26,21 @@

[workspace.dependencies]

bevy_lunex = { path = "crates/bevy_lunex", version = "0.1.0" }
lunex_engine = { path = "crates/lunex_engine", version = "0.1.0" }
bevy_lunex = { path = "crates/bevy_lunex", version = "0.1.1" }
lunex_engine = { path = "crates/lunex_engine", version = "0.1.1" }

colored = { version = "^2.1" }
indexmap = { version = "^2.1" }
thiserror = { version = "^1.0" }

bevy = { version = "0.13.2", default_features = false, features = [
bevy = { version = "0.14.0-rc.3", default_features = false, features = [
"bevy_pbr",
"bevy_sprite",
"bevy_audio",
"bevy_text",
"multi-threaded",
"multi_threaded",
"bevy_gizmos",
] }

bevy_mod_picking = { version = "0.19.0", default_features = false, features = [] }
#bevy_mod_picking = { path = "../../Prototypes/bevy_mod_picking", default_features = false, features = [] }
bevy_mod_picking = { version = "0.20.0-rc.0", default_features = false, features = ["selection","highlight","debug",] }
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ For production ready example/template check out [`Bevypunk source code`](https:/

| Bevy | Bevy Lunex |
|--------|-----------------|
| 0.13.2 | 0.1.0 - latest |
| 0.14.0 | 0.1.1 |
| 0.13.2 | 0.1.0 |
| 0.12.1 | 0.0.10 - 0.0.11 |
| 0.12.0 | 0.0.7 - 0.0.9 |
| 0.11.2 | 0.0.1 - 0.0.6 |
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_lunex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ For production ready example/template check out [`Bevypunk source code`](https:/

| Bevy | Bevy Lunex |
|--------|-----------------|
| 0.13.2 | 0.1.0 - latest |
| 0.14.0 | 0.1.1 |
| 0.13.2 | 0.1.0 |
| 0.12.1 | 0.0.10 - 0.0.11 |
| 0.12.0 | 0.0.7 - 0.0.9 |
| 0.11.2 | 0.0.1 - 0.0.6 |
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_lunex/src/logic/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ fn ui_click_emitter_system(mut events: EventReader<Pointer<Down>>, mut write: Ev
}



#[derive(Component, Clone, PartialEq, Eq)]
pub struct OnUiClickCommands {
pub closure: fn(&mut Commands),
Expand Down
71 changes: 48 additions & 23 deletions crates/bevy_lunex/src/logic/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ pub struct UiAnimator<S: UiState> {
pub (crate) animation_direction: f32,
/// Range from `0.0` to `1.0`
pub (crate) animation_transition: f32,
/// Range from `0.0` to `1.0`, animation_transition from last tick
previous_transition: f32,
/// Whenever the hover transition is currently changing
pub is_changing: bool,
/// Setting this to true will disable logic with intention that something else will pipe the control data instead
pub receiver: bool,
/// Hover animation speed when transitioning to state
Expand All @@ -56,8 +52,6 @@ impl <S: UiState> UiAnimator<S> {
marker: PhantomData,
animation_direction: 0.0,
animation_transition: 0.0,
previous_transition: 0.0,
is_changing: false,
receiver: false,
animation_speed_backward: 8.0,
animation_speed_forward: 8.0,
Expand Down Expand Up @@ -85,14 +79,14 @@ impl <S: UiState> UiAnimator<S> {
}
fn ui_animation<S: UiState>(time: Res<Time>, mut query: Query<&mut UiAnimator<S>>) {
for mut control in &mut query {
control.is_changing = control.previous_transition != control.animation_transition;
control.previous_transition = control.animation_transition;
if control.receiver { continue }
control.animation_transition += time.delta_seconds() * control.animation_direction * if control.animation_direction == 1.0 { control.animation_speed_forward } else { control.animation_speed_backward };
control.animation_transition = control.animation_transition.clamp(0.0, 1.0);
if !((control.animation_transition == 0.0 && control.animation_direction.is_sign_negative()) && (control.animation_transition == 1.0 && control.animation_direction.is_sign_positive())) {
control.animation_transition += time.delta_seconds() * control.animation_direction * if control.animation_direction == 1.0 { control.animation_speed_forward } else { control.animation_speed_backward };
control.animation_transition = control.animation_transition.clamp(0.0, 1.0);
}
}
}
fn ui_animation_state<S: UiState>(mut query: Query<(&UiAnimator<S>, &mut UiLayoutController)>) {
fn ui_animation_state<S: UiState>(mut query: Query<(&UiAnimator<S>, &mut UiLayoutController), Changed<UiAnimator<S>>>) {
for (animator, mut controller) in &mut query {
controller.index[1] = Hover::INDEX;
controller.tween = animator.animation_transition;
Expand All @@ -119,10 +113,8 @@ impl <S: UiState> UiAnimatorPipe<S> {
}
fn ui_state_pipe_system<S: UiState>(query: Query<(&UiAnimator<S>, &UiAnimatorPipe<S>), Changed<UiAnimator<S>>>, mut event: EventWriter<SetUiStateTransition<S>>) {
for (state, pipe) in &query {
if state.is_changing {
for e in &pipe.entity {
event.send(SetUiStateTransition::new(*e, state.animation_transition));
}
for e in &pipe.entity {
event.send(SetUiStateTransition::new(*e, state.animation_transition));
}
}
}
Expand All @@ -145,21 +137,52 @@ impl <S: UiState> UiColor<S> {
}
}
}
fn set_ui_color<S: UiState>(query: Query<(&UiAnimator<S>, &UiColor<Base>, &UiColor<S>, Entity)>, mut set_color: EventWriter<actions::SetColor>) {
fn set_ui_color<S: UiState>(query: Query<(&UiAnimator<S>, &UiColor<Base>, &UiColor<S>, Entity), Changed<UiAnimator<S>>>, mut set_color: EventWriter<actions::SetColor>) {
for (hover, basecolor, hovercolor, entity) in &query {
if hover.is_changing {
set_color.send(actions::SetColor {
target: entity,
color: basecolor.color.lerp(hovercolor.color, hover.animation_transition),
});
}
set_color.send(actions::SetColor {
target: entity,
color: basecolor.color.lerp(hovercolor.color, hover.animation_transition),
});
}
}


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

#[derive(Resource)]
struct UiSoundPlayer {
entity: Option<Entity>,
}

#[derive(Component, Debug, Clone, PartialEq, Eq)]
pub struct OnHoverPlaySound {
pub sound: Handle<AudioSource>,
}
impl OnHoverPlaySound {
/// Specify the entity you want to create events for.
pub fn new(sound: Handle<AudioSource>) -> Self {
OnHoverPlaySound {
sound,
}
}
}
fn on_hover_play_sound_system(mut events: EventReader<Pointer<Over>>, mut commands: Commands, query: Query<&OnHoverPlaySound>, mut player: ResMut<UiSoundPlayer>) {
for event in events.read() {
if let Ok(listener) = query.get(event.target) {

if let Some(entity) = player.entity {
if let Some(cmd) = commands.get_entity(entity) {
cmd.despawn_recursive();
}
}

let entity = commands.spawn(AudioBundle { source: listener.sound.clone(), settings: PlaybackSettings::DESPAWN.with_volume(bevy::audio::Volume::new(0.3)) }).id();
player.entity = Some(entity);
}
}
}

/// System that changes animation direction on hover
fn hover_enter_system(mut events: EventReader<Pointer<Over>>, mut query: Query<&mut UiAnimator<Hover>>) {
for event in events.read() {
Expand Down Expand Up @@ -198,7 +221,7 @@ impl <T:Component, N:Default + Component, S: UiState> Plugin for StatePlugin<T,N

.add_systems(Update, ui_animation_state::<S>)

.add_systems(Update, (ui_animation::<S>, set_ui_color::<S>).chain())
.add_systems(Update, (ui_animation::<S>, set_ui_color::<S>.after(UiSystems::Process)).chain())

.add_systems(Update, send_layout_to_node::<T, N, S>.in_set(UiSystems::Send).before(send_content_size_to_node::<T, N>));
}
Expand All @@ -208,6 +231,8 @@ pub struct DefaultStatesPlugin;
impl Plugin for DefaultStatesPlugin {
fn build(&self, app: &mut App) {
app
.insert_resource(UiSoundPlayer { entity: None })
.add_systems(Update, on_hover_play_sound_system.run_if(on_event::<Pointer<Over>>()))
.add_systems(Update, hover_enter_system.run_if(on_event::<Pointer<Over>>()))
.add_systems(Update, hover_leave_system.run_if(on_event::<Pointer<Out>>()));
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_lunex/src/logic/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ pub trait LerpColor {
}
impl LerpColor for Color {
fn lerp(&self, color: Color, value: f32) -> Color {
let c1 = self.hsla_to_vec4();
let c2 = color.hsla_to_vec4();
Color::hsla(c1.x.lerp(c2.x, value), c1.y.lerp(c2.y, value), c1.z.lerp(c2.z, value), c1.w.lerp(c2.w, value))
let c1: Hsla = self.clone().into();
let c2: Hsla = color.into();
Color::hsla(c1.hue.lerp(c2.hue, value), c1.saturation.lerp(c2.saturation, value), c1.lightness.lerp(c2.lightness, value), c1.alpha.lerp(c2.alpha, value))
}
}

Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_lunex/src/picking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ pub fn lunex_picking(
.copied()
.filter(|(.., visibility)| visibility.get())
.filter_map(
|(entity, dimension, element, sprite_transform, pickable, ..)| {
|(entity, dimension, element, node_transform, pickable, ..)| {
if blocked {
return None;
}

let pos = if !element.is_some() {
dimension.size.invert_y() / 2.0
} else {
Vec2::new(0.0, 0.0)
};
let pos = if !element.is_some() { dimension.size.invert_y() / 2.0 } else { Vec2::ZERO };

let rect = Rect::from_center_size(pos, dimension.size);

/* let s = rect.max - rect.min;
let p = (rect.min + s/2.0).extend(0.0) + node_transform.translation();
gizmos.rect(p, Quat::from_rotation_y(0.0), s, Color::linear_rgb(0.0, 0.0, 1.0)); */

// Transform cursor pos to sprite coordinate system
let cursor_pos_sprite = sprite_transform
let cursor_pos_sprite = node_transform
.affine()
.inverse()
.transform_point3((cursor_pos_world, 0.0).into());
Expand All @@ -82,7 +82,7 @@ pub fn lunex_picking(
blocked = is_cursor_in_sprite && pickable.map(|p| p.should_block_lower) != Some(false);

// HitData requires a depth as calculated from the camera's near clipping plane
let depth = -cam_ortho.near - sprite_transform.translation().z;
let depth = -cam_ortho.near - node_transform.translation().z;

is_cursor_in_sprite.then_some((entity, HitData::new(cam_entity, depth, None, None)))
},
Expand Down
6 changes: 5 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, text::{Text2dBounds, TextLayoutInfo}};
use bevy::{render::primitives::Aabb, sprite::{Anchor, SpriteSource}, text::{Text2dBounds, TextLayoutInfo}};


// #=====================#
Expand Down Expand Up @@ -352,6 +352,8 @@ pub struct UiElementBundle {
pub struct UiZoneBundle {
/// The required bundle to make entity pickable
pub pickable: PickableBundle,
/// This component is required for picking to work on non-sprite entities
pub sprite_source: SpriteSource,
/// The required components for entity to exist in space
pub spatial: UiSpatialBundle,
}
Expand Down Expand Up @@ -437,6 +439,8 @@ impl From<Handle<Image>> for UiImage2dBundle {
pub struct UiText2dBundle {
/// Contains the text.
pub text: Text,
/// This is needed for visibility computation to work properly.
pub sprite_source: SpriteSource,
/// How the text is positioned relative to its transform.
pub text_anchor: Anchor,
/// The maximum width and height of the text.
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_lunex/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ pub fn debug_draw_gizmo<T:Component, N:Default + Component>(
for node in list {
if let Some(container) = node.obtain_data() {

let mut color = Color::LIME_GREEN;
let mut color = Color::linear_rgb(0.0, 1.0, 0.0);

if let Some(Layout::Solid(_)) = container.layout.get(&container.layout_index[0]) { color = Color::YELLOW }
if let Some(Layout::Solid(_)) = container.layout.get(&container.layout_index[0]) { color = Color::linear_rgb(1.0, 1.0, 0.0) }

let mut pos = container.rectangle.pos.invert_y() + transform.translation();
pos.x += container.rectangle.size.x / 2.0;
Expand Down
2 changes: 1 addition & 1 deletion crates/lunex_engine/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub mod prelude {
// #=== ALL DEFAULT UI UNITS ===#

pub use super::UiValue;
pub use super::{Ab, Rl, Rw, Rh, Em, Sp, Vw, Vh};
pub use super::{Ab, Rl, Rw, Rh, Em, Sp, Vp, Vw, Vh};


// #===================#
Expand Down
2 changes: 1 addition & 1 deletion crates/lunex_engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ pub(crate) mod import {
pub(crate) use colored::Colorize;

pub(crate) use bevy::math::{Vec2, Vec3, Vec4};
pub(crate) use bevy::utils::thiserror::Error;
pub(crate) use thiserror::Error;
}
6 changes: 3 additions & 3 deletions docs/src/2_installation.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Installation

Installing `Bevy_Lunex` is straightforward, just like any other Rust crate. Ensure that the version is `0.1.0` and not `0.0.X`.
Installing `Bevy_Lunex` is straightforward, just like any other Rust crate. Ensure that the version is `0.1.1` and not `0.0.X`.

Add the following to your `Cargo.toml`:

```toml
[dependencies]
bevy_lunex = { version = "0.1.0" }
bevy_lunex = { version = "0.1.1" }
```

Alternatively, you can use the latest bleeding edge version from the Git repository:
Expand All @@ -23,7 +23,7 @@ To avoid potential conflicts with `bevy_ui`, you can disable the default feature
Add the following to your `Cargo.toml`:

```TOML
bevy = { version = "0.13.2", default_features = false, features = [
bevy = { version = "0.14.0", default_features = false, features = [
# Core
"bevy_core_pipeline",
"multi-threaded",
Expand Down
4 changes: 2 additions & 2 deletions examples/minimal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
publish = false

[dependencies]
bevy = { version = "0.13.2", default_features = false, features = [
bevy = { version = "0.14.0-rc.2", default_features = false, features = [
"bevy_asset",
"bevy_gilrs",
"bevy_winit",
Expand All @@ -15,7 +15,7 @@
"bevy_render",
"bevy_sprite",
"bevy_text",
"multi-threaded",
"multi_threaded",
"png",
"hdr",
"x11",
Expand Down

0 comments on commit 6b469a5

Please sign in to comment.