Implement widget and button

This commit is contained in:
NaiJi ✨ 2021-07-27 01:44:10 +03:00
parent 92fd5c0c27
commit 686107b215
4 changed files with 114 additions and 0 deletions

48
src/gui/button.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "button.h"
Button::Button(const std::string &text)
{
_button_text.setString(text);
_button_text.setFillColor(sf::Color::Black);
_button_content.setFillColor(sf::Color::White);
}
void Button::input(const sf::Event& event)
{
switch (event.type)
{
case sf::Event::MouseButtonPressed:
break;
}
}
void Button::update()
{
}
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
}
void Button::setRect(const sf::IntRect& rect)
{
}
void Button::setPosition(const sf::Vector2f& position)
{
}
void Button::setText(const std::string& text)
{
}
void Button::setCallback(std::function<void(void)> callback)
{
}

26
src/gui/button.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include "widget.h"
#include <functional>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Text.hpp>
class Button : public Widget
{
public:
Button(const std::string& text);
virtual void input(const sf::Event& event) override;
virtual void update() override;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
virtual void setRect(const sf::IntRect& rect) override;
virtual void setPosition(const sf::Vector2f& position) override;
void setText(const std::string& text);
void setCallback(std::function<void(void)> callback);
private:
sf::RectangleShape _button_content;
sf::Text _button_text;
};

13
src/gui/widget.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "widget.h"
Widget::Widget(const std::shared_ptr<Widget>& parent) :
_parent(parent)
{
if (_parent)
_parent->addChild(shared_from_this());
}
void Widget::addChild(const std::shared_ptr<Widget> &child)
{
_children.emplace_back(child);
}

27
src/gui/widget.h Normal file
View File

@ -0,0 +1,27 @@
#pragma once
#include <vector>
#include <memory>
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/Drawable.hpp>
class Widget : public sf::Drawable, std::enable_shared_from_this<Widget>
{
public:
Widget(const std::shared_ptr<Widget>& parent = nullptr);
virtual ~Widget() = default;
virtual void input(const sf::Event& event) = 0;
virtual void update() = 0;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
virtual void setRect(const sf::FloatRect& rect) = 0;
virtual void setPosition(const sf::Vector2f& position) = 0;
protected:
void addChild(const std::shared_ptr<Widget>& child);
std::vector<std::shared_ptr<Widget>> _children;
const std::shared_ptr<Widget> _parent;
};