66 lines
1.8 KiB
C++
66 lines
1.8 KiB
C++
#include "menubar.h"
|
|
#include "iostream"
|
|
|
|
MenuBar::MenuBar() :
|
|
_button_width(170)
|
|
{
|
|
_bar_rect.setFillColor(sf::Color(88, 57, 107));
|
|
}
|
|
|
|
void MenuBar::input(const sf::Event &event)
|
|
{
|
|
Widget::input(event);
|
|
}
|
|
|
|
void MenuBar::update()
|
|
{
|
|
Widget::update();
|
|
}
|
|
|
|
void MenuBar::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
|
{
|
|
target.draw(_bar_rect, states);
|
|
Widget::draw(target, states);
|
|
}
|
|
|
|
void MenuBar::setRect(const sf::FloatRect& rect)
|
|
{
|
|
_bar_rect.setPosition(rect.left, rect.top);
|
|
_bar_rect.setSize({rect.width, rect.height});
|
|
|
|
// Buttons will not resize
|
|
}
|
|
|
|
void MenuBar::setPosition(const sf::Vector2f& position)
|
|
{
|
|
_bar_rect.setPosition(position);
|
|
}
|
|
|
|
bool MenuBar::isUnderMouse(int mouse_x, int mouse_y) const
|
|
{
|
|
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);
|
|
}
|