From 5540361f94f7613defc3736e74557a047cb87494 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Sat, 21 Aug 2021 15:29:58 +0300 Subject: [PATCH] Add resource holder --- include/gui/state.h | 1 + include/tools/resourceholder.h | 42 +++++++++++++++++++ ...calculator.cpp => bpmcalculatorwidget.cpp} | 16 +++---- ...{bpmcalculator.h => bpmcalculatorwidget.h} | 4 +- 4 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 include/tools/resourceholder.h rename src/gui/widgets/{bpmcalculator.cpp => bpmcalculatorwidget.cpp} (73%) rename src/gui/widgets/{bpmcalculator.h => bpmcalculatorwidget.h} (90%) diff --git a/include/gui/state.h b/include/gui/state.h index ddf9ac6..6652ab8 100644 --- a/include/gui/state.h +++ b/include/gui/state.h @@ -2,6 +2,7 @@ #include #include +#include #include class GUIState diff --git a/include/tools/resourceholder.h b/include/tools/resourceholder.h new file mode 100644 index 0000000..5c057cf --- /dev/null +++ b/include/tools/resourceholder.h @@ -0,0 +1,42 @@ +#pragma once + +#include +#include + +namespace sf { class Texture; class Font; } + +template +class ResourceHolder +{ +public: + bool load(Id id, const std::string& filename) + { + auto resource = std::make_unique(); + 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> _resources; +}; + +namespace Fonts +{ + enum class Id + { + GUI + }; +} + +using FontHolder = ResourceHolder; + diff --git a/src/gui/widgets/bpmcalculator.cpp b/src/gui/widgets/bpmcalculatorwidget.cpp similarity index 73% rename from src/gui/widgets/bpmcalculator.cpp rename to src/gui/widgets/bpmcalculatorwidget.cpp index 0129209..a37c444 100644 --- a/src/gui/widgets/bpmcalculator.cpp +++ b/src/gui/widgets/bpmcalculatorwidget.cpp @@ -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); } diff --git a/src/gui/widgets/bpmcalculator.h b/src/gui/widgets/bpmcalculatorwidget.h similarity index 90% rename from src/gui/widgets/bpmcalculator.h rename to src/gui/widgets/bpmcalculatorwidget.h index 1c44ed8..b5b4aae 100644 --- a/src/gui/widgets/bpmcalculator.h +++ b/src/gui/widgets/bpmcalculatorwidget.h @@ -5,10 +5,10 @@ #include #include -class BPMCalculatorWindow : public Widget +class BPMCalculatorWidget : public Widget { public: - BPMCalculatorWindow(); + BPMCalculatorWidget(); virtual void input(const sf::Event& event) override; virtual void update() override;