[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
{
@@ -21,7 +19,8 @@ namespace gui
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)
{
@@ -54,7 +54,8 @@ namespace gui
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(); }
};
}

View File

@@ -9,6 +9,7 @@
#include <opencv2/core.hpp>
#include "gui/gui_interactable.h"
#include "models/model.h"
#include "renderEngine/loader.h"
#include "renderEngine/obj_loader.h"
@@ -85,6 +86,10 @@ int main(void)
gui::Button button(render_engine::loader::LoadTexture("res/Mayo.png"), glm::vec2(0.5f, 0.0f), glm::vec2(0.25f, 0.25f));
button.SetHoverTexture(render_engine::loader::LoadTexture("res/Texture.png"));
button.SetClickedTexture(render_engine::loader::LoadTexture("res/Mayo.png"));
button.SetOnClickAction([]()
{
std::cout << "I got clicked on!" << std::endl;
});
guis.push_back(&button);

View File

@@ -21,7 +21,7 @@
<ItemGroup>
<ClCompile Include="src\entities\camera.cpp" />
<ClCompile Include="src\entities\entity.cpp" />
<ClCompile Include="src\gui\gui_element.cpp" />
<ClCompile Include="src\gui\gui_interactable.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\renderEngine\loader.cpp" />
<ClCompile Include="src\renderEngine\obj_loader.cpp" />
@@ -36,6 +36,7 @@
<ClInclude Include="src\entities\entity.h" />
<ClInclude Include="src\entities\light.h" />
<ClInclude Include="src\gui\gui_element.h" />
<ClInclude Include="src\gui\gui_interactable.h" />
<ClInclude Include="src\models\model.h" />
<ClInclude Include="src\renderEngine\loader.h" />
<ClInclude Include="src\renderEngine\obj_loader.h" />

View File

@@ -45,7 +45,7 @@
<ClCompile Include="src\shaders\gui_shader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\gui\gui_element.cpp">
<ClCompile Include="src\gui\gui_interactable.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
@@ -89,5 +89,8 @@
<ClInclude Include="src\gui\gui_element.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\gui\gui_interactable.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>