1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
//! A simple joystick for macroquad games //! //! The joystick can be updated by touches or mouse //! //! # Example //! ``` //! use macroquad::prelude::*; //! use macroquad_virtual_joystick::Joystick; //! //! #[macroquad::main("Simple Joystick")] //! async fn main() { //! const SPEED: f32 = 2.5; //! let mut position = Vec2::new(screen_width() / 2.0, screen_height() / 4.); //! let mut joystick = Joystick::new(100.0, 200.0, 50.0); //! 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., YELLOW); //! //! //next_frame().await //! break; //! } //! } //! ``` use macroquad::prelude::{ draw_circle, is_mouse_button_down, mouse_position, touches, Color, MouseButton, TouchPhase, Vec2, }; /// The joystick component /// /// # Examples /// ```no_run /// 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(); /// ``` pub struct Joystick { center: Vec2, size: f32, background: JoystickElement, knob: JoystickElement, dragging: bool, touch_id: u64, event: JoystickDirectionalEvent, } impl Joystick { /// 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 new(x: f32, y: f32, size: f32) -> Self { Self::from_custom_elements(x, y, size, None, None) } /// 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 /// * `background`, `knob`: custom elements /// /// # Examples /// ``` /// use macroquad::prelude::Color; /// use macroquad_virtual_joystick::{Joystick, JoystickElement}; /// /// let center_x = 100.0; /// let center_y = 50.0; /// let radius = 50.0; /// let background_color = Color::from_rgba(255, 0, 0, 255); /// let knob_color = Color::from_rgba(0, 255, 0, 255); /// /// // create the background element /// let background = JoystickElement::new( /// center_x, /// center_y, /// radius, /// background_color, /// ); /// /// // create the knob element /// let knob = JoystickElement::new( /// center_x, /// center_y, /// radius * 0.75, /// knob_color, /// ); /// /// let joystick = Joystick::from_custom_elements( /// center_x, /// center_y, /// radius * 2.0, /// Some(background), /// Some(knob), /// ); /// ``` pub fn from_custom_elements( x: f32, y: f32, size: f32, background: Option<JoystickElement>, knob: Option<JoystickElement>, ) -> Self { let radius = size / 2.; let center = Vec2::new(x, y); let background = background.unwrap_or_else(|| { JoystickElement::new(x, y, radius, Color::from_rgba(96, 125, 139, 128)) }); let knob = knob.unwrap_or_else(|| { JoystickElement::new(x, y, radius / 2., Color::from_rgba(96, 125, 139, 168)) }); Self { center, size, background, knob, dragging: false, touch_id: 0, event: JoystickDirectionalEvent::default(), } } /// renders the background and knob fn render(&self) { self.background.render(); self.knob.render(); } /// update the joystick from touch fn update_touch(&mut self) { for touch in touches() { match touch.phase { TouchPhase::Started => { // a touch starts in the joystick if (touch.position - self.center).length() < (self.size / 2.) { self.dragging = true; self.touch_id = touch.id; self.moving(touch.position); } } TouchPhase::Moved => { if self.dragging && touch.id == self.touch_id { self.moving(touch.position); } } TouchPhase::Ended | TouchPhase::Cancelled => { if self.dragging && touch.id == self.touch_id { self.reset(); } } _ => {} } } } /// update the joystick from mouse drag fn update_mouse(&mut self) { let (mouse_x, mouse_y) = mouse_position(); let mouse = Vec2::new(mouse_x, mouse_y); let mouse_down = is_mouse_button_down(MouseButton::Left); if self.dragging { if mouse_down { self.moving(mouse) } else { self.reset(); } } else if mouse_down && (self.center - mouse).length() < (self.size / 2.) { self.dragging = true; self.moving(mouse) } } /// reset the joystick fn reset(&mut self) { self.dragging = false; self.knob.x = self.center.x; self.knob.y = self.center.y; self.event = JoystickDirectionalEvent::default(); } /// update the joystick /// /// this updates the joystick and renders it /// returns the current [`JoystickDirectionalEvent`] /// /// # Examples /// see [`Joystick`] pub fn update(&mut self) -> JoystickDirectionalEvent { if touches().len() > 0 { self.update_touch(); } else { self.update_mouse(); } self.render(); self.event } /// move the knob according to the drag position and update the [`self.event`] fn moving(&mut self, position: Vec2) { let delta = position - self.center; let angle = delta.y.atan2(delta.x); let angle_degrees = angle.to_degrees(); let radius = self.size / 2.; // maximum distance for the knob let dist = f32::min(delta.length(), radius); self.knob.x = self.center.x + dist * angle.cos(); self.knob.y = self.center.y + dist * angle.sin(); let intensity = dist / radius; let direction = if intensity == 0. { JoystickDirection::Idle } else { JoystickDirection::from_degrees(angle_degrees as f64) }; self.event = JoystickDirectionalEvent::new(direction, intensity, angle); } } /// element of the [`Joystick`] /// /// can be used for the background or the knob pub struct JoystickElement { x: f32, y: f32, radius: f32, color: Color, } impl JoystickElement { /// create a new [`JoystickElement`] /// /// # Examples /// ``` /// use macroquad::prelude::Color; /// use macroquad_virtual_joystick::JoystickElement; /// /// let center_x = 50.0; /// let center_y = 100.0; /// let radius = 30.0; /// let background_color = Color::from_rgba(255, 0, 0, 255); /// /// let background = JoystickElement::new( /// center_x, /// center_y, /// radius, /// background_color, /// ); /// ``` pub fn new(x: f32, y: f32, radius: f32, color: Color) -> Self { Self { x, y, radius, color, } } /// render the element pub fn render(&self) { draw_circle(self.x, self.y, self.radius, self.color); } } /// different directions of the [`Joystick`] #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum JoystickDirection { Up, UpLeft, Left, DownLeft, Down, DownRight, Right, UpRight, Idle, } impl JoystickDirection { /// calculate a JoystickDirection from degrees /// /// 0 degrees are on the positive X-Axis and then it rotates clockwise /// /// # Examples /// ``` /// use macroquad_virtual_joystick::JoystickDirection; /// /// let degrees = 153.5; /// let direction = JoystickDirection::from_degrees(degrees); /// /// assert_eq!(direction, JoystickDirection::DownLeft); /// ``` pub fn from_degrees(degrees: f64) -> Self { if degrees > -22.5 && degrees <= 22.5 { Self::Right } else if degrees > 22.5 && degrees <= 67.5 { Self::DownRight } else if degrees > 67.5 && degrees <= 112.5 { Self::Down } else if degrees > 112.5 && degrees <= 157.5 { Self::DownLeft } else if degrees > 157.5 && degrees <= 180. { Self::Left } else if degrees > -157.5 && degrees <= -112.5 { Self::UpLeft } else if degrees > -112.5 && degrees <= -67.5 { Self::Up } else if degrees > -67.5 && degrees <= -22.5 { Self::UpRight } else { Self::Idle } } /// convert the direction to a Vec2 with x and y /// /// x and y are both one of these: [-1.0, 0.0, 1.0] /// /// # Examples /// ``` /// use macroquad::prelude::Vec2; /// use macroquad_virtual_joystick::JoystickDirection; /// /// let direction = JoystickDirection::Up; /// assert_eq!(direction.to_local(), Vec2::new(0.0, -1.0)) /// ``` pub fn to_local(&self) -> Vec2 { let (x, y) = match self { Self::Right => (1., 0.), Self::DownRight => (1., 1.), Self::Down => (0., 1.), Self::DownLeft => (-1., 1.), Self::Left => (-1., 0.), Self::UpLeft => (-1., -1.), Self::Up => (0., -1.), Self::UpRight => (1., -1.), Self::Idle => (0., 0.), }; Vec2::new(x, y) } } /// the event of the [`Joystick`] /// /// call [`Joystick::update`] to get the current event #[derive(Clone, Copy, Debug)] pub struct JoystickDirectionalEvent { /// the direction to which the knob was moved pub direction: JoystickDirection, /// the intensity of the knob move, from 0 (center) to 1 (edge) pub intensity: f32, /// the angle of the knob (in radians) /// /// starting on the positive x-axis and rotating counter-clockwise pub angle: f32, } impl JoystickDirectionalEvent { fn new(direction: JoystickDirection, intensity: f32, angle: f32) -> Self { Self { direction, intensity, angle, } } } impl Default for JoystickDirectionalEvent { fn default() -> Self { Self { direction: JoystickDirection::Idle, intensity: 0., angle: 0., } } }