From 8b654fe6cd65a82b8b2d8cf91cdf26e2d047875d Mon Sep 17 00:00:00 2001
From: Paolo Brasolin <paolo.brasolin@eurac.edu>
Date: Tue, 8 Mar 2022 16:59:34 +0100
Subject: [PATCH] feat: #fe user can type onscreen

---
 frontend/src/js/main.js | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/frontend/src/js/main.js b/frontend/src/js/main.js
index efc40a4..b88254d 100644
--- a/frontend/src/js/main.js
+++ b/frontend/src/js/main.js
@@ -196,6 +196,7 @@ function create() {
     this.cameras.main.width / this.cameras.main.height,
   );
   this.scale.refresh();
+  initAndBindGuessPreview(this);
 }
 
 function createAnim(key, refKey, from, to) {
@@ -473,3 +474,26 @@ class enemy {
 function setAnimation(obj, idleKey) {
   obj.play({ key: idleKey, repeat: -1 });
 }
+
+function initAndBindGuessPreview(scene) {
+  var textEntry = scene.add.text(10, scene.cameras.main.height / 2 - 32, "", {
+    font: "bold 64px Courier",
+    fill: "#ffffff",
+  });
+  scene.input.keyboard.on("keydown", function (event) {
+    if (
+      event.keyCode === Phaser.Input.Keyboard.KeyCodes.BACKSPACE &&
+      textEntry.text.length > 0
+    ) {
+      textEntry.text = textEntry.text.substr(0, textEntry.text.length - 1);
+    } else if (
+      event.keyCode === Phaser.Input.Keyboard.KeyCodes.SPACE ||
+      (event.keyCode >= Phaser.Input.Keyboard.KeyCodes.A &&
+        event.keyCode <= Phaser.Input.Keyboard.KeyCodes.Z)
+    ) {
+      textEntry.text += event.key;
+    } else if (event.keyCode === Phaser.Input.Keyboard.KeyCodes.ENTER) {
+      textEntry.text = textEntry.text.substr(0, 0);
+    }
+  });
+}
-- 
GitLab