Make Widgets clickable

This commit is contained in:
2021-01-16 21:58:42 +01:00
parent 7e9702431f
commit f187a25e9e
13 changed files with 156 additions and 30 deletions

4
.gitmodules vendored
View File

@@ -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

View File

@@ -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"
}
]
}

View File

@@ -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);
}
}

View File

@@ -2,8 +2,9 @@
#include <iostream>
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;

View File

@@ -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;

View File

@@ -238,11 +238,13 @@ std::vector<Widget*> 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(),

View File

@@ -1,6 +1,7 @@
#include "Menu.h"
#include "JsonParser.h"
#include "eventActions.h"
#include <iostream>
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;
}
}
}
}

View File

@@ -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<Screen*> screens;
Screen *activeScreen;
/*Screen *loadingScreen;
Screen *mainMenuScreen;
Screen *optionMenuScreen;

View File

@@ -27,6 +27,20 @@ std::string Screen::getUniqueName()
return unique_name;
}
std::vector<Widget*> 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);

View File

@@ -14,6 +14,8 @@ public:
void draw();
std::string getUniqueName();
std::vector<Widget*> getWidgets();
Widget *getWidgetByName(const char* name);
private:
uint32_t id;

View File

@@ -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;
}

View File

@@ -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<Vertex> widgetVertices;
std::vector<uint32_t> widgetIndices;

View File

@@ -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
};