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]
impl Joystick
[src]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]
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 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 } }
pub fn render(&self)
[src]
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]
pub fn update(&mut self) -> JoystickEvent
[src]update the joystick
this updates the joystick and returns the current JoystickEvent
Examples
see Joystick