ADD: Click Events
This commit is contained in:
parent
d7ea3b7e3c
commit
0ebdd76b04
@ -5,6 +5,7 @@
|
|||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
|
|
||||||
#include "../layer/Layer.h"
|
#include "../layer/Layer.h"
|
||||||
|
#include "../platform/glfw/InputManager.h"
|
||||||
|
|
||||||
Application* Application::instance = nullptr;
|
Application* Application::instance = nullptr;
|
||||||
|
|
||||||
@ -38,6 +39,7 @@ void Application::run() {
|
|||||||
layer->onUpdate();
|
layer->onUpdate();
|
||||||
}
|
}
|
||||||
window->OnUpdate();
|
window->OnUpdate();
|
||||||
|
InputManager::update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -11,17 +11,38 @@
|
|||||||
UiButton::UiButton(const GLuint textureID, std::string label, Font &font, const LayoutStyle &style): textureID(textureID), text(std::move(label)), font(font), UiComponent(style) {
|
UiButton::UiButton(const GLuint textureID, std::string label, Font &font, const LayoutStyle &style): textureID(textureID), text(std::move(label)), font(font), UiComponent(style) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UiButton::onClick() {
|
||||||
|
if (clickListener) {
|
||||||
|
clickListener();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void UiButton::onCollectRenderData(UiRenderBundle &uiRenderBundle) {
|
void UiButton::onCollectRenderData(UiRenderBundle &uiRenderBundle) {
|
||||||
glm::vec2 position = glm::vec2(uiPositioner.screenSpace.x, uiPositioner.screenSpace.y);
|
glm::vec2 position = glm::vec2(uiPositioner.screenSpace.x, uiPositioner.screenSpace.y);
|
||||||
glm::vec2 size = glm::vec2(uiPositioner.screenSpace.width, uiPositioner.screenSpace.height);
|
glm::vec2 size = glm::vec2(uiPositioner.screenSpace.width, uiPositioner.screenSpace.height);
|
||||||
|
|
||||||
if (isHovered()) {
|
VisualStyle visualStyle = getVisualStyle();
|
||||||
float brightness = 1.15f;
|
uiRenderBundle.addGUITexture(
|
||||||
float tintStrength = 0.f;
|
std::make_shared<GUITexture>(
|
||||||
uiRenderBundle.addGUITexture(std::make_shared<GUITexture>(textureID, position, size, brightness, glm::vec3(0.3, 0.6, 1.0), tintStrength));
|
textureID,
|
||||||
} else {
|
position,
|
||||||
uiRenderBundle.addGUITexture(std::make_shared<GUITexture>(textureID, position, size));
|
size,
|
||||||
}
|
visualStyle.brightness,
|
||||||
|
visualStyle.tintColor,
|
||||||
|
visualStyle.tintStrength
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
uiRenderBundle.addGUIText(std::make_shared<GUIText>(font, text, uiPositioner.screenSpace));
|
uiRenderBundle.addGUIText(std::make_shared<GUIText>(font, text, uiPositioner.screenSpace));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VisualStyle UiButton::getVisualStyle() const {
|
||||||
|
switch (state) {
|
||||||
|
case UiState::PRESSED:
|
||||||
|
return {1.0f, glm::vec3(0.3, 0.6, 1.0), 0.15f};
|
||||||
|
case UiState::HOVERED:
|
||||||
|
return {1.15f, glm::vec3(0.0f), 0.0f};
|
||||||
|
default:
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -6,13 +6,23 @@
|
|||||||
#define DICEWARS_SIEDLER_UIBUTTON_H
|
#define DICEWARS_SIEDLER_UIBUTTON_H
|
||||||
#include "UiComponent.h"
|
#include "UiComponent.h"
|
||||||
|
|
||||||
|
struct VisualStyle {
|
||||||
|
float brightness = 1.0f;
|
||||||
|
glm::vec3 tintColor = glm::vec3(0.0f);
|
||||||
|
float tintStrength = 0.0f;
|
||||||
|
};
|
||||||
|
|
||||||
class Font;
|
class Font;
|
||||||
|
|
||||||
class UiButton : public UiComponent{
|
class UiButton : public UiComponent{
|
||||||
public:
|
public:
|
||||||
UiButton(GLuint textureID, std::string label, Font& font, const LayoutStyle& style);
|
UiButton(GLuint textureID, std::string label, Font& font, const LayoutStyle& style);
|
||||||
|
void setOnClick(std::function<void()> callback) {
|
||||||
|
printf("Clicklister added!\n");
|
||||||
|
clickListener = std::move(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onClick() override;
|
||||||
protected:
|
protected:
|
||||||
void onCollectRenderData(UiRenderBundle &uiRenderBundle) override;
|
void onCollectRenderData(UiRenderBundle &uiRenderBundle) override;
|
||||||
|
|
||||||
@ -20,6 +30,10 @@ private:
|
|||||||
std::string text;
|
std::string text;
|
||||||
Font& font;
|
Font& font;
|
||||||
GLuint textureID;
|
GLuint textureID;
|
||||||
|
|
||||||
|
VisualStyle getVisualStyle() const;
|
||||||
|
|
||||||
|
std::function<void()> clickListener;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,9 @@
|
|||||||
|
|
||||||
#include "UiComponent.h"
|
#include "UiComponent.h"
|
||||||
|
|
||||||
|
#include "../../Application.h"
|
||||||
#include "../../../platform/glfw/InputManager.h"
|
#include "../../../platform/glfw/InputManager.h"
|
||||||
|
#include "GLFW/glfw3.h"
|
||||||
|
|
||||||
void UiComponent::addChild(std::unique_ptr<UiComponent> child) {
|
void UiComponent::addChild(std::unique_ptr<UiComponent> child) {
|
||||||
child->parent = this;
|
child->parent = this;
|
||||||
@ -46,6 +48,21 @@ bool UiComponent::isMouseOver(float mouseX, float mouseY) {
|
|||||||
|
|
||||||
void UiComponent::onUpdate(float) {
|
void UiComponent::onUpdate(float) {
|
||||||
glm::vec2 mousePos = InputManager::getMousePositionNormalized();
|
glm::vec2 mousePos = InputManager::getMousePositionNormalized();
|
||||||
hovered = isMouseOver(mousePos.x, mousePos.y);
|
bool hoveredNow = isMouseOver(mousePos.x, mousePos.y);
|
||||||
|
|
||||||
|
bool pressedThisFrame = hoveredNow && InputManager::isMouseButtonPressed(GLFW_MOUSE_BUTTON_LEFT);
|
||||||
|
bool releasedFrame = InputManager::isMouseButtonReleased(GLFW_MOUSE_BUTTON_LEFT);
|
||||||
|
|
||||||
|
if (pressedThisFrame) {
|
||||||
|
state = UiState::PRESSED;
|
||||||
|
} else if (releasedFrame) {
|
||||||
|
if (state == UiState::PRESSED && hoveredNow) {
|
||||||
|
onClick();
|
||||||
|
}
|
||||||
|
state = hoveredNow ? UiState::HOVERED : UiState::NORMAL;
|
||||||
|
} else {
|
||||||
|
if (state != UiState::PRESSED)
|
||||||
|
state = hoveredNow ? UiState::HOVERED : UiState::NORMAL;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,10 @@
|
|||||||
#include "UiRenderBundle.h"
|
#include "UiRenderBundle.h"
|
||||||
#include "../../../renderer/model/GUITexture.h"
|
#include "../../../renderer/model/GUITexture.h"
|
||||||
|
|
||||||
|
enum class UiState {
|
||||||
|
NORMAL, HOVERED, PRESSED
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class UiComponent {
|
class UiComponent {
|
||||||
public:
|
public:
|
||||||
@ -34,10 +38,11 @@ public:
|
|||||||
uiPositioner.setLayout(style);
|
uiPositioner.setLayout(style);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] bool isHovered() const { return hovered; }
|
[[nodiscard]] bool isHovered() const { return state == UiState::HOVERED; }
|
||||||
|
virtual void onClick() {};
|
||||||
protected:
|
protected:
|
||||||
bool visible = true;
|
bool visible = true;
|
||||||
bool hovered = false;
|
UiState state = UiState::NORMAL;
|
||||||
|
|
||||||
virtual void onUpdate(float );
|
virtual void onUpdate(float );
|
||||||
virtual void onCollectRenderData(UiRenderBundle& uiRenderBundle) {}
|
virtual void onCollectRenderData(UiRenderBundle& uiRenderBundle) {}
|
||||||
|
|||||||
@ -5,7 +5,9 @@
|
|||||||
#include "InputManager.h"
|
#include "InputManager.h"
|
||||||
|
|
||||||
#include "../../core/Application.h"
|
#include "../../core/Application.h"
|
||||||
#include "GLFW/glfw3.h"
|
|
||||||
|
bool InputManager::current[GLFW_MOUSE_BUTTON_LAST] = { false };
|
||||||
|
bool InputManager::previous[GLFW_MOUSE_BUTTON_LAST] = { false };
|
||||||
|
|
||||||
bool InputManager::isKeyPressed(int keycode) {
|
bool InputManager::isKeyPressed(int keycode) {
|
||||||
auto window = static_cast<GLFWwindow*>(Application::getInstance().getWindow().GetNativeWindow());
|
auto window = static_cast<GLFWwindow*>(Application::getInstance().getWindow().GetNativeWindow());
|
||||||
@ -13,8 +15,18 @@ bool InputManager::isKeyPressed(int keycode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool InputManager::isMouseButtonPressed(int button) {
|
bool InputManager::isMouseButtonPressed(int button) {
|
||||||
auto window = static_cast<GLFWwindow*>(Application::getInstance().getWindow().GetNativeWindow());
|
return current[button] && !previous[button];
|
||||||
return glfwGetMouseButton(window, button) == GLFW_PRESS;
|
}
|
||||||
|
|
||||||
|
bool InputManager::isMouseButtonReleased(int button) {
|
||||||
|
return !current[button] && previous[button];
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputManager::update() {
|
||||||
|
for (int i=0; i < GLFW_MOUSE_BUTTON_LAST; ++i) {
|
||||||
|
previous[i] = current[i];
|
||||||
|
current[i] = glfwGetMouseButton(static_cast<GLFWwindow*>(Application::getInstance().getWindow().GetNativeWindow()), i) == GLFW_PRESS;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec2 InputManager::getMousePosition() {
|
glm::vec2 InputManager::getMousePosition() {
|
||||||
@ -41,3 +53,7 @@ glm::vec2 InputManager::getMouseDelta() {
|
|||||||
return delta;
|
return delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool InputManager::isMouseButtonDown(int button) {
|
||||||
|
return current[button];
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,18 +4,29 @@
|
|||||||
|
|
||||||
#ifndef INPUTMANAGER_H
|
#ifndef INPUTMANAGER_H
|
||||||
#define INPUTMANAGER_H
|
#define INPUTMANAGER_H
|
||||||
|
#define GLFW_INCLUDE_NONE
|
||||||
|
#include "GLFW/glfw3.h"
|
||||||
#include "glm/vec2.hpp"
|
#include "glm/vec2.hpp"
|
||||||
|
|
||||||
|
|
||||||
class InputManager {
|
class InputManager {
|
||||||
public:
|
public:
|
||||||
static bool isKeyPressed(int keycode);
|
static bool isKeyPressed(int keycode);
|
||||||
static bool isMouseButtonPressed(int button);
|
|
||||||
|
|
||||||
static glm::vec2 getMousePosition();
|
static glm::vec2 getMousePosition();
|
||||||
static glm::vec2 getMousePositionNormalized();
|
static glm::vec2 getMousePositionNormalized();
|
||||||
glm::vec2 getMouseDelta();
|
glm::vec2 getMouseDelta();
|
||||||
|
|
||||||
|
static bool isMouseButtonDown(int button);
|
||||||
|
static bool isMouseButtonPressed(int button);
|
||||||
|
static bool isMouseButtonReleased(int button);
|
||||||
|
|
||||||
|
static void update();
|
||||||
|
|
||||||
|
private:
|
||||||
|
static bool current[GLFW_MOUSE_BUTTON_LAST];
|
||||||
|
static bool previous[GLFW_MOUSE_BUTTON_LAST];
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,9 @@
|
|||||||
|
|
||||||
#include "UILayer.h"
|
#include "UILayer.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
#include "../engine/core/gui/text/Font.h"
|
#include "../engine/core/gui/text/Font.h"
|
||||||
#include "../engine/core/gui/uiComponent/UiButton.h"
|
#include "../engine/core/gui/uiComponent/UiButton.h"
|
||||||
#include "../engine/core/gui/uiComponent/UiImage.h"
|
#include "../engine/core/gui/uiComponent/UiImage.h"
|
||||||
@ -52,6 +55,11 @@ void UILayer::onAttach() {
|
|||||||
//auto text = std::make_unique<UiText>(*font, "Hello World!", glm::vec2(0.5f, 0.5f), glm::vec3(1,1,1));
|
//auto text = std::make_unique<UiText>(*font, "Hello World!", glm::vec2(0.5f, 0.5f), glm::vec3(1,1,1));
|
||||||
|
|
||||||
auto button = std::make_unique<UiButton>(Loader::instance().loadTextureFromFile("assets/textures/texture.png").getTextureID(), "Button", *font, imageStyle);
|
auto button = std::make_unique<UiButton>(Loader::instance().loadTextureFromFile("assets/textures/texture.png").getTextureID(), "Button", *font, imageStyle);
|
||||||
|
|
||||||
|
button->setOnClick([]() {
|
||||||
|
std::cout << "Button Clicked!" << std::endl;
|
||||||
|
});
|
||||||
|
|
||||||
rootContainer->addChild(std::move(button));
|
rootContainer->addChild(std::move(button));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
#include "BuildingPlacementSystem.h"
|
#include "BuildingPlacementSystem.h"
|
||||||
|
|
||||||
#include "../../RessourceType.h"
|
#include "../../RessourceType.h"
|
||||||
|
#include "../../../../engine/core/Application.h"
|
||||||
#include "../../../../engine/core/ECS/ModelComponent.h"
|
#include "../../../../engine/core/ECS/ModelComponent.h"
|
||||||
#include "../../../../engine/core/ECS/TileRenderComponent.h"
|
#include "../../../../engine/core/ECS/TileRenderComponent.h"
|
||||||
#include "../../../../engine/core/ECS/TransformComponent.h"
|
#include "../../../../engine/core/ECS/TransformComponent.h"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user