[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;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user