From 4917b14eba6dd0ecd06cf6160a517a313dcb5acd Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Sat, 18 Nov 2023 15:49:00 -0400 Subject: [PATCH] refactor: move into ui module --- src/input.rs | 4 +- src/lib.rs | 6 +- src/ui.rs | 5 ++ src/ui/bundles.rs | 107 +++++++++++++++++++++++++++ src/ui/components.rs | 0 src/{joystick.rs => ui/systems.rs} | 115 ++--------------------------- 6 files changed, 123 insertions(+), 114 deletions(-) create mode 100644 src/ui.rs create mode 100644 src/ui/bundles.rs create mode 100644 src/ui/components.rs rename src/{joystick.rs => ui/systems.rs} (50%) diff --git a/src/input.rs b/src/input.rs index 2ba11de..51af182 100644 --- a/src/input.rs +++ b/src/input.rs @@ -6,8 +6,8 @@ use bevy::{ use crate::VirtualJoystickID; use crate::{ - joystick::VirtualJoystickData, VirtualJoystickEvent, VirtualJoystickEventType, - VirtualJoystickNode, VirtualJoystickType, + ui::VirtualJoystickData, VirtualJoystickEvent, VirtualJoystickEventType, VirtualJoystickNode, + VirtualJoystickType, }; #[derive(Event)] diff --git a/src/lib.rs b/src/lib.rs index 1f2e44b..5eb1ff3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,18 +9,18 @@ use bevy::{ mod behaviour; mod input; -mod joystick; +mod ui; mod utils; pub use behaviour::{VirtualJoystickAxis, VirtualJoystickType}; use input::{run_if_pc, update_input, update_joystick, update_joystick_by_mouse, InputEvent}; -pub use joystick::{ +pub use ui::{ VirtualJoystickBundle, VirtualJoystickInteractionArea, VirtualJoystickNode, VirtualJoystickUIBackground, VirtualJoystickUIKnob, }; pub use utils::create_joystick; -use joystick::{extract_joystick_node, VirtualJoystickData}; +use ui::{extract_joystick_node, VirtualJoystickData}; #[derive(Default)] pub struct VirtualJoystickPlugin { diff --git a/src/ui.rs b/src/ui.rs new file mode 100644 index 0000000..f88bd26 --- /dev/null +++ b/src/ui.rs @@ -0,0 +1,5 @@ +mod bundles; +mod systems; + +pub use bundles::*; +pub use systems::*; diff --git a/src/ui/bundles.rs b/src/ui/bundles.rs new file mode 100644 index 0000000..520b4ad --- /dev/null +++ b/src/ui/bundles.rs @@ -0,0 +1,107 @@ +use bevy::{prelude::*, ui::RelativeCursorPosition}; + +#[cfg(feature = "inspect")] +use bevy_inspector_egui::prelude::*; + +use crate::{VirtualJoystickAxis, VirtualJoystickID, VirtualJoystickType}; + +#[derive(Component, Clone, Debug, Default, Reflect)] +#[reflect(Component, Default)] +#[cfg_attr(feature = "inspect", derive(InspectorOptions))] +#[cfg_attr(feature = "inspect", reflect(InspectorOptions))] +pub struct VirtualJoystickInteractionArea; + +#[derive(Component, Copy, Clone, Debug, Default, Reflect)] +#[reflect(Component, Default)] +#[cfg_attr(feature = "inspect", derive(InspectorOptions))] +#[cfg_attr(feature = "inspect", reflect(InspectorOptions))] +pub struct VirtualJoystickUIKnob; + +#[derive(Component, Copy, Clone, Debug, Default, Reflect)] +#[reflect(Component, Default)] +#[cfg_attr(feature = "inspect", derive(InspectorOptions))] +#[cfg_attr(feature = "inspect", reflect(InspectorOptions))] +pub struct VirtualJoystickUIBackground; + +#[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 texture atlas image of the node + pub(crate) joystick: VirtualJoystickNode, + /// The transform of the node + pub(crate) transform: Transform, + /// The global transform of the node + pub(crate) global_transform: GlobalTransform, + /// Describes the visibility properties of the node + pub visibility: Visibility, + /// Inherited visibility of an entity. + pub inherited_visibility: InheritedVisibility, + /// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering + pub view_visibility: ViewVisibility, + /// Indicates the depth at which the node should appear in the UI + pub(crate) z_index: ZIndex, + pub(crate) knob_data: VirtualJoystickData, + pub(crate) cursor_pos: RelativeCursorPosition, +} + +#[derive(Component, Clone, Debug, Default, Reflect)] +#[reflect(Component, Default)] +pub struct VirtualJoystickNode { + /// Identifier of joystick + pub id: S, + /// Zone to ignore movement + pub dead_zone: f32, + /// Define Axis for this joystick + pub axis: VirtualJoystickAxis, + /// Define the behaviour of joystick + pub behaviour: VirtualJoystickType, +} + +#[derive(Component, Clone, Debug, Default, Reflect)] +#[reflect(Component, Default)] +pub struct VirtualJoystickData { + pub id_drag: Option, + pub dead_zone: f32, + pub base_pos: Vec2, + pub start_pos: Vec2, + pub current_pos: Vec2, + pub delta: Vec2, + pub interactable_zone_rect: Rect, +} + +impl VirtualJoystickBundle { + pub fn new(joystick: VirtualJoystickNode) -> Self { + Self { + joystick, + ..default() + } + } + + pub fn set_node(mut self, node: Node) -> Self { + self.node = node; + self + } + + pub fn set_style(mut self, style: Style) -> Self { + self.style = style; + self + } + + pub fn set_transform(mut self, transform: Transform) -> Self { + self.transform = transform; + self + } + + pub fn set_global_transform(mut self, global_transform: GlobalTransform) -> Self { + self.global_transform = global_transform; + self + } + + pub fn set_z_index(mut self, z_index: ZIndex) -> Self { + self.z_index = z_index; + self + } +} diff --git a/src/ui/components.rs b/src/ui/components.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/joystick.rs b/src/ui/systems.rs similarity index 50% rename from src/joystick.rs rename to src/ui/systems.rs index 6d9476c..8a57153 100644 --- a/src/joystick.rs +++ b/src/ui/systems.rs @@ -1,114 +1,11 @@ -use bevy::{ - prelude::*, - render::Extract, - ui::{ExtractedUiNodes, RelativeCursorPosition}, -}; - -#[cfg(feature = "inspect")] -use bevy_inspector_egui::prelude::*; - -use crate::{VirtualJoystickAxis, VirtualJoystickID, VirtualJoystickType}; - -#[derive(Component, Clone, Debug, Default, Reflect)] -#[reflect(Component, Default)] -#[cfg_attr(feature = "inspect", derive(InspectorOptions))] -#[cfg_attr(feature = "inspect", reflect(InspectorOptions))] -pub struct VirtualJoystickInteractionArea; +use bevy::{prelude::*, render::Extract, ui::ExtractedUiNodes}; -#[derive(Component, Copy, Clone, Debug, Default, Reflect)] -#[reflect(Component, Default)] -#[cfg_attr(feature = "inspect", derive(InspectorOptions))] -#[cfg_attr(feature = "inspect", reflect(InspectorOptions))] -pub struct VirtualJoystickUIKnob; - -#[derive(Component, Copy, Clone, Debug, Default, Reflect)] -#[reflect(Component, Default)] -#[cfg_attr(feature = "inspect", derive(InspectorOptions))] -#[cfg_attr(feature = "inspect", reflect(InspectorOptions))] -pub struct VirtualJoystickUIBackground; - -#[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 texture atlas image of the node - pub(crate) joystick: VirtualJoystickNode, - /// The transform of the node - pub(crate) transform: Transform, - /// The global transform of the node - pub(crate) global_transform: GlobalTransform, - /// Describes the visibility properties of the node - pub visibility: Visibility, - /// Inherited visibility of an entity. - pub inherited_visibility: InheritedVisibility, - /// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering - pub view_visibility: ViewVisibility, - /// Indicates the depth at which the node should appear in the UI - pub(crate) z_index: ZIndex, - pub(crate) knob_data: VirtualJoystickData, - pub(crate) cursor_pos: RelativeCursorPosition, -} - -#[derive(Component, Clone, Debug, Default, Reflect)] -#[reflect(Component, Default)] -pub struct VirtualJoystickNode { - /// Identifier of joystick - pub id: S, - /// Zone to ignore movement - pub dead_zone: f32, - /// Define Axis for this joystick - pub axis: VirtualJoystickAxis, - /// Define the behaviour of joystick - pub behaviour: VirtualJoystickType, -} - -#[derive(Component, Clone, Debug, Default, Reflect)] -#[reflect(Component, Default)] -pub struct VirtualJoystickData { - pub id_drag: Option, - pub dead_zone: f32, - pub base_pos: Vec2, - pub start_pos: Vec2, - pub current_pos: Vec2, - pub delta: Vec2, - pub interactable_zone_rect: Rect, -} - -impl VirtualJoystickBundle { - pub fn new(joystick: VirtualJoystickNode) -> Self { - Self { - joystick, - ..default() - } - } - - pub fn set_node(mut self, node: Node) -> Self { - self.node = node; - self - } - - pub fn set_style(mut self, style: Style) -> Self { - self.style = style; - self - } - - pub fn set_transform(mut self, transform: Transform) -> Self { - self.transform = transform; - self - } - - pub fn set_global_transform(mut self, global_transform: GlobalTransform) -> Self { - self.global_transform = global_transform; - self - } +use crate::{ + VirtualJoystickID, VirtualJoystickNode, VirtualJoystickType, VirtualJoystickUIBackground, + VirtualJoystickUIKnob, +}; - pub fn set_z_index(mut self, z_index: ZIndex) -> Self { - self.z_index = z_index; - self - } -} +use super::VirtualJoystickData; #[allow(clippy::type_complexity)] pub fn extract_joystick_node(