Joystick

Struct Joystick 

Source
pub struct Joystick { /* private fields */ }
Expand description

The joystick component

§Examples

use macroquad_virtual_joystick::Joystick;
let center_x = 100.0;
let center_y = 50.0;
let size = 50.0;
// create a new joystick
let mut joystick = Joystick::new(center_x, center_y, size);
// render the joystick and determine the action
let joystick_action = joystick.update();

Implementations§

Source§

impl Joystick

Source

pub fn new(x: f32, y: f32, size: f32) -> Self

create a new joystick

§Arguments
  • x, y: center of the joystick
  • size: diameter of the joystick
§Examples
use macroquad_virtual_joystick::Joystick;
let center_x = 100.0;
let center_y = 50.0;
let size = 50.0;
let joystick = Joystick::new(center_x, center_y, size);
Examples found in repository?
examples/simple.rs (line 8)
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}
Source

pub fn from_custom_elements( x: f32, y: f32, size: f32, knob_size: f32, render_background: Box<fn(f32, f32, f32)>, render_knob: Box<fn(f32, f32, f32)>, ) -> Self

create a new Joystick with custom elements for background and knob

§Arguments
  • x, y: center of the joystick
  • size: diameter of the joystick, should have the same size as the background element
  • knob_size: diameter of the knob, should have the same size as the background element
  • render_background, render_knob: custom drawing functions with the following arguments:
    • x the x coordinate of the center of the component
    • y the y coordinate of the center of the component
    • radius the radius used for mouse/ touch collision for good UX this should also be the size of the drawing
§Examples
use macroquad::prelude::*;
use macroquad_virtual_joystick::Joystick;

fn render_background(x: f32, y: f32, radius: f32) {
    draw_circle(x, y, radius, RED);
}

fn render_knob(x: f32, y: f32, radius: f32) {
    draw_circle(x, y, radius, GREEN);
}

#[macroquad::main("Custom Joystick")]
async fn main() {
    const SPEED: f32 = 2.5;
    let mut position = Vec2::new(screen_width() / 2.0, screen_height() / 4.0);

    let background_size = 50.0;
    let knob_size = 32.0;

    let mut joystick = Joystick::from_custom_elements(
        100.0,
        200.0,
        background_size,
        knob_size,
        Box::new(render_background),
        Box::new(render_knob),
    );
    loop {
        clear_background(WHITE);

        let joystick_event = joystick.update();
        position += joystick_event.direction.to_local() * joystick_event.intensity * SPEED;

        draw_circle(position.x, position.y, 50.0, YELLOW);

        joystick.render();
        next_frame().await
    }
}
Examples found in repository?
examples/custom.rs (lines 20-27)
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}
Source

pub fn render(&self)

render the joystick

renders the background and knob

call macroquad::prelude::set_default_camera() before!

Examples found in repository?
examples/simple.rs (line 18)
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 36)
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}
Source

pub fn update(&mut self) -> JoystickEvent

update the joystick

this updates the joystick and returns the current JoystickEvent

§Examples

see Joystick

Examples found in repository?
examples/simple.rs (line 12)
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 31)
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}

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> 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, 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.