misc/virtualpad
가상 패드.
- 원본 misc/virtualpad.js 를 옮긴 것이다. 화면 조이스틱의 패드(바깥 원)와 구슬(안쪽 원) 움직임을 계산하고 그린다.
- 원본처럼 컴포넌트가 아니라 그냥 값이다. 들고 있는 쪽이 touch_press / touch_move / touch_release 와 tick, draw 를 불러 준다.
- 구슬을 움직이는 동안 구슬 이동 이벤트로 방향(0 ~ 1, 0 ~ 1 로 바꾼 단위 벡터)과 당긴 세기(0 ~ 1)를 알린다. 떼면 구슬이 초당 1600 픽셀로 패드 가운데로 돌아가며 알리고, 다 돌아오면 (0.5, 0.5), 0 을 한 번 알린다.
- 원본과 다른 점: 상태 이름 move 는 러스트 예약어라 moved 다. 원본 상태 값의 글자("wait", "pressed", "move", "released")는 get_name 으로 얻는다. 이벤트 콜백은 FnMut(Vector2, f64) 다.
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).
VirtualPadstruct traits Default
가상 패드.
- 들고 있는 쪽이 터치 입력을 touch_press, touch_move, touch_release 로 넘기고 매 프레임 tick 과 draw 를 부른다.
Virtual pad.
- The owner passes touch input with touch_press, touch_move, touch_release and calls tick and draw every frame.
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));생성. (패드 반지름 200 검은색, 구슬 반지름 50 흰색, 돌아가는 속도 초당 1600)
Constructor. (pad radius 200 black, knob radius 50 white, return speed 1600 per second)
반환ReturnsVirtualPad 패드와 구슬이 (0, 0) 에 있고 기다리는 상태인 새 가상 패드.A new virtual pad with the pad and knob at (0, 0), in the waiting state.
짝Counterpart: vanilla.js VirtualPad.constructor
갱신. (누르고 끄는 동안 구슬을 패드 안에 가두고, 떼면 가운데로 돌려보낸다)
- 기다리는 상태면 아무것도 하지 않는다. 누르거나 끄는 동안은 tick 마다 구슬 이동 이벤트를 부른다.
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_deltaf64
- 지난 시간. (초, 돌아가는 거리는 초당 1600 픽셀 * time_delta)Elapsed time. (seconds, the return distance is 1600 pixels per second * time_delta)
짝Counterpart: vanilla.js VirtualPad.tick
출력. (바깥 원 다음 안쪽 원, 부른 쪽의 좌표계 그대로)
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)
짝Counterpart: vanilla.js VirtualPad.draw
터치 누름. (구슬을 누른 곳으로)
Touch press. (moves the knob to the press point)
- view_input_positionVector2
- 누른 위치. (패드 위치와 같은 좌표계, 보통 뷰 좌표)Press position. (same coordinate system as the pad position, usually view coordinates)
짝Counterpart: vanilla.js VirtualPad.touchPress
터치 끌기. (구슬을 끈 곳으로)
- 패드 밖이어도 그대로 두고, 다음 tick 에서 패드 가장자리로 가둔다.
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_positionVector2
- 끈 위치. (패드 위치와 같은 좌표계)Drag position. (same coordinate system as the pad position)
짝Counterpart: vanilla.js VirtualPad.touchMove
터치 뗌. (구슬은 그 자리에서 가운데로 돌아가기 시작한다)
Touch release. (the knob starts returning to the center from where it is)
- _view_input_positionVector2
- 뗀 위치. (쓰지 않는다)Release position. (unused)
짝Counterpart: vanilla.js VirtualPad.touchRelease
터치 취소. (뗌과 같다)
Touch cancel. (same as release)
짝Counterpart: vanilla.js VirtualPad.touchCancel
가상 패드 상태 반환.
Returns the virtual pad state.
반환ReturnsVirtualPadState 지금 상태.The current state.
짝Counterpart: vanilla.js VirtualPad.getVirtualPadState
구슬 이동 이벤트 설정. (방향 0 ~ 1, 당긴 세기 0 ~ 1)
Sets the knob move event. (direction 0 ~ 1, pull strength 0 ~ 1)
- callbackimpl FnMut(Vector2, f64) + 'static
- 구슬이 움직일 때 부를 함수. 방향(단위 벡터의 각 축을 (값 + 1) / 2 로 바꾼 것, 가운데가 (0.5, 0.5))과 당긴 세기(패드 가운데에서 거리 / 패드 반지름)를 받는다.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).
짝Counterpart: vanilla.js VirtualPad.setBallMoveEvent
패드 위치 설정. (구슬도 그 자리로)
Sets the pad position. (the knob moves there too)
- pad_positionVector2
- 패드 가운데 위치. (픽셀)Pad center position. (pixels)
짝Counterpart: vanilla.js VirtualPad.setPadPosition
패드 위치 반환.
Returns the pad position.
반환ReturnsVector2 패드 가운데 위치. (픽셀)Pad center position. (pixels)
짝Counterpart: vanilla.js VirtualPad.getPadPosition
패드 반지름 설정.
Sets the pad radius.
- pad_radiusf64
- 패드 반지름. (픽셀, 구슬 중심이 이 안에 갇힌다)Pad radius. (pixels, the knob center is confined within it)
짝Counterpart: vanilla.js VirtualPad.setPadRadius
패드 반지름 반환.
Returns the pad radius.
반환Returnsf64 패드 반지름. (픽셀)Pad radius. (pixels)
짝Counterpart: vanilla.js VirtualPad.getPadRadius
구슬 위치 반환.
Returns the ball position.
반환ReturnsVector2 구슬 가운데 위치. (픽셀)Knob center position. (pixels)
짝Counterpart: vanilla.js VirtualPad.getBallPosition
구슬 반지름 설정.
Sets the knob radius.
- ball_radiusf64
- 구슬 반지름. (픽셀, 그리기에만 쓴다)Knob radius. (pixels, used only for drawing)
짝Counterpart: vanilla.js VirtualPad.setBallRadius
구슬 반지름 반환.
Returns the knob radius.
반환Returnsf64 구슬 반지름. (픽셀)Ball radius. (pixels)
짝Counterpart: vanilla.js VirtualPad.getBallRadius
패드 색 설정.
Sets the pad color.
- pad_colorimpl Into<Color>
- 패드 색. (Color 나 "#000000" 같은 색 글)Pad color. (a Color or a color string like "#000000")
짝Counterpart: vanilla.js VirtualPad.setPadColor
패드 색 반환.
Returns the pad color.
반환ReturnsColor 패드 색.Pad color.
짝Counterpart: vanilla.js VirtualPad.getPadColor
구슬 색 설정.
Sets the knob color.
- ball_colorimpl Into<Color>
- 구슬 색. (Color 나 "#ffffff" 같은 색 글)Knob color. (a Color or a color string like "#ffffff")
짝Counterpart: vanilla.js VirtualPad.setBallColor
구슬 색 반환.
Returns the knob color.
반환ReturnsColor 구슬 색.Ball color.
짝Counterpart: vanilla.js VirtualPad.getBallColor
VirtualPadStateenum traits Clone, Copy, Debug, PartialEq, Eq
가상 패드 상태.
Virtual pad state.
| 이름Name | 설명Description |
|---|---|
None | 기다림. (누르지 않았고 구슬이 가운데에 있다, 원본 "wait")Waiting. (not pressed and the knob is at the center, original "wait") |
Pressed | 눌렀다. (구슬이 누른 곳에 있다)Pressed. (the knob is at the press point) |
Moved | 끄는 중. (원본 "move")Dragging. (original "move") |
Released | 뗐다. (구슬이 가운데로 돌아가는 중, 다 돌아오면 None)Released. (the knob is returning to the center, None once back) |
원본 상태 값의 글자.
String of the original state value.
반환Returns&'static str None 은 "wait", Pressed 는 "pressed", Moved 는 "move", Released 는 "released"."wait" for None, "pressed" for Pressed, "move" for Moved, "released" for Released.