# misc/virtualpad (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/misc/virtualpad/

Virtual pad.
- A port of the original misc/virtualpad.js. Computes and draws the movement of an on-screen joystick's pad (outer circle) and knob (inner circle).
- Unlike the original, it is a plain value, not a component. The owner calls touch_press / touch_move / touch_release and tick, draw.
- While the knob moves, the knob move event reports the direction (a unit vector mapped to 0 ~ 1, 0 ~ 1) and the pull strength (0 ~ 1).
  On release, the knob returns to the pad center at 1600 pixels per second while reporting, and once back reports (0.5, 0.5), 0 once.
- Differences from the original:
  The state name move is a Rust keyword, so it is moved. The original state value strings ("wait", "pressed", "move", "released") are obtained with get_name.
  The event callback is FnMut(Vector2, f64).

## VirtualPad (struct) (Default)

Virtual pad.
- The owner passes touch input with touch_press, touch_move, touch_release and calls tick and draw every frame.

Example: Drag all the way to the right and release

```rust
use std::cell::RefCell;
use std::rc::Rc;
use vanilla::{Vector2, VirtualPad, VirtualPadState};

let last_event = Rc::new(RefCell::new((Vector2::zero(), 0.0)));
let last_event_for_callback = last_event.clone();
let mut virtual_pad = VirtualPad::new();
virtual_pad.set_pad_radius(100.0);
virtual_pad.set_pad_position(Vector2::create(200.0, 200.0));
virtual_pad.set_ball_move_event(move |direction, pull_strength| {
	*last_event_for_callback.borrow_mut() = (direction, pull_strength);
});

virtual_pad.touch_press(Vector2::create(200.0, 200.0));
virtual_pad.touch_move(Vector2::create(500.0, 200.0));
virtual_pad.tick(1.0 / 60.0);
assert_eq!(virtual_pad.get_ball_position(), Vector2::create(300.0, 200.0));
assert_eq!(*last_event.borrow(), (Vector2::create(1.0, 0.5), 1.0));

virtual_pad.touch_release(Vector2::create(500.0, 200.0));
for _ in 0..10 {
	virtual_pad.tick(1.0 / 60.0);
}
assert_eq!(virtual_pad.get_virtual_pad_state(), VirtualPadState::None);
assert_eq!(*last_event.borrow(), (Vector2::create(0.5, 0.5), 0.0));
```

- `pub fn new() -> VirtualPad` (constructor, since 0.3.2): Constructor. (pad radius 200 black, knob radius 50 white, return speed 1600 per second)
  - returns VirtualPad: A new virtual pad with the pad and knob at (0, 0), in the waiting state.
- `pub fn tick(&mut self, time_delta: f64)` (method, since 0.3.2): Update. (keeps the knob inside the pad while pressed and dragged, and sends it back to the center on release)
  - Does nothing in the waiting state. While pressed or dragged, calls the knob move event every tick.
  - `time_delta` f64: Elapsed time. (seconds, the return distance is 1600 pixels per second * time_delta)
- `pub fn draw(&self, graphic: &mut Graphic)` (method, since 0.3.2): Draw. (outer circle then inner circle, in the caller's coordinate system as is)
  - `graphic` &mut Graphic: Graphic to draw to. (the fill color is changed to the pad color and knob color and not restored)
- `pub fn touch_press(&mut self, view_input_position: Vector2)` (method, since 0.3.2): Touch press. (moves the knob to the press point)
  - `view_input_position` Vector2: Press position. (same coordinate system as the pad position, usually view coordinates)
- `pub fn touch_move(&mut self, view_input_position: Vector2)` (method, since 0.3.2): Touch drag. (moves the knob to the drag point)
  - Left as is even outside the pad, and confined to the pad edge on the next tick.
  - `view_input_position` Vector2: Drag position. (same coordinate system as the pad position)
- `pub fn touch_release(&mut self, _view_input_position: Vector2)` (method, since 0.3.2): Touch release. (the knob starts returning to the center from where it is)
  - `_view_input_position` Vector2: Release position. (unused)
- `pub fn touch_cancel(&mut self)` (method, since 0.3.2): Touch cancel. (same as release)
- `pub fn get_virtual_pad_state(&self) -> VirtualPadState` (method, since 0.3.2): Returns the virtual pad state.
  - returns VirtualPadState: The current state.
- `pub fn set_ball_move_event(&mut self, callback: impl FnMut(Vector2, f64) + 'static)` (method, since 0.3.2): Sets the knob move event. (direction 0 ~ 1, pull strength 0 ~ 1)
  - `callback` impl FnMut(Vector2, f64) + 'static: Function to call when the knob moves. Receives the direction (each axis of the unit vector mapped to (value + 1) / 2, center is (0.5, 0.5)) and the pull strength (distance from the pad center / pad radius).
- `pub fn set_pad_position(&mut self, pad_position: Vector2)` (method, since 0.3.2): Sets the pad position. (the knob moves there too)
  - `pad_position` Vector2: Pad center position. (pixels)
- `pub fn get_pad_position(&self) -> Vector2` (method, since 0.3.2): Returns the pad position.
  - returns Vector2: Pad center position. (pixels)
- `pub fn set_pad_radius(&mut self, pad_radius: f64)` (method, since 0.3.2): Sets the pad radius.
  - `pad_radius` f64: Pad radius. (pixels, the knob center is confined within it)
- `pub fn get_pad_radius(&self) -> f64` (method, since 0.3.2): Returns the pad radius.
  - returns f64: Pad radius. (pixels)
- `pub fn get_ball_position(&self) -> Vector2` (method, since 0.3.2): Returns the ball position.
  - returns Vector2: Knob center position. (pixels)
- `pub fn set_ball_radius(&mut self, ball_radius: f64)` (method, since 0.3.2): Sets the knob radius.
  - `ball_radius` f64: Knob radius. (pixels, used only for drawing)
- `pub fn get_ball_radius(&self) -> f64` (method, since 0.3.2): Returns the knob radius.
  - returns f64: Ball radius. (pixels)
- `pub fn set_pad_color(&mut self, pad_color: impl Into<Color>)` (method, since 0.3.2): Sets the pad color.
  - `pad_color` impl Into<Color>: Pad color. (a Color or a color string like "#000000")
- `pub fn get_pad_color(&self) -> Color` (method, since 0.3.2): Returns the pad color.
  - returns Color: Pad color.
- `pub fn set_ball_color(&mut self, ball_color: impl Into<Color>)` (method, since 0.3.2): Sets the knob color.
  - `ball_color` impl Into<Color>: Knob color. (a Color or a color string like "#ffffff")
- `pub fn get_ball_color(&self) -> Color` (method, since 0.3.2): Returns the knob color.
  - returns Color: Ball color.

## VirtualPadState (enum) (Clone, Copy, Debug, PartialEq, Eq)

Virtual pad state.

- `None`: Waiting. (not pressed and the knob is at the center, original "wait")
- `Pressed`: Pressed. (the knob is at the press point)
- `Moved`: Dragging. (original "move")
- `Released`: Released. (the knob is returning to the center, None once back)

- `pub fn get_name(&self) -> &'static str` (method, since 0.3.2): String of the original state value.
  - returns &'static str: "wait" for None, "pressed" for Pressed, "move" for Moved, "released" for Released.
