namespace ui
{
class panel : public sf::Drawable
{
public:
panel(dword id, sf::FloatRect rect);
explicit panel(dword id);
void add_widget(dword id, std::shared_ptr<widget> wg);
bool get_widget(dword id, std::shared_ptr<widget>& wg);
bool is_visible();
void on_update();
void on_key(char c);
void on_mouse_pressed(bool left, sf::Vector2f v);
void on_mouse_released(sf::Vector2f v);
void on_mouse_move(sf::Vector2f v);
void on_mouse_left();
void set_pos(sf::Vector2f v);
void set_size(float width, float height);
void set_visible(bool visible);
sf::FloatRect getGlobalBounds();
dword get_id();
private:
dword m_id = 0;
bool m_visible = false;
sf::RectangleShape m_background;
sf::RectangleShape m_titlebar;
sf::Vector2f m_pressed;
bool m_title_bar_pressed = false;
std::unordered_map<dword, std::shared_ptr<widget>> m_widgets;
dword m_widget_with_focus_id = 0;
dword m_widget_hovered_id = 0;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};
}
//.cpp file
#include "panel.hpp"
#include <iostream>
namespace ui
{
panel::panel(dword id, sf::FloatRect rect)
{
m_id = id;
m_background.setSize(sf::Vector2f(rect.width, rect.height));
m_background.setPosition(rect.left, rect.top);
}
panel::panel(dword id)
{
m_id = id;
if (m_id == 1)
m_background.setFillColor(sf::Color::Green);
if (m_id == 2)
m_background.setFillColor(sf::Color::Red);
if (m_id == 3)
m_background.setFillColor(sf::Color(211, 211, 211));
m_background.setOutlineThickness(3);
m_background.setOutlineColor(sf::Color::Black);
m_titlebar.setFillColor(sf::Color::Blue);
}
void panel::add_widget(dword id, std::shared_ptr<widget> wg)
{
}
bool panel::get_widget(dword id, std::shared_ptr<widget>& wg)
{
auto it = m_widgets.find(id);
if (it == m_widgets.end())
return false;
wg = it->second;
return true;
}
bool panel::is_visible()
{
return m_visible;
}
void panel::on_update()
{
}
void panel::on_key(char c)
{
}
void panel::on_mouse_pressed(bool left, sf::Vector2f v)
{
if (m_titlebar.getGlobalBounds().contains(v))
{
m_title_bar_pressed = true;
m_pressed.x = v.x - m_background.getGlobalBounds().left;
m_pressed.y = v.y - m_background.getGlobalBounds().top;
}
}
void panel::on_mouse_released(sf::Vector2f v)
{
m_title_bar_pressed = false;
}
void panel::on_mouse_move(sf::Vector2f v)
{
if (m_title_bar_pressed)
{
m_background.setPosition(v.x - m_pressed.x, v.y - m_pressed.y);
m_titlebar.setPosition(v.x - m_pressed.x, v.y - m_pressed.y);
}
}
void panel::on_mouse_left()
{
}
void panel::set_visible(bool visible)
{
m_visible = visible;
}
void panel::set_pos(sf::Vector2f v)
{
m_background.setPosition(v);
m_titlebar.setPosition(v);
}
void panel::set_size(float width, float height)
{
m_background.setSize(sf::Vector2f(width, height));
m_titlebar.setSize(sf::Vector2f(width, 15));
}
sf::FloatRect panel::getGlobalBounds()
{
return m_background.getGlobalBounds();
}
dword panel::get_id()
{
return m_id;
}
void panel::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(m_background);
target.draw(m_titlebar);
}
}