Skip to content

Commit

Permalink
feat: add helper function for creating joystick
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed Nov 18, 2023
1 parent 3b03e9d commit 1caa245
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ use bevy::{
mod behaviour;
mod input;
mod joystick;
mod utils;

pub use behaviour::{VirtualJoystickAxis, VirtualJoystickType};
use input::{run_if_pc, update_input, update_joystick, update_joystick_by_mouse, InputEvent};
pub use joystick::{
VirtualJoystickBundle, VirtualJoystickInteractionArea, VirtualJoystickNode,
VirtualJoystickUIBackground, VirtualJoystickUIKnob,
};
pub use utils::create_joystick;

use joystick::{extract_joystick_node, VirtualJoystickData};

Expand Down
147 changes: 147 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
use bevy::prelude::*;

use crate::{
VirtualJoystickBundle, VirtualJoystickID, VirtualJoystickInteractionArea, VirtualJoystickNode,
VirtualJoystickUIBackground, VirtualJoystickUIKnob,
};

/// This function is a simple helper to create a joystick
/// Entity with all needed without complexity
///
/// * `cmd`: bevy Commands, this required to spawn entity
/// * `knob_img`: Handle image for joystick knob
/// * `background_img`: Handle image for joystick border
/// * `knob_color`: Color for joystick knob
/// * `background_color`: Color for joystick border
/// * `interactable_area_color`: Color for interactable zone
/// * `knob_size`: Size for knob ui
/// * `background_size`: Size for joystick border ui
/// * `joystick_node`: [`JoystickNode`] struct
/// * `joystick_node_style`: bevy Style for joystick
///
/// Usage:
/// ```no_run
/// create_joystick(
/// cmd,
/// asset_server.load("Knob.png"),
/// asset_server.load("Outline.png"),
/// None,
/// None,
/// Some(Color::ORANGE_RED.with_a(0.3)),
/// Vec2::new(75., 75.),
/// Vec2::new(150., 150.),
/// VirtualJoystickNode {
/// dead_zone: 0.,
/// id: "UniqueJoystick".to_string(),
/// axis: VirtualJoystickAxis::Both,
/// behaviour: VirtualJoystickType::Floating,
/// },
/// Style {
/// width: Val::Px(150.),
/// height: Val::Px(150.),
/// position_type: PositionType::Absolute,
/// left: Val::Percent(50.),
/// bottom: Val::Percent(15.),
/// ..default()
/// },
/// );
/// ```
///
/// if you not want use this function helper, you need do that
/// ```no_run
/// cmd.spawn((
/// VirtualJoystickBundle::new(VirtualJoystickNode {
/// dead_zone: 0.,
/// id: "UniqueJoystick".to_string(),
/// axis: VirtualJoystickAxis::Both,
/// behaviour: VirtualJoystickType::Floating,
/// })
/// .set_style(Style {
/// width: Val::Px(150.),
/// height: Val::Px(150.),
/// position_type: PositionType::Absolute,
/// left: Val::Percent(50.),
/// bottom: Val::Percent(15.),
/// ..default()
/// }),
/// BackgroundColor(Color::ORANGE_RED.with_a(0.3)),
/// ))
/// .insert(VirtualJoystickInteractionArea)
/// .with_children(|parent| {
/// parent.spawn((
/// VirtualJoystickUIKnob,
/// ImageBundle {
/// image: asset_server.load("Knob.png").into(),
/// style: Style {
/// width: Val::Px(75.),
/// height: Val::Px(75.),
/// ..default()
/// },
/// background_color: Color::WHITE.into(),
/// ..default()
/// },
/// ));
/// parent.spawn((
/// VirtualJoystickUIBackground,
/// ImageBundle {
/// image: asset_server.load("Outline.png").into(),
/// style: Style {
/// width: Val::Px(150.),
/// height: Val::Px(150.),
/// ..default()
/// },
/// background_color: Color::WHITE.into(),
/// ..default()
/// },
/// ));
/// });
/// ```
pub fn create_joystick<I: VirtualJoystickID>(
cmd: &mut Commands,
knob_img: Handle<Image>,
background_img: Handle<Image>,
knob_color: Option<Color>,
background_color: Option<Color>,
interactable_area_color: Option<Color>,
knob_size: Vec2,
background_size: Vec2,
joystick_node: VirtualJoystickNode<I>,
joystick_node_style: Style,
) {
let mut spawn = cmd
.spawn(VirtualJoystickBundle::new(joystick_node).set_style(joystick_node_style));
let spawn = spawn
.insert(VirtualJoystickInteractionArea)
.with_children(|parent| {
parent.spawn((
VirtualJoystickUIKnob,
ImageBundle {
image: knob_img.into(),
style: Style {
width: Val::Px(knob_size.x),
height: Val::Px(knob_size.y),
..default()
},
background_color: knob_color.unwrap_or(Color::WHITE).into(),
..default()
},
));
parent.spawn((
VirtualJoystickUIBackground,
ImageBundle {
image: background_img.into(),
style: Style {
width: Val::Px(background_size.x),
height: Val::Px(background_size.y),
..default()
},
background_color: background_color.unwrap_or(Color::WHITE).into(),
..default()
},
));
});

if let Some(c) = interactable_area_color {
spawn.insert(BackgroundColor(c));
}
}

0 comments on commit 1caa245

Please sign in to comment.