[ADD] comments

This commit is contained in:
Menno
2021-05-25 14:55:27 +02:00
parent 977d377fe5
commit cc7fae5d2f
6 changed files with 133 additions and 67 deletions

View File

@@ -1,7 +1,7 @@
#pragma once
#include "../toolbox/toolbox.h"
#include <glm/gtc/matrix_transform.hpp>
#include "../toolbox/toolbox.h"
namespace gui
{
@@ -23,60 +23,4 @@ namespace gui
scale.x /= (WINDOW_WIDTH / WINDOW_HEIGT);
}
};
/*
* This class represents a gui item which can be interacted with
*/
class InteractableGui : public GuiTexture
{
private:
int default_texture;
int clicked_texture = 0;
int hover_texture = 0;
bool is_hovering = false;
bool is_clicking = false;
glm::vec2 minXY;
glm::vec2 maxXY;
public:
InteractableGui(int default_texture, glm::vec2 position, glm::vec2 scale);
void Update(GLFWwindow* window);
virtual void OnClick() = 0;
virtual void OnEnter() = 0;
virtual void OnExit() = 0;
void SetClickedTexture(int texture) { clicked_texture = texture; }
void SetHoverTexture(int texture) { hover_texture = texture; }
private:
bool IsHoveringAbove(GLFWwindow* window);
};
/*
* This class represents a button
*/
class Button : public InteractableGui
{
private:
void (*on_click_action)();
void (*on_enter_action)();
void (*on_exit_action)();
public:
Button(int default_texture, glm::vec2 position, glm::vec2 scale) : InteractableGui(default_texture, position, scale) {}
void SetOnClickAction(void (*fun)()) { on_click_action = fun; }
void SetOnEnterAction(void (*fun)()) { on_enter_action = fun; }
void SetOnExitAction(void (*fun)()) { on_exit_action = fun; }
protected:
void OnClick() override { if (on_click_action != nullptr) on_click_action(); }
void OnEnter() override { if (on_enter_action != nullptr) on_enter_action(); }
void OnExit() override { if (on_exit_action != nullptr) on_exit_action(); }
};
}

View File

@@ -1,7 +1,5 @@
#include <GLFW/glfw3.h>
#include "gui_element.h"
#include <iostream>
#include "gui_interactable.h"
namespace gui
{
@@ -17,11 +15,12 @@ namespace gui
void InteractableGui::Update(GLFWwindow* window)
{
if (IsHoveringAbove(window) && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
{
if (clicked_texture != 0)
{
texture = clicked_texture;
} else
}
else
{
texture = default_texture;
}
@@ -31,7 +30,8 @@ namespace gui
OnClick();
is_clicking = true;
}
} else
}
else
{
if (is_clicking)
{
@@ -47,14 +47,15 @@ namespace gui
const float x_rel = (x_pos / SCALED_WIDTH / DEFAULT_WIDTH) * 2.0f - 1.0f;
const float y_rel = -((y_pos / SCALED_HEIGHT / DEFAULT_HEIGHT) * 2.0f - 1.0f);
if (x_rel >= minXY.x && x_rel <= maxXY.x &&
y_rel >= minXY.y && y_rel <= maxXY.y)
{
if (hover_texture != 0)
{
texture = hover_texture;
} else
}
else
{
texture = default_texture;
}

112
src/gui/gui_interactable.h Normal file
View File

@@ -0,0 +1,112 @@
#pragma once
#include <glm/gtc/matrix_transform.hpp>
#include "../toolbox/toolbox.h"
#include "gui_element.h"
namespace gui
{
/*
* This class represents a gui item which can be interacted with
*/
class InteractableGui : public GuiTexture
{
private:
int default_texture;
int clicked_texture = 0;
int hover_texture = 0;
bool is_hovering = false;
bool is_clicking = false;
glm::vec2 minXY;
glm::vec2 maxXY;
public:
InteractableGui(int default_texture, glm::vec2 position, glm::vec2 scale);
/*
* @brief: Call this function every frame
*
* @param window: An openGL window
*/
void Update(GLFWwindow* window);
/*
* @brief: This function gets called when the InteractabeGui is clicked
*/
virtual void OnClick() = 0;
/*
* @brief: This function gets called when the mouse starts hovering above the InteractableGUI
*/
virtual void OnEnter() = 0;
/*
* @brief: This function gets called when the mouse stops hovering above the InteractableGUI
*/
virtual void OnExit() = 0;
/*
* @brief: This function sets the texture of the InteractableGUI for when the InteractableGUI is clicked
*/
void SetClickedTexture(int texture) { clicked_texture = texture; }
/*
* @brief: This function sets the texture of the InteractableGUI for when the mouse is hovering above the InteractableGUI
*/
void SetHoverTexture(int texture) { hover_texture = texture; }
private:
/*
* @brief: This function checks if the mouse is hovering above the InteractableGUI
*
* @param window: An openGL window
*
* @return: True or false
*/
bool IsHoveringAbove(GLFWwindow* window);
};
/*
* This class represents a button
*/
class Button : public InteractableGui
{
private:
void (*on_click_action)();
void (*on_enter_action)();
void (*on_exit_action)();
public:
Button(int default_texture, glm::vec2 position, glm::vec2 scale) : InteractableGui(default_texture, position, scale) {}
/*
* @brief: This function sets an action (function pointer) to the OnClick function
*
* @param fun: A function pointer to a function (or lambda)
*/
void SetOnClickAction(void (*fun)()) { on_click_action = fun; }
/*
* @brief: This function sets an action (function pointer) to the OnEnter function
*
* @param fun: A function pointer to a function (or lambda)
*/
void SetOnEnterAction(void (*fun)()) { on_enter_action = fun; }
/*
* @brief: This function sets an action (function pointer) to the OnExit function
*
* @param fun: A function pointer to a function (or lambda)
*/
void SetOnExitAction(void (*fun)()) { on_exit_action = fun; }
protected:
void OnClick() override { if (on_click_action != nullptr) on_click_action(); }
void OnEnter() override { if (on_enter_action != nullptr) on_enter_action(); }
void OnExit() override { if (on_exit_action != nullptr) on_exit_action(); }
};
}