diff --git a/Cargo.toml b/Cargo.toml index 34cce68..b033b95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/input.rs b/src/input.rs index 281c951..7ba796b 100644 --- a/src/input.rs +++ b/src/input.rs @@ -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 }, @@ -24,7 +25,7 @@ fn is_some_and(opt: Option, cb: impl FnOnce(T) -> bool) -> bool { false } -pub fn update_input( +pub fn update_input( mut input_events: EventReader, mut send_values: EventWriter>, mut joysticks: Query<(&VirtualJoystickNode, &mut VirtualJoystickKnob)>, @@ -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 }); } } @@ -137,33 +138,33 @@ pub fn update_joystick( pub fn update_joystick_by_mouse( mouse_button_input: Res>, - mut cursor_evr: EventReader, + mut mousebtn_evr: EventReader, mut send_values: EventWriter, - windows: Query<&Window>, + windows: Query<&Window, With>, ) { let window = windows.single(); - let mouse_positions = cursor_evr - .iter() - .map(|e| Vec2::new(e.position.x, window.height() - e.position.y)) - .collect::>(); + 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 }); } } diff --git a/src/joystick.rs b/src/joystick.rs index a7e2ecb..febf4a3 100644 --- a/src/joystick.rs +++ b/src/joystick.rs @@ -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")] @@ -43,14 +43,14 @@ impl From for TintColor { #[cfg_attr(feature = "inspect", reflect(InspectorOptions))] pub struct VirtualJoystickInteractionArea; -#[derive(Bundle, Clone, Debug, Default)] -pub struct VirtualJoystickBundle { +#[derive(Bundle, Debug, Default)] +pub struct VirtualJoystickBundle { /// 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 @@ -73,7 +73,7 @@ pub struct VirtualJoystickBundle { +pub struct VirtualJoystickNode { /// Identifier of joystick pub id: S, /// Image for background or border image on joystick @@ -102,7 +102,7 @@ pub struct VirtualJoystickKnob { pub interactable_zone_rect: Rect, } -impl VirtualJoystickBundle { +impl VirtualJoystickBundle { pub fn new(joystick: VirtualJoystickNode) -> Self { Self { joystick, @@ -157,7 +157,7 @@ impl VirtualJoystic } #[allow(clippy::type_complexity)] -pub fn extract_joystick_node( +pub fn extract_joystick_node( mut extracted_uinodes: ResMut, images: Extract>>, ui_stack: Extract>, diff --git a/src/lib.rs b/src/lib.rs index 2e3dae8..fb77df7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -23,7 +23,7 @@ pub struct VirtualJoystickPlugin { _marker: PhantomData, } -impl Plugin +impl Plugin for VirtualJoystickPlugin { fn build(&self, app: &mut bevy::prelude::App) { @@ -36,32 +36,34 @@ impl Plugin .register_type::() .add_event::>() .add_event::() - .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::.in_base_set(CoreSet::PreUpdateFlush), - joystick_image_node_system:: - .before(UiSystem::Flex) - .in_base_set(CoreSet::PostUpdate), - )); + .add_systems( + PreUpdate, + update_joystick.before(update_input::).run_if(not(run_if_pc)) + ) + .add_systems( + PreUpdate, + update_joystick_by_mouse.before(update_input::).run_if(run_if_pc)) + .add_systems( + PreUpdate, + update_input:: + ) + .add_systems( + PostUpdate, + joystick_image_node_system::.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:: - .after(RenderUiSystem::ExtractNode) - .in_schedule(ExtractSchedule), + render_app.add_systems( + ExtractSchedule, + extract_joystick_node::.after(RenderUiSystem::ExtractNode), ); } } -fn joystick_image_node_system( +fn joystick_image_node_system( interaction_area: Query<(&Node, With)>, mut joystick: Query<( &Transform, @@ -93,7 +95,8 @@ pub enum VirtualJoystickEventType { Up, } -pub struct VirtualJoystickEvent { +#[derive(Event)] +pub struct VirtualJoystickEvent { id: S, event: VirtualJoystickEventType, value: Vec2, @@ -101,7 +104,7 @@ pub struct VirtualJoystickEvent VirtualJoystickEvent { +impl VirtualJoystickEvent { /// Get ID of joystick throw event pub fn id(&self) -> S { self.id.clone()