[ADD] comments
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../toolbox/toolbox.h"
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
#include "../toolbox/toolbox.h"
|
||||||
|
|
||||||
namespace gui
|
namespace gui
|
||||||
{
|
{
|
||||||
@@ -23,60 +23,4 @@ namespace gui
|
|||||||
scale.x /= (WINDOW_WIDTH / WINDOW_HEIGT);
|
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(); }
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include "gui_element.h"
|
#include "gui_interactable.h"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
namespace gui
|
namespace gui
|
||||||
{
|
{
|
||||||
@@ -17,11 +15,12 @@ namespace gui
|
|||||||
void InteractableGui::Update(GLFWwindow* window)
|
void InteractableGui::Update(GLFWwindow* window)
|
||||||
{
|
{
|
||||||
if (IsHoveringAbove(window) && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
|
if (IsHoveringAbove(window) && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
|
||||||
{
|
{
|
||||||
if (clicked_texture != 0)
|
if (clicked_texture != 0)
|
||||||
{
|
{
|
||||||
texture = clicked_texture;
|
texture = clicked_texture;
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
texture = default_texture;
|
texture = default_texture;
|
||||||
}
|
}
|
||||||
@@ -31,7 +30,8 @@ namespace gui
|
|||||||
OnClick();
|
OnClick();
|
||||||
is_clicking = true;
|
is_clicking = true;
|
||||||
}
|
}
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
if (is_clicking)
|
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 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);
|
const float y_rel = -((y_pos / SCALED_HEIGHT / DEFAULT_HEIGHT) * 2.0f - 1.0f);
|
||||||
|
|
||||||
if (x_rel >= minXY.x && x_rel <= maxXY.x &&
|
if (x_rel >= minXY.x && x_rel <= maxXY.x &&
|
||||||
y_rel >= minXY.y && y_rel <= maxXY.y)
|
y_rel >= minXY.y && y_rel <= maxXY.y)
|
||||||
{
|
{
|
||||||
if (hover_texture != 0)
|
if (hover_texture != 0)
|
||||||
{
|
{
|
||||||
texture = hover_texture;
|
texture = hover_texture;
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
texture = default_texture;
|
texture = default_texture;
|
||||||
}
|
}
|
||||||
112
src/gui/gui_interactable.h
Normal file
112
src/gui/gui_interactable.h
Normal 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(); }
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include <opencv2/core.hpp>
|
#include <opencv2/core.hpp>
|
||||||
|
|
||||||
|
#include "gui/gui_interactable.h"
|
||||||
#include "models/model.h"
|
#include "models/model.h"
|
||||||
#include "renderEngine/loader.h"
|
#include "renderEngine/loader.h"
|
||||||
#include "renderEngine/obj_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));
|
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.SetHoverTexture(render_engine::loader::LoadTexture("res/Texture.png"));
|
||||||
button.SetClickedTexture(render_engine::loader::LoadTexture("res/Mayo.png"));
|
button.SetClickedTexture(render_engine::loader::LoadTexture("res/Mayo.png"));
|
||||||
|
button.SetOnClickAction([]()
|
||||||
|
{
|
||||||
|
std::cout << "I got clicked on!" << std::endl;
|
||||||
|
});
|
||||||
guis.push_back(&button);
|
guis.push_back(&button);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="src\entities\camera.cpp" />
|
<ClCompile Include="src\entities\camera.cpp" />
|
||||||
<ClCompile Include="src\entities\entity.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\main.cpp" />
|
||||||
<ClCompile Include="src\renderEngine\loader.cpp" />
|
<ClCompile Include="src\renderEngine\loader.cpp" />
|
||||||
<ClCompile Include="src\renderEngine\obj_loader.cpp" />
|
<ClCompile Include="src\renderEngine\obj_loader.cpp" />
|
||||||
@@ -36,6 +36,7 @@
|
|||||||
<ClInclude Include="src\entities\entity.h" />
|
<ClInclude Include="src\entities\entity.h" />
|
||||||
<ClInclude Include="src\entities\light.h" />
|
<ClInclude Include="src\entities\light.h" />
|
||||||
<ClInclude Include="src\gui\gui_element.h" />
|
<ClInclude Include="src\gui\gui_element.h" />
|
||||||
|
<ClInclude Include="src\gui\gui_interactable.h" />
|
||||||
<ClInclude Include="src\models\model.h" />
|
<ClInclude Include="src\models\model.h" />
|
||||||
<ClInclude Include="src\renderEngine\loader.h" />
|
<ClInclude Include="src\renderEngine\loader.h" />
|
||||||
<ClInclude Include="src\renderEngine\obj_loader.h" />
|
<ClInclude Include="src\renderEngine\obj_loader.h" />
|
||||||
|
|||||||
@@ -45,7 +45,7 @@
|
|||||||
<ClCompile Include="src\shaders\gui_shader.cpp">
|
<ClCompile Include="src\shaders\gui_shader.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="src\gui\gui_element.cpp">
|
<ClCompile Include="src\gui\gui_interactable.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@@ -89,5 +89,8 @@
|
|||||||
<ClInclude Include="src\gui\gui_element.h">
|
<ClInclude Include="src\gui\gui_element.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\gui\gui_interactable.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
Reference in New Issue
Block a user