Skip to content

Commit

Permalink
Bevy 0.15 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Leinnan committed Dec 7, 2024
1 parent 8b97367 commit 03a73d1
Show file tree
Hide file tree
Showing 9 changed files with 1,679 additions and 937 deletions.
2,453 changes: 1,594 additions & 859 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ inspect = ["bevy-inspector-egui"]
serde = ["dep:serde"]

[dependencies]
bevy = { version = "0.14", default-features = false, features = [
bevy = { version = "0.15", features = [
"bevy_render",
"bevy_ui",
] }
bevy-inspector-egui = { version = "0.25", optional = true }
bevy-inspector-egui = { version = "0.28", optional = true }
serde = { version = "^1", features = ["derive"], optional = true }

[dev-dependencies]
bevy = "0.14"
bevy = "0.15"
2 changes: 1 addition & 1 deletion examples/invisible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
translation: Vec3::new(0., 0., 0.),
..default()
},
texture: asset_server.load("Knob.png"),
sprite: Sprite {
image: asset_server.load("Knob.png"),
color: Color::srgb(0.5, 0.0, 0.5),
custom_size: Some(Vec2::new(50., 50.)),
..default()
Expand Down
26 changes: 8 additions & 18 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,12 @@ fn main() {
struct Player(pub f32);

fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
cmd.spawn(Camera2dBundle {
transform: Transform::from_xyz(0., 0., 5.0),
..default()
});
cmd.spawn((Camera2d, Transform::from_xyz(0., 0., 5.0)));
// Fake Player
cmd.spawn(SpriteBundle {
transform: Transform {
translation: Vec3::new(0., 0., 0.),
..default()
},
texture: asset_server.load("Knob.png"),
sprite: Sprite {
color: Color::srgb(0.5, 0.0, 0.5), //Purple
custom_size: Some(Vec2::new(50., 50.)),
..default()
},
cmd.spawn(Sprite {
image: asset_server.load("Knob.png"),
color: Color::srgb(0.5, 0.0, 0.5), //Purple
custom_size: Some(Vec2::new(50., 50.)),
..default()
})
.insert(Player(50.));
Expand All @@ -49,7 +39,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
Some(Color::srgba(1.0, 0.27, 0.0, 0.3)),
Vec2::new(75., 75.),
Vec2::new(150., 150.),
Style {
Node {
width: Val::Px(150.),
height: Val::Px(150.),
position_type: PositionType::Absolute,
Expand All @@ -71,7 +61,7 @@ fn update_joystick(

for j in joystick.read() {
let Vec2 { x, y } = j.axis();
player.translation.x += x * player_data.0 * time_step.delta_seconds();
player.translation.y += y * player_data.0 * time_step.delta_seconds();
player.translation.x += x * player_data.0 * time_step.delta_secs();
player.translation.y += y * player_data.0 * time_step.delta_secs();
}
}
38 changes: 25 additions & 13 deletions src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::sync::Arc;
use bevy::{
ecs::{entity::Entity, world::World},
hierarchy::Children,
math::{Rect, Vec2},
math::{Rect, Vec2, Vec3Swizzles},
reflect::Reflect,
render::view::Visibility,
transform::components::GlobalTransform,
ui::Node,
ui::ComputedNode,
utils::all_tuples,
};

Expand Down Expand Up @@ -148,14 +148,17 @@ impl VirtualJoystickBehavior for JoystickFixed {
if world.get::<VirtualJoystickUIBackground>(child).is_none() {
continue;
}
let Some(joystick_base_node) = world.get::<Node>(child) else {
let Some(joystick_base_node) = world.get::<ComputedNode>(child) else {
continue;
};
let Some(joystick_base_global_transform) = world.get::<GlobalTransform>(child) else {
continue;
};
joystick_base_rect =
Some(joystick_base_node.logical_rect(joystick_base_global_transform));
let rect = Rect::from_center_size(
joystick_base_global_transform.translation().xy(),
joystick_base_node.size(),
);
joystick_base_rect = Some(rect);
break;
}
let Some(joystick_base_rect) = joystick_base_rect else {
Expand Down Expand Up @@ -195,14 +198,17 @@ impl VirtualJoystickBehavior for JoystickFloating {
if world.get::<VirtualJoystickUIBackground>(child).is_none() {
continue;
}
let Some(joystick_base_node) = world.get::<Node>(child) else {
let Some(joystick_base_node) = world.get::<ComputedNode>(child) else {
continue;
};
let Some(joystick_base_global_transform) = world.get::<GlobalTransform>(child) else {
continue;
};
joystick_base_rect =
Some(joystick_base_node.logical_rect(joystick_base_global_transform));
let rect = Rect::from_center_size(
joystick_base_global_transform.translation().xy(),
joystick_base_node.size(),
);
joystick_base_rect = Some(rect);
break;
}
let Some(joystick_base_rect) = joystick_base_rect else {
Expand Down Expand Up @@ -247,13 +253,16 @@ impl VirtualJoystickBehavior for JoystickDynamic {
fn update_at_delta_stage(&self, world: &mut World, entity: Entity) {
let joystick_rect: Rect;
{
let Some(joystick_node) = world.get::<Node>(entity) else {
let Some(joystick_node) = world.get::<ComputedNode>(entity) else {
return;
};
let Some(joystick_global_transform) = world.get::<GlobalTransform>(entity) else {
return;
};
joystick_rect = joystick_node.logical_rect(joystick_global_transform);
joystick_rect = Rect::from_center_size(
joystick_global_transform.translation().xy(),
joystick_node.size(),
);
}
let mut children_entities: Vec<Entity> = Vec::new();
{
Expand All @@ -269,14 +278,17 @@ impl VirtualJoystickBehavior for JoystickDynamic {
if world.get::<VirtualJoystickUIBackground>(child).is_none() {
continue;
}
let Some(joystick_base_node) = world.get::<Node>(child) else {
let Some(joystick_base_node) = world.get::<ComputedNode>(child) else {
continue;
};
let Some(joystick_base_global_transform) = world.get::<GlobalTransform>(child) else {
continue;
};
joystick_base_rect =
Some(joystick_base_node.logical_rect(joystick_base_global_transform));
let rect = Rect::from_center_size(
joystick_base_global_transform.translation().xy(),
joystick_base_node.size(),
);
joystick_base_rect = Some(rect);
break;
}
let Some(joystick_base_rect) = joystick_base_rect else {
Expand Down
10 changes: 5 additions & 5 deletions src/bundles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ use bevy::{
prelude::default,
render::view::{InheritedVisibility, ViewVisibility, Visibility},
transform::components::{GlobalTransform, Transform},
ui::{Node, Style, ZIndex},
ui::{ComputedNode, Node, ZIndex},
};

use crate::{VirtualJoystickID, VirtualJoystickNode};

#[derive(Bundle, Debug, Default)]
pub struct VirtualJoystickBundle<S: VirtualJoystickID> {
/// Describes the size of the node
pub(crate) node: Node,
pub(crate) node: ComputedNode,
/// Describes the style including flexbox settings
pub(crate) style: Style,
pub(crate) style: Node,
/// The texture atlas image of the node
pub(crate) joystick: VirtualJoystickNode<S>,
/// The transform of the node
Expand All @@ -38,12 +38,12 @@ impl<S: VirtualJoystickID> VirtualJoystickBundle<S> {
}
}

pub fn set_node(mut self, node: Node) -> Self {
pub fn set_node(mut self, node: ComputedNode) -> Self {
self.node = node;
self
}

pub fn set_style(mut self, style: Style) -> Self {
pub fn set_style(mut self, style: Node) -> Self {
self.style = style;
self
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ impl<
{
}

impl<S: VirtualJoystickID + GetTypeRegistration> Plugin for VirtualJoystickPlugin<S> {
impl<S: VirtualJoystickID + GetTypeRegistration + bevy::reflect::Typed> Plugin
for VirtualJoystickPlugin<S>
{
fn build(&self, app: &mut bevy::prelude::App) {
app.register_type::<VirtualJoystickNode<S>>()
.register_type::<VirtualJoystickEventType>()
Expand Down
29 changes: 19 additions & 10 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use bevy::{
},
hierarchy::Children,
input::{mouse::MouseButton, touch::Touches, ButtonInput},
math::{Rect, Vec2},
math::{Rect, Vec2, Vec3Swizzles},
transform::components::GlobalTransform,
ui::{Node, PositionType, Style, Val},
ui::{ComputedNode, Node, PositionType, Val},
window::{PrimaryWindow, Window},
};

Expand Down Expand Up @@ -41,7 +41,7 @@ pub fn update_missing_state<S: VirtualJoystickID>(world: &mut World) {
}

pub fn update_input(
mut joysticks: Query<(&Node, &GlobalTransform, &mut VirtualJoystickState)>,
mut joysticks: Query<(&ComputedNode, &GlobalTransform, &mut VirtualJoystickState)>,
mouse_buttons: Res<ButtonInput<MouseButton>>,
touches: Res<Touches>,
q_windows: Query<&Window, With<PrimaryWindow>>,
Expand All @@ -52,7 +52,10 @@ pub fn update_input(
touch_state.just_pressed = false;
}
if joystick_state.touch_state.is_none() {
let rect = joystick_node.logical_rect(joystick_global_transform);
let rect = Rect::from_center_size(
joystick_global_transform.translation().xy(),
joystick_node.size(),
);
for touch in touches.iter() {
if rect.contains(touch.position()) {
joystick_state.touch_state = Some(TouchState {
Expand Down Expand Up @@ -265,11 +268,11 @@ pub fn update_fire_events<S: VirtualJoystickID>(
pub fn update_ui(
joysticks: Query<(&VirtualJoystickState, &Children)>,
mut joystick_bases: Query<
(&mut Style, &Node, &GlobalTransform),
(&mut Node, &ComputedNode, &GlobalTransform),
With<VirtualJoystickUIBackground>,
>,
mut joystick_knobs: Query<
(&mut Style, &Node, &GlobalTransform),
(&mut Node, &ComputedNode, &GlobalTransform),
(
With<VirtualJoystickUIKnob>,
Without<VirtualJoystickUIBackground>,
Expand All @@ -285,8 +288,12 @@ pub fn update_ui(
joystick_base_style.position_type = PositionType::Absolute;
joystick_base_style.left = Val::Px(joystick_state.base_offset.x);
joystick_base_style.top = Val::Px(joystick_state.base_offset.y);
joystick_base_rect =
Some(joystick_base_node.logical_rect(joystick_base_global_transform));

let rect = Rect::from_center_size(
joystick_base_global_transform.translation().xy(),
joystick_base_node.size(),
);
joystick_base_rect = Some(rect);
}
}
if joystick_base_rect.is_none() {
Expand All @@ -298,8 +305,10 @@ pub fn update_ui(
if joystick_knobs.contains(*child) {
let (mut joystick_knob_style, joystick_knob_node, joystick_knob_global_transform) =
joystick_knobs.get_mut(*child).unwrap();
let joystick_knob_rect =
joystick_knob_node.logical_rect(joystick_knob_global_transform);
let joystick_knob_rect = Rect::from_center_size(
joystick_knob_global_transform.translation().xy(),
joystick_knob_node.size(),
);
let joystick_knob_half_size = joystick_knob_rect.half_size();
joystick_knob_style.position_type = PositionType::Absolute;
joystick_knob_style.left = Val::Px(
Expand Down
48 changes: 21 additions & 27 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub fn create_joystick<I: VirtualJoystickID>(
interactable_area_color: Option<Color>,
knob_size: Vec2,
background_size: Vec2,
joystick_node_style: Style,
joystick_node_style: Node,
behavior: impl VirtualJoystickBehavior,
action: impl VirtualJoystickAction<I>,
) {
Expand All @@ -123,40 +123,34 @@ pub fn create_joystick<I: VirtualJoystickID>(
let spawn = spawn.with_children(|parent| {
parent.spawn((
VirtualJoystickUIKnob,
ImageBundle {
image: UiImage {
color: knob_color.unwrap_or(Color::WHITE.with_alpha(1.0)),
texture: knob_img,
..default()
},
style: Style {
position_type: PositionType::Absolute,
width: Val::Px(knob_size.x),
height: Val::Px(knob_size.y),
..default()
},
z_index: ZIndex::Local(1),
ImageNode {
color: knob_color.unwrap_or(Color::WHITE.with_alpha(1.0)),
image: knob_img,
..default()
},
Node {
position_type: PositionType::Absolute,
width: Val::Px(knob_size.x),
height: Val::Px(knob_size.y),
..default()
},
ZIndex(1),
));

parent.spawn((
VirtualJoystickUIBackground,
ImageBundle {
image: UiImage {
color: background_color.unwrap_or(Color::WHITE.with_alpha(1.0)),
texture: background_img,
..default()
},
style: Style {
position_type: PositionType::Absolute,
width: Val::Px(background_size.x),
height: Val::Px(background_size.y),
..default()
},
z_index: ZIndex::Local(0),
ImageNode {
color: background_color.unwrap_or(Color::WHITE.with_alpha(1.0)),
image: background_img,
..default()
},
Node {
position_type: PositionType::Absolute,
width: Val::Px(background_size.x),
height: Val::Px(background_size.y),
..default()
},
ZIndex(0),
));
});

Expand Down

0 comments on commit 03a73d1

Please sign in to comment.