Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
clinuxrulz committed Mar 23, 2024
1 parent 1bc84af commit 4fe2e10
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 97 deletions.
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
..default()
},
(JoystickFloating),
NoAction
NoAction,
);
}

Expand Down
26 changes: 18 additions & 8 deletions examples/tint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Player(pub f32);

struct TintAction {
down: Color,
up: Color
up: Color,
}

impl VirtualJoystickAction<String> for TintAction {
Expand All @@ -32,17 +32,22 @@ impl VirtualJoystickAction<String> for TintAction {
) {
let mut child_entities: Vec<Entity> = Vec::new();
{
let Some(children) = world.get::<Children>(entity) else { return; };
let Some(children) = world.get::<Children>(entity) else {
return;
};
for &child in children.iter() {
child_entities.push(child);
}
}
for &child in &child_entities {
let is_base_or_knob: bool;
{
is_base_or_knob = world.get::<VirtualJoystickUIBackground>(entity).is_some() || world.get::<VirtualJoystickUIKnob>(entity).is_some();
is_base_or_knob = world.get::<VirtualJoystickUIBackground>(entity).is_some()
|| world.get::<VirtualJoystickUIKnob>(entity).is_some();
}
let Some(mut bg_color) = world.get_mut::<BackgroundColor>(child) else { continue; };
let Some(mut bg_color) = world.get_mut::<BackgroundColor>(child) else {
continue;
};
bg_color.0 = self.down;
}
}
Expand All @@ -56,17 +61,22 @@ impl VirtualJoystickAction<String> for TintAction {
) {
let mut child_entities: Vec<Entity> = Vec::new();
{
let Some(children) = world.get::<Children>(entity) else { return; };
let Some(children) = world.get::<Children>(entity) else {
return;
};
for &child in children.iter() {
child_entities.push(child);
}
}
for &child in &child_entities {
let is_base_or_knob: bool;
{
is_base_or_knob = world.get::<VirtualJoystickUIBackground>(entity).is_some() || world.get::<VirtualJoystickUIKnob>(entity).is_some();
is_base_or_knob = world.get::<VirtualJoystickUIBackground>(entity).is_some()
|| world.get::<VirtualJoystickUIKnob>(entity).is_some();
}
let Some(mut bg_color) = world.get_mut::<BackgroundColor>(child) else { continue; };
let Some(mut bg_color) = world.get_mut::<BackgroundColor>(child) else {
continue;
};
bg_color.0 = self.up;
}
}
Expand Down Expand Up @@ -116,7 +126,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
TintAction {
down: Color::RED.with_a(1.0),
up: Color::GREEN.with_a(0.5),
}
},
);
}

Expand Down
9 changes: 1 addition & 8 deletions src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ pub trait VirtualJoystickAction<I>: Send + Sync + 'static {
_entity: Entity,
) {
}
fn on_drag(
&self,
_id: I,
_data: VirtualJoystickState,
_world: &mut World,
_entity: Entity,
) {
}
fn on_drag(&self, _id: I, _data: VirtualJoystickState, _world: &mut World, _entity: Entity) {}
fn on_end_drag(
&self,
_id: I,
Expand Down
133 changes: 100 additions & 33 deletions src/behavior.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use std::sync::Arc;

use bevy::{ecs::{all_tuples, entity::Entity, world::World}, hierarchy::Children, math::{Rect, Vec2}, reflect::Reflect, render::view::Visibility, transform::components::GlobalTransform, ui::Node};
use bevy::{
ecs::{all_tuples, entity::Entity, world::World},
hierarchy::Children,
math::{Rect, Vec2},
reflect::Reflect,
render::view::Visibility,
transform::components::GlobalTransform,
ui::Node,
};

use crate::{components::VirtualJoystickState, VirtualJoystickUIBackground};

Expand Down Expand Up @@ -70,7 +78,9 @@ pub struct JoystickDynamic;

impl VirtualJoystickBehavior for JoystickDeadZone {
fn update_at_constraint_stage(&self, world: &mut World, entity: Entity) {
let Some(mut joystick_state) = world.get_mut::<VirtualJoystickState>(entity) else { return; };
let Some(mut joystick_state) = world.get_mut::<VirtualJoystickState>(entity) else {
return;
};
let dead_zone = self.0;
if joystick_state.delta.x.abs() < dead_zone {
joystick_state.delta.x = 0.0;
Expand All @@ -83,24 +93,34 @@ impl VirtualJoystickBehavior for JoystickDeadZone {

impl VirtualJoystickBehavior for JoystickHorizontalOnly {
fn update_at_constraint_stage(&self, world: &mut World, entity: Entity) {
let Some(mut joystick_state) = world.get_mut::<VirtualJoystickState>(entity) else { return; };
let Some(mut joystick_state) = world.get_mut::<VirtualJoystickState>(entity) else {
return;
};
joystick_state.delta.y = 0.0;
}
}

impl VirtualJoystickBehavior for JoystickVerticalOnly {
fn update_at_constraint_stage(&self, world: &mut World, entity: Entity) {
let Some(mut joystick_state) = world.get_mut::<VirtualJoystickState>(entity) else { return; };
let Some(mut joystick_state) = world.get_mut::<VirtualJoystickState>(entity) else {
return;
};
joystick_state.delta.x = 0.0;
}
}

impl VirtualJoystickBehavior for JoystickInvisible {
fn update(&self, world: &mut World, entity: Entity) {
let joystick_state = world.get::<VirtualJoystickState>(entity).map(|x| x.clone());
let Some(joystick_state) = joystick_state else { return; };
let Some(mut joystick_visibility) = world.get_mut::<Visibility>(entity) else { return; };
if joystick_state.just_released || *joystick_visibility != Visibility::Hidden && joystick_state.touch_state.is_none() {
let Some(joystick_state) = joystick_state else {
return;
};
let Some(mut joystick_visibility) = world.get_mut::<Visibility>(entity) else {
return;
};
if joystick_state.just_released
|| *joystick_visibility != Visibility::Hidden && joystick_state.touch_state.is_none()
{
*joystick_visibility = Visibility::Hidden;
}
if let Some(touch_state) = &joystick_state.touch_state {
Expand All @@ -115,21 +135,34 @@ impl VirtualJoystickBehavior for JoystickFixed {
fn update_at_delta_stage(&self, world: &mut World, entity: Entity) {
let mut children_entities: Vec<Entity> = Vec::new();
{
let Some(children) = world.get::<Children>(entity) else { return; };
let Some(children) = world.get::<Children>(entity) else {
return;
};
for &child in children.iter() {
children_entities.push(child);
}
}
let mut joystick_base_rect: Option<Rect> = None;
for &child in &children_entities {
if world.get::<VirtualJoystickUIBackground>(child).is_none() { continue; }
let Some(joystick_base_node) = world.get::<Node>(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));
if world.get::<VirtualJoystickUIBackground>(child).is_none() {
continue;
}
let Some(joystick_base_node) = world.get::<Node>(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));
break;
}
let Some(joystick_base_rect) = joystick_base_rect else { return; };
let Some(mut joystick_state) = world.get_mut::<VirtualJoystickState>(entity) else { return; };
let Some(joystick_base_rect) = joystick_base_rect else {
return;
};
let Some(mut joystick_state) = world.get_mut::<VirtualJoystickState>(entity) else {
return;
};
joystick_state.base_offset = Vec2::ZERO;
let new_delta: Vec2;
if let Some(touch_state) = &joystick_state.touch_state {
Expand All @@ -149,21 +182,34 @@ impl VirtualJoystickBehavior for JoystickFloating {
fn update_at_delta_stage(&self, world: &mut World, entity: Entity) {
let mut children_entities: Vec<Entity> = Vec::new();
{
let Some(children) = world.get::<Children>(entity) else { return; };
let Some(children) = world.get::<Children>(entity) else {
return;
};
for &child in children.iter() {
children_entities.push(child);
}
}
let mut joystick_base_rect: Option<Rect> = None;
for &child in &children_entities {
if world.get::<VirtualJoystickUIBackground>(child).is_none() { continue; }
let Some(joystick_base_node) = world.get::<Node>(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));
if world.get::<VirtualJoystickUIBackground>(child).is_none() {
continue;
}
let Some(joystick_base_node) = world.get::<Node>(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));
break;
}
let Some(joystick_base_rect) = joystick_base_rect else { return; };
let Some(mut joystick_state) = world.get_mut::<VirtualJoystickState>(entity) else { return; };
let Some(joystick_base_rect) = joystick_base_rect else {
return;
};
let Some(mut joystick_state) = world.get_mut::<VirtualJoystickState>(entity) else {
return;
};
let base_offset: Vec2;
let mut assign_base_offset = false;
if let Some(touch_state) = &joystick_state.touch_state {
Expand Down Expand Up @@ -200,27 +246,44 @@ 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 { return; };
let Some(joystick_global_transform) = world.get::<GlobalTransform>(entity) else { return; };
let Some(joystick_node) = world.get::<Node>(entity) else {
return;
};
let Some(joystick_global_transform) = world.get::<GlobalTransform>(entity) else {
return;
};
joystick_rect = joystick_node.logical_rect(joystick_global_transform);
}
let mut children_entities: Vec<Entity> = Vec::new();
{
let Some(children) = world.get::<Children>(entity) else { return; };
let Some(children) = world.get::<Children>(entity) else {
return;
};
for &child in children.iter() {
children_entities.push(child);
}
}
let mut joystick_base_rect: Option<Rect> = None;
for &child in &children_entities {
if world.get::<VirtualJoystickUIBackground>(child).is_none() { continue; }
let Some(joystick_base_node) = world.get::<Node>(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));
if world.get::<VirtualJoystickUIBackground>(child).is_none() {
continue;
}
let Some(joystick_base_node) = world.get::<Node>(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));
break;
}
let Some(joystick_base_rect) = joystick_base_rect else { return; };
let Some(mut joystick_state) = world.get_mut::<VirtualJoystickState>(entity) else { return; };
let Some(joystick_base_rect) = joystick_base_rect else {
return;
};
let Some(mut joystick_state) = world.get_mut::<VirtualJoystickState>(entity) else {
return;
};
let joystick_base_rect_center = joystick_base_rect.center();
let joystick_base_rect_half_size = joystick_base_rect.half_size();
let base_offset: Vec2;
Expand All @@ -244,9 +307,13 @@ impl VirtualJoystickBehavior for JoystickDynamic {
let new_delta: Vec2;
let mut new_base_offset: Option<Vec2> = None;
if let Some(touch_state) = &joystick_state.touch_state {
let mut offset = touch_state.current - (joystick_rect.min + base_offset + joystick_base_rect.half_size());
if offset.length_squared() > joystick_base_rect_half_size.x * joystick_base_rect_half_size.x {
let adjustment = offset - offset * (joystick_base_rect_half_size.x / offset.length());
let mut offset = touch_state.current
- (joystick_rect.min + base_offset + joystick_base_rect.half_size());
if offset.length_squared()
> joystick_base_rect_half_size.x * joystick_base_rect_half_size.x
{
let adjustment =
offset - offset * (joystick_base_rect_half_size.x / offset.length());
offset += adjustment;
new_base_offset = Some(base_offset + adjustment);
}
Expand Down
17 changes: 13 additions & 4 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use bevy_inspector_egui::prelude::ReflectInspectorOptions;
#[cfg(feature = "inspect")]
use bevy_inspector_egui::InspectorOptions;

use crate::{action::NoAction, behavior::JoystickFloating, VirtualJoystickAction, VirtualJoystickBehavior, VirtualJoystickID};
use crate::{
action::NoAction, behavior::JoystickFloating, VirtualJoystickAction, VirtualJoystickBehavior,
VirtualJoystickID,
};

#[derive(Component, Copy, Clone, Debug, Default, Reflect)]
#[reflect(Component, Default)]
Expand All @@ -31,18 +34,24 @@ pub struct VirtualJoystickNode<S: VirtualJoystickID> {
#[reflect(ignore)]
pub behavior: Arc<dyn VirtualJoystickBehavior>,
#[reflect(ignore)]
pub action: Arc<dyn VirtualJoystickAction<S>>
pub action: Arc<dyn VirtualJoystickAction<S>>,
}

impl<S: VirtualJoystickID> Default for VirtualJoystickNode<S> {
fn default() -> Self {
Self { id: Default::default(), behavior: Arc::new(JoystickFloating), action: Arc::new(NoAction::default()), }
Self {
id: Default::default(),
behavior: Arc::new(JoystickFloating),
action: Arc::new(NoAction::default()),
}
}
}

impl<S: VirtualJoystickID> std::fmt::Debug for VirtualJoystickNode<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("VirtualJoystickNode").field("id", &self.id).finish()
f.debug_struct("VirtualJoystickNode")
.field("id", &self.id)
.finish()
}
}

Expand Down
Loading

0 comments on commit 4fe2e10

Please sign in to comment.