Skip to main content

custom/
custom.rs

1use macroquad::prelude::*;
2use macroquad_virtual_joystick::Joystick;
3
4fn render_background(x: f32, y: f32, radius: f32) {
5    draw_circle(x, y, radius, RED);
6}
7
8fn render_knob(x: f32, y: f32, radius: f32) {
9    draw_circle(x, y, radius, GREEN);
10}
11
12#[macroquad::main("Custom Joystick")]
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}