diff --git a/src/lib.rs b/src/lib.rs index 5c29ae1..1f2e44b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ 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}; @@ -17,6 +18,7 @@ pub use joystick::{ VirtualJoystickBundle, VirtualJoystickInteractionArea, VirtualJoystickNode, VirtualJoystickUIBackground, VirtualJoystickUIKnob, }; +pub use utils::create_joystick; use joystick::{extract_joystick_node, VirtualJoystickData}; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..9aba1e5 --- /dev/null +++ b/src/utils.rs @@ -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( + cmd: &mut Commands, + knob_img: Handle, + background_img: Handle, + knob_color: Option, + background_color: Option, + interactable_area_color: Option, + knob_size: Vec2, + background_size: Vec2, + joystick_node: VirtualJoystickNode, + 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)); + } +}