# misc/typewriter (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/typewriter/

타자기 연출 러너.

## Typewriter extends Object

타자기 연출 러너.
- Text / UILabel 처럼 setVisibleCharacterCount() 를 가진 대상의 글자를
  초당 지정 속도로 한 자씩 드러낸다. (비주얼노벨 / RPG 대사 연출)
- 사용:
    const typewriter = new Typewriter(label, { charactersPerSecond: 30 });
    typewriter.start("안녕하세요. 모험을 시작합니다.");
    // 매 프레임: typewriter.tick(timeDelta);
    // 입력에서: typewriter.isFinished() ? next() : typewriter.skipToEnd();
- 글자는 글자 묶음(받침, 결합 부호, 이모지 묶음을 한 자로) 단위로 센다. 글자 공개 알림은 (글자 묶음, 번호) 를 받는다.

예제: 라벨에 대사를 한 자씩 드러내고 누르면 건너뛰기

```js
import { Engine, EngineConfiguration, Scene, WorldNode, UILabel, Pivot, Vector2, Typewriter } from "./build/vanilla.js";

const labelNode = new WorldNode();
const label = labelNode.addComponent(UILabel);
const typewriter = new Typewriter(label, { charactersPerSecond: 20 });

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		labelNode.setPivot(Pivot.topLeft);
		labelNode.setLocalPosition(Vector2.create(40, 40));
		labelNode.setContentSize(Vector2.create(560, 40));
		const root = this.getRoot();
		root.addChild(labelNode);
		typewriter.setCompleteEvent(() => {
			console.log("complete");
		});
		typewriter.start("Hello, adventurer. Your journey begins.");
	}

	tick(timeDelta) {
		super.tick(timeDelta);
		typewriter.tick(timeDelta);
	}

	touchPress(viewInputPosition) {
		super.touchPress(viewInputPosition);
		if (!typewriter.isFinished()) {
			typewriter.skipToEnd();
		}
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor(target, options = {})` (constructor, since 0.3.0): 생성.
  - `target` object: setVisibleCharacterCount() 를 가진 대상. (Text / UILabel)
  - `options` object: { charactersPerSecond = 30, fastForwardMultiplier = 8 }
- `start(text = null)` (method, since 0.3.0): 시작.
  - text 를 넘기면 대상의 setText 도 함께 부른다. 안 넘기면 대상의 현재 글을 쓴다.
  - `text` string | null: 드러낼 글. (null 이면 대상의 지금 글)
- `tick(timeDelta)` (method, since 0.3.0): 갱신.
  - `timeDelta` number: 지난 프레임부터의 시간. (초)
- `skipToEnd()` (method, since 0.3.0): 곧바로 끝까지 드러내기.
- `isFinished()` (method, since 0.3.0): 완료 여부.
  - returns boolean: 다 드러냈으면 참. (start 전에도 참)
- `setFastForward(isFastForward)` (method, since 0.3.0): 빨리 감기 설정. (누르고 있는 동안 등)
  - `isFastForward` boolean: 참이면 fastForwardMultiplier 배로 빨리 드러낸다.
- `setCharactersPerSecond(charactersPerSecond)` (method, since 0.3.0): 속도 설정. (초당 글자 수)
  - `charactersPerSecond` number: 초당 드러낼 글자 수.
- `setCharacterRevealedEvent(characterRevealedEvent)` (method, since 0.3.0): 글자 공개 알림 설정. (character, index) => void
  - `characterRevealedEvent` Function: 글자 하나가 드러날 때마다 부를 함수. (character, index) => void

  예제: 글자가 드러날 때마다 알림 받기

  ```js
  import { Typewriter } from "./build/vanilla.js";
  
  const target = {
  	text: "",
  	visibleCharacterCount: -1,
  	setText(text) {
  		this.text = text;
  	},
  	getText() {
  		return this.text;
  	},
  	setVisibleCharacterCount(visibleCharacterCount) {
  		this.visibleCharacterCount = visibleCharacterCount;
  	},
  };
  const typewriter = new Typewriter(target, { charactersPerSecond: 10 });
  typewriter.setCharacterRevealedEvent((character, index) => {
  	console.log(index, character);
  });
  typewriter.start("Hi!");
  typewriter.tick(0.25);
  console.log(target.visibleCharacterCount, typewriter.isFinished());
  typewriter.tick(0.1);
  console.log(target.visibleCharacterCount, typewriter.isFinished());
  ```

- `setCompleteEvent(completeEvent)` (method, since 0.3.0): 완료 알림 설정.
  - `completeEvent` Function: 다 드러났을 때 부를 함수. () => void
