2020-04-26 17:54:56 +02:00
|
|
|
|
2021-02-03 23:11:19 +01:00
|
|
|
const IGNORED_KEYS = new Set([
|
|
|
|
'Esc',
|
|
|
|
'Escape',
|
|
|
|
'Alt',
|
|
|
|
'Meta',
|
|
|
|
'Control',
|
|
|
|
'Ctrl',
|
|
|
|
'Space',
|
|
|
|
'Backspace'
|
|
|
|
])
|
|
|
|
|
2020-05-01 23:19:51 +02:00
|
|
|
export class TextInput extends Phaser.GameObjects.BitmapText {
|
2020-10-15 12:05:40 +02:00
|
|
|
private minUnderLineLength = 4;
|
2020-04-26 18:48:41 +02:00
|
|
|
private underLine: Phaser.GameObjects.Text;
|
2021-02-03 23:11:19 +01:00
|
|
|
private domInput = document.createElement('input');
|
2020-05-26 22:17:00 +02:00
|
|
|
|
2021-02-03 23:11:19 +01:00
|
|
|
constructor(scene: Phaser.Scene, x: number, y: number, maxLength: number, text: string,
|
|
|
|
onChange: (text: string) => void) {
|
2020-05-26 22:17:00 +02:00
|
|
|
super(scene, x, y, 'main_font', text, 32);
|
2021-02-03 23:11:19 +01:00
|
|
|
this.setOrigin(0.5).setCenterAlign();
|
2020-04-26 17:54:56 +02:00
|
|
|
this.scene.add.existing(this);
|
|
|
|
|
2021-02-03 23:11:19 +01:00
|
|
|
const style = {fontFamily: 'Arial', fontSize: "32px", color: '#ffffff'};
|
|
|
|
this.underLine = this.scene.add.text(x, y+1, this.getUnderLineBody(text.length), style);
|
|
|
|
this.underLine.setOrigin(0.5);
|
2020-05-01 23:19:51 +02:00
|
|
|
|
2021-02-03 23:11:19 +01:00
|
|
|
this.domInput.maxLength = maxLength;
|
|
|
|
this.domInput.style.opacity = "0";
|
|
|
|
if (text) {
|
|
|
|
this.domInput.value = text;
|
|
|
|
}
|
2020-04-26 18:48:41 +02:00
|
|
|
|
2021-02-03 23:11:19 +01:00
|
|
|
this.domInput.addEventListener('keydown', event => {
|
|
|
|
if (IGNORED_KEYS.has(event.key)) {
|
|
|
|
return;
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
2021-02-03 23:11:19 +01:00
|
|
|
|
|
|
|
if (!/[a-zA-Z0-9:.!&?()+-]/.exec(event.key)) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.domInput.addEventListener('input', (event) => {
|
|
|
|
if (event.defaultPrevented) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.text = this.domInput.value;
|
2020-10-15 12:05:40 +02:00
|
|
|
this.underLine.text = this.getUnderLineBody(this.text.length);
|
2020-05-26 22:17:00 +02:00
|
|
|
onChange(this.text);
|
2020-04-26 17:54:56 +02:00
|
|
|
});
|
2021-02-03 23:11:19 +01:00
|
|
|
|
|
|
|
document.body.append(this.domInput);
|
|
|
|
this.focus();
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
2021-02-03 23:11:19 +01:00
|
|
|
|
2020-10-15 12:05:40 +02:00
|
|
|
private getUnderLineBody(textLength:number): string {
|
|
|
|
if (textLength < this.minUnderLineLength) textLength = this.minUnderLineLength;
|
|
|
|
let text = '_______';
|
|
|
|
for (let i = this.minUnderLineLength; i < textLength; i++) {
|
2021-02-03 23:11:19 +01:00
|
|
|
text += '__';
|
2020-10-15 12:05:40 +02:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
2020-05-01 23:19:51 +02:00
|
|
|
|
2020-04-26 17:54:56 +02:00
|
|
|
getText(): string {
|
|
|
|
return this.text;
|
|
|
|
}
|
2020-10-07 18:03:34 +02:00
|
|
|
|
|
|
|
setX(x: number): this {
|
|
|
|
super.setX(x);
|
|
|
|
this.underLine.x = x;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
setY(y: number): this {
|
|
|
|
super.setY(y);
|
|
|
|
this.underLine.y = y+1;
|
|
|
|
return this;
|
|
|
|
}
|
2021-02-03 23:11:19 +01:00
|
|
|
|
|
|
|
focus() {
|
|
|
|
this.domInput.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(): void {
|
|
|
|
super.destroy();
|
|
|
|
this.domInput.remove();
|
|
|
|
}
|
2020-05-01 23:19:51 +02:00
|
|
|
}
|