diff --git a/src/gui/widgets/button.cpp b/src/gui/widgets/button.cpp index 7f9b1b7..34b8fdb 100644 --- a/src/gui/widgets/button.cpp +++ b/src/gui/widgets/button.cpp @@ -1,42 +1,12 @@ #include "button.h" -Button::Button(const std::string &text) : - _pressed(false) +Button::Button(const std::string &text) { setText(text); _button_text.setFillColor(sf::Color::Black); _button_content.setFillColor(sf::Color::White); } -void Button::input(const sf::Event& event) -{ - switch (event.type) - { - default: - break; - - case sf::Event::MouseButtonPressed: - if (isUnderMouse(event.mouseButton.x, event.mouseButton.y)) - { - _pressed = true; - _button_content.setFillColor(sf::Color(155, 155, 155)); - } - break; - - case sf::Event::MouseButtonReleased: - if (_pressed) - { - _button_content.setFillColor(sf::Color::White); - _pressed = false; - if (isUnderMouse(event.mouseButton.x, event.mouseButton.y)) - _on_click_callback(); - } - break; - } - - Widget::input(event); -} - void Button::update() { Widget::update(); @@ -63,7 +33,7 @@ void Button::setPosition(const sf::Vector2f &position) bool Button::isUnderMouse(int mouse_x, int mouse_y) const { - return _button_content.getGlobalBounds().contains(mouse_x, mouse_y); + return _is_visible && _button_content.getGlobalBounds().contains(mouse_x, mouse_y); } void Button::setFillColor(sf::Color&& color) @@ -75,9 +45,3 @@ void Button::setText(const std::string& text) { _button_text.setString(text); } - -void Button::setCallback(std::function callback) -{ - _on_click_callback = callback; -} - diff --git a/src/gui/widgets/button.h b/src/gui/widgets/button.h index f342484..97f44c9 100644 --- a/src/gui/widgets/button.h +++ b/src/gui/widgets/button.h @@ -8,24 +8,20 @@ class Button : public Widget { public: - Button(const std::string& text); + explicit Button(const std::string& text); - virtual void input(const sf::Event& event) override; - virtual void update() override; - virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override; - virtual void setRect(const sf::FloatRect& rect) override; - virtual void setPosition(const sf::Vector2f& position) override; - virtual bool isUnderMouse(int mouse_x, int mouse_y) const override; + virtual void input(const sf::Event& event) override = 0; + virtual void update() override final; + virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override final; + virtual void setRect(const sf::FloatRect& rect) override final; + virtual void setPosition(const sf::Vector2f& position) override final; + virtual bool isUnderMouse(int mouse_x, int mouse_y) const override final; - void setFillColor(sf::Color&& color); - void setText(const std::string& text); - void setCallback(std::function callback); + virtual void setFillColor(sf::Color&& color); + virtual void setText(const std::string& text); -private: +protected: sf::RectangleShape _button_content; sf::Text _button_text; - bool _pressed; - - std::function _on_click_callback; }; diff --git a/src/gui/widgets/cascademenubutton.cpp b/src/gui/widgets/cascademenubutton.cpp new file mode 100644 index 0000000..a0b6e44 --- /dev/null +++ b/src/gui/widgets/cascademenubutton.cpp @@ -0,0 +1,35 @@ +#include "cascademenubutton.h" +#include "menudrop.h" + +CascadeMenuButton::CascadeMenuButton(const std::string& text) : + Button(text) +{} + +void CascadeMenuButton::input(const sf::Event& event) +{ + Button::input(event); + + switch (event.type) + { + default: + break; + + case sf::Event::MouseMoved: + if (isUnderMouse(event.mouseButton.x, event.mouseButton.y)) + { + _button_content.setFillColor(sf::Color(84, 158, 253)); + _submenu->lock(); + } + else + { + _button_content.setFillColor(sf::Color::White); + _submenu->unlock(); + } + break; + } +} + +void CascadeMenuButton::setSubmenu(const std::shared_ptr& submenu) +{ + _submenu = submenu; +} diff --git a/src/gui/widgets/cascademenubutton.h b/src/gui/widgets/cascademenubutton.h new file mode 100644 index 0000000..d15efdd --- /dev/null +++ b/src/gui/widgets/cascademenubutton.h @@ -0,0 +1,18 @@ +#pragma once + +#include "button.h" + +class MenuDrop; + +class CascadeMenuButton : public Button +{ +public: + explicit CascadeMenuButton(const std::string& text); + virtual void input(const sf::Event& event) override final; + + void setSubmenu(const std::shared_ptr& submenu); + +private: + std::shared_ptr _submenu; +}; + diff --git a/src/gui/widgets/menubutton.h b/src/gui/widgets/menubutton.h deleted file mode 100644 index 55155e1..0000000 --- a/src/gui/widgets/menubutton.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "widget.h" - -#include - -class MenuButton : public Widget -{ -public: - explicit MenuButton(std::string&& text); - - virtual void input(const sf::Event& event) = 0; - virtual void update() = 0; - virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0; - virtual void setRect(const sf::FloatRect& rect) = 0; - virtual void setPosition(const sf::Vector2f& position) = 0; - virtual bool isUnderMouse(int mouse_x, int mouse_y) const = 0; - - virtual bool isActive() const = 0; - virtual void setActive(bool is_active) = 0; - virtual void setHovered(bool is_hovered) = 0; -}; diff --git a/src/gui/widgets/menudrop.cpp b/src/gui/widgets/menudrop.cpp index 2be2073..95864dd 100644 --- a/src/gui/widgets/menudrop.cpp +++ b/src/gui/widgets/menudrop.cpp @@ -1,53 +1,98 @@ #include "menudrop.h" +#include "menuseparator.h" -void MenuDrop::input(const sf::Event &event) -{ - for (auto& child : _children) - child->input(event); -} +MenuDrop::MenuDrop() : + _is_locked(false), + _button_height(27), + _button_index(0) +{} -void MenuDrop::update() -{ - for (auto& child : _children) - child->update(); -} - -void MenuDrop::draw(sf::RenderTarget& target, sf::RenderStates states) const +void MenuDrop::input(const sf::Event& event) { if (!_is_visible) return; - for (auto& child : _children) - child->draw(target, states); + switch (event.type) + { + default: + break; + + case sf::Event::MouseMoved: + if (isUnderMouse(event.mouseButton.x, event.mouseButton.y)) + { + Widget::input(event); + } + else + { + if (!isLocked()) + setVisibility(false); + } + break; + } +} + +void MenuDrop::update() +{ + Widget::update(); +} + +void MenuDrop::draw(sf::RenderTarget& target, sf::RenderStates states) const +{ + target.draw(_content_rect); + Widget::draw(target, states); } void MenuDrop::setRect(const sf::FloatRect& rect) { - (void)rect; - /*_bar_rect.setPosition(rect.left, rect.top); - _bar_rect.setSize({rect.width, rect.height}); - - _bar_button_rect.setSize(sf::Vector2f(100, _bar_rect.getSize().y));*/ + _content_rect.setPosition(rect.left, rect.top); + _content_rect.setSize({rect.width, rect.height}); } void MenuDrop::setPosition(const sf::Vector2f& position) { - (void)position; - //_bar_rect.setPosition(position); + _content_rect.setPosition(position); } bool MenuDrop::isUnderMouse(int mouse_x, int mouse_y) const { - (void)mouse_x; - (void)mouse_y; - return false;//_bar_rect.getGlobalBounds().contains(mouse_x, mouse_y); + return _is_visible && _content_rect.getGlobalBounds().contains(mouse_x, mouse_y); } -void MenuDrop::trigger() +void MenuDrop::addButton(const std::shared_ptr