From a06d24aad82b73897dc2095f80236bd6ed408d06 Mon Sep 17 00:00:00 2001
From: Dominic Masters <dominic@domsplace.com>
Date: Sat, 28 Jan 2023 21:28:10 -0800
Subject: [PATCH] Menu basically done, grid is a bit buggy but good enough.

---
 assets/games/pokergame/locale/en.csv     |  2 +-
 src/dawn/ui/UIGrid.cpp                   | 12 ++++-----
 src/dawn/ui/UIGrid.hpp                   |  1 +
 src/dawn/ui/UIMenu.cpp                   | 33 +++++++++++++++++++++++-
 src/dawnglfw/host/DawnGLFWHost.cpp       |  8 +++---
 src/dawnpokergame/input/InputBinds.hpp   |  8 +++++-
 src/dawnpokergame/scenes/TestUIScene.cpp |  2 +-
 7 files changed, 52 insertions(+), 14 deletions(-)

diff --git a/assets/games/pokergame/locale/en.csv b/assets/games/pokergame/locale/en.csv
index dad5cdd7..1b8b6cbb 100644
--- a/assets/games/pokergame/locale/en.csv
+++ b/assets/games/pokergame/locale/en.csv
@@ -29,5 +29,5 @@ scene.22.1,"King Fight"
 scene.23.1,"Post King Fight, angry he leaves, town rallies around you"
 scene.24.1,"Go to one of the kingsmen who's working with the big bad, he's worried that the townspeople know more than they let on."
 unknown3,"Something here has to bring the big bad to town."
-test.1,"Test "
+test.1,"Test"
 test.2,"Test*"
\ No newline at end of file
diff --git a/src/dawn/ui/UIGrid.cpp b/src/dawn/ui/UIGrid.cpp
index 2a7f51b9..f4b66b32 100644
--- a/src/dawn/ui/UIGrid.cpp
+++ b/src/dawn/ui/UIGrid.cpp
@@ -37,17 +37,17 @@ std::vector<struct ShaderPassItem> UIGrid::getSelfPassItems(
 }
 
 void UIGrid::onChildAligned(UIComponent *child) {
+  if(this->alignmentListenerLocked) return;
   assertNotNull(child);
   assertMapHasKey(this->gridChildren, child);
+  this->alignmentListenerLocked = true;
   this->alignChild(child, this->gridChildren[child]);
+  this->alignmentListenerLocked = false;
 }
 
 void UIGrid::alignChild(UIComponent *child, struct UIGridPosition pos) {
   assertNotNull(child);
 
-  // Remove event listener
-  child->eventAlignmentUpdated.addListener(this, &UIGrid::onChildAligned);
-
   float_t gridX = (this->sizeCol * pos.x) + (this->gutterX * pos.x);
   float_t gridY = (this->sizeRow * pos.y) + (this->gutterY * pos.y);
 
@@ -79,9 +79,6 @@ void UIGrid::alignChild(UIComponent *child, struct UIGridPosition pos) {
     glm::vec4(gridX + x, gridY + y, sizeX, sizeY),
     0.0f
   );
-
-  // Re-Add event listener
-  child->eventAlignmentUpdated.addListener(this, &UIGrid::onChildAligned);
 }
 
 void UIGrid::setGridSize(
@@ -114,6 +111,9 @@ void UIGrid::addToGrid(
   pos.alignY = alignY;
   this->gridChildren[ui] = pos;
   this->alignChild(ui, pos);
+
+  // Re-Add event listener
+  ui->eventAlignmentUpdated.addListener(this, &UIGrid::onChildAligned);
 }
 
 int32_t UIGrid::getRows() {
diff --git a/src/dawn/ui/UIGrid.hpp b/src/dawn/ui/UIGrid.hpp
index 02b5e873..685678b2 100644
--- a/src/dawn/ui/UIGrid.hpp
+++ b/src/dawn/ui/UIGrid.hpp
@@ -24,6 +24,7 @@ namespace Dawn {
       float_t gutterY = 0;
       float_t sizeRow, sizeCol;
       std::map<UIComponent*, struct UIGridPosition> gridChildren;
+      bool_t alignmentListenerLocked = false;
 
       /**
        * Internal method to update the alignment of a child.
diff --git a/src/dawn/ui/UIMenu.cpp b/src/dawn/ui/UIMenu.cpp
index fada72a2..0078de40 100644
--- a/src/dawn/ui/UIMenu.cpp
+++ b/src/dawn/ui/UIMenu.cpp
@@ -4,6 +4,7 @@
 // https://opensource.org/licenses/MIT
 
 #include "UIMenu.hpp"
+#include "game/DawnGame.hpp"
 
 using namespace Dawn;
 
@@ -85,7 +86,37 @@ void UIMenu::onActive() {
 }
 
 void UIMenu::onTick() {
-  std::cout << "Tick menu" << std::endl;
+  auto im = &this->canvas->getGame()->inputManager;
+
+  if(im->isPressed(INPUT_BIND_ACCEPT)) {
+    auto item = this->getItem(this->x, this->y);
+    if(item != nullptr && item->canBeSelected()) {
+      item->onItemSelected();
+      return;
+    }
+  }
+  
+  if(im->isPressed(INPUT_BIND_NEGATIVE_Y)) {
+    this->moveRelative(0, 1);
+    return;
+  }
+  
+  if(im->isPressed(INPUT_BIND_POSITIVE_Y)) {
+    this->moveRelative(0, -1);
+    return;
+  }
+  
+  if(im->isPressed(INPUT_BIND_NEGATIVE_X)) {
+    this->moveRelative(-1, 0);
+    return;
+  }
+
+  if(im->isPressed(INPUT_BIND_POSITIVE_X)) {
+    this->moveRelative(1, 0);
+    return;
+  }
+
+  // TODO: Support more modes and holding buttons to scroll.
 }
 
 UIMenu::~UIMenu() {
diff --git a/src/dawnglfw/host/DawnGLFWHost.cpp b/src/dawnglfw/host/DawnGLFWHost.cpp
index 07728722..31fafe34 100644
--- a/src/dawnglfw/host/DawnGLFWHost.cpp
+++ b/src/dawnglfw/host/DawnGLFWHost.cpp
@@ -61,10 +61,10 @@ int32_t DawnHost::init(DawnGame *game) {
   game->inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_ENTER);
   game->inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_E);
   game->inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_SPACE);
-  // game->inputManager.bind(INPUT_BIND_NEGATIVE_X, GLFW_KEY_A);
-  // game->inputManager.bind(INPUT_BIND_POSITIVE_X, GLFW_KEY_D);
-  // game->inputManager.bind(INPUT_BIND_NEGATIVE_Y, GLFW_KEY_S);
-  // game->inputManager.bind(INPUT_BIND_POSITIVE_Y, GLFW_KEY_W);
+  game->inputManager.bind(INPUT_BIND_NEGATIVE_X, GLFW_KEY_A);
+  game->inputManager.bind(INPUT_BIND_POSITIVE_X, GLFW_KEY_D);
+  game->inputManager.bind(INPUT_BIND_NEGATIVE_Y, GLFW_KEY_S);
+  game->inputManager.bind(INPUT_BIND_POSITIVE_Y, GLFW_KEY_W);
 
   // Initialize the game
   auto result = game->init();
diff --git a/src/dawnpokergame/input/InputBinds.hpp b/src/dawnpokergame/input/InputBinds.hpp
index 9e400ad8..1a97e236 100644
--- a/src/dawnpokergame/input/InputBinds.hpp
+++ b/src/dawnpokergame/input/InputBinds.hpp
@@ -6,4 +6,10 @@
 #pragma once
 #include "input/InputManager.hpp"
 
-#define INPUT_BIND_ACCEPT ((inputbind_t)1)
\ No newline at end of file
+#define dbind(n) ((inputbind_t)n)
+
+#define INPUT_BIND_ACCEPT dbind(1)
+#define INPUT_BIND_NEGATIVE_X dbind(2)
+#define INPUT_BIND_POSITIVE_X dbind(3)
+#define INPUT_BIND_NEGATIVE_Y dbind(4)
+#define INPUT_BIND_POSITIVE_Y dbind(5)
\ No newline at end of file
diff --git a/src/dawnpokergame/scenes/TestUIScene.cpp b/src/dawnpokergame/scenes/TestUIScene.cpp
index 050e8db6..8329135d 100644
--- a/src/dawnpokergame/scenes/TestUIScene.cpp
+++ b/src/dawnpokergame/scenes/TestUIScene.cpp
@@ -55,7 +55,7 @@ void TestUIScene::stage() {
       label->setFont(&assetFont->font);
       label->setText("test.1");
       label->setFontSize(24);
-      grid->addToGrid(label, x, y, UI_COMPONENT_ALIGN_END, UI_COMPONENT_ALIGN_END);
+      grid->addToGrid(label, x, y, UI_COMPONENT_ALIGN_MIDDLE, UI_COMPONENT_ALIGN_MIDDLE);
 
       auto menuItem = new TestMenuItem;
       menuItem->label = label;