Skip to content

Commit

Permalink
refactor: move into ui module
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed Nov 18, 2023
1 parent d5377e8 commit 4917b14
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 114 deletions.
4 changes: 2 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S> {
Expand Down
5 changes: 5 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod bundles;
mod systems;

pub use bundles::*;
pub use systems::*;
107 changes: 107 additions & 0 deletions src/ui/bundles.rs
Original file line number Diff line number Diff line change
@@ -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<S: VirtualJoystickID> {
/// 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<S>,
/// 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<S: VirtualJoystickID> {
/// 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<u64>,
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<S: VirtualJoystickID> VirtualJoystickBundle<S> {
pub fn new(joystick: VirtualJoystickNode<S>) -> 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
}
}
Empty file added src/ui/components.rs
Empty file.
115 changes: 6 additions & 109 deletions src/joystick.rs → src/ui/systems.rs
Original file line number Diff line number Diff line change
@@ -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<S: VirtualJoystickID> {
/// 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<S>,
/// 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<S: VirtualJoystickID> {
/// 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<u64>,
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<S: VirtualJoystickID> VirtualJoystickBundle<S> {
pub fn new(joystick: VirtualJoystickNode<S>) -> 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<S: VirtualJoystickID>(
Expand Down

0 comments on commit 4917b14

Please sign in to comment.