diff --git a/sutk/include/sutk/Button.hpp b/sutk/include/sutk/Button.hpp index 7b1340fe..212703fe 100644 --- a/sutk/include/sutk/Button.hpp +++ b/sutk/include/sutk/Button.hpp @@ -9,89 +9,12 @@ #include #include -#include // for SUTK::RenderableContainer -#include // for SUTK::RenderRectFillRound -#include // for SUTK::ColorTransitionAutomaton #include // for SUTK::MouseMoveHandlerObject, and SUTK::MouseClickHandlerObject -#include // for SUTK::Label - namespace SUTK { - class SUTK_API VisualButtonRect : public RenderRectFillRound - { - // States - enum State : u32 - { - Hover, - Press, - Idle - }; - - ColorTransitionAutomaton m_cta; - - friend class DefaultButtonGraphic; - public: - VisualButtonRect(SUTK::UIDriver& driver, SUTK::RenderableContainer* container) noexcept; - - virtual void setActive(bool isActive) noexcept override; - - virtual bool isDirty() noexcept override; - virtual void update() noexcept override; - - void setTransitionDelay(const f32 transitionDelay) noexcept; - - void setHoverColor(Color4 color) noexcept; - void setPressColor(Color4 color) noexcept; - void setIdleColor(Color4 color) noexcept; - - - void setState(State state) noexcept; - }; - - struct HoverInfo - { - Vec2Df position; - bool isEnter; - bool isExit; - }; - - class SUTK_API IButtonGraphic - { - public: - // Called every time mouse pointer changes its position but still stays within the boundaries of Button's Rect - // position is in local coordinates of the Button's Rect - virtual void onHover(HoverInfo info) = 0; - // Called once for each left-mouse-button down within the boundaries of Button's Rect - virtual void onPress() = 0; - // Called once for each left-mouse-button up/release within the boundaries of Button's Rect or even outside if earlier press was within the boundaries. - virtual void onRelease() = 0; - virtual Vec2Df getMinBoundSize() = 0; - }; - - class Label; - - class SUTK_API DefaultButtonGraphic : public RenderableContainer, public IButtonGraphic - { - private: - - // Visual Representation of the Button's existence - VisualButtonRect* m_visualButton; - Label* m_label; - - public: - DefaultButtonGraphic(UIDriver& driver, Container* parent) noexcept; - - // Implementation of IButtonGraphic's functions - virtual void onHover(HoverInfo info) noexcept override; - virtual void onPress() noexcept override; - virtual void onRelease() noexcept override; - virtual Vec2Df getMinBoundSize() noexcept override; - - VisualButtonRect& getVisualButtonRect() noexcept { return *m_visualButton; } - Label& getLabel() noexcept { return *m_label; } - }; - + class IButtonGraphic; + class SUTK_API Button : public Container, public MouseMoveHandlerObject, public MouseClickHandlerObject diff --git a/sutk/include/sutk/ButtonGraphic.hpp b/sutk/include/sutk/ButtonGraphic.hpp new file mode 100644 index 00000000..0b2a7ae2 --- /dev/null +++ b/sutk/include/sutk/ButtonGraphic.hpp @@ -0,0 +1,94 @@ +#pragma once + +#include +#include // for SUTK::RenderRectFillRound +#include // for SUTK::ColorTransitionAutomaton +#include // for SUTK::RenderableContainer + +namespace SUTK +{ + struct HoverInfo + { + Vec2Df position; + bool isEnter; + bool isExit; + }; + + class SUTK_API IButtonGraphic + { + public: + // Called every time mouse pointer changes its position but still stays within the boundaries of Button's Rect + // position is in local coordinates of the Button's Rect + virtual void onHover(HoverInfo info) = 0; + // Called once for each left-mouse-button down within the boundaries of Button's Rect + virtual void onPress() = 0; + // Called once for each left-mouse-button up/release within the boundaries of Button's Rect or even outside if earlier press was within the boundaries. + virtual void onRelease() = 0; + virtual Vec2Df getMinBoundSize() = 0; + }; + + class SUTK_API VisualButtonRect : public RenderRectFillRound + { + // States + enum State : u32 + { + Hover, + Press, + Idle + }; + + ColorTransitionAutomaton m_cta; + + friend class DefaultButtonGraphicNoLabel; + public: + VisualButtonRect(SUTK::UIDriver& driver, SUTK::RenderableContainer* container) noexcept; + + virtual void setActive(bool isActive) noexcept override; + + virtual bool isDirty() noexcept override; + virtual void update() noexcept override; + + void setTransitionDelay(const f32 transitionDelay) noexcept; + + void setHoverColor(Color4 color) noexcept; + void setPressColor(Color4 color) noexcept; + void setIdleColor(Color4 color) noexcept; + + + void setState(State state) noexcept; + }; + + + class SUTK_API DefaultButtonGraphicNoLabel : public RenderableContainer, public IButtonGraphic + { + private: + + // Visual Representation of the Button's existence + VisualButtonRect* m_visualButton; + + public: + DefaultButtonGraphicNoLabel(UIDriver& driver, Container* parent) noexcept; + + // Implementation of IButtonGraphic's functions + virtual void onHover(HoverInfo info) noexcept override; + virtual void onPress() noexcept override; + virtual void onRelease() noexcept override; + virtual Vec2Df getMinBoundSize() noexcept override; + + VisualButtonRect& getVisualButtonRect() noexcept { return *m_visualButton; } + }; + + class Label; + class SUTK_API DefaultButtonGraphic : public DefaultButtonGraphicNoLabel + { + private: + Label* m_label; + public: + DefaultButtonGraphic(UIDriver& driver, Container* parent) noexcept; + + // Overrides + virtual Vec2Df getMinBoundSize() noexcept override; + + Label& getLabel() noexcept { return *m_label; } + }; +} \ No newline at end of file diff --git a/sutk/source/Button.cpp b/sutk/source/Button.cpp index f225508a..c0e8b6e2 100644 --- a/sutk/source/Button.cpp +++ b/sutk/source/Button.cpp @@ -1,113 +1,9 @@ #include -#include // for SUTK::SmallText -#include // for SUTK::Label +#include namespace SUTK { - VisualButtonRect::VisualButtonRect(SUTK::UIDriver& driver, SUTK::RenderableContainer* container) noexcept : RenderRectFillRound(driver, container), - m_cta({ { State::Hover, Color4::grey(0.7f) }, - { State::Press, Color4::grey(0.3f) }, - { State::Idle, Color4::grey(1.0f) } }) - { - m_cta.setDefault(State::Idle); - } - - void VisualButtonRect::setActive(bool isActive) noexcept - { - RenderRectFillRound::setActive(isActive); - if(isActive) - { - m_cta.setDefault(State::Idle); - setColor(m_cta.getValue()); - } - } - - bool VisualButtonRect::isDirty() noexcept - { - return m_cta.isRunning() || RenderRectFillRound::isDirty(); - } - void VisualButtonRect::update() noexcept - { - if(m_cta.isRunning()) - { - m_cta.update(); - setColor(m_cta.getValue()); - } - RenderRectFillRound::update(); - } - - void VisualButtonRect::setTransitionDelay(const f32 transitionDelay) noexcept - { - m_cta.setTransitionDelay(transitionDelay); - } - - void VisualButtonRect::setHoverColor(Color4 color) noexcept - { - m_cta.set(State::Hover, color); - } - void VisualButtonRect::setPressColor(Color4 color) noexcept - { - m_cta.set(State::Press, color); - } - void VisualButtonRect::setIdleColor(Color4 color) noexcept - { - m_cta.set(State::Idle, color); - m_cta.setDefault(State::Idle); - setColor(m_cta.getValue()); - } - void VisualButtonRect::setState(State state) noexcept - { - m_cta.transitionTo(state); - } - - DefaultButtonGraphic::DefaultButtonGraphic(UIDriver& driver, Container* parent) noexcept : RenderableContainer(driver, parent) - { - _com_assert(parent != NULL); - - // size of this graphic should be as that of Button's rect - setRect({ { 0, 0 }, parent->getSize() }); - getAnchorRect()->setRect( { 0, 0, 1, 1 }); - - m_visualButton = driver.createRenderable(this); - - m_label = driver.createContainer