From ed10fd6b6d82481bb34563d0ca009cff89e98ca3 Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Sun, 17 Jan 2021 11:07:15 +0100 Subject: [PATCH] Enable mainMenu --- data/screens.json | 19 +++++++++++-------- src/Controller.cpp | 3 ++- src/EventHandler.cpp | 2 +- src/JsonParser.cpp | 3 ++- src/Menu.cpp | 27 ++++++++++++++++++++++++++- src/Menu.h | 9 +++++++++ src/Screen.h | 2 ++ src/Widget.cpp | 11 +++++++++-- src/Widget.h | 8 +++++++- src/Window.cpp | 14 +++++++++----- src/Window.h | 2 +- src/eventActions.h | 7 +++++++ 12 files changed, 86 insertions(+), 21 deletions(-) diff --git a/data/screens.json b/data/screens.json index 5effff6..4535d31 100644 --- a/data/screens.json +++ b/data/screens.json @@ -8,23 +8,26 @@ } ], "mainMenuScreen": [ - { - "unique_name": "exit", - "position": [0.4, 0.2], - "dimensions": [0.25, 0.10], - "texture": "data/res/textures/exit.png" - }, { "unique_name": "play", "position": [0.4, 0.5], "dimensions": [0.25, 0.1], - "texture": "data/res/textures/play.png" + "texture": "data/res/textures/play.png", + "callbackId": 1 + }, + { + "unique_name": "exit", + "position": [0.4, 0.2], + "dimensions": [0.25, 0.10], + "texture": "data/res/textures/exit.png", + "callbackId": 2 }, { "unique_name": "background", "position": [0.0, 0.0], "dimensions": [1.0, 1.0], - "texture": "data/res/textures/mainMenu.png" + "texture": "data/res/textures/mainMenu.png", + "callbackId": 0 } ] } diff --git a/src/Controller.cpp b/src/Controller.cpp index 235a866..916593e 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -169,7 +169,8 @@ void Controller::run() camera->updateDirectionFromMouseInput(gameEventHandler->getCursorDelta()); } - gameWindow->handleActionRegister(gameEventHandler->getWindowActionRegister()); + menu->writeWindowActions(gameEventHandler->getWindowActionRegister()); + gameWindow->handleWindowActionRegister(gameEventHandler->getWindowActionRegister()); menu->handleMouseButtonActionRegister(gameEventHandler->getMouseButtonActionRegister(), gameWindow); } diff --git a/src/EventHandler.cpp b/src/EventHandler.cpp index d0a37e0..008f187 100644 --- a/src/EventHandler.cpp +++ b/src/EventHandler.cpp @@ -41,7 +41,7 @@ void EventHandler::key_callback(GLFWwindow *window, int key, int scancode, int a (void)mods; if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { - glfwSetWindowShouldClose(window, true); + windowActionRegister[windowActions::windowShouldClose] = 1; } if (key == GLFW_KEY_O && action == GLFW_PRESS) { diff --git a/src/JsonParser.cpp b/src/JsonParser.cpp index 2f14b7b..24ef0be 100644 --- a/src/JsonParser.cpp +++ b/src/JsonParser.cpp @@ -249,7 +249,8 @@ std::vector JsonParser::getWidgetsFromScreen(const Json::Value &screenJ currentWidgetPosition[0].asFloat(), currentWidgetPosition[1].asFloat(), currentWidgetDimensions[0].asFloat(), - currentWidgetDimensions[1].asFloat() + currentWidgetDimensions[1].asFloat(), + currentWidgetJson["callbackId"].asUInt() ); temp_widgets.push_back(currentWidget); } diff --git a/src/Menu.cpp b/src/Menu.cpp index 7ee9bf2..457f106 100644 --- a/src/Menu.cpp +++ b/src/Menu.cpp @@ -2,8 +2,13 @@ #include "JsonParser.h" #include "eventActions.h" #include "helper.h" - #include + +void (Menu::*widgetPressedActionRegister[widgetPressedActions::WIDGET_PRESSED_ACTION_NUM_ITEMS])() = { + &Menu::onPlayPressed, + &Menu::onExitPressed +}; + Menu::Menu(Framebuffer *p_framebuffer, ShaderProgram *p_shaderProgram) : framebuffer(p_framebuffer), shaderProgram(p_shaderProgram) { @@ -58,7 +63,27 @@ void Menu::handleMouseButtonActionRegister(bool *mouseButtonActionRegister, Wind for (auto it = widgets.begin(); it != widgets.end(); it++) { if ((*it)->isHovered(window)) { std::cout << (*it)->getUniqueName() << " clicked!" << std::endl; + if((*it)->getCallbackId() == 1) + resetActiveScreen(); + if((*it)->getCallbackId() == 2) + shouldExit = true; } } } } + +void Menu::writeWindowActions(bool *windowActionRegister) +{ + if (shouldExit) + windowActionRegister[windowActions::windowShouldClose] = true; +} + +void Menu::onPlayPressed() +{ + std::cout << "Hello, from Widget play in Menu :)" << std::endl; +} + +void Menu::onExitPressed() +{ + +} diff --git a/src/Menu.h b/src/Menu.h index 9487c24..eba66a0 100644 --- a/src/Menu.h +++ b/src/Menu.h @@ -5,6 +5,7 @@ #include "Screen.h" #include "Framebuffer.h" #include "JsonParser.h" +#include "eventActions.h" class Menu { @@ -16,15 +17,23 @@ public: void showScreenByName(const char *unique_name); Screen *getActiveScreen(); + void writeWindowActions(bool *windowActionRegister); void resetActiveScreen(); void handleMouseButtonActionRegister(bool *mouseButtonActionRegister, Window* window); + void onPlayPressed(); + void onExitPressed(); + + void (*widgetPressedActionRegister[widgetPressedActions::WIDGET_PRESSED_ACTION_NUM_ITEMS])(); + private: Framebuffer *framebuffer; ShaderProgram *shaderProgram; + bool shouldExit = false; + std::vector screens; Screen *activeScreen; /*Screen *loadingScreen; diff --git a/src/Screen.h b/src/Screen.h index e488346..451fdf2 100644 --- a/src/Screen.h +++ b/src/Screen.h @@ -4,6 +4,8 @@ #include "Framebuffer.h" #include "Widget.h" +class Menu; + class Screen { public: diff --git a/src/Widget.cpp b/src/Widget.cpp index bc6e32a..b25cfb0 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -1,12 +1,14 @@ #include "Widget.h" #include "VertexArray.h" +#include "Menu.h" -Widget::Widget(std::string &name, Texture *texture, float p_x, float p_y, float p_w, float p_h) : +Widget::Widget(std::string &name, Texture *texture, float p_x, float p_y, float p_w, float p_h, uint16_t callbackId) : x(p_x), y(p_y), w(p_w), h(p_h), - unique_name(name) + unique_name(name), + callbackId(callbackId) { widgetTextures.push_back(texture); @@ -44,6 +46,11 @@ std::string Widget::getUniqueName() return unique_name; } +uint16_t Widget::getCallbackId() +{ + return callbackId; +} + void Widget::draw(ShaderProgram *shaderProgram) { shaderProgram->bind(); diff --git a/src/Widget.h b/src/Widget.h index 345fe2b..ba7df76 100644 --- a/src/Widget.h +++ b/src/Widget.h @@ -4,16 +4,20 @@ #include "Mesh.h" #include "Framebuffer.h" #include "Window.h" +#include "eventActions.h" + +class Menu; class Widget { public: - Widget(std::string &name, Texture *texture, float x, float y, float w, float h); + Widget(std::string &name, Texture *texture, float x, float y, float w, float h, uint16_t callbackId); ~Widget(); void draw(ShaderProgram *shaderProgram); std::string getUniqueName(); + uint16_t getCallbackId(); bool isHovered(Window *window); @@ -21,6 +25,8 @@ private: double x, y, w, h; std::string unique_name; + + uint16_t callbackId; std::vector widgetVertices; std::vector widgetIndices; diff --git a/src/Window.cpp b/src/Window.cpp index 8b9b93e..2419470 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -113,10 +113,10 @@ void Window::setCatchedCursor(bool value) } } -void Window::handleActionRegister(bool *windowActionRegister) +void Window::handleWindowActionRegister(bool *windowActionRegister) { - if (windowActionRegister[wireFrameToggle]) { - windowActionRegister[wireFrameToggle] = 0; + if (windowActionRegister[windowActions::wireFrameToggle]) { + windowActionRegister[windowActions::wireFrameToggle] = 0; wireFrameMode = !wireFrameMode; if (wireFrameMode) { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); @@ -125,11 +125,15 @@ void Window::handleActionRegister(bool *windowActionRegister) } } - if (windowActionRegister[mouseCatchToggle]) { - windowActionRegister[mouseCatchToggle] = 0; + if (windowActionRegister[windowActions::mouseCatchToggle]) { + windowActionRegister[windowActions::mouseCatchToggle] = 0; mouseCatched = !mouseCatched; setCatchedCursor(mouseCatched); } + + if (windowActionRegister[windowActions::windowShouldClose]) { + glfwSetWindowShouldClose(window, true); + } } // GLFW error function diff --git a/src/Window.h b/src/Window.h index 5081faa..25aa910 100644 --- a/src/Window.h +++ b/src/Window.h @@ -18,7 +18,7 @@ public: bool isWindowResized(); void updateWindowDimensions(); - void handleActionRegister(bool *windowActionRegister); + void handleWindowActionRegister(bool *windowActionRegister); private: static void glfw_error_callback(int error, const char *description); diff --git a/src/eventActions.h b/src/eventActions.h index 8d7dc40..7c96594 100644 --- a/src/eventActions.h +++ b/src/eventActions.h @@ -19,6 +19,7 @@ enum cameraMouseActions { enum windowActions { wireFrameToggle, mouseCatchToggle, + windowShouldClose, WINDOW_ACTION_NUM_ITEMS }; @@ -28,3 +29,9 @@ enum mouseButtonActions { middleClicked, MOUSE_BUTTON_ACTION_NUM_ITEMS }; + +enum widgetPressedActions { + playClicked, + exitClicked, + WIDGET_PRESSED_ACTION_NUM_ITEMS +};