core/inputactions
입력 동작.
Input actions.
InputActionsextends Object
입력 동작.
- 동작 이름("confirm", "left" 같은 게임이 정한 글)에 키, 게임패드 단추, 게임패드 축의 한쪽을 묶는다. 한 동작에 여럿을 묶을 수 있고, 하나라도 눌렸으면 눌린 것이다.
- 엔진의 InputManager 가 하나 들고(getInputActions) 프레임 첫머리의 tick 에서 update 를 부른다. 게임은 읽기만 한다.
- 유니티 Input System 처럼 프레임 처음의 update 에서 한 번 갱신하고, 그 프레임 안의 읽기(readGamepadStick, readMoveAxis 도)는 그때 적은 값이다. (러스트 판과 같다)
- 키는 프레임 사이에 눌렀다 뗀 것도 그 프레임에 한 번 눌린 것으로 본다. (InputManager.isKeyJustPressed)
- setRepeat 를 건 동작은 누르고 있으면 기다린 뒤부터 간격마다 isJustPressed 가 다시 참이 된다. (메뉴 고르기)
- 축은 죽은 구역(setDeadZone)을 빼고 나머지를 0 ~ 1 로 다시 편다. 스틱은 길이로, 묶은 축은 한쪽 값으로 거른다.
- 바깥 입력원(addSource)은 엔진이 모르는 입력(플랫폼 SDK 의 패드 같은 것)을 게임이 끼우는 자리다. 아래 함수를 가진 객체다. readActionValue(action) → 0 ~ 1 의 수 또는 참거짓. (0.5 이상이면 눌림) readMoveAxis() → { x, y } 또는 null. (없어도 된다, readMoveAxis 에 합친다) readGamepadKind() → GamepadKind 의 값. (없어도 된다, 이 입력원이 입력을 넣은 프레임에 마지막 장치를 게임패드와 그 종류로 적는다)
- 글자 입력칸에 쓰는 동안은 키를 듣지 않는다. (setKeysIgnoredWhileTyping 으로 끈다)
Input actions.
- Binds keys, gamepad buttons, and one side of gamepad axes to action names (strings the game defines, like "confirm", "left"). Several can be bound to one action, and it is pressed if any one of them is pressed.
- The engine's InputManager holds one (getInputActions) and calls update in tick at the start of the frame. The game only reads.
- Like the Unity Input System, it updates once in update at the start of the frame, and reads within that frame (readGamepadStick and readMoveAxis too) return the values recorded then. (same as the Rust engine)
- A key pressed and released between frames counts as pressed once in that frame. (InputManager.isKeyJustPressed)
- For an action with setRepeat, holding it makes isJustPressed true again at each interval after a wait. (menu selection)
- Axes subtract the dead zone (setDeadZone) and rescale the rest to 0 ~ 1. Sticks are filtered by length, bound axes by their one-side value.
- External sources (addSource) are where the game plugs in input the engine doesn't know (such as a platform SDK's pad). They are objects with the functions below. readActionValue(action) → a number 0 ~ 1 or a boolean. (pressed if 0.5 or more) readMoveAxis() → { x, y } or null. (optional, merged into readMoveAxis) readGamepadKind() → a GamepadKind value. (optional, in frames where this source provided input, records the last device as a gamepad of that kind)
- Keys are not listened to while typing in a text field. (turned off with setKeysIgnoredWhileTyping)
import { Engine, EngineConfiguration, Scene } from "./build/vanilla.js";
class MainScene extends Scene {
initialize(engine) {
const inputManager = engine.getInputManager();
const inputActions = inputManager.getInputActions();
inputActions.bindKey("confirm", "Enter");
inputActions.bindKey("confirm", "Space");
inputActions.bindGamepadButton("confirm", 0);
inputActions.bindKey("down", "ArrowDown");
inputActions.bindAxis("down", 1, 1);
inputActions.setRepeat("down", 0.4, 0.1);
}
tick(timeDelta) {
super.tick(timeDelta);
const engine = this.getEngine();
const inputManager = engine.getInputManager();
const inputActions = inputManager.getInputActions();
if (inputActions.isJustPressed("confirm")) {
console.log("confirm");
}
if (inputActions.isJustPressed("down")) {
console.log("next item");
}
const moveAxis = inputActions.readMoveAxis();
if (moveAxis.x !== 0 || moveAxis.y !== 0) {
console.log(moveAxis.x, moveAxis.y);
}
}
}
const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());동작의 상태 칸 반환. (없으면 만든다)
Returns the action's state slot. (creates it if missing)
- actionstring
- 동작 이름. (게임이 정한 글)Action name. (text chosen by the game)
반환Returnsobject 동작의 상태. { isPressed, wasPressed, value, repeatSeconds, isRepeatFired }The action's state. { isPressed, wasPressed, value, repeatSeconds, isRepeatFired }
키 묶기. (KeyboardEvent.code, KeyCode 의 값)
Binds a key. (KeyboardEvent.code, a KeyCode value)
- actionstring
- 동작 이름. (게임이 정한 글)Action name. (text chosen by the game)
- keystring
- 묶을 키 코드. (KeyboardEvent.code, 같은 키는 한 번만)Key code to bind. (KeyboardEvent.code, the same key only once)
짝Counterpart: vanilla-engine InputActions.bind_key
게임패드 단추 묶기. (GamepadButtonCode, 연결된 패드 모두)
Binds a gamepad button. (GamepadButtonCode, all connected pads)
- actionstring
- 동작 이름. (게임이 정한 글)Action name. (text chosen by the game)
- buttonCodenumber
- 묶을 단추 번호. (W3C 표준 매핑, 0 이 A 나 크로스 단추)Button index to bind. (W3C standard mapping, 0 is the A or cross button)
짝Counterpart: vanilla-engine InputActions.bind_gamepad_button
게임패드 축의 한쪽 묶기. (GamepadAnalogStickCode, direction 이 1 이면 + 쪽, -1 이면 - 쪽, 축 Y 는 아래가 +)
- 그쪽으로 setAxisPressThreshold 만큼 기울면 눌린 것이고, 값(readValue)은 죽은 구역을 뺀 기울기다.
Binds one side of a gamepad axis. (GamepadAnalogStickCode, direction 1 is the + side, -1 is the - side, axis Y is + downward)
- Tilting that way by setAxisPressThreshold counts as pressed, and the value (readValue) is the tilt minus the dead zone.
- actionstring
- 동작 이름. (게임이 정한 글)Action name. (text chosen by the game)
- axisIndexnumber
- 축 번호. (0 왼쪽 가로, 1 왼쪽 세로, 2 오른쪽 가로, 3 오른쪽 세로)Axis index. (0 left horizontal, 1 left vertical, 2 right horizontal, 3 right vertical)
- directionnumber
- 묶을 쪽. (0 이상이면 + 쪽, 음수면 - 쪽)Side to bind. (+ side if 0 or more, - side if negative)
짝Counterpart: vanilla-engine InputActions.bind_axis
동작에 묶은 것 모두 풀기. (키 바꾸기 화면에서 다시 묶기 전에, 반복 설정은 남는다)
Unbinds everything bound to an action. (before rebinding in a key remapping screen, repeat settings remain)
- actionstring
- 동작 이름. (게임이 정한 글)Action name. (text chosen by the game)
짝Counterpart: vanilla-engine InputActions.unbind_action
동작에 묶은 키 목록 반환. (사본, 단추 안내를 그릴 때)
Returns the list of keys bound to an action. (a copy, for drawing button prompts)
- actionstring
- 동작 이름. (게임이 정한 글)Action name. (text chosen by the game)
반환Returnsstring[] 묶은 키 코드 목록의 사본. (없으면 빈 배열)A copy of the bound key code list. (empty array if none)
짝Counterpart: vanilla-engine InputActions.get_bound_keys
누르고 있으면 반복 설정. (기다리는 초 뒤부터 간격 초마다 isJustPressed 가 참, 간격이 0 이하면 반복을 끈다)
Sets repeat while held. (isJustPressed is true every interval seconds after the wait seconds, an interval of 0 or less turns repeat off)
- actionstring
- 동작 이름. (게임이 정한 글)Action name. (text chosen by the game)
- delaySecondsnumber
- 첫 반복까지 기다리는 시간. (초, 음수는 0)Time to wait before the first repeat. (seconds, negative becomes 0)
- intervalSecondsnumber
- 반복 간격. (초, 0 이하면 반복을 끈다)Repeat interval. (seconds, 0 or less turns repeat off)
짝Counterpart: vanilla-engine InputActions.set_repeat
죽은 구역 설정. (0 ~ 1 미만, 처음은 0.2)
Sets the dead zone. (0 ~ less than 1, initially 0.2)
- deadZonenumber
- 죽은 구역. (0 ~ 0.99 로 자른다)Dead zone. (clamped to 0 ~ 0.99)
짝Counterpart: vanilla-engine InputActions.set_dead_zone
죽은 구역 반환.
Returns the dead zone.
반환Returnsnumber 죽은 구역. (0 ~ 0.99)Dead zone. (0 ~ 0.99)
짝Counterpart: vanilla-engine InputActions.get_dead_zone
축에 묶은 동작을 눌린 것으로 보는 기울기 설정. (0 ~ 1, 처음은 0.5)
Sets the tilt at which an axis-bound action counts as pressed. (0 ~ 1, initially 0.5)
- axisPressThresholdnumber
- 눌린 것으로 보는 기울기. (0 ~ 1 로 자른다)Tilt counted as pressed. (clamped to 0 ~ 1)
짝Counterpart: vanilla-engine InputActions.set_axis_press_threshold
축에 묶은 동작을 눌린 것으로 보는 기울기 반환.
Returns the tilt at which an axis-bound action counts as pressed.
반환Returnsnumber 눌린 것으로 보는 기울기. (0 ~ 1)Tilt counted as pressed. (0 ~ 1)
짝Counterpart: vanilla-engine InputActions.get_axis_press_threshold
글자 입력칸에 쓰는 동안 키를 듣지 않을지 설정. (처음은 참)
Sets whether keys are ignored while typing in a text field. (initially true)
- isIgnoredboolean
- 참이면 글자 입력칸에 쓰는 동안 키를 듣지 않는다.If true, keys are ignored while typing in a text input.
짝Counterpart: vanilla-engine InputActions.set_keys_ignored_while_typing
글자 입력칸에 쓰는 동안 키를 듣지 않는지 반환.
Returns whether keys are ignored while typing in a text field.
반환Returnsboolean 글자 입력칸에 쓰는 동안 키를 듣지 않으면 참.True if keys are ignored while typing in a text input.
짝Counterpart: vanilla-engine InputActions.is_keys_ignored_while_typing
바깥 입력원 더하기. (이미 있으면 그대로)
Adds an external source. (unchanged if already present)
- sourceobject
- { readActionValue(action), readMoveAxis(), readGamepadKind() }
짝Counterpart: vanilla-engine InputActions.add_source
import { Engine, EngineConfiguration, GamepadKind } from "./build/vanilla.js";
const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const inputManager = engine.getInputManager();
const inputActions = inputManager.getInputActions();
inputActions.bindKey("confirm", "Enter");
const platformSource = {
readActionValue(action) {
if (action === "confirm") {
return 1;
}
return 0;
},
readMoveAxis() {
return { x: 0.5, y: 0 };
},
readGamepadKind() {
return GamepadKind.xbox;
},
};
inputActions.addSource(platformSource);
inputActions.update(inputManager, 0);
const moveAxis = inputActions.readMoveAxis();
const isConfirmPressed = inputActions.isPressed("confirm");
const lastInputDevice = inputManager.getLastInputDevice();
console.log(isConfirmPressed, moveAxis.x, lastInputDevice);
inputActions.removeSource(platformSource);바깥 입력원 빼기.
Removes an external source.
- sourceobject
- 더했던 입력원. (없으면 아무것도 하지 않는다)A source added before. (does nothing if not found)
짝Counterpart: vanilla-engine InputActions.remove_source
프레임마다 갱신. (InputManager.tick 이 부른다)
Per-frame update. (called by InputManager.tick)
- inputManagerobject
- InputManager
- timeDeltanumber
- 지난 프레임부터의 시간. (초)Time since the last frame. (Seconds)
짝Counterpart: vanilla-engine InputActions.update
모두 뗀 것으로 되돌리기. (창이 포커스를 잃었을 때, 뗌도 알리지 않는다)
Resets everything to released. (when the window loses focus, releases are not reported either)
짝Counterpart: vanilla-engine InputActions.clear
동작이 눌려 있는지.
Whether the action is held.
- actionstring
- 동작 이름. (게임이 정한 글)Action name. (text chosen by the game)
반환Returnsboolean 눌려 있으면 참. (없는 동작이면 거짓)True if held down. (false for an unknown action)
짝Counterpart: vanilla-engine InputActions.is_pressed
이번 프레임에 눌렸는지. (setRepeat 를 건 동작은 반복 때도 참)
Whether it was pressed this frame. (also true on repeats for actions with setRepeat)
- actionstring
- 동작 이름. (게임이 정한 글)Action name. (text chosen by the game)
반환Returnsboolean 이번 프레임에 눌렸거나 반복이 왔으면 참.True if pressed this frame or a repeat fired.
짝Counterpart: vanilla-engine InputActions.is_just_pressed
이번 프레임에 떼어졌는지.
Whether it was released this frame.
- actionstring
- 동작 이름. (게임이 정한 글)Action name. (text chosen by the game)
반환Returnsboolean 이번 프레임에 떼어졌으면 참.True if released this frame.
짝Counterpart: vanilla-engine InputActions.is_just_released
동작의 값. (0 ~ 1, 키와 디지털 단추는 1, 트리거와 축은 기운 만큼)
The action's value. (0 ~ 1, 1 for keys and digital buttons, triggers and axes by how far they are tilted)
- actionstring
- 동작 이름. (게임이 정한 글)Action name. (text chosen by the game)
반환Returnsnumber 동작의 값. (0 ~ 1, 없는 동작이면 0)The action's value. (0 ~ 1, 0 for an unknown action)
짝Counterpart: vanilla-engine InputActions.read_value
두 동작으로 만든 축. (-1 ~ 1, 양쪽 동작의 값의 차)
An axis made from two actions. (-1 ~ 1, the difference between the two actions' values)
- negativeActionstring
- 음수 쪽 동작 이름. (값을 뺀다)Action name for the negative side. (its value is subtracted)
- positiveActionstring
- 양수 쪽 동작 이름. (값을 더한다)Action name for the positive side. (its value is added)
반환Returnsnumber 양수 쪽 값에서 음수 쪽 값을 뺀 축 값. (-1 ~ 1)Axis value of the positive side minus the negative side. (-1 ~ 1)
짝Counterpart: vanilla-engine InputActions.read_axis
게임패드 스틱. (연결된 패드 가운데 가장 크게 기운 것, 죽은 구역을 길이로 빼고 길이 1 이하, 오른쪽 +x 아래 +y)
Gamepad stick. (the most tilted among connected pads, dead zone subtracted by length, length 1 or less, right +x down +y)
- xAxisIndexnumber
- GamepadAnalogStickCode
- yAxisIndexnumber
- GamepadAnalogStickCode
반환ReturnsVector2 스틱 벡터. (길이 0 ~ 1, 오른쪽 +x 아래 +y, update 전이면 (0, 0))Stick vector. (length 0 ~ 1, right +x down +y, (0, 0) before update)
짝Counterpart: vanilla-engine InputActions.read_gamepad_stick
이동 축. (WASD 와 방향키, 왼쪽 스틱, 바깥 입력원의 이동 축 가운데 가장 긴 것, 길이 1 이하, 오른쪽 +x 아래 +y)
- 키는 대각선도 길이 1 이다. 글자 입력칸에 쓰는 동안은 키를 듣지 않는다. 값은 이번 프레임 update 때 적은 것이다.
Movement axis. (the longest of WASD and arrow keys, the left stick, and external sources' movement axes, length 1 or less, right +x down +y)
- Keys have length 1 diagonally too. Keys are not listened to while typing in a text field. The values are the ones recorded in this frame's update.
반환ReturnsVector2 이동 벡터. (길이 0 ~ 1, 오른쪽 +x 아래 +y, update 전이면 (0, 0))Move vector. (length 0 ~ 1, right +x down +y, (0, 0) before update)
짝Counterpart: vanilla-engine InputActions.read_move_axis
죽은 구역 빼기. (죽은 구역 이하면 0, 나머지를 0 ~ 1 로 다시 펴서 조금 기울였을 때 튀지 않게)
Subtracts the dead zone. (0 at or below the dead zone, rescales the rest to 0 ~ 1 so a slight tilt doesn't jump)
- valuenumber
- 거를 값. (0 이상의 길이나 한쪽 기울기)Value to filter. (a length or one-sided tilt of 0 or more)
반환Returnsnumber 죽은 구역을 뺀 값. (0 ~ 1)Value with the dead zone removed. (0 ~ 1)
길이 1 로 자르기.
Clamps to length 1.
- xnumber
- X 성분. (유한한 수가 아니면 0)X component. (0 if not a finite number)
- ynumber
- Y 성분. (유한한 수가 아니면 0)Y component. (0 if not a finite number)
반환ReturnsVector2 길이가 1 이하인 새 벡터.A new vector with length 1 or less.