Menu basically done, grid is a bit buggy but good enough.

This commit is contained in:
2023-01-28 21:28:10 -08:00
parent d14e063ba6
commit a06d24aad8
7 changed files with 52 additions and 14 deletions

View File

@ -37,17 +37,17 @@ std::vector<struct ShaderPassItem> UIGrid::getSelfPassItems(
} }
void UIGrid::onChildAligned(UIComponent *child) { void UIGrid::onChildAligned(UIComponent *child) {
if(this->alignmentListenerLocked) return;
assertNotNull(child); assertNotNull(child);
assertMapHasKey(this->gridChildren, child); assertMapHasKey(this->gridChildren, child);
this->alignmentListenerLocked = true;
this->alignChild(child, this->gridChildren[child]); this->alignChild(child, this->gridChildren[child]);
this->alignmentListenerLocked = false;
} }
void UIGrid::alignChild(UIComponent *child, struct UIGridPosition pos) { void UIGrid::alignChild(UIComponent *child, struct UIGridPosition pos) {
assertNotNull(child); assertNotNull(child);
// Remove event listener
child->eventAlignmentUpdated.addListener(this, &UIGrid::onChildAligned);
float_t gridX = (this->sizeCol * pos.x) + (this->gutterX * pos.x); float_t gridX = (this->sizeCol * pos.x) + (this->gutterX * pos.x);
float_t gridY = (this->sizeRow * pos.y) + (this->gutterY * pos.y); 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), glm::vec4(gridX + x, gridY + y, sizeX, sizeY),
0.0f 0.0f
); );
// Re-Add event listener
child->eventAlignmentUpdated.addListener(this, &UIGrid::onChildAligned);
} }
void UIGrid::setGridSize( void UIGrid::setGridSize(
@ -114,6 +111,9 @@ void UIGrid::addToGrid(
pos.alignY = alignY; pos.alignY = alignY;
this->gridChildren[ui] = pos; this->gridChildren[ui] = pos;
this->alignChild(ui, pos); this->alignChild(ui, pos);
// Re-Add event listener
ui->eventAlignmentUpdated.addListener(this, &UIGrid::onChildAligned);
} }
int32_t UIGrid::getRows() { int32_t UIGrid::getRows() {

View File

@ -24,6 +24,7 @@ namespace Dawn {
float_t gutterY = 0; float_t gutterY = 0;
float_t sizeRow, sizeCol; float_t sizeRow, sizeCol;
std::map<UIComponent*, struct UIGridPosition> gridChildren; std::map<UIComponent*, struct UIGridPosition> gridChildren;
bool_t alignmentListenerLocked = false;
/** /**
* Internal method to update the alignment of a child. * Internal method to update the alignment of a child.

View File

@ -4,6 +4,7 @@
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#include "UIMenu.hpp" #include "UIMenu.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn; using namespace Dawn;
@ -85,7 +86,37 @@ void UIMenu::onActive() {
} }
void UIMenu::onTick() { 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() { UIMenu::~UIMenu() {

View File

@ -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_ENTER);
game->inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_E); game->inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_E);
game->inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_SPACE); game->inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_SPACE);
// game->inputManager.bind(INPUT_BIND_NEGATIVE_X, GLFW_KEY_A); 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_POSITIVE_X, GLFW_KEY_D);
// game->inputManager.bind(INPUT_BIND_NEGATIVE_Y, GLFW_KEY_S); 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_POSITIVE_Y, GLFW_KEY_W);
// Initialize the game // Initialize the game
auto result = game->init(); auto result = game->init();

View File

@ -6,4 +6,10 @@
#pragma once #pragma once
#include "input/InputManager.hpp" #include "input/InputManager.hpp"
#define INPUT_BIND_ACCEPT ((inputbind_t)1) #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)

View File

@ -55,7 +55,7 @@ void TestUIScene::stage() {
label->setFont(&assetFont->font); label->setFont(&assetFont->font);
label->setText("test.1"); label->setText("test.1");
label->setFontSize(24); 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; auto menuItem = new TestMenuItem;
menuItem->label = label; menuItem->label = label;