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
impl Joystick
Sourcepub fn new(x: f32, y: f32, size: f32) -> Self
pub fn new(x: f32, y: f32, size: f32) -> Self
create a new joystick
§Arguments
x
,y
: center of the joysticksize
: 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}
Sourcepub 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
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 joysticksize
: diameter of the joystick, should have the same size as the background elementknob_size
: diameter of the knob, should have the same size as the background elementrender_background
,render_knob
: custom drawing functions with the following arguments:x
the x coordinate of the center of the componenty
the y coordinate of the center of the componentradius
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}
Sourcepub fn render(&self)
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
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}
Sourcepub fn update(&mut self) -> JoystickEvent
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
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§
impl Freeze for Joystick
impl !RefUnwindSafe for Joystick
impl !Send for Joystick
impl !Sync for Joystick
impl Unpin for Joystick
impl !UnwindSafe for Joystick
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more