From 5b7f2c1aa28303652aac49e01bc8c2fda6eb9532 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Fri, 20 Aug 2021 21:33:23 +0300 Subject: [PATCH] Implement a way to add submenus to main menu bar --- src/gui/widgets/button.cpp | 5 ++++ src/gui/widgets/button.h | 1 + src/gui/widgets/menubar.cpp | 47 +++++++++++++++++++++++------------- src/gui/widgets/menubar.h | 8 +++--- src/gui/widgets/menudrop.cpp | 9 +++++++ src/gui/widgets/menudrop.h | 3 +++ 6 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/gui/widgets/button.cpp b/src/gui/widgets/button.cpp index d982983..7f9b1b7 100644 --- a/src/gui/widgets/button.cpp +++ b/src/gui/widgets/button.cpp @@ -66,6 +66,11 @@ bool Button::isUnderMouse(int mouse_x, int mouse_y) const return _button_content.getGlobalBounds().contains(mouse_x, mouse_y); } +void Button::setFillColor(sf::Color&& color) +{ + _button_content.setFillColor(std::move(color)); +} + void Button::setText(const std::string& text) { _button_text.setString(text); diff --git a/src/gui/widgets/button.h b/src/gui/widgets/button.h index 44236c2..f342484 100644 --- a/src/gui/widgets/button.h +++ b/src/gui/widgets/button.h @@ -17,6 +17,7 @@ public: virtual void setPosition(const sf::Vector2f& position) override; virtual bool isUnderMouse(int mouse_x, int mouse_y) const override; + void setFillColor(sf::Color&& color); void setText(const std::string& text); void setCallback(std::function callback); diff --git a/src/gui/widgets/menubar.cpp b/src/gui/widgets/menubar.cpp index de0510b..a5a8380 100644 --- a/src/gui/widgets/menubar.cpp +++ b/src/gui/widgets/menubar.cpp @@ -1,15 +1,15 @@ #include "menubar.h" #include "iostream" -MenuBar::MenuBar() + +MenuBar::MenuBar() : + _button_width(170) { + _bar_rect.setFillColor(sf::Color(88, 57, 107)); } void MenuBar::input(const sf::Event &event) { Widget::input(event); - - for (const auto& bar_button : _bar_buttons) - bar_button->input(event); } void MenuBar::update() @@ -19,12 +19,8 @@ void MenuBar::update() void MenuBar::draw(sf::RenderTarget& target, sf::RenderStates states) const { - Widget::draw(target, states); - target.draw(_bar_rect, states); - - for (const auto& bar_button : _bar_buttons) - target.draw(*bar_button, states); + Widget::draw(target, states); } void MenuBar::setRect(const sf::FloatRect& rect) @@ -32,13 +28,7 @@ void MenuBar::setRect(const sf::FloatRect& rect) _bar_rect.setPosition(rect.left, rect.top); _bar_rect.setSize({rect.width, rect.height}); - int x = 0; - int button_width = 100; - for (const auto& bar_button : _bar_buttons) - { - bar_button->setRect(sf::FloatRect(x, 0, button_width, _bar_rect.getSize().y)); - x += button_width; - } + // Buttons will not resize } void MenuBar::setPosition(const sf::Vector2f& position) @@ -48,5 +38,28 @@ void MenuBar::setPosition(const sf::Vector2f& position) bool MenuBar::isUnderMouse(int mouse_x, int mouse_y) const { - return _bar_rect.getGlobalBounds().contains(mouse_x, mouse_y); + bool bar_under_mouse = _bar_rect.getGlobalBounds().contains(mouse_x, mouse_y); + + bool submenus_under_mouse = std::any_of(_children.begin(), _children.end(), + [x=mouse_x, y=mouse_y](const auto& child) + { + return child->isUnderMouse(x, y); + }); + + return bar_under_mouse || submenus_under_mouse; +} + +void MenuBar::addSubMenu(std::string name, const std::shared_ptr& submenu) +{ + const auto new_button = std::make_shared