vanilladocs
vanillapunch.com
API / misc/cameragestures

misc/cameragestures

카메라 손짓. (vanilla.js misc/cameragestures.js 의 CameraGestures)

  • 3D 판이나 지도를 돌리고 밀고 키우는 손짓이다. 엔진의 터치(왼쪽 단추, 첫 손가락) 밖의 입력을 쓴다.
  • 마우스 오른쪽 단추를 누른 채 끌기: 가로로 끈 만큼 돌리고(캔버스 너비만큼이 한 바퀴), 세로로 끈 만큼 기울인다. 끌지 않고 떼면 오른쪽 클릭이다.
  • 마우스 가운데 단추를 누른 채 끌기: 민다.
  • 두 손가락: 같은 쪽으로 끌면 밀고, 오므리고 벌리면 두 손가락 가운데를 두고 키우고 줄이고, 비틀기 문턱을 넘게 비틀면 돌린다. (오므려 키우다가 조금씩 도는 것을 막는다) 원본처럼 모든 손가락을 뗄 때까지 손짓이 이어진다. 한 손가락만 남으면 움직임을 쓰지 않고, 손가락 수가 바뀌면 그 자리를 새 기준으로 잡는다.
  • 휠은 엔진이 넘긴다. 여기서는 Ctrl 을 눌렀는지만 적어 둔다. (is_last_wheel_ctrl, 노트북 터치패드의 오므리기도 Ctrl 휠로 온다)
  • 마지막 손이 손가락인지(is_touch_input, 처음에는 기기가 손가락이 주된 입력인지)도 읽는다. 손가락을 뗀 뒤 흉내 낸 마우스를 무시하는 시간(set_thresholds 의 넷째 인자, 처음 800 밀리초) 안의 마우스 움직임과 누름은 손을 바꾸지 않고, 그때 누른 오른쪽, 가운데 단추는 뗄 때까지 손짓을 시작하지 않는다.
  • 원본은 캔버스의 DOM 사건을 직접 듣는다(그래서 addListener, dispose 가 있다). 러스트 판은 매 프레임 update 로 입력 매니저(마우스 단추, 손가락 목록, 휠의 Ctrl)를 읽는다. 웹 플랫폼은 원본 engine.js 처럼 흉내 낸 마우스 사건의 단추와 자리를 거르지 않고 넘겨서(마지막 장치만 800 밀리초 거른다), 넷째 인자 문턱이 원본처럼 800 보다 짧아도 길어도 먹는다. 사건의 때 대신 프레임의 때로 잰다. (손가락 사건이 있었던 프레임과 마우스 단추를 읽은 프레임)
  • 손짓 중에는 엔진이 넘기는 누름과 뗌을 놀이에 쓰지 않도록 is_gesture_active 로 알린다.
  • 모인 양(밀기, 돌리기, 기울이기, 키우기)은 매 프레임 take 로 꺼내 간다. 밀기는 뷰 단위, 돌리기와 기울이기는 라디안, 키우기는 곱할 비다.

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.

CameraGesturesstruct traits Default

짝Counterpart: vanilla.js CameraGestures소스source

카메라 손짓. (마우스 오른쪽, 가운데 단추 끌기와 두 손가락으로 돌리기, 기울이기, 밀기, 키우기)

  • 매 프레임 update 로 입력을 읽고, 모인 양을 take_pan, take_rotation, take_tilt, take_zoom 으로 꺼내 카메라에 건다.

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손짓으로 2D 카메라 움직이기Move a 2D camera with gestures
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);
	}
}
생성자constructorpub fn new() -> CameraGestures#0.4.0 부터since 0.4.0소스source

생성.

  • 비틀기 문턱 15 도, 캔버스 높이 반만큼 끌 때 기울기 35 도, 오른쪽 클릭 거리 8 픽셀, 흉내 낸 마우스 시간 800 밀리초.
  • 마지막 손은 기기가 손가락이 주된 입력인지(is_coarse_pointer)로 시작한다.

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).

반환ReturnsCameraGestures 모인 양이 없는(밀기 0, 돌리기 0, 기울이기 0, 키우기 1) 새 손짓.A new gesture with nothing accumulated (pan 0, rotation 0, tilt 0, zoom 1).

짝Counterpart: vanilla.js CameraGestures.constructor

정적staticpub fn read_units_per_canvas_pixel(view_manager: &ViewManager) -> f64#0.4.0 부터since 0.4.0소스source

캔버스 논리 픽셀 하나가 뷰 단위로 얼마인지.

How many view units one canvas logical pixel is.

view_manager&ViewManager
기준 해상도 배율을 읽을 뷰 매니저.View manager to read the reference resolution scale from.

반환Returnsf64 1 / 기준 해상도 배율. 배율이 0 이하이면 1.1 / reference resolution scale. 1 if the scale is 0 or less.

메서드methodpub fn update(&mut self, input_manager: &InputManager, view_manager: &ViewManager)#0.4.0 부터since 0.4.0소스source

매 프레임 입력 읽기. (씬의 tick 에서 부른다)

  • 마우스 자리와 손가락 자리는 캔버스 논리 좌표로 읽는다. 오른쪽 끌기의 돌리기는 캔버스 너비만큼이 한 바퀴(2π 라디안)다.

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
마우스 단추, 마우스 자리, 손가락 목록, 휠의 Ctrl 을 읽을 입력 매니저.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오른쪽 단추로 캔버스 너비의 4 분의 1 을 끌면 4 분의 1 바퀴Dragging a quarter of the canvas width with the right button is a quarter turn
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");
메서드methodpub fn is_touch_input(&self) -> bool#0.4.0 부터since 0.4.0소스source

마지막 손이 손가락인지. (처음에는 기기가 손가락이 주된 입력인지로)

Whether the last input was a finger. (initially, whether the device's primary input is touch)

반환Returnsbool 마지막 손이 손가락이면 true, 마우스면 false.true if the last input was a finger, false if a mouse.

짝Counterpart: vanilla.js CameraGestures.isTouchInput

메서드methodpub fn is_coarse_pointer(&self) -> bool#0.4.0 부터since 0.4.0소스source

기기가 손가락이 주된 입력인지. (폰과 태블릿, 원본 matchMedia("(pointer: coarse)"))

Whether the device's primary input is touch. (phones and tablets, original matchMedia("(pointer: coarse)"))

반환Returnsbool 주된 손이 손가락이면 true. (부를 때마다 플랫폼에 묻는다, 맥은 늘 false)true if the primary input is a finger. (asks the platform on every call, always false on Mac)

짝Counterpart: vanilla.js CameraGestures.isCoarsePointer

메서드methodpub fn is_last_wheel_ctrl(&self) -> bool#0.4.0 부터since 0.4.0소스source

마지막 휠이 Ctrl 을 누른 휠이었는지. (휠로 밀기를 켰어도 그 휠은 키우기다)

Whether the last wheel was a wheel with Ctrl held. (that wheel zooms even if wheel panning is on)

반환Returnsbool 마지막 update 에서 읽은 휠이 Ctrl 휠이면 true.true if the wheel read in the last update was a Ctrl wheel.

짝Counterpart: vanilla.js CameraGestures.isLastWheelCtrl

메서드methodpub fn is_gesture_active(&self) -> bool#0.4.0 부터since 0.4.0소스source

손짓 중인지. (오른쪽이나 가운데 단추를 누르고 있거나 두 손가락, 이때 엔진의 누름은 놀이에 쓰지 않는다)

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)

반환Returnsbool 손짓 중이면 true. (두 손가락 손짓은 모든 손가락을 뗄 때까지 이어진다)true during a gesture. (a two-finger gesture continues until every finger is lifted)

짝Counterpart: vanilla.js CameraGestures.isGestureActive

메서드methodpub fn read_mouse_drag_cursor(&self) -> &'static str#0.4.0 부터since 0.4.0소스source

마우스로 끄는 동안의 커서 이름. (돌리거나 미는 중이면 grabbing, 오른쪽 단추를 누르고 아직 끌지 않았으면 grab, 아니면 빈 글)

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 "grabbing", "grab", "" 가운데 하나.One of "grabbing", "grab", "".

짝Counterpart: vanilla.js CameraGestures.readMouseDragCursor

메서드methodpub fn take_pan(&mut self) -> Vector2#0.4.0 부터since 0.4.0소스source

모인 밀기 꺼내기. (뷰 단위, 꺼내면 비운다)

Takes the accumulated pan. (view units, cleared once taken)

반환ReturnsVector2 지난번 꺼낸 뒤 모인 밀기. (뷰 단위, 손이나 커서가 움직인 쪽이 +)Pan accumulated since the last take. (view units, the direction the hand or cursor moved is +)

짝Counterpart: vanilla.js CameraGestures.takePan

메서드methodpub fn take_rotation(&mut self) -> f64#0.4.0 부터since 0.4.0소스source

모인 돌리기 꺼내기. (라디안, 꺼내면 비운다)

Takes the accumulated rotation. (radians, cleared once taken)

반환Returnsf64 지난번 꺼낸 뒤 모인 돌리기. (라디안)Rotation accumulated since the last take. (radians)

짝Counterpart: vanilla.js CameraGestures.takeRotation

메서드methodpub fn take_tilt(&mut self) -> f64#0.4.0 부터since 0.4.0소스source

모인 기울이기 꺼내기. (라디안, + 가 위에서 보는 쪽, 꺼내면 비운다)

Takes the accumulated tilt. (radians, + is toward looking from above, cleared once taken)

반환Returnsf64 지난번 꺼낸 뒤 모인 기울이기. (라디안, 위로 끌면 +)Tilt accumulated since the last take. (radians, dragging up is +)

짝Counterpart: vanilla.js CameraGestures.takeTilt

메서드methodpub fn take_zoom(&mut self) -> f64#0.4.0 부터since 0.4.0소스source

모인 키우기 꺼내기. (곱할 비, 꺼내면 1)

Takes the accumulated zoom. (ratio to multiply, reset to 1 when taken)

반환Returnsf64 지난번 꺼낸 뒤 모인 키우기 비. (1 보다 크면 벌린 것, 손짓이 없었으면 1)Zoom ratio accumulated since the last take. (greater than 1 means spread apart, 1 if there was no gesture)

짝Counterpart: vanilla.js CameraGestures.takeZoom

메서드methodpub fn get_zoom_anchor(&self) -> Option<Vector2>#0.4.0 부터since 0.4.0소스source

키우기의 가운데. (뷰 좌표, 두 손가락 가운데, 없으면 None)

Center of the zoom. (view coordinates, midpoint of the two fingers, None if none)

반환ReturnsOption<Vector2> 마지막으로 두 손가락을 움직인 프레임의 가운데. (뷰 좌표) 아직 그런 프레임이 없었으면 None. (손가락을 떼도 지우지 않는다)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)

짝Counterpart: vanilla.js CameraGestures.getZoomAnchor

메서드methodpub fn take_right_click(&mut self) -> bool#0.4.0 부터since 0.4.0소스source

오른쪽 클릭이 있었는지 꺼내기. (끌지 않고 뗀 오른쪽 단추, 꺼내면 지운다)

Takes whether a right click happened. (right button released without dragging, cleared once taken)

반환Returnsbool 지난번 꺼낸 뒤 오른쪽 클릭이 있었으면 true.true if a right click happened since the last take.

짝Counterpart: vanilla.js CameraGestures.takeRightClick

메서드methodpub fn take_rotation_released(&mut self) -> bool#0.4.0 부터since 0.4.0소스source

돌리기를 놓았는지 꺼내기. (꺼내면 지운다)

Takes whether the rotation was released. (cleared once taken)

반환Returnsbool 지난번 꺼낸 뒤 오른쪽 단추를 떼었거나 두 손가락 손짓이 끝났으면 true.true if the right button was released or a two-finger gesture ended since the last take.

짝Counterpart: vanilla.js CameraGestures.takeRotationReleased

메서드methodpub fn set_thresholds(&mut self, twist_start_radians: f64, tilt_per_half_height_radians: f64, right_click_distance: f64, compatibility_mouse_milliseconds: f64)#0.4.0 부터since 0.4.0소스source

문턱 설정. (비틀기를 시작하는 각도, 캔버스 높이의 반만큼 끌 때 기울어지는 각도, 오른쪽 클릭으로 보는 거리, 흉내 낸 마우스를 무시하는 밀리초)

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_radiansf64
두 손가락 비틀기가 이만큼 넘어야 돌리기를 시작한다. (라디안, 처음 15 도)A two-finger twist must exceed this to start rotating. (radians, initially 15 degrees)
tilt_per_half_height_radiansf64
오른쪽 단추로 캔버스 높이의 반만큼 세로로 끌 때 기울어지는 각도. (라디안, 처음 35 도)Tilt angle when dragging vertically half the canvas height with the right button. (radians, initially 35 degrees)
right_click_distancef64
누른 자리에서 이만큼 안에서 떼면 오른쪽 클릭이다. (캔버스 논리 픽셀, 처음 8)Releasing within this distance of the press point is a right click. (canvas logical pixels, initially 8)
compatibility_mouse_millisecondsf64
손가락 사건 뒤 흉내 낸 마우스를 무시하는 시간. (밀리초, 처음 800, 0 이면 무시하지 않는다)Time to ignore emulated mouse events after a finger event. (milliseconds, initially 800, 0 means never ignored)

짝Counterpart: vanilla.js CameraGestures.setThresholds