From f187a25e9e1f3f5664f51415f5413f55e85b7b28 Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Sat, 16 Jan 2021 21:58:42 +0100 Subject: [PATCH] Make Widgets clickable --- .gitmodules | 4 ---- data/screens.json | 9 ++++++++ src/Controller.cpp | 4 +++- src/EventHandler.cpp | 54 ++++++++++++++++++++++++++++++-------------- src/EventHandler.h | 3 +++ src/JsonParser.cpp | 8 ++++--- src/Menu.cpp | 34 ++++++++++++++++++++++++++-- src/Menu.h | 6 +++++ src/Screen.cpp | 14 ++++++++++++ src/Screen.h | 2 ++ src/Widget.cpp | 30 ++++++++++++++++++++++-- src/Widget.h | 9 +++++++- src/eventActions.h | 9 ++++++++ 13 files changed, 156 insertions(+), 30 deletions(-) diff --git a/.gitmodules b/.gitmodules index 9dadc07..7a527c5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,7 +1,3 @@ -[submodule "resources"] - path = data/res - url = git@gitlab.com:4VRDriver/fall-fever-resources.git - [submodule "data/res"] path = data/res url = git@gitlab.com:4VRDriver/fall-fever-resources.git diff --git a/data/screens.json b/data/screens.json index 9dd0ee2..302fbe9 100644 --- a/data/screens.json +++ b/data/screens.json @@ -1,6 +1,7 @@ { "loadingScreen": [ { + "unique_name": "widget0", "position": [0.0, 0.0], "dimensions": [1.0, 1.0], "texture": "data/res/textures/loading.png" @@ -8,14 +9,22 @@ ], "mainMenuScreen": [ { + "unique_name": "widget1", "position": [0.5, 0.5], "dimensions": [0.25, 0.25], "texture": "data/res/textures/container.png" }, { + "unique_name": "widget2", "position": [0.75, 0.0], "dimensions": [0.25, 0.25], "texture": "data/res/textures/tex2.png" + }, + { + "unique_name": "widget3", + "position": [0.05, 0.05], + "dimensions": [0.35, 0.35], + "texture": "data/res/textures/loading.png" } ] } diff --git a/src/Controller.cpp b/src/Controller.cpp index 07d2857..4a3e41d 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -129,7 +129,7 @@ void Controller::run() } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - + menu->showScreenByName("mainMenuScreen"); pp_framebuffer->bind(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -162,6 +162,8 @@ void Controller::run() } gameWindow->handleActionRegister(gameEventHandler->getWindowActionRegister()); + + menu->handleMouseButtonActionRegister(gameEventHandler->getMouseButtonActionRegister(), gameWindow); } } diff --git a/src/EventHandler.cpp b/src/EventHandler.cpp index 63e717b..d0a37e0 100644 --- a/src/EventHandler.cpp +++ b/src/EventHandler.cpp @@ -2,8 +2,9 @@ #include -bool EventHandler::cameraActionRegister[CAMERA_ACTION_NUM_ITEMS] = {0}; double EventHandler::cameraMouseActionRegister[CAMERA_MOUSE_ACTION_NUM_ITEMS] = {0.0, 0.0}; +bool EventHandler::cameraActionRegister[CAMERA_ACTION_NUM_ITEMS] = {0}; +bool EventHandler::mouseButtonActionRegister[MOUSE_BUTTON_ACTION_NUM_ITEMS] = {0}; bool EventHandler::windowActionRegister[WINDOW_ACTION_NUM_ITEMS] = {0}; bool EventHandler::firstMouseInput = 1; @@ -15,6 +16,7 @@ EventHandler::EventHandler(GLFWwindow *p_window) : { glfwSetKeyCallback(window, key_callback); glfwSetCursorPosCallback(window, mouse_callback); + glfwSetMouseButtonCallback(window, mouse_button_callback); } void EventHandler::handleEvents() @@ -28,6 +30,7 @@ void EventHandler::clearActionRegisters() //std::fill_n(cameraActionRegister, CAMERA_ACTION_NUM_ITEMS, 0); std::fill_n(cameraMouseActionRegister, CAMERA_MOUSE_ACTION_NUM_ITEMS, 0.0); std::fill_n(windowActionRegister, WINDOW_ACTION_NUM_ITEMS, 0); + std::fill_n(mouseButtonActionRegister, MOUSE_BUTTON_ACTION_NUM_ITEMS, 0); } void EventHandler::key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) @@ -42,51 +45,51 @@ void EventHandler::key_callback(GLFWwindow *window, int key, int scancode, int a } if (key == GLFW_KEY_O && action == GLFW_PRESS) { - windowActionRegister[wireFrameToggle] = 1; + windowActionRegister[windowActions::wireFrameToggle] = 1; } if (key == GLFW_KEY_LEFT_CONTROL && action == GLFW_PRESS) { - windowActionRegister[mouseCatchToggle] = 1; + windowActionRegister[windowActions::mouseCatchToggle] = 1; firstMouseInput = 1; } // Movement press if (key == GLFW_KEY_W && action == GLFW_PRESS) { - cameraActionRegister[cameraForward] = 1; + cameraActionRegister[cameraActions::cameraForward] = 1; } if (key == GLFW_KEY_S && action == GLFW_PRESS) { - cameraActionRegister[cameraBackward] = 1; + cameraActionRegister[cameraActions::cameraBackward] = 1; } if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) { - cameraActionRegister[cameraUp] = 1; + cameraActionRegister[cameraActions::cameraUp] = 1; } if (key == GLFW_KEY_LEFT_SHIFT && action == GLFW_PRESS) { - cameraActionRegister[cameraDown] = 1; + cameraActionRegister[cameraActions::cameraDown] = 1; } if (key == GLFW_KEY_A && action == GLFW_PRESS) { - cameraActionRegister[cameraLeft] = 1; + cameraActionRegister[cameraActions::cameraLeft] = 1; } if (key == GLFW_KEY_D && action == GLFW_PRESS) { - cameraActionRegister[cameraRight] = 1; + cameraActionRegister[cameraActions::cameraRight] = 1; } // Movement release if (key == GLFW_KEY_W && action == GLFW_RELEASE) { - cameraActionRegister[cameraForward] = 0; + cameraActionRegister[cameraActions::cameraForward] = 0; } if (key == GLFW_KEY_S && action == GLFW_RELEASE) { - cameraActionRegister[cameraBackward] = 0; + cameraActionRegister[cameraActions::cameraBackward] = 0; } if (key == GLFW_KEY_SPACE && action == GLFW_RELEASE) { - cameraActionRegister[cameraUp] = 0; + cameraActionRegister[cameraActions::cameraUp] = 0; } if (key == GLFW_KEY_LEFT_SHIFT && action == GLFW_RELEASE) { - cameraActionRegister[cameraDown] = 0; + cameraActionRegister[cameraActions::cameraDown] = 0; } if (key == GLFW_KEY_A && action == GLFW_RELEASE) { - cameraActionRegister[cameraLeft] = 0; + cameraActionRegister[cameraActions::cameraLeft] = 0; } if (key == GLFW_KEY_D && action == GLFW_RELEASE) { - cameraActionRegister[cameraRight] = 0; + cameraActionRegister[cameraActions::cameraRight] = 0; } } @@ -113,8 +116,20 @@ void EventHandler::mouse_callback(GLFWwindow *window, double xpos, double ypos) deltaCursorPosX *= mouseSensitivity; deltaCursorPosY *= mouseSensitivity; - cameraMouseActionRegister[cameraMouseDeltaX] += deltaCursorPosX; - cameraMouseActionRegister[cameraMouseDeltaY] += deltaCursorPosY; + cameraMouseActionRegister[cameraMouseActions::cameraMouseDeltaX] += deltaCursorPosX; + cameraMouseActionRegister[cameraMouseActions::cameraMouseDeltaY] += deltaCursorPosY; +} + +void EventHandler::mouse_button_callback(GLFWwindow* window, int button, int action, int mods) +{ + (void) window; (void) mods; + + if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) + mouseButtonActionRegister[mouseButtonActions::leftClicked] = true; + if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) + mouseButtonActionRegister[mouseButtonActions::rightClicked] = true; + if (button == GLFW_MOUSE_BUTTON_MIDDLE && action == GLFW_PRESS) + mouseButtonActionRegister[mouseButtonActions::middleClicked] = true; } bool * EventHandler::getCameraActionRegister() @@ -127,6 +142,11 @@ bool * EventHandler::getWindowActionRegister() return windowActionRegister; } +bool * EventHandler::getMouseButtonActionRegister() +{ + return mouseButtonActionRegister; +} + double * EventHandler::getCursorDelta() { return cameraMouseActionRegister; diff --git a/src/EventHandler.h b/src/EventHandler.h index be3de19..2c433ec 100644 --- a/src/EventHandler.h +++ b/src/EventHandler.h @@ -14,6 +14,7 @@ public: bool *getCameraActionRegister(); bool *getWindowActionRegister(); + bool *getMouseButtonActionRegister(); double *getCursorDelta(); void setFirstMouseInput(bool val); @@ -21,12 +22,14 @@ private: void clearActionRegisters(); static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods); + static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods); static void mouse_callback(GLFWwindow *window, double xpos, double ypos); private: static bool cameraActionRegister[CAMERA_ACTION_NUM_ITEMS]; static double cameraMouseActionRegister[CAMERA_MOUSE_ACTION_NUM_ITEMS]; static bool windowActionRegister[WINDOW_ACTION_NUM_ITEMS]; + static bool mouseButtonActionRegister[MOUSE_BUTTON_ACTION_NUM_ITEMS]; GLFWwindow *window; diff --git a/src/JsonParser.cpp b/src/JsonParser.cpp index 9a8e3e9..2f14b7b 100644 --- a/src/JsonParser.cpp +++ b/src/JsonParser.cpp @@ -238,11 +238,13 @@ std::vector JsonParser::getWidgetsFromScreen(const Json::Value &screenJ unsigned int index = 0; for (; index < screenJson.size(); index++) { const Json::Value currentWidgetJson = screenJson[index]; - const Json::Value currentWidgetTextureJson =currentWidgetJson["texture"]; - const Json::Value currentWidgetPosition =currentWidgetJson["position"]; - const Json::Value currentWidgetDimensions =currentWidgetJson["dimensions"]; + const Json::Value currentWidgetTextureJson = currentWidgetJson["texture"]; + const Json::Value currentWidgetPosition = currentWidgetJson["position"]; + const Json::Value currentWidgetDimensions = currentWidgetJson["dimensions"]; + std::string name = currentWidgetJson["unique_name"].asString(); Texture *currentWidgetTexture = new Texture(currentWidgetTextureJson.asString().c_str(), textureType::texture_diffuse); Widget *currentWidget = new Widget( + name, currentWidgetTexture, currentWidgetPosition[0].asFloat(), currentWidgetPosition[1].asFloat(), diff --git a/src/Menu.cpp b/src/Menu.cpp index 2ea165d..8abfb4e 100644 --- a/src/Menu.cpp +++ b/src/Menu.cpp @@ -1,6 +1,7 @@ #include "Menu.h" #include "JsonParser.h" - +#include "eventActions.h" +#include Menu::Menu(Framebuffer *p_framebuffer, ShaderProgram *p_shaderProgram) : framebuffer(p_framebuffer), shaderProgram(p_shaderProgram) { @@ -16,12 +17,41 @@ Menu::~Menu() } } -void Menu::showScreenByName(const char *unique_name) +Screen *Menu::getScreenByName(const char* unique_name) { for (auto it = screens.begin(); it != screens.end(); it++) { + if((*it)->getUniqueName() == unique_name) { + return *it; + } + } + return nullptr; +} + +void Menu::showScreenByName(const char *unique_name) +{ + auto it = screens.begin(); + for (; it != screens.end(); it++) { if((*it)->getUniqueName() == unique_name) { (*it)->draw(); break; } } + activeScreen = *it; +} + +void Menu::resetActiveScreen() +{ + activeScreen = nullptr; +} + +void Menu::handleMouseButtonActionRegister(bool *mouseButtonActionRegister, Window* window) +{ + if (mouseButtonActionRegister[mouseButtonActions::leftClicked]) { + auto widgets = activeScreen->getWidgets(); + for (auto it = widgets.begin(); it != widgets.end(); it++) { + if ((*it)->isHovered(window)) { + std::cout << (*it)->getUniqueName() << " clicked!" << std::endl; + } + } + } } diff --git a/src/Menu.h b/src/Menu.h index 4edef1b..ebfd721 100644 --- a/src/Menu.h +++ b/src/Menu.h @@ -12,13 +12,19 @@ public: Menu(Framebuffer *p_framebuffer, ShaderProgram *p_shaderProgram); ~Menu(); + Screen *getScreenByName(const char *unique_name); void showScreenByName(const char *unique_name); + void resetActiveScreen(); + + void handleMouseButtonActionRegister(bool *mouseButtonActionRegister, Window* window); + private: Framebuffer *framebuffer; ShaderProgram *shaderProgram; std::vector screens; + Screen *activeScreen; /*Screen *loadingScreen; Screen *mainMenuScreen; Screen *optionMenuScreen; diff --git a/src/Screen.cpp b/src/Screen.cpp index e676e28..b1a9103 100644 --- a/src/Screen.cpp +++ b/src/Screen.cpp @@ -27,6 +27,20 @@ std::string Screen::getUniqueName() return unique_name; } +std::vector Screen::getWidgets() +{ + return widgets; +} + +Widget *Screen::getWidgetByName(const char* name) +{ + for (auto it = widgets.begin(); it != widgets.end(); it++) { + if((*it)->getUniqueName() == name) + return *it; + } + return nullptr; +} + void Screen::addWidget(Widget *widget) { widgets.push_back(widget); diff --git a/src/Screen.h b/src/Screen.h index 7a41d5d..e488346 100644 --- a/src/Screen.h +++ b/src/Screen.h @@ -14,6 +14,8 @@ public: void draw(); std::string getUniqueName(); + std::vector getWidgets(); + Widget *getWidgetByName(const char* name); private: uint32_t id; diff --git a/src/Widget.cpp b/src/Widget.cpp index 047bc33..a8c29f8 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -1,8 +1,12 @@ #include "Widget.h" #include "VertexArray.h" -Widget::Widget(Texture *texture, float p_x, float p_y, float p_w, float p_h) : - x(p_x), y(p_y), w(p_w), h(p_h) +Widget::Widget(std::string &name, Texture *texture, float p_x, float p_y, float p_w, float p_h) : + x(p_x), + y(p_y), + w(p_w), + h(p_h), + unique_name(name) { widgetTextures.push_back(texture); @@ -37,9 +41,31 @@ Widget::~Widget() delete widgetMesh; } +std::string Widget::getUniqueName() +{ + return unique_name; +} + void Widget::draw(ShaderProgram *shaderProgram) { shaderProgram->bind(); widgetMesh->draw(shaderProgram); shaderProgram->unbind(); } + +bool Widget::isHovered(Window *window) +{ + double xpos, ypos, width, height; + glfwGetCursorPos(window->getGLFWwindow(), &xpos, &ypos); + width = window->getWindowWidth(); + height = window->getWindowHeight(); + + double xrel = xpos / width; + double yrel = -ypos / height + 1; + + bool isHovered = false; + if(xrel >= x && xrel <= x + w && yrel >= y && yrel <= y + h) + isHovered = true; + + return isHovered; +} diff --git a/src/Widget.h b/src/Widget.h index 2018359..345fe2b 100644 --- a/src/Widget.h +++ b/src/Widget.h @@ -3,17 +3,24 @@ #include "Texture.h" #include "Mesh.h" #include "Framebuffer.h" +#include "Window.h" class Widget { public: - Widget(Texture *texture, float x, float y, float w, float h); + Widget(std::string &name, Texture *texture, float x, float y, float w, float h); ~Widget(); void draw(ShaderProgram *shaderProgram); + std::string getUniqueName(); + + bool isHovered(Window *window); + private: double x, y, w, h; + + std::string unique_name; std::vector widgetVertices; std::vector widgetIndices; diff --git a/src/eventActions.h b/src/eventActions.h index 844d7f4..8d7dc40 100644 --- a/src/eventActions.h +++ b/src/eventActions.h @@ -1,3 +1,5 @@ +#pragma once + enum cameraActions { cameraUp, cameraDown, @@ -19,3 +21,10 @@ enum windowActions { mouseCatchToggle, WINDOW_ACTION_NUM_ITEMS }; + +enum mouseButtonActions { + leftClicked, + rightClicked, + middleClicked, + MOUSE_BUTTON_ACTION_NUM_ITEMS +};