assassin-bug/framework/ui/text/index.js

103 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-11-26 01:22:02 +00:00
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import * as EventEmitter from 'eventemitter3';
import { Line } from './line';
import { SoundManager } from './sound-manager';
import { KeyboardManager } from './keyboard-manager';
export class ScrollingText extends EventEmitter {
constructor(text = null, delimiter = '\n', soundSet = null, appearingCharacters = false, characterAppearSpeed = 0) {
super();
this.text = text;
this.delimiter = delimiter;
this.soundSet = soundSet;
this.appearingCharacters = appearingCharacters;
this.characterAppearSpeed = characterAppearSpeed;
this.lines = [];
this.soundManager = new SoundManager(this, this.soundSet);
this.keyboardManager = new KeyboardManager(this);
this.init();
}
setText(text) {
this.text = text;
this.init();
return this;
}
setSoundSet(soundSet) {
this.soundSet = soundSet;
this.init();
this.soundManager.setSoundSet(this.soundSet);
return this;
}
setDelimiter(delimiter) {
this.delimiter = delimiter;
this.init();
return this;
}
setAppearingCharacters(appearing) {
this.appearingCharacters = appearing;
this.init();
return this;
}
setAppearingCharacterSpeed(speed) {
this.characterAppearSpeed = speed;
this.init();
return this;
}
init() {
const split = this.text.split(this.delimiter);
this.lines = split.map((line) => new Line(line));
}
run(element) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
this.wrapper = document.createElement('div');
this.wrapper.setAttribute('aria-role', 'polite');
this.container = document.createElement('div');
this.wrapper.appendChild(this.container);
element.appendChild(this.wrapper);
this.soundManager.init();
this.keyboardManager.init();
this.emit('open');
let index = 0;
this.currentLineIndex = 0;
while (index < this.lines.length) {
this.currentLineIndex = index;
yield this.displayLine(index);
index++;
}
this.emit('close');
this.keyboardManager.release();
this.container.remove();
resolve();
}));
});
}
displayLine(index) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
this.container.innerHTML = '';
this.container.appendChild(this.lines[index].getDOMNode());
this.lines[index].display(this.container, this.appearingCharacters, this.characterAppearSpeed);
this.lines[index].on('character.appear', (event) => this.emit('character.appear', event));
this.lines[index].on('advance', () => {
this.emit('advance');
resolve();
});
});
});
}
getContainer() {
return this.wrapper;
}
getCurrentLine() {
return this.lines[this.currentLineIndex];
}
}