diff --git a/src/gui/gui_element.h b/src/gui/gui_element.h index ccfa363..9643674 100644 --- a/src/gui/gui_element.h +++ b/src/gui/gui_element.h @@ -1,7 +1,7 @@ #pragma once -#include "../toolbox/toolbox.h" #include +#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(); } - }; } diff --git a/src/gui/gui_element.cpp b/src/gui/gui_interactable.cpp similarity index 94% rename from src/gui/gui_element.cpp rename to src/gui/gui_interactable.cpp index 0c8b579..d96250c 100644 --- a/src/gui/gui_element.cpp +++ b/src/gui/gui_interactable.cpp @@ -1,7 +1,5 @@ #include -#include "gui_element.h" - -#include +#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; } diff --git a/src/gui/gui_interactable.h b/src/gui/gui_interactable.h new file mode 100644 index 0000000..2957e7b --- /dev/null +++ b/src/gui/gui_interactable.h @@ -0,0 +1,112 @@ +#pragma once + +#include +#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(); } + }; +} diff --git a/src/main.cpp b/src/main.cpp index f50e7a7..26894ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include +#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); diff --git a/wk2_fps.vcxproj b/wk2_fps.vcxproj index ba5c3c5..c644541 100644 --- a/wk2_fps.vcxproj +++ b/wk2_fps.vcxproj @@ -21,7 +21,7 @@ - + @@ -36,6 +36,7 @@ + diff --git a/wk2_fps.vcxproj.filters b/wk2_fps.vcxproj.filters index a4bf002..6b55913 100644 --- a/wk2_fps.vcxproj.filters +++ b/wk2_fps.vcxproj.filters @@ -45,7 +45,7 @@ Source Files - + Source Files @@ -89,5 +89,8 @@ Header Files + + Header Files + \ No newline at end of file