2021-08-16 20:54:03 +02:00
|
|
|
#include "menubar.h"
|
|
|
|
#include "iostream"
|
2021-08-20 20:33:23 +02:00
|
|
|
|
2021-08-26 19:41:16 +02:00
|
|
|
MenuBar::MenuBar(const std::shared_ptr<sf::Font>& font) :
|
|
|
|
_font(font),
|
2021-08-20 20:33:23 +02:00
|
|
|
_button_width(170)
|
2021-08-16 20:54:03 +02:00
|
|
|
{
|
2021-08-20 20:33:23 +02:00
|
|
|
_bar_rect.setFillColor(sf::Color(88, 57, 107));
|
2021-08-16 20:54:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MenuBar::input(const sf::Event &event)
|
|
|
|
{
|
2021-08-26 18:54:30 +02:00
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sf::Event::MouseButtonReleased:
|
|
|
|
if (!isUnderMouse(event.mouseButton.x, event.mouseButton.y))
|
|
|
|
{
|
|
|
|
for (auto& submenu : _submenus)
|
|
|
|
submenu->unlock();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-16 20:54:03 +02:00
|
|
|
Widget::input(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuBar::update()
|
|
|
|
{
|
|
|
|
Widget::update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuBar::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
|
|
|
{
|
2021-08-26 18:54:30 +02:00
|
|
|
if (_is_visible)
|
|
|
|
target.draw(_bar_rect, states);
|
|
|
|
|
2021-08-20 20:33:23 +02:00
|
|
|
Widget::draw(target, states);
|
2021-08-16 20:54:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MenuBar::setRect(const sf::FloatRect& rect)
|
|
|
|
{
|
|
|
|
_bar_rect.setPosition(rect.left, rect.top);
|
|
|
|
_bar_rect.setSize({rect.width, rect.height});
|
|
|
|
|
2021-08-20 20:33:23 +02:00
|
|
|
// Buttons will not resize
|
2021-08-16 20:54:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MenuBar::setPosition(const sf::Vector2f& position)
|
|
|
|
{
|
|
|
|
_bar_rect.setPosition(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MenuBar::isUnderMouse(int mouse_x, int mouse_y) const
|
|
|
|
{
|
2021-08-20 20:33:23 +02:00
|
|
|
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)
|
|
|
|
{
|
2021-08-26 19:41:16 +02:00
|
|
|
const auto new_button = std::make_shared<PushButton>(name, _font);
|
2021-08-20 20:33:23 +02:00
|
|
|
|
|
|
|
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]()
|
|
|
|
{
|
2021-08-26 18:54:30 +02:00
|
|
|
submenu->setVisibility(true);
|
|
|
|
submenu->lock();
|
2021-08-20 20:33:23 +02:00
|
|
|
});
|
|
|
|
|
2021-08-26 18:54:30 +02:00
|
|
|
submenu->setPosition({static_cast<float>(current_index * _button_width),
|
|
|
|
_bar_rect.getSize().y});
|
|
|
|
|
|
|
|
new_button->setFillColors(sf::Color(171, 141, 189), sf::Color(48, 27, 57));
|
2021-08-20 20:33:23 +02:00
|
|
|
addChild(new_button);
|
2021-08-26 18:54:30 +02:00
|
|
|
addChild(submenu);
|
|
|
|
|
|
|
|
_submenus.emplace_back(submenu);
|
|
|
|
|
|
|
|
++_amount_buttons;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuBar::setVisibility(bool is_visible)
|
|
|
|
{
|
|
|
|
Widget::setVisibility(is_visible);
|
|
|
|
|
|
|
|
for (auto& submenu : _submenus)
|
|
|
|
submenu->setVisibility(false);
|
2021-08-16 20:54:03 +02:00
|
|
|
}
|