2020-04-26 17:54:56 +02:00
|
|
|
|
|
|
|
export class TextInput extends Phaser.GameObjects.Text {
|
2020-04-26 18:48:41 +02:00
|
|
|
private underLineLength = 10;
|
|
|
|
private underLine: Phaser.GameObjects.Text;
|
2020-04-26 17:54:56 +02:00
|
|
|
constructor(scene: Phaser.Scene, x: number, y: number) {
|
2020-04-26 18:48:41 +02:00
|
|
|
super(scene, x, y, '', { fontFamily: 'Arial', fontSize: "20px", color: '#ffffff'});
|
2020-04-26 17:54:56 +02:00
|
|
|
this.scene.add.existing(this);
|
|
|
|
|
2020-04-26 18:48:41 +02:00
|
|
|
this.underLine = this.scene.add.text(x, y+1, '__________', { fontFamily: 'Arial', fontSize: "20px", color: '#ffffff'})
|
|
|
|
|
2020-04-26 17:54:56 +02:00
|
|
|
|
|
|
|
let keySpace = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
|
|
|
|
let keyBackspace = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.BACKSPACE);
|
|
|
|
this.scene.input.keyboard.on('keydown', (event: any) => {
|
|
|
|
if (event.keyCode === 8 && this.text.length > 0) {
|
2020-04-26 18:48:41 +02:00
|
|
|
this.deleteLetter();
|
2020-04-26 17:54:56 +02:00
|
|
|
} else if (event.keyCode === 32 || (event.keyCode >= 48 && event.keyCode < 90)) {
|
2020-04-26 18:48:41 +02:00
|
|
|
this.addLetter(event.key);
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-26 18:48:41 +02:00
|
|
|
private deleteLetter() {
|
|
|
|
this.text = this.text.substr(0, this.text.length - 1);
|
|
|
|
if (this.underLine.text.length > this.underLineLength) {
|
|
|
|
this.underLine.text = this.underLine.text.substr(0, this.underLine.text.length - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private addLetter(letter: string) {
|
|
|
|
this.text += letter;
|
|
|
|
if (this.text.length > this.underLineLength) {
|
|
|
|
this.underLine.text += '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 17:54:56 +02:00
|
|
|
getText(): string {
|
|
|
|
return this.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|