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
+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
Frame(dd::EventBroker* eventBroker)
: EventBroker(eventBroker)
, Rectangle()
{ Initialize(); }
, Rectangle() { }
// Create a frame as a child
Frame(Frame* parent, std::string name)
: m_Name(name)
{
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()
@@ -221,24 +222,13 @@ protected:
std::map<int, Children_t> m_Children; // layer -> Children_t
virtual bool OnKeyDown(const Events::KeyDown& 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; }
private:
EventRelay<Frame, Events::KeyDown> m_EKeyDown;
EventRelay<Frame, Events::KeyUp> m_EKeyUp;
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);
}
};
}