# misc/virtualpad (vanilla.js 0.4.0)

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

Virtual pad state.

## VirtualPad extends Object

Virtual pad.

Example: Pass the scene's touches to read a direction from the virtual pad

```js
import { Engine, EngineConfiguration, Scene, VirtualPad, Vector2, Color } from "./build/vanilla.js";

const virtualPad = new VirtualPad();
virtualPad.setPadPosition(Vector2.create(120, 240));
virtualPad.setPadRadius(80);
virtualPad.setBallRadius(30);
virtualPad.setPadColor(new Color(0, 0, 0, 0.4));
virtualPad.setBallColor(new Color(1, 1, 1, 0.8));
virtualPad.setBallMoveEvent((normalizedDirection, pullStrength) => {
	const moveX = (normalizedDirection.x * 2 - 1) * pullStrength;
	const moveY = (normalizedDirection.y * 2 - 1) * pullStrength;
	console.log(moveX.toFixed(2), moveY.toFixed(2));
});

class MainScene extends Scene {
	tick(timeDelta) {
		super.tick(timeDelta);
		virtualPad.tick(timeDelta);
	}

	draw(graphic) {
		super.draw(graphic);
		virtualPad.draw(graphic);
	}

	touchPress(viewInputPosition) {
		super.touchPress(viewInputPosition);
		virtualPad.touchPress(viewInputPosition);
	}

	touchMove(viewInputPosition) {
		super.touchMove(viewInputPosition);
		virtualPad.touchMove(viewInputPosition);
	}

	touchRelease(viewInputPosition) {
		super.touchRelease(viewInputPosition);
		virtualPad.touchRelease(viewInputPosition);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.0.11): Constructor.
- `tick(timeDelta)` (method, since 0.0.11): Update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `draw(graphic)` (method, since 0.0.11): Draw.
  - `graphic` Grpahic: Graphic to draw with.
- `touchPress(viewInputPosition)` (method, since 0.0.11): Touch press.
  - `viewInputPosition` Vector2: Pressed position. (View coordinates, moves the ball here)
- `touchMove(viewInputPosition)` (method, since 0.0.11): Touch drag.
  - `viewInputPosition` Vector2: Dragged position. (View coordinates, tick keeps the ball on the rim if outside the pad radius)
- `touchRelease(viewInputPosition)` (method, since 0.0.11): Touch release.
  - `viewInputPosition` Vector2: Released position. (Unused, the ball returns to the pad center in tick)
- `touchCancel()` (method, since 0.2.0): Touch cancel.
- `getVirtualPadState()` (method, since 0.0.11): Returns the virtual pad state.
  - returns VirtualPadState: Current state. ("wait", "pressed", "move", "released")
- `setBallMoveEvent(callback)` (method, since 0.0.11): Sets the ball move event function.
  - `callback` Function: Function called when the ball moves. (normalizedDirection, pullStrength) => void, the direction is 0 ~ 1 per axis (0.5 is the center), the pull is 0 ~ 1

  Example: Drag and release without the engine to see the callback values

  ```js
  import { VirtualPad, Vector2 } from "./build/vanilla.js";
  
  const virtualPad = new VirtualPad();
  virtualPad.setPadPosition(Vector2.create(100, 100));
  virtualPad.setPadRadius(50);
  virtualPad.setBallMoveEvent((normalizedDirection, pullStrength) => {
  	console.log(normalizedDirection.x.toFixed(2), normalizedDirection.y.toFixed(2), pullStrength.toFixed(2));
  });
  virtualPad.touchPress(Vector2.create(100, 100));
  virtualPad.touchMove(Vector2.create(200, 100));
  virtualPad.tick(1 / 60);
  virtualPad.touchRelease(Vector2.create(200, 100));
  virtualPad.tick(1 / 60);
  virtualPad.tick(1 / 60);
  console.log(virtualPad.getVirtualPadState());
  ```

- `setPadPosition(padPosition)` (method, since 0.0.11): Sets the pad position.
  - `padPosition` Function: Pad center position. (Also moves the ball here)
- `getPadPosition()` (method, since 0.0.11): Returns the pad position.
  - returns Color: Pad center position.
- `setPadRadius(padRadius)` (method, since 0.0.11): Sets the pad size.
  - `padRadius` Function: Pad radius. (pixels, default 200)
- `getPadRadius()` (method, since 0.0.11): Returns the pad size.
  - returns Color: Pad radius. (pixels)
- `getBallPosition()` (method, since 0.0.11): Returns the ball position.
  - returns Vector2: Ball center position.
- `setBallRadius(ballRadius)` (method, since 0.0.11): Sets the ball size.
  - `ballRadius` Function: Ball radius. (pixels, default 50)
- `getBallRadius()` (method, since 0.0.11): Returns the ball size.
  - returns Color: Ball radius. (pixels)
- `setPadColor(padColor)` (method, since 0.0.11): Sets the pad color.
  - `padColor` Color | string: Pad color. (default black)
- `getPadColor()` (method, since 0.0.11): Returns the pad color.
  - returns Color: Pad color.
- `setBallColor(ballColor)` (method, since 0.0.11): Sets the ball color.
  - `ballColor` Color | string: Ball color. (default white)
- `getBallColor()` (method, since 0.0.11): Returns the ball color.
  - returns Color: Ball color.
