Implement widget and button

selection
NaiJi ✨ 3 years ago
parent 92fd5c0c27
commit 686107b215

@ -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)
{
}

@ -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;
};

@ -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);
}

@ -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;
};
Loading…
Cancel
Save