Skip to main content

JoystickDirection

Enum JoystickDirection 

Source
pub enum JoystickDirection {
    Up,
    UpLeft,
    Left,
    DownLeft,
    Down,
    DownRight,
    Right,
    UpRight,
    Idle,
}
Expand description

different directions of the Joystick

Variants§

§

Up

§

UpLeft

§

Left

§

DownLeft

§

Down

§

DownRight

§

Right

§

UpRight

§

Idle

Implementations§

Source§

impl JoystickDirection

Source

pub fn from_degrees(degrees: f64) -> Self

calculate a JoystickDirection from degrees

0 degrees are on the positive X-Axis and then it rotates clockwise

§Examples
use macroquad_virtual_joystick::JoystickDirection;

let degrees = 153.5;
let direction = JoystickDirection::from_degrees(degrees);

assert_eq!(direction, JoystickDirection::DownLeft);
Source

pub fn to_local(&self) -> Vec2

convert the direction to a Vec2 with x and y

x and y are both one of these: [-1.0, 0.0, 1.0]

§Examples
use macroquad::prelude::Vec2;
use macroquad_virtual_joystick::JoystickDirection;

let direction = JoystickDirection::Up;
assert_eq!(direction.to_local(), Vec2::new(0.0, -1.0))
Examples found in repository?
examples/simple.rs (line 13)
5async fn main() {
6    const SPEED: f32 = 2.5;
7    let mut position = Vec2::new(screen_width() / 2.0, screen_height() / 4.0);
8    let mut joystick = Joystick::new(100.0, 200.0, 50.0);
9    loop {
10        clear_background(WHITE);
11
12        let joystick_event = joystick.update();
13        position += joystick_event.direction.to_local() * joystick_event.intensity * SPEED;
14
15        draw_circle(position.x, position.y, 50.0, YELLOW);
16
17        // if you use a camera, reset the camera here with `set_default_camera()`
18        joystick.render();
19        next_frame().await
20    }
21}
More examples
Hide additional examples
examples/custom.rs (line 32)
13async fn main() {
14    const SPEED: f32 = 2.5;
15    let mut position = Vec2::new(screen_width() / 2.0, screen_height() / 4.0);
16
17    let background_size = 50.0;
18    let knob_size = 32.0;
19
20    let mut joystick = Joystick::from_custom_elements(
21        100.0,
22        200.0,
23        background_size,
24        knob_size,
25        Box::new(render_background),
26        Box::new(render_knob),
27    );
28    loop {
29        clear_background(WHITE);
30
31        let joystick_event = joystick.update();
32        position += joystick_event.direction.to_local() * joystick_event.intensity * SPEED;
33
34        draw_circle(position.x, position.y, 50.0, YELLOW);
35
36        joystick.render();
37        next_frame().await
38    }
39}

Trait Implementations§

Source§

impl Clone for JoystickDirection

Source§

fn clone(&self) -> JoystickDirection

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for JoystickDirection

Source§

impl Debug for JoystickDirection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for JoystickDirection

Source§

impl PartialEq for JoystickDirection

Source§

fn eq(&self, other: &JoystickDirection) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for JoystickDirection

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.