# core/inputactions (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/inputactions/

입력 동작.

## InputActions extends 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 으로 끈다)

예제: 동작에 키, 단추, 축 묶고 읽기

```js
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());
```

- `constructor()` (constructor, since 0.4.0): 생성.
- `createActionState(action)` (method, since 0.4.0): 동작의 상태 칸 반환. (없으면 만든다)
  - `action` string: 동작 이름. (게임이 정한 글)
  - returns object: 동작의 상태. { isPressed, wasPressed, value, repeatSeconds, isRepeatFired }
- `bindKey(action, key)` (method, since 0.4.0): 키 묶기. (KeyboardEvent.code, KeyCode 의 값)
  - `action` string: 동작 이름. (게임이 정한 글)
  - `key` string: 묶을 키 코드. (KeyboardEvent.code, 같은 키는 한 번만)
- `bindGamepadButton(action, buttonCode)` (method, since 0.4.0): 게임패드 단추 묶기. (GamepadButtonCode, 연결된 패드 모두)
  - `action` string: 동작 이름. (게임이 정한 글)
  - `buttonCode` number: 묶을 단추 번호. (W3C 표준 매핑, 0 이 A 나 크로스 단추)
- `bindAxis(action, axisIndex, direction)` (method, since 0.4.0): 게임패드 축의 한쪽 묶기. (GamepadAnalogStickCode, direction 이 1 이면 + 쪽, -1 이면 - 쪽, 축 Y 는 아래가 +)
  - 그쪽으로 setAxisPressThreshold 만큼 기울면 눌린 것이고, 값(readValue)은 죽은 구역을 뺀 기울기다.
  - `action` string: 동작 이름. (게임이 정한 글)
  - `axisIndex` number: 축 번호. (0 왼쪽 가로, 1 왼쪽 세로, 2 오른쪽 가로, 3 오른쪽 세로)
  - `direction` number: 묶을 쪽. (0 이상이면 + 쪽, 음수면 - 쪽)
- `unbindAction(action)` (method, since 0.4.0): 동작에 묶은 것 모두 풀기. (키 바꾸기 화면에서 다시 묶기 전에, 반복 설정은 남는다)
  - `action` string: 동작 이름. (게임이 정한 글)
- `getBoundKeys(action)` (method, since 0.4.0): 동작에 묶은 키 목록 반환. (사본, 단추 안내를 그릴 때)
  - `action` string: 동작 이름. (게임이 정한 글)
  - returns string[]: 묶은 키 코드 목록의 사본. (없으면 빈 배열)
- `setRepeat(action, delaySeconds, intervalSeconds)` (method, since 0.4.0): 누르고 있으면 반복 설정. (기다리는 초 뒤부터 간격 초마다 isJustPressed 가 참, 간격이 0 이하면 반복을 끈다)
  - `action` string: 동작 이름. (게임이 정한 글)
  - `delaySeconds` number: 첫 반복까지 기다리는 시간. (초, 음수는 0)
  - `intervalSeconds` number: 반복 간격. (초, 0 이하면 반복을 끈다)
- `setDeadZone(deadZone)` (method, since 0.4.0): 죽은 구역 설정. (0 ~ 1 미만, 처음은 0.2)
  - `deadZone` number: 죽은 구역. (0 ~ 0.99 로 자른다)
- `getDeadZone()` (method, since 0.4.0): 죽은 구역 반환.
  - returns number: 죽은 구역. (0 ~ 0.99)
- `setAxisPressThreshold(axisPressThreshold)` (method, since 0.4.0): 축에 묶은 동작을 눌린 것으로 보는 기울기 설정. (0 ~ 1, 처음은 0.5)
  - `axisPressThreshold` number: 눌린 것으로 보는 기울기. (0 ~ 1 로 자른다)
- `getAxisPressThreshold()` (method, since 0.4.0): 축에 묶은 동작을 눌린 것으로 보는 기울기 반환.
  - returns number: 눌린 것으로 보는 기울기. (0 ~ 1)
- `setKeysIgnoredWhileTyping(isIgnored)` (method, since 0.4.0): 글자 입력칸에 쓰는 동안 키를 듣지 않을지 설정. (처음은 참)
  - `isIgnored` boolean: 참이면 글자 입력칸에 쓰는 동안 키를 듣지 않는다.
- `isKeysIgnoredWhileTyping()` (method, since 0.4.0): 글자 입력칸에 쓰는 동안 키를 듣지 않는지 반환.
  - returns boolean: 글자 입력칸에 쓰는 동안 키를 듣지 않으면 참.
- `addSource(source)` (method, since 0.4.0): 바깥 입력원 더하기. (이미 있으면 그대로)
  - `source` object: { readActionValue(action), readMoveAxis(), readGamepadKind() }

  예제: 바깥 입력원 끼우기

  ```js
  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);
  ```

- `removeSource(source)` (method, since 0.4.0): 바깥 입력원 빼기.
  - `source` object: 더했던 입력원. (없으면 아무것도 하지 않는다)
- `update(inputManager, timeDelta)` (method, since 0.4.0): 프레임마다 갱신. (InputManager.tick 이 부른다)
  - `inputManager` object: InputManager
  - `timeDelta` number: 지난 프레임부터의 시간. (초)
- `clear()` (method, since 0.4.0): 모두 뗀 것으로 되돌리기. (창이 포커스를 잃었을 때, 뗌도 알리지 않는다)
- `isPressed(action)` (method, since 0.4.0): 동작이 눌려 있는지.
  - `action` string: 동작 이름. (게임이 정한 글)
  - returns boolean: 눌려 있으면 참. (없는 동작이면 거짓)
- `isJustPressed(action)` (method, since 0.4.0): 이번 프레임에 눌렸는지. (setRepeat 를 건 동작은 반복 때도 참)
  - `action` string: 동작 이름. (게임이 정한 글)
  - returns boolean: 이번 프레임에 눌렸거나 반복이 왔으면 참.
- `isJustReleased(action)` (method, since 0.4.0): 이번 프레임에 떼어졌는지.
  - `action` string: 동작 이름. (게임이 정한 글)
  - returns boolean: 이번 프레임에 떼어졌으면 참.
- `readValue(action)` (method, since 0.4.0): 동작의 값. (0 ~ 1, 키와 디지털 단추는 1, 트리거와 축은 기운 만큼)
  - `action` string: 동작 이름. (게임이 정한 글)
  - returns number: 동작의 값. (0 ~ 1, 없는 동작이면 0)
- `readAxis(negativeAction, positiveAction)` (method, since 0.4.0): 두 동작으로 만든 축. (-1 ~ 1, 양쪽 동작의 값의 차)
  - `negativeAction` string: 음수 쪽 동작 이름. (값을 뺀다)
  - `positiveAction` string: 양수 쪽 동작 이름. (값을 더한다)
  - returns number: 양수 쪽 값에서 음수 쪽 값을 뺀 축 값. (-1 ~ 1)
- `readGamepadStick(xAxisIndex, yAxisIndex)` (method, since 0.4.0): 게임패드 스틱. (연결된 패드 가운데 가장 크게 기운 것, 죽은 구역을 길이로 빼고 길이 1 이하, 오른쪽 +x 아래 +y)
  - `xAxisIndex` number: GamepadAnalogStickCode
  - `yAxisIndex` number: GamepadAnalogStickCode
  - returns Vector2: 스틱 벡터. (길이 0 ~ 1, 오른쪽 +x 아래 +y, update 전이면 (0, 0))
- `readMoveAxis()` (method, since 0.4.0): 이동 축. (WASD 와 방향키, 왼쪽 스틱, 바깥 입력원의 이동 축 가운데 가장 긴 것, 길이 1 이하, 오른쪽 +x 아래 +y)
  - 키는 대각선도 길이 1 이다. 글자 입력칸에 쓰는 동안은 키를 듣지 않는다. 값은 이번 프레임 update 때 적은 것이다.
  - returns Vector2: 이동 벡터. (길이 0 ~ 1, 오른쪽 +x 아래 +y, update 전이면 (0, 0))
- `applyDeadZone(value)` (method, since 0.4.0): 죽은 구역 빼기. (죽은 구역 이하면 0, 나머지를 0 ~ 1 로 다시 펴서 조금 기울였을 때 튀지 않게)
  - `value` number: 거를 값. (0 이상의 길이나 한쪽 기울기)
  - returns number: 죽은 구역을 뺀 값. (0 ~ 1)
- `clampLength(x, y)` (method, since 0.4.0): 길이 1 로 자르기.
  - `x` number: X 성분. (유한한 수가 아니면 0)
  - `y` number: Y 성분. (유한한 수가 아니면 0)
  - returns Vector2: 길이가 1 이하인 새 벡터.
