project-kyoku/src/gui/widgets/menudrop.cpp

99 lines
1.9 KiB
C++

#include "menudrop.h"
#include "menuseparator.h"
MenuDrop::MenuDrop() :
_is_locked(false),
_button_height(27),
_button_index(0)
{}
void MenuDrop::input(const sf::Event& event)
{
if (!_is_visible)
return;
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)
{
_content_rect.setPosition(rect.left, rect.top);
_content_rect.setSize({rect.width, rect.height});
}
void MenuDrop::setPosition(const sf::Vector2f& position)
{
_content_rect.setPosition(position);
}
bool MenuDrop::isUnderMouse(int mouse_x, int mouse_y) const
{
return _is_visible && _content_rect.getGlobalBounds().contains(mouse_x, mouse_y);
}
void MenuDrop::addButton(const std::shared_ptr<Button>& button)
{
add(button);
}
void MenuDrop::addSeparator()
{
add(std::make_shared<MenuSeparator>());
}
void MenuDrop::add(const std::shared_ptr<Widget> &widget)
{
const auto& parent_size = _content_rect.getSize();
const auto& parent_position = _content_rect.getPosition();
widget->setRect(sf::FloatRect(parent_position.x,
parent_position.y + (_button_height * _button_index),
parent_size.x,
_button_height));
addChild(widget);
}
void MenuDrop::lock()
{
_is_locked = true;
}
void MenuDrop::unlock()
{
_is_locked = false;
}
bool MenuDrop::isLocked() const
{
return _is_locked;
}