Add resource holder

This commit is contained in:
NaiJi ✨ 2021-08-21 15:29:58 +03:00
parent 5b7f2c1aa2
commit 5540361f94
4 changed files with 53 additions and 10 deletions

View File

@ -2,6 +2,7 @@
#include <stack>
#include <memory>
#include <functional>
#include <SFML/Window/Event.hpp>
class GUIState

View File

@ -0,0 +1,42 @@
#pragma once
#include <memory>
#include <map>
namespace sf { class Texture; class Font; }
template <typename Resource, typename Id>
class ResourceHolder
{
public:
bool load(Id id, const std::string& filename)
{
auto resource = std::make_unique<Resource>();
if (!resource->loadFromFile(filename))
return false;
_resources[id] = std::move(resource);
return true;
}
Resource& get(Id id) const
{
const auto found = _resources.find(id);
return *found->second;
}
private:
std::map<Id, std::unique_ptr<Resource>> _resources;
};
namespace Fonts
{
enum class Id
{
GUI
};
}
using FontHolder = ResourceHolder<sf::Font, Fonts::Id>;

View File

@ -1,13 +1,13 @@
#include "bpmcalculator.h"
#include "bpmcalculatorwidget.h"
BPMCalculatorWindow::BPMCalculatorWindow() :
BPMCalculatorWidget::BPMCalculatorWidget() :
_pressed(false)
{
_button_text.setFillColor(sf::Color::Black);
_button_content.setFillColor(sf::Color::White);
}
void BPMCalculatorWindow::input(const sf::Event& event)
void BPMCalculatorWidget::input(const sf::Event& event)
{
switch (event.type)
{
@ -36,12 +36,12 @@ void BPMCalculatorWindow::input(const sf::Event& event)
Widget::input(event);
}
void BPMCalculatorWindow::update()
void BPMCalculatorWidget::update()
{
Widget::update();
}
void BPMCalculatorWindow::draw(sf::RenderTarget& target, sf::RenderStates states) const
void BPMCalculatorWidget::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(_button_content, states);
target.draw(_button_text, states);
@ -49,18 +49,18 @@ void BPMCalculatorWindow::draw(sf::RenderTarget& target, sf::RenderStates states
Widget::draw(target, states);
}
void BPMCalculatorWindow::setRect(const sf::FloatRect& rect)
void BPMCalculatorWidget::setRect(const sf::FloatRect& rect)
{
_button_content.setPosition(rect.left, rect.top);
_button_content.setSize({rect.width, rect.height});
}
void BPMCalculatorWindow::setPosition(const sf::Vector2f &position)
void BPMCalculatorWidget::setPosition(const sf::Vector2f &position)
{
_button_content.setPosition(position);
}
bool BPMCalculatorWindow::isUnderMouse(int mouse_x, int mouse_y) const
bool BPMCalculatorWidget::isUnderMouse(int mouse_x, int mouse_y) const
{
return _button_content.getGlobalBounds().contains(mouse_x, mouse_y);
}

View File

@ -5,10 +5,10 @@
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Text.hpp>
class BPMCalculatorWindow : public Widget
class BPMCalculatorWidget : public Widget
{
public:
BPMCalculatorWindow();
BPMCalculatorWidget();
virtual void input(const sf::Event& event) override;
virtual void update() override;