Skip to content

Commit

Permalink
Update for bevy 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
ostwilkens committed Jul 28, 2023
1 parent eee7fa4 commit 0df845c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 53 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ inspect = ["bevy-inspector-egui"]
serialize = ["serde"]

[dependencies]
bevy = { version = "0.10.1" }
bevy-inspector-egui = { version = "0.18", optional = true }
bevy = { version = "0.11" }
bevy-inspector-egui = { version = "0.19", optional = true }
serde = { version = "^1", features = ["derive"], optional = true }
47 changes: 24 additions & 23 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::hash::Hash;

use bevy::{input::touch::TouchPhase, prelude::*};
use bevy::{input::{touch::TouchPhase, mouse::MouseButtonInput, ButtonState}, prelude::*, reflect::TypePath, window::PrimaryWindow};

use crate::{
joystick::VirtualJoystickKnob, VirtualJoystickEvent, VirtualJoystickEventType,
VirtualJoystickNode, VirtualJoystickType,
};

#[derive(Event)]
pub enum InputEvent {
StartDrag { id: u64, pos: Vec2 },
Dragging { id: u64, pos: Vec2 },
Expand All @@ -24,7 +25,7 @@ fn is_some_and<T>(opt: Option<T>, cb: impl FnOnce(T) -> bool) -> bool {
false
}

pub fn update_input<S: Hash + Sync + Send + Clone + Default + Reflect + 'static>(
pub fn update_input<S: Hash + Sync + Send + Clone + Default + Reflect + TypePath + FromReflect + 'static>(
mut input_events: EventReader<InputEvent>,
mut send_values: EventWriter<VirtualJoystickEvent<S>>,
mut joysticks: Query<(&VirtualJoystickNode<S>, &mut VirtualJoystickKnob)>,
Expand Down Expand Up @@ -128,7 +129,7 @@ pub fn update_joystick(
send_values.send(InputEvent::Dragging { id: *id, pos: *pos });
}
// End drag
TouchPhase::Ended | TouchPhase::Cancelled => {
TouchPhase::Ended | TouchPhase::Canceled => {
send_values.send(InputEvent::EndDrag { id: *id, pos: *pos });
}
}
Expand All @@ -137,33 +138,33 @@ pub fn update_joystick(

pub fn update_joystick_by_mouse(
mouse_button_input: Res<Input<MouseButton>>,
mut cursor_evr: EventReader<CursorMoved>,
mut mousebtn_evr: EventReader<MouseButtonInput>,
mut send_values: EventWriter<InputEvent>,
windows: Query<&Window>,
windows: Query<&Window, With<PrimaryWindow>>,
) {
let window = windows.single();
let mouse_positions = cursor_evr
.iter()
.map(|e| Vec2::new(e.position.x, window.height() - e.position.y))
.collect::<Vec<Vec2>>();
let pos = window.cursor_position().unwrap_or(Vec2::ZERO);

// End drag
if mouse_button_input.just_released(MouseButton::Left) {
send_values.send(InputEvent::EndDrag {
id: 0,
pos: Vec2::ZERO,
});
}
for mousebtn in mousebtn_evr.iter() {
// End drag
if mousebtn.button == MouseButton::Left && mousebtn.state == ButtonState::Released {
send_values.send(InputEvent::EndDrag {
id: 0,
pos: pos,
});
}

for pos in &mouse_positions {
// Start drag
if mouse_button_input.just_pressed(MouseButton::Left) {
send_values.send(InputEvent::StartDrag { id: 0, pos: *pos });
if mousebtn.button == MouseButton::Left && mousebtn.state == ButtonState::Pressed {
send_values.send(InputEvent::StartDrag {
id: 0,
pos: pos,
});
}
}

// Dragging
if mouse_button_input.pressed(MouseButton::Left) {
send_values.send(InputEvent::Dragging { id: 0, pos: *pos });
}
// Dragging
if mouse_button_input.pressed(MouseButton::Left) {
send_values.send(InputEvent::Dragging { id: 0, pos: pos });
}
}
14 changes: 7 additions & 7 deletions src/joystick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::Hash;
use bevy::{
prelude::*,
render::Extract,
ui::{ExtractedUiNode, ExtractedUiNodes, FocusPolicy, RelativeCursorPosition, UiStack},
ui::{ExtractedUiNode, ExtractedUiNodes, FocusPolicy, RelativeCursorPosition, UiStack, widget::UiImageSize, ContentSize},
};

#[cfg(feature = "inspect")]
Expand Down Expand Up @@ -43,14 +43,14 @@ impl From<Color> for TintColor {
#[cfg_attr(feature = "inspect", reflect(InspectorOptions))]
pub struct VirtualJoystickInteractionArea;

#[derive(Bundle, Clone, Debug, Default)]
pub struct VirtualJoystickBundle<S: Hash + Sync + Send + Clone + Default + Reflect + 'static> {
#[derive(Bundle, Debug, Default)]
pub struct VirtualJoystickBundle<S: Hash + Sync + Send + Clone + Default + Reflect + FromReflect + 'static> {
/// Describes the size of the node
pub(crate) node: Node,
/// Describes the style including flexbox settings
pub(crate) style: Style,
/// The calculated size based on the given image
pub(crate) calculated_size: CalculatedSize,
pub(crate) calculated_size: ContentSize,
/// The tint color of the image
pub(crate) color: TintColor,
/// The texture atlas image of the node
Expand All @@ -73,7 +73,7 @@ pub struct VirtualJoystickBundle<S: Hash + Sync + Send + Clone + Default + Refle

#[derive(Component, Clone, Debug, Default, Reflect)]
#[reflect(Component, Default)]
pub struct VirtualJoystickNode<S: Hash + Sync + Send + Clone + Default + Reflect + 'static> {
pub struct VirtualJoystickNode<S: Hash + Sync + Send + Clone + Default + Reflect + FromReflect + 'static> {
/// Identifier of joystick
pub id: S,
/// Image for background or border image on joystick
Expand Down Expand Up @@ -102,7 +102,7 @@ pub struct VirtualJoystickKnob {
pub interactable_zone_rect: Rect,
}

impl<S: Hash + Sync + Send + Clone + Default + Reflect + 'static> VirtualJoystickBundle<S> {
impl<S: Hash + Sync + Send + Clone + Default + Reflect + FromReflect + 'static> VirtualJoystickBundle<S> {
pub fn new(joystick: VirtualJoystickNode<S>) -> Self {
Self {
joystick,
Expand Down Expand Up @@ -157,7 +157,7 @@ impl<S: Hash + Sync + Send + Clone + Default + Reflect + 'static> VirtualJoystic
}

#[allow(clippy::type_complexity)]
pub fn extract_joystick_node<S: Hash + Sync + Send + Clone + Default + Reflect + 'static>(
pub fn extract_joystick_node<S: Hash + Sync + Send + Clone + Default + Reflect + FromReflect + 'static>(
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
images: Extract<Res<Assets<Image>>>,
ui_stack: Extract<Res<UiStack>>,
Expand Down
45 changes: 24 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{hash::Hash, marker::PhantomData};
use bevy::{
prelude::*,
render::RenderApp,
ui::{RenderUiSystem, UiSystem},
ui::{RenderUiSystem, UiSystem}, reflect::TypePath,
};

mod behaviour;
Expand All @@ -23,7 +23,7 @@ pub struct VirtualJoystickPlugin<S> {
_marker: PhantomData<S>,
}

impl<S: Hash + Sync + Send + Clone + Default + Reflect + 'static> Plugin
impl<S: Hash + Sync + Send + Clone + Default + Reflect + TypePath + FromReflect + 'static> Plugin
for VirtualJoystickPlugin<S>
{
fn build(&self, app: &mut bevy::prelude::App) {
Expand All @@ -36,32 +36,34 @@ impl<S: Hash + Sync + Send + Clone + Default + Reflect + 'static> Plugin
.register_type::<VirtualJoystickEventType>()
.add_event::<VirtualJoystickEvent<S>>()
.add_event::<InputEvent>()
.add_systems((
update_joystick
.run_if(not(run_if_pc))
.in_base_set(CoreSet::PreUpdate),
update_joystick_by_mouse
.run_if(run_if_pc)
.in_base_set(CoreSet::PreUpdate),
update_input::<S>.in_base_set(CoreSet::PreUpdateFlush),
joystick_image_node_system::<S>
.before(UiSystem::Flex)
.in_base_set(CoreSet::PostUpdate),
));
.add_systems(
PreUpdate,
update_joystick.before(update_input::<S>).run_if(not(run_if_pc))
)
.add_systems(
PreUpdate,
update_joystick_by_mouse.before(update_input::<S>).run_if(run_if_pc))
.add_systems(
PreUpdate,
update_input::<S>
)
.add_systems(
PostUpdate,
joystick_image_node_system::<S>.before(UiSystem::Layout)
);

let render_app = match app.get_sub_app_mut(RenderApp) {
Ok(render_app) => render_app,
Err(_) => return,
};
render_app.add_system(
extract_joystick_node::<S>
.after(RenderUiSystem::ExtractNode)
.in_schedule(ExtractSchedule),
render_app.add_systems(
ExtractSchedule,
extract_joystick_node::<S>.after(RenderUiSystem::ExtractNode),
);
}
}

fn joystick_image_node_system<S: Hash + Sync + Send + Clone + Default + Reflect + 'static>(
fn joystick_image_node_system<S: Hash + Sync + Send + Clone + Default + Reflect + FromReflect + 'static>(
interaction_area: Query<(&Node, With<VirtualJoystickInteractionArea>)>,
mut joystick: Query<(
&Transform,
Expand Down Expand Up @@ -93,15 +95,16 @@ pub enum VirtualJoystickEventType {
Up,
}

pub struct VirtualJoystickEvent<S: Hash + Sync + Send + Clone + Default + Reflect + 'static> {
#[derive(Event)]
pub struct VirtualJoystickEvent<S: Hash + Sync + Send + Clone + Default + Reflect + 'static + TypePath> {
id: S,
event: VirtualJoystickEventType,
value: Vec2,
delta: Vec2,
axis: VirtualJoystickAxis,
}

impl<S: Hash + Sync + Send + Clone + Default + Reflect + 'static> VirtualJoystickEvent<S> {
impl<S: Hash + Sync + Send + Clone + Default + Reflect + TypePath + 'static> VirtualJoystickEvent<S> {
/// Get ID of joystick throw event
pub fn id(&self) -> S {
self.id.clone()
Expand Down

0 comments on commit 0df845c

Please sign in to comment.