[FEATURE] working gui buttons
This commit is contained in:
81
src/gui/gui_element.cpp
Normal file
81
src/gui/gui_element.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "gui_element.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace gui
|
||||
{
|
||||
InteractableGui::InteractableGui(int default_texture, glm::vec2 position, glm::vec2 scale)
|
||||
: GuiTexture(default_texture, position, scale)
|
||||
{
|
||||
this->default_texture = default_texture;
|
||||
|
||||
minXY = glm::vec2(position.x - scale.x, position.y - scale.y);
|
||||
maxXY = glm::vec2(position.x + scale.x, position.y + scale.y);
|
||||
}
|
||||
|
||||
void InteractableGui::Update(GLFWwindow* window)
|
||||
{
|
||||
if (IsHoveringAbove(window) && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
|
||||
{
|
||||
if (clicked_texture != 0)
|
||||
{
|
||||
texture = clicked_texture;
|
||||
} else
|
||||
{
|
||||
texture = default_texture;
|
||||
}
|
||||
|
||||
if (!is_clicking)
|
||||
{
|
||||
OnClick();
|
||||
is_clicking = true;
|
||||
}
|
||||
} else
|
||||
{
|
||||
if (is_clicking)
|
||||
{
|
||||
is_clicking = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool InteractableGui::IsHoveringAbove(GLFWwindow* window)
|
||||
{
|
||||
double x_pos, y_pos;
|
||||
glfwGetCursorPos(window, &x_pos, &y_pos);
|
||||
|
||||
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
|
||||
{
|
||||
texture = default_texture;
|
||||
}
|
||||
|
||||
if (!is_hovering)
|
||||
{
|
||||
OnEnter();
|
||||
is_hovering = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
texture = default_texture;
|
||||
|
||||
if (is_hovering)
|
||||
{
|
||||
OnExit();
|
||||
is_hovering = false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
82
src/gui/gui_element.h
Normal file
82
src/gui/gui_element.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#pragma once
|
||||
|
||||
#include "../toolbox/toolbox.h"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
namespace gui
|
||||
{
|
||||
/*
|
||||
* Structure for representing a gui item to display on the screen
|
||||
*
|
||||
* texture = The texture for the gui
|
||||
* position = The center position of the gui
|
||||
* scale = The size (scale) of the gui
|
||||
*/
|
||||
struct GuiTexture
|
||||
{
|
||||
int texture;
|
||||
glm::vec2 position;
|
||||
glm::vec2 scale;
|
||||
|
||||
GuiTexture(int texture, glm::vec2 position, glm::vec2 scale): texture(texture), position(position), scale(scale)
|
||||
{
|
||||
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,20 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
namespace gui
|
||||
{
|
||||
/*
|
||||
* Structure for representing a gui item to display on the screen
|
||||
*
|
||||
* texture = The texture for the gui
|
||||
* position = The center position of the gui
|
||||
* scale = The size (scale) of the gui
|
||||
*/
|
||||
struct GuiTexture
|
||||
{
|
||||
int texture;
|
||||
glm::vec2 position;
|
||||
glm::vec2 scale;
|
||||
};
|
||||
}
|
||||
12
src/main.cpp
12
src/main.cpp
@@ -2,6 +2,8 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <iostream>
|
||||
|
||||
#include "stb_image.h"
|
||||
#include <ostream>
|
||||
|
||||
@@ -79,9 +81,11 @@ int main(void)
|
||||
shaders::GuiShader gui_shader;
|
||||
gui_shader.Init();
|
||||
|
||||
std::vector<gui::GuiTexture> guis;
|
||||
gui::GuiTexture gui = { render_engine::loader::LoadTexture("res/Mayo.png"), glm::vec2(0.5f, 0.5f), glm::vec2(0.25f, 0.25f) };
|
||||
guis.push_back(gui);
|
||||
std::vector<gui::GuiTexture*> guis;
|
||||
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"));
|
||||
guis.push_back(&button);
|
||||
|
||||
|
||||
// Main game loop
|
||||
@@ -91,6 +95,8 @@ int main(void)
|
||||
const double delta = UpdateDelta();
|
||||
camera.Move(window);
|
||||
|
||||
button.Update(window);
|
||||
|
||||
// Render
|
||||
render_engine::renderer::Prepare();
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ namespace render_engine
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Render(std::vector<gui::GuiTexture>& guis, shaders::GuiShader& shader)
|
||||
void Render(std::vector<gui::GuiTexture*>& guis, shaders::GuiShader& shader)
|
||||
{
|
||||
shader.Start();
|
||||
|
||||
@@ -98,13 +98,13 @@ namespace render_engine
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
// Render each gui to the screen
|
||||
for (gui::GuiTexture& gui : guis)
|
||||
for (gui::GuiTexture* gui : guis)
|
||||
{
|
||||
// Bind the texture of the gui to the shader
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, gui.texture);
|
||||
glBindTexture(GL_TEXTURE_2D, gui->texture);
|
||||
|
||||
glm::mat4 matrix = toolbox::CreateModelMatrix(gui.position, gui.scale);
|
||||
glm::mat4 matrix = toolbox::CreateModelMatrix(gui->position, gui->scale);
|
||||
shader.LoadModelMatrix(matrix);
|
||||
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, quad.vertex_count);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../gui/gui_texture.h"
|
||||
#include "../gui/gui_element.h"
|
||||
#include "../entities/entity.h"
|
||||
#include "../shaders/entity_shader.h"
|
||||
#include "../shaders/gui_shader.h"
|
||||
@@ -38,6 +38,6 @@ namespace render_engine
|
||||
@param guis: A list with all the GUI textures you want to render
|
||||
@param shade: The shader the GUI textures need to be rendered with
|
||||
*/
|
||||
void Render(std::vector<gui::GuiTexture>& guis, shaders::GuiShader& shader);
|
||||
void Render(std::vector<gui::GuiTexture*>& guis, shaders::GuiShader& shader);
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,17 @@
|
||||
|
||||
namespace toolbox
|
||||
{
|
||||
#define WINDOW_WIDTH 1400.0f
|
||||
#define WINDOW_HEIGT 800.0f
|
||||
// Window macro's
|
||||
#define DEFAULT_WIDTH 1920
|
||||
#define DEFAULT_HEIGHT 1080
|
||||
|
||||
// Change these macros to change the window size
|
||||
#define WINDOW_WIDTH 1400.0f
|
||||
#define WINDOW_HEIGT 800.0f
|
||||
|
||||
#define SCALED_WIDTH (WINDOW_WIDTH/DEFAULT_WIDTH)
|
||||
#define SCALED_HEIGHT (WINDOW_HEIGT/DEFAULT_HEIGHT)
|
||||
//
|
||||
|
||||
/*
|
||||
* @brief: This function will create a model matrix
|
||||
|
||||
@@ -21,6 +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\main.cpp" />
|
||||
<ClCompile Include="src\renderEngine\loader.cpp" />
|
||||
<ClCompile Include="src\renderEngine\obj_loader.cpp" />
|
||||
@@ -34,7 +35,7 @@
|
||||
<ClInclude Include="src\entities\camera.h" />
|
||||
<ClInclude Include="src\entities\entity.h" />
|
||||
<ClInclude Include="src\entities\light.h" />
|
||||
<ClInclude Include="src\gui\gui_texture.h" />
|
||||
<ClInclude Include="src\gui\gui_element.h" />
|
||||
<ClInclude Include="src\models\model.h" />
|
||||
<ClInclude Include="src\renderEngine\loader.h" />
|
||||
<ClInclude Include="src\renderEngine\obj_loader.h" />
|
||||
|
||||
@@ -45,6 +45,9 @@
|
||||
<ClCompile Include="src\shaders\gui_shader.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\gui\gui_element.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\entities\Camera.h">
|
||||
@@ -80,10 +83,10 @@
|
||||
<ClInclude Include="src\shaders\entity_shader.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\gui\gui_texture.h">
|
||||
<ClInclude Include="src\shaders\gui_shader.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\shaders\gui_shader.h">
|
||||
<ClInclude Include="src\gui\gui_element.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user