Implement a way to add submenus to main menu bar
This commit is contained in:
parent
7e2f038a8b
commit
5b7f2c1aa2
|
@ -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);
|
||||
|
|
|
@ -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<void(void)> callback);
|
||||
|
||||
|
|
|
@ -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<MenuDrop>& submenu)
|
||||
{
|
||||
const auto new_button = std::make_shared<Button>(name);
|
||||
|
||||
std::size_t current_index = _amount_buttons;
|
||||
new_button->setRect(sf::FloatRect(current_index * _button_width, 0, _button_width, _bar_rect.getSize().y));
|
||||
new_button->setCallback([submenu=submenu]()
|
||||
{
|
||||
submenu->trigger();
|
||||
});
|
||||
|
||||
new_button->setFillColor(sf::Color(171, 141, 189));
|
||||
addChild(new_button);
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include "widget.h"
|
||||
#include "menubutton.h"
|
||||
#include "menudrop.h"
|
||||
#include "button.h"
|
||||
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
|
||||
|
@ -18,10 +18,10 @@ public:
|
|||
virtual void setPosition(const sf::Vector2f& position) override;
|
||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
||||
|
||||
bool isActive() const;
|
||||
void addSubMenu(std::unique_ptr<MenuDrop>&& submenu);
|
||||
void addSubMenu(std::string name, const std::shared_ptr<MenuDrop>& submenu);
|
||||
|
||||
private:
|
||||
sf::RectangleShape _bar_rect;
|
||||
std::vector<std::unique_ptr<MenuButton>> _bar_buttons;
|
||||
std::size_t _amount_buttons;
|
||||
std::size_t _button_width;
|
||||
};
|
||||
|
|
|
@ -42,3 +42,12 @@ bool MenuDrop::isUnderMouse(int mouse_x, int mouse_y) const
|
|||
(void)mouse_y;
|
||||
return false;//_bar_rect.getGlobalBounds().contains(mouse_x, mouse_y);
|
||||
}
|
||||
|
||||
void MenuDrop::trigger()
|
||||
{
|
||||
bool new_visibility = !_is_visible;
|
||||
|
||||
setVisibility(new_visibility);
|
||||
for (const auto& child : _children)
|
||||
child->setVisibility(new_visibility);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "widget.h"
|
||||
#include "menubutton.h"
|
||||
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
|
||||
|
@ -14,6 +15,8 @@ public:
|
|||
virtual void setPosition(const sf::Vector2f& position) override;
|
||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
||||
|
||||
void trigger();
|
||||
|
||||
private:
|
||||
sf::RectangleShape _content_rect;
|
||||
sf::RectangleShape _bar_button_rect;
|
||||
|
|
Loading…
Reference in New Issue