# misc/cameragestures (vanilla-engine 0.4.0)

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

Camera gestures. (CameraGestures of vanilla.js misc/cameragestures.js)
- Gestures that rotate, pan, and zoom a 3D board or a map. Uses input outside the engine's touch (left button, first finger).
  - Dragging with the right mouse button held: rotates by the horizontal drag (the canvas width is one full turn) and tilts by the vertical drag. Releasing without dragging is a right click.
  - Dragging with the middle mouse button held: pans.
  - Two fingers: dragging in the same direction pans, pinching in and out zooms around the midpoint of the two fingers, and twisting past the twist threshold rotates. (prevents slowly rotating while pinch zooming)
    As in the original, the gesture continues until every finger is lifted. When only one finger remains its movement is ignored, and when the finger count changes that spot becomes the new reference.
- The engine handles the wheel. This only records whether Ctrl was held. (is_last_wheel_ctrl, laptop touchpad pinches also arrive as Ctrl wheel)
- Also reads whether the last input was a finger (is_touch_input, initially whether the device's primary input is touch). Within the time that emulated mouse events are ignored after a finger is lifted (fourth argument of set_thresholds, initially 800 milliseconds),
  mouse moves and presses do not switch the input, and right or middle buttons pressed then do not start a gesture until released.
- The original listens to the canvas DOM events directly (hence addListener, dispose). The Rust version reads the input manager (mouse buttons, finger list, wheel Ctrl) with update every frame.
  The web platform passes the buttons and positions of emulated mouse events without filtering, like the original engine.js (only the last device is filtered for 800 milliseconds), so the fourth argument threshold works whether it is shorter or longer than 800, as in the original.
  It measures by frame time instead of event time. (the frame that had the finger event and the frame that read the mouse button)
- During a gesture it reports is_gesture_active so presses and releases passed by the engine are not used for gameplay.
- The accumulated amounts (pan, rotation, tilt, zoom) are taken out every frame with take. Pan is in view units, rotation and tilt in radians, and zoom is a ratio to multiply by.

## CameraGestures (struct) (Default)

Camera gestures. (right and middle mouse button drags, and two-finger rotate, tilt, pan, and zoom)
- Reads input with update every frame, and takes the accumulated amounts out with take_pan, take_rotation, take_tilt, take_zoom to apply to the camera.

Example: Move a 2D camera with gestures

```rust
use vanilla::{Camera2D, CameraGestures, Engine, Scene, SceneBase};

struct MapScene {
	scene_base: SceneBase,
	camera: Camera2D,
	camera_gestures: CameraGestures,
}

impl Scene for MapScene {
	fn get_scene_base(&self) -> &SceneBase {
		&self.scene_base
	}

	fn get_scene_base_mut(&mut self) -> &mut SceneBase {
		&mut self.scene_base
	}

	fn tick(&mut self, time_delta: f64) {
		let engine = Engine::get_engine().unwrap();
		self.scene_base.tick(time_delta);
		self.camera_gestures.update(&engine.get_input_manager(), &engine.get_view_manager());
		let pan = self.camera_gestures.take_pan();
		self.camera.pan_by_screen(pan.x, pan.y);
		let zoom = self.camera_gestures.take_zoom();
		if let Some(zoom_anchor) = self.camera_gestures.get_zoom_anchor() {
			self.camera.zoom_at(zoom_anchor, zoom);
		}
		let rotation = self.camera_gestures.take_rotation();
		self.camera.set_rotation(self.camera.get_rotation() + rotation.to_degrees());
		self.camera.tick(time_delta);
	}
}
```

- `pub fn new() -> CameraGestures` (constructor, since 0.4.0): Constructor.
  - Twist threshold 15 degrees, 35 degrees of tilt when dragging half the canvas height, right click distance 8 pixels, emulated mouse time 800 milliseconds.
  - The last input starts as whether the device's primary input is touch (is_coarse_pointer).
  - returns CameraGestures: A new gesture with nothing accumulated (pan 0, rotation 0, tilt 0, zoom 1).
- `pub fn update(&mut self, input_manager: &InputManager, view_manager: &ViewManager)` (method, since 0.4.0): Reads input every frame. (called from the scene's tick)
  - Mouse and finger positions are read in canvas logical coordinates. For right-drag rotation, the canvas width is one full turn (2π radians).
  - `input_manager` &InputManager: Input manager to read the mouse buttons, mouse position, finger list, and wheel Ctrl from.
  - `view_manager` &ViewManager: View manager to read the canvas size and reference resolution scale from.

  Example: Dragging a quarter of the canvas width with the right button is a quarter turn

  ```rust
  use vanilla::{CameraGestures, InputManager, MouseButton, Vector2, ViewManager, ViewScaleMode};
  
  let mut view_manager = ViewManager::new(Vector2::create(800.0, 600.0), ViewScaleMode::ReferenceResolution);
  view_manager.calculate_view_rect(800, 600, 1.0);
  let mut input_manager = InputManager::new();
  let mut camera_gestures = CameraGestures::new();
  
  input_manager.set_canvas_native_input_position(Vector2::create(100.0, 300.0));
  input_manager.set_mouse_button_down(MouseButton::Right, true);
  camera_gestures.update(&input_manager, &view_manager);
  assert!(camera_gestures.is_gesture_active());
  input_manager.update_frame_snapshot();
  
  input_manager.set_canvas_native_input_position(Vector2::create(300.0, 300.0));
  camera_gestures.update(&input_manager, &view_manager);
  assert!((camera_gestures.take_rotation() - std::f64::consts::FRAC_PI_2).abs() < 0.0001);
  assert_eq!(camera_gestures.read_mouse_drag_cursor(), "grabbing");
  ```

- `pub fn is_touch_input(&self) -> bool` (method, since 0.4.0): Whether the last input was a finger. (initially, whether the device's primary input is touch)
  - returns bool: true if the last input was a finger, false if a mouse.
- `pub fn is_coarse_pointer(&self) -> bool` (method, since 0.4.0): Whether the device's primary input is touch. (phones and tablets, original matchMedia("(pointer: coarse)"))
  - returns bool: true if the primary input is a finger. (asks the platform on every call, always false on Mac)
- `pub fn is_last_wheel_ctrl(&self) -> bool` (method, since 0.4.0): Whether the last wheel was a wheel with Ctrl held. (that wheel zooms even if wheel panning is on)
  - returns bool: true if the wheel read in the last update was a Ctrl wheel.
- `pub fn read_units_per_canvas_pixel(view_manager: &ViewManager) -> f64` (static, since 0.4.0): How many view units one canvas logical pixel is.
  - `view_manager` &ViewManager: View manager to read the reference resolution scale from.
  - returns f64: 1 / reference resolution scale. 1 if the scale is 0 or less.
- `pub fn is_gesture_active(&self) -> bool` (method, since 0.4.0): Whether a gesture is in progress. (the right or middle button is held or two fingers are down, during which the engine's presses are not used for gameplay)
  - returns bool: true during a gesture. (a two-finger gesture continues until every finger is lifted)
- `pub fn read_mouse_drag_cursor(&self) -> &'static str` (method, since 0.4.0): Cursor name while dragging with the mouse. (grabbing while rotating or panning, grab if the right button is pressed but not yet dragged, otherwise an empty string)
  - returns &'static str: One of "grabbing", "grab", "".
- `pub fn take_pan(&mut self) -> Vector2` (method, since 0.4.0): Takes the accumulated pan. (view units, cleared once taken)
  - returns Vector2: Pan accumulated since the last take. (view units, the direction the hand or cursor moved is +)
- `pub fn take_rotation(&mut self) -> f64` (method, since 0.4.0): Takes the accumulated rotation. (radians, cleared once taken)
  - returns f64: Rotation accumulated since the last take. (radians)
- `pub fn take_tilt(&mut self) -> f64` (method, since 0.4.0): Takes the accumulated tilt. (radians, + is toward looking from above, cleared once taken)
  - returns f64: Tilt accumulated since the last take. (radians, dragging up is +)
- `pub fn take_zoom(&mut self) -> f64` (method, since 0.4.0): Takes the accumulated zoom. (ratio to multiply, reset to 1 when taken)
  - returns f64: Zoom ratio accumulated since the last take. (greater than 1 means spread apart, 1 if there was no gesture)
- `pub fn get_zoom_anchor(&self) -> Option<Vector2>` (method, since 0.4.0): Center of the zoom. (view coordinates, midpoint of the two fingers, None if none)
  - returns Option<Vector2>: Midpoint from the last frame the two fingers moved. (view coordinates) None if there has been no such frame yet. (not cleared when the fingers are lifted)
- `pub fn take_right_click(&mut self) -> bool` (method, since 0.4.0): Takes whether a right click happened. (right button released without dragging, cleared once taken)
  - returns bool: true if a right click happened since the last take.
- `pub fn take_rotation_released(&mut self) -> bool` (method, since 0.4.0): Takes whether the rotation was released. (cleared once taken)
  - returns bool: true if the right button was released or a two-finger gesture ended since the last take.
- `pub fn set_thresholds(&mut self, twist_start_radians: f64, tilt_per_half_height_radians: f64, right_click_distance: f64, compatibility_mouse_milliseconds: f64)` (method, since 0.4.0): Sets the thresholds. (angle at which twisting starts, tilt angle when dragging half the canvas height, distance treated as a right click, milliseconds to ignore emulated mouse events)
  - `twist_start_radians` f64: A two-finger twist must exceed this to start rotating. (radians, initially 15 degrees)
  - `tilt_per_half_height_radians` f64: Tilt angle when dragging vertically half the canvas height with the right button. (radians, initially 35 degrees)
  - `right_click_distance` f64: Releasing within this distance of the press point is a right click. (canvas logical pixels, initially 8)
  - `compatibility_mouse_milliseconds` f64: Time to ignore emulated mouse events after a finger event. (milliseconds, initially 800, 0 means never ignored)
