Struct macroquad_virtual_joystick::Joystick[][src]

pub struct Joystick { /* fields omitted */ }
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

impl Joystick[src]

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

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);

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
[src]

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
    }
}

pub fn render(&self)[src]

render the joystick

renders the background and knob

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

pub fn update(&mut self) -> JoystickEvent[src]

update the joystick

this updates the joystick and returns the current JoystickEvent

Examples

see Joystick

Auto Trait Implementations

impl !RefUnwindSafe for Joystick

impl !Send for Joystick

impl !Sync for Joystick

impl Unpin for Joystick

impl !UnwindSafe for Joystick

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.