Working GUI Button

This commit is contained in:
2015-09-23 16:51:20 +02:00
parent 8e00b76036
commit b83de44715
4 changed files with 128 additions and 21 deletions
+21 -6
View File
@@ -53,7 +53,7 @@
#include "Physics/ESetImpulse.h" #include "Physics/ESetImpulse.h"
#include "GUI/Frame.h" #include "GUI/Frame.h"
#include "GUI/TextureFrame.h" #include "GUI/Button.h"
namespace dd namespace dd
{ {
@@ -74,11 +74,23 @@ public:
m_FrameStack->Width = 1920; m_FrameStack->Width = 1920;
m_FrameStack->Height = 1080; m_FrameStack->Height = 1080;
{ {
auto button = new GUI::TextureFrame(m_FrameStack, "SuperButt"); auto button = new GUI::Button(m_FrameStack, "SuperButt");
button->SetTexture("Textures/Test/TestBrick_Diffuse.png"); button->TextureReleased = "Textures/Test/TestBrick_Specular.png";
button->TexturePressed = "Textures/Test/TestBrick_Normal.png";
button->TextureHover = "Textures/Test/TestBrick_Diffuse.png";
button->X = 50;
button->Y = 75;
button->Width = 100; button->Width = 100;
button->Height = 25; button->Height = 25;
} }
{
auto button = new GUI::TextureFrame(m_FrameStack, "BALLZ");
button->SetTexture("Textures/Ball.png");
button->X = 50;
button->Y = 75;
button->Width = 100;
button->Height = 100;
}
m_InputManager = std::make_shared<InputManager>(m_Renderer->Window(), m_EventBroker); m_InputManager = std::make_shared<InputManager>(m_Renderer->Window(), m_EventBroker);
@@ -250,13 +262,16 @@ public:
double dt = currentTime - m_LastTime; double dt = currentTime - m_LastTime;
m_LastTime = currentTime; m_LastTime = currentTime;
ResourceManager::Update();
// Update input // Update input
m_InputManager->Update(dt); m_InputManager->Update(dt);
// Swap event queues to get fresh input data in the read queue
m_EventBroker->Swap();
ResourceManager::Update();
m_World->Update(dt); m_World->Update(dt);
m_EventBroker->Process<GUI::Frame>();
m_FrameStack->UpdateLayered(dt);
// //
// if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) { // if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) {
+102
View File
@@ -0,0 +1,102 @@
#ifndef GUI_BUTTON_H__
#define GUI_BUTTON_H__
#include "GUI/TextureFrame.h"
#include "Core/EMouseMove.h"
#include "Core/EMousePress.h"
#include "Core/EMouseRelease.h"
namespace dd
{
namespace GUI
{
class Button : public TextureFrame
{
public:
Button(Frame* parent, std::string name)
: TextureFrame(parent, name)
{
EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &Button::OnMouseMove);
EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &Button::OnMousePress);
EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &Button::OnMouseRelease);
}
std::string TextureHover;
std::string TexturePressed;
std::string TextureReleased;
void Draw(RenderQueueCollection& rq) override
{
if (m_Texture == nullptr && !TextureReleased.empty()) {
SetTexture(TextureReleased);
}
TextureFrame::Draw(rq);
}
protected:
bool m_MouseIsOver = false;
bool m_IsDown = false;
virtual bool OnMouseMove(const Events::MouseMove& event)
{
bool isOver = Rectangle::Intersects(AbsoluteRectangle(), Rectangle(event.X, event.Y, 1, 1));
if (isOver && !m_MouseIsOver) { // Enter
LOG_INFO("%s mouse enter", m_Name.c_str());
if (!m_IsDown) {
SetTexture(TextureHover);
}
} else if (!isOver && m_MouseIsOver) { // Leave
LOG_INFO("%s mouse left", m_Name.c_str());
if (!m_IsDown) {
SetTexture(TextureReleased);
}
}
m_MouseIsOver = isOver;
return true;
}
virtual bool OnMousePress(const Events::MousePress& event)
{
if (!Rectangle::Intersects(AbsoluteRectangle(), Rectangle(event.X, event.Y, 1, 1))) {
return false;
}
if (!TexturePressed.empty()) {
SetTexture(TexturePressed);
}
LOG_INFO("%s mouse pressed", m_Name.c_str());
m_IsDown = true;
return true;
}
virtual bool OnMouseRelease(const Events::MouseRelease& event)
{
bool isOver = Rectangle::Intersects(AbsoluteRectangle(), Rectangle(event.X, event.Y, 1, 1));
if (!isOver && !m_IsDown) {
return false;
}
if (!TextureReleased.empty()) {
SetTexture(TextureReleased);
}
LOG_INFO("%s mouse released", m_Name.c_str());
m_IsDown = false;
return true;
}
private:
EventRelay<Frame, Events::MouseMove> m_EMouseMove;
EventRelay<Frame, Events::MousePress> m_EMousePress;
EventRelay<Frame, Events::MouseRelease> m_EMouseRelease;
};
}
}
#endif
+4 -14
View File
@@ -36,15 +36,16 @@ public:
// Set up a base frame with an event broker // Set up a base frame with an event broker
Frame(dd::EventBroker* eventBroker) Frame(dd::EventBroker* eventBroker)
: EventBroker(eventBroker) : EventBroker(eventBroker)
, Rectangle() , Rectangle() { }
{ Initialize(); }
// Create a frame as a child // Create a frame as a child
Frame(Frame* parent, std::string name) Frame(Frame* parent, std::string name)
: m_Name(name) : m_Name(name)
{ {
SetParent(parent); SetParent(parent);
Initialize(); EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Frame::OnKeyDown);
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Frame::OnKeyUp);
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Frame::OnCommand);
} }
~Frame() ~Frame()
@@ -221,24 +222,13 @@ protected:
std::map<int, Children_t> m_Children; // layer -> Children_t std::map<int, Children_t> m_Children; // layer -> Children_t
virtual bool OnKeyDown(const Events::KeyDown& event) { return false; } virtual bool OnKeyDown(const Events::KeyDown& event) { return false; }
virtual bool OnKeyUp(const Events::KeyUp& event) { return false; } virtual bool OnKeyUp(const Events::KeyUp& event) { return false; }
//virtual bool OnMouseDown(const Events::KeyDown &event) { }
//virtual bool OnMouseUp(const Events::KeyDown &event) { }
virtual bool OnCommand(const Events::InputCommand& event) { return false; } virtual bool OnCommand(const Events::InputCommand& event) { return false; }
private: private:
EventRelay<Frame, Events::KeyDown> m_EKeyDown; EventRelay<Frame, Events::KeyDown> m_EKeyDown;
EventRelay<Frame, Events::KeyUp> m_EKeyUp; EventRelay<Frame, Events::KeyUp> m_EKeyUp;
EventRelay<Frame, Events::InputCommand> m_EInputCommand; EventRelay<Frame, Events::InputCommand> m_EInputCommand;
void Initialize()
{
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Frame::OnKeyDown);
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Frame::OnKeyUp);
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Frame::OnCommand);
}
}; };
} }
+1 -1
View File
@@ -85,7 +85,7 @@ source_group(Sound FILES ${SOURCE_FILES_Sound})
#) #)
set(SOURCE_FILES_GUI set(SOURCE_FILES_GUI
"${INCLUDE_PATH}/GUI/Frame.h" "${INCLUDE_PATH}/GUI/Frame.h"
) ../../include/GUI/Button.h)
source_group(GUI FILES ${SOURCE_FILES_GUI}) source_group(GUI FILES ${SOURCE_FILES_GUI})
set(SOURCE_FILES set(SOURCE_FILES