# core/gamepadmanager (vanilla.js 0.4.0)

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

입력 매니저.

## GamepadManager extends Object

입력 매니저.

예제: 패드 단추와 스틱 읽기

```js
import { Engine, EngineConfiguration, Scene } from "./build/vanilla.js";

class MainScene extends Scene {
	tick(timeDelta) {
		super.tick(timeDelta);
		const engine = this.getEngine();
		const inputManager = engine.getInputManager();
		const gamepadManager = inputManager.getGamepadManager();
		const gamepads = gamepadManager.getAllConnectedGamepads();
		for (const gamepad of gamepads) {
			if (gamepadManager.isButtonJustPressed(gamepad.index, 0)) {
				console.log("jump", gamepad.id);
			}
			const horizontal = gamepadManager.getAxisValue(gamepad.index, 0);
			if (Math.abs(horizontal) > 0.2) {
				console.log("move", horizontal);
			}
		}
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const inputManager = engine.getInputManager();
const gamepadManager = inputManager.getGamepadManager();
gamepadManager.setCallback((gamepadIndex, inputType, inputIndex, value) => {
	console.log(gamepadIndex, inputType, inputIndex, value);
});
engine.run(new MainScene());
```

- `constructor(engine)` (constructor, since 0.0.9): 생성.
  - `engine` Engine: 이 매니저를 가진 엔진. (쓰지 않는다)
- `tick(timeDelta)` (method, since 0.0.9): 갱신.
  - `timeDelta` number: 지난 프레임부터의 시간. (초)
- `connect(gamepad)` (method, since 0.0.9): 게임패드 연결.
  - `gamepad` Gamepad: 붙일 게임패드. (null 이거나 이미 붙은 번호면 아무것도 하지 않는다)
- `disconnect(gamepad)` (method, since 0.0.9): 게임패드 연결 해제.
  - `gamepad` Gamepad: 놓을 게임패드. (붙지 않은 번호면 아무것도 하지 않는다)
- `setCallback(callback)` (method, since 0.0.9): 콜백 설정.
  - `callback` (gamepadIndex: number, inputType: "button" | "value" | "axis", inputIndex: number, value: any) => void: 단추나 축이 바뀔 때 부를 함수. (button 은 눌림 참 거짓, value 는 0 보다 큰 아날로그 값, axis 는 -1 ~ 1, null 이면 부르지 않는다)
- `updateAllGamepads()` (method, since 0.0.9): 모든 게임패드 갱신.
- `isButtonPressed(gamepadIndex, buttonIndex)` (method, since 0.0.9): 버튼 눌림 상태 반환.
  - `gamepadIndex` number: 패드 번호. (Gamepad.index)
  - `buttonIndex` number: 단추 번호. (W3C 표준 매핑, 0 이 A 나 크로스 단추)
  - returns boolean: 눌려 있으면 참. (없는 패드나 단추면 거짓)
- `isButtonJustPressed(gamepadIndex, buttonIndex)` (method, since 0.4.0): 이번 갱신에 방금 눌린 버튼인지. (키보드 isKeyJustPressed 와 같은 쓰임)
  - `gamepadIndex` number: 패드 번호. (Gamepad.index)
  - `buttonIndex` number: 단추 번호. (W3C 표준 매핑, 0 이 A 나 크로스 단추)
  - returns boolean: 이번 갱신에 눌렸으면 참.
- `isButtonJustReleased(gamepadIndex, buttonIndex)` (method, since 0.4.0): 이번 갱신에 방금 떼어진 버튼인지.
  - `gamepadIndex` number: 패드 번호. (Gamepad.index)
  - `buttonIndex` number: 단추 번호. (W3C 표준 매핑, 0 이 A 나 크로스 단추)
  - returns boolean: 이번 갱신에 떼어졌으면 참.
- `getButtonValue(gamepadIndex, buttonIndex)` (method, since 0.0.9): 버튼/트리거 아날로그 값 반환 (0.0 ~ 1.0).
  - `gamepadIndex` number: 패드 번호. (Gamepad.index)
  - `buttonIndex` number: 단추 번호. (W3C 표준 매핑, 0 이 A 나 크로스 단추)
  - returns number: 아날로그 값. (0 ~ 1, 소수 둘째 자리까지, 없으면 0)
- `getAxisValue(gamepadIndex, axisIndex)` (method, since 0.0.9): 아날로그 스틱 축 값 반환 (-1.0 ~ 1.0).
  - `gamepadIndex` number: 패드 번호. (Gamepad.index)
  - `axisIndex` number: 축 번호. (0 왼쪽 가로, 1 왼쪽 세로, 2 오른쪽 가로, 3 오른쪽 세로)
  - returns number: 축 값. (-1 ~ 1, 소수 둘째 자리까지, 없으면 0)
- `getAllConnectedGamepads()` (method, since 0.0.9): 연결된 모든 게임패드 반환.
  - returns Gamepad[]: 연결된 게임패드 목록. (브라우저의 지금 Gamepad)
- `getConnectedGamepadCount()` (method, since 0.0.9): 연결된 게임패드 수 반환.
  - returns number: 연결된 게임패드 수.
- `getConnectedGamepad(gamepadIndex)` (method, since 0.0.9): 연결된 게임패드 반환.
  - `gamepadIndex` number: 연결된 차례의 번호. (0 ~ 연결 수 - 1, Gamepad.index 가 아니다)
  - returns Gamepad | undefined: 그 차례의 게임패드. (범위 밖이면 undefined)
- `setInputLogEnabled(isEnabled)` (method, since 0.4.0): 단추와 축이 바뀔 때마다 콘솔에 찍을지 설정. (기본은 끔, 연결과 해제는 늘 찍는다)
  - `isEnabled` boolean: 참이면 콘솔에 찍는다.
- `isInputLogEnabled()` (method, since 0.4.0): 단추와 축이 바뀔 때마다 콘솔에 찍는지 반환.
  - returns boolean: 콘솔에 찍으면 참.
- `getActiveGamepadId()` (method, since 0.4.0): 마지막 갱신에 단추를 누르거나 축을 크게 기울인 패드의 이름 반환. (없으면 빈 글, 마지막으로 쓴 장치를 가릴 때)
  - returns string: 패드 이름. (Gamepad.id, 없으면 빈 글)
- `vibrate(strongMagnitude, weakMagnitude, seconds)` (method, since 0.4.0): 연결된 패드를 모두 떨기. (세기는 0 ~ 1, 브라우저 Gamepad.vibrationActuator 의 dual-rumble)
  - 진동 장치가 없는 패드와 브라우저(사파리 등)는 건너뛴다.
  - 앞의 진동이 끝나기 전에 다시 부르면 새 진동으로 바뀐다. (브라우저가 앞 약속을 거절하는 것은 흘린다)
  - `strongMagnitude` number: 무거운 모터의 세기. (묵직한 울림)
  - `weakMagnitude` number: 가벼운 모터의 세기. (잘게 떨림)
  - `seconds` number: 떠는 시간. (초)

  예제: 연결된 패드를 0.25 초 떨기

  ```js
  import { Engine, EngineConfiguration } from "./build/vanilla.js";
  
  const engineConfiguration = new EngineConfiguration();
  const engine = new Engine(engineConfiguration);
  const inputManager = engine.getInputManager();
  const gamepadManager = inputManager.getGamepadManager();
  gamepadManager.vibrate(1, 0.3, 0.25);
  const connectedGamepadCount = gamepadManager.getConnectedGamepadCount();
  console.log(connectedGamepadCount);
  ```

## Functions and constants

- `readGamepadKind(gamepadId)` (method, since 0.4.0): 패드 이름으로 종류 가르기. (플레이스테이션 054c, 닌텐도 057e, 엑스박스 045e 와 이름, 모르면 other)
  - `gamepadId` string: Gamepad.id
  - returns string: GamepadKind 의 값.

## GamepadAnalogStickCode (enum)

게임패드 아날로그 스틱 식별자. (W3C Standard Gamepad 매핑 규격)

- `LEFT_X` = `0`: 왼쪽 스틱 가로축.
- `LEFT_Y` = `1`: 왼쪽 스틱 세로축.
- `RIGHT_X` = `2`: 오른쪽 스틱 가로축.
- `RIGHT_Y` = `3`: 오른쪽 스틱 세로축.

## GamepadTriggerCode (enum)

게임패드 트리거 식별자. (W3C Standard Gamepad 매핑 규격)

- `L2` = `6`: 왼쪽 트리거.
- `R2` = `7`: 오른쪽 트리거.

## GamepadButtonCode (enum)

게임패드 버튼 식별자. (W3C Standard Gamepad 매핑 규격)

- `A_CROSS` = `0`: Face Buttons
- `B_CIRCLE` = `1`: 오른쪽 얼굴 단추. (엑스박스 B, 플레이스테이션 동그라미)
- `X_SQUARE` = `2`: 왼쪽 얼굴 단추. (엑스박스 X, 플레이스테이션 네모)
- `Y_TRIANGLE` = `3`: 위쪽 얼굴 단추. (엑스박스 Y, 플레이스테이션 세모)
- `L1` = `4`: Bumpers
- `R1` = `5`: 오른쪽 범퍼.
- `L2` = `6`: Triggers (버튼으로도 인식됨)
- `R2` = `7`: 오른쪽 트리거 단추.
- `SHARE_VIEW` = `8`: System
- `OPTIONS_MENU` = `9`: 옵션, 메뉴 단추.
- `L3` = `10`: Stick Clicks
- `R3` = `11`: 오른쪽 스틱 누름.
- `DPAD_UP` = `12`: D-Pad
- `DPAD_DOWN` = `13`: 방향 패드 아래.
- `DPAD_LEFT` = `14`: 방향 패드 왼쪽.
- `DPAD_RIGHT` = `15`: 방향 패드 오른쪽.
- `HOME_PS` = `16`: Center
- `TOUCHPAD` = `17`: 터치패드 누름.

## GamepadKind (enum)

게임패드 종류. (단추 안내를 그 패드의 표기로 보일 때 쓴다)

- `xbox` = `xbox`: 엑스박스 패드.
- `playstation` = `playstation`: 플레이스테이션 패드.
- `nintendo` = `nintendo`: 닌텐도 패드.
- `other` = `other`: 그 밖의 패드, 또는 모름.
