1
0
mirror of synced 2025-02-10 15:43:00 +01:00

switch to shared_ptr

This commit is contained in:
Stepland 2020-03-03 23:30:45 +01:00
parent e2d4e50004
commit 5c0861ae0b
8 changed files with 39 additions and 42 deletions

View File

@ -1,6 +1,7 @@
#include "OptionPage.hpp" #include "OptionPage.hpp"
#include <iostream> #include <iostream>
#include <memory>
#include <vector> #include <vector>
#include "Ribbon.hpp" #include "Ribbon.hpp"
@ -17,7 +18,6 @@ namespace MusicSelect {
OptionPage(resources), OptionPage(resources),
m_ribbon(layout, resources) m_ribbon(layout, resources)
{ {
update();
} }
void RibbonPage::click(const Data::Button& button) { void RibbonPage::click(const Data::Button& button) {
@ -46,21 +46,19 @@ namespace MusicSelect {
MainOptionPage::MainOptionPage(SharedResources& resources) : MainOptionPage::MainOptionPage(SharedResources& resources) :
RibbonPage(MainOptionPage::create_layout(resources), resources) RibbonPage(MainOptionPage::create_layout(resources), resources)
{ {
update();
} }
PanelLayout MainOptionPage::create_layout(SharedResources& resources) { PanelLayout MainOptionPage::create_layout(SharedResources& resources) {
std::vector<std::unique_ptr<Panel>> subpages; std::vector<std::shared_ptr<Panel>> subpages;
jbcoe::polymorphic_value<OptionPage> marker_select{MarkerSelect{resources}}; auto marker_select = std::make_shared<MarkerSelect>(MarkerSelect{resources});
subpages.emplace_back(SubpagePanel{resources, marker_select, "markers"}); subpages.emplace_back(std::make_shared<SubpagePanel>(resources, marker_select, "markers"));
return PanelLayout{subpages, resources}; return PanelLayout{subpages, resources};
} }
MarkerSelect::MarkerSelect(SharedResources& resources) : MarkerSelect::MarkerSelect(SharedResources& resources) :
RibbonPage(MarkerSelect::create_layout(resources), resources) RibbonPage(MarkerSelect::create_layout(resources), resources)
{ {
update();
} }
MarkerSelect::~MarkerSelect() { MarkerSelect::~MarkerSelect() {
@ -68,9 +66,9 @@ namespace MusicSelect {
} }
PanelLayout MarkerSelect::create_layout(SharedResources& resources) { PanelLayout MarkerSelect::create_layout(SharedResources& resources) {
std::vector<std::unique_ptr<Panel>> markers; std::vector<std::shared_ptr<Panel>> markers;
for (const auto &[name, marker] : resources.markers) { for (const auto &[name, marker] : resources.markers) {
markers.emplace_back(MarkerPanel{resources, marker}); markers.emplace_back(std::make_shared<MarkerPanel>(resources, marker));
} }
return PanelLayout{markers, resources}; return PanelLayout{markers, resources};
} }

View File

@ -14,7 +14,7 @@ namespace MusicSelect {
class OptionPage : public sf::Drawable, public sf::Transformable, public HoldsSharedResources { class OptionPage : public sf::Drawable, public sf::Transformable, public HoldsSharedResources {
public: public:
OptionPage(SharedResources& resources) : HoldsSharedResources(resources) {}; OptionPage(SharedResources& resources) : HoldsSharedResources(resources) {update();};
// An option page should only every recive button presses ranging for 1 to 14 // An option page should only every recive button presses ranging for 1 to 14
// Going back a menu should be handled by the MusicSelect screen to avoid destroying the menu // Going back a menu should be handled by the MusicSelect screen to avoid destroying the menu
// while still being in a click() call on it // while still being in a click() call on it

View File

@ -4,13 +4,13 @@
namespace MusicSelect { namespace MusicSelect {
PanelLayout::PanelLayout( PanelLayout::PanelLayout(
const std::map<std::string,std::vector<std::unique_ptr<Panel>>>& categories, const std::map<std::string,std::vector<std::shared_ptr<Panel>>>& categories,
SharedResources& resources SharedResources& resources
) { ) {
for (auto &&[category, panels] : categories) { for (auto &&[category, panels] : categories) {
if (not panels.empty()) { if (not panels.empty()) {
std::vector<std::unique_ptr<Panel>> current_column; std::vector<std::shared_ptr<Panel>> current_column;
current_column.emplace_back(CategoryPanel{resources, category}); current_column.emplace_back(std::make_shared<CategoryPanel>(resources, category));
for (auto& panel : panels) { for (auto& panel : panels) {
if (current_column.size() == 3) { if (current_column.size() == 3) {
push_back({ push_back({
@ -20,11 +20,11 @@ namespace MusicSelect {
}); });
current_column.clear(); current_column.clear();
} }
current_column.push_back(std::move(panel)); current_column.push_back(panel);
} }
if (not current_column.empty()) { if (not current_column.empty()) {
while (current_column.size() < 3) { while (current_column.size() < 3) {
current_column.emplace_back(EmptyPanel{resources}); current_column.emplace_back(std::make_shared<EmptyPanel>(resources));
} }
push_back({ push_back({
std::move(current_column[0]), std::move(current_column[0]),
@ -38,10 +38,10 @@ namespace MusicSelect {
} }
PanelLayout::PanelLayout( PanelLayout::PanelLayout(
const std::vector<std::unique_ptr<Panel>>& panels, const std::vector<std::shared_ptr<Panel>>& panels,
SharedResources& resources SharedResources& resources
) { ) {
std::vector<std::unique_ptr<Panel>> current_column; std::vector<std::shared_ptr<Panel>> current_column;
for (auto& panel : panels) { for (auto& panel : panels) {
if (current_column.size() == 3) { if (current_column.size() == 3) {
push_back({ push_back({
@ -51,11 +51,11 @@ namespace MusicSelect {
}); });
current_column.clear(); current_column.clear();
} }
current_column.push_back(std::move(panel)); current_column.push_back(panel);
} }
if (not current_column.empty()) { if (not current_column.empty()) {
while (current_column.size() < 3) { while (current_column.size() < 3) {
current_column.emplace_back(EmptyPanel{resources}); current_column.emplace_back(std::make_shared<EmptyPanel>(resources));
} }
push_back({ push_back({
std::move(current_column[0]), std::move(current_column[0]),
@ -67,9 +67,9 @@ namespace MusicSelect {
} }
PanelLayout PanelLayout::red_empty_layout(SharedResources& resources) { PanelLayout PanelLayout::red_empty_layout(SharedResources& resources) {
std::vector<std::unique_ptr<Panel>> panels; std::vector<std::shared_ptr<Panel>> panels;
for (size_t i = 0; i < 3*4; i++) { for (size_t i = 0; i < 3*4; i++) {
panels.emplace(std::make_unique<ColoredMessagePanel>(resources, sf::Color::Red, "- EMPTY -")); panels.emplace_back(std::make_shared<ColoredMessagePanel>(resources, sf::Color::Red, "- EMPTY -"));
} }
return PanelLayout{panels, resources}; return PanelLayout{panels, resources};
} }
@ -84,38 +84,34 @@ namespace MusicSelect {
songs.end(), songs.end(),
[](std::shared_ptr<const Data::Song> a, std::shared_ptr<const Data::Song> b){return Data::Song::sort_by_title(*a, *b);} [](std::shared_ptr<const Data::Song> a, std::shared_ptr<const Data::Song> b){return Data::Song::sort_by_title(*a, *b);}
); );
std::map<std::string, std::vector<std::unique_ptr<Panel>>> categories; std::map<std::string, std::vector<std::shared_ptr<Panel>>> categories;
for (const auto &song : songs) { for (const auto &song : songs) {
if (song->title.size() > 0) { if (song->title.size() > 0) {
char letter = song->title[0]; char letter = song->title[0];
if ('A' <= letter and letter <= 'Z') { if ('A' <= letter and letter <= 'Z') {
categories categories
[std::string(1, letter)] [std::string(1, letter)]
.emplace_back(SongPanel{resources, song}); .emplace_back(std::make_shared<SongPanel>(resources, song));
} else if ('a' <= letter and letter <= 'z') { } else if ('a' <= letter and letter <= 'z') {
categories categories
[std::string(1, 'A' + (letter - 'a'))] [std::string(1, 'A' + (letter - 'a'))]
.emplace_back(SongPanel{resources, song}); .emplace_back(std::make_shared<SongPanel>(resources, song));
} else { } else {
categories["?"].emplace_back(SongPanel{resources, song}); categories["?"].emplace_back(std::make_shared<SongPanel>(resources, song));
} }
} else { } else {
categories["?"].emplace_back(SongPanel{resources, song}); categories["?"].emplace_back(std::make_shared<SongPanel>(resources, song));
} }
} }
return PanelLayout{categories, resources}; return PanelLayout{categories, resources};
} }
void PanelLayout::PanelLayout::push_vector(const std::vector<std::unique_ptr<Panel>>& panels) {
}
void PanelLayout::fill_layout(SharedResources& resources) { void PanelLayout::fill_layout(SharedResources& resources) {
while (size() < 4) { while (size() < 4) {
push_back({ push_back({
std::unique_ptr<Panel>{EmptyPanel{resources}}, std::make_shared<EmptyPanel>(resources),
std::unique_ptr<Panel>{EmptyPanel{resources}}, std::make_shared<EmptyPanel>(resources),
std::unique_ptr<Panel>{EmptyPanel{resources}} std::make_shared<EmptyPanel>(resources)
}); });
} }
} }

View File

@ -11,10 +11,10 @@ namespace MusicSelect {
class Panel; class Panel;
// PanelLayout restricts the ways you can create a scrollable grid of panels // PanelLayout restricts the ways you can create a scrollable grid of panels
class PanelLayout : public std::vector<std::array<std::unique_ptr<Panel>,3>> { class PanelLayout : public std::vector<std::array<std::shared_ptr<Panel>,3>> {
public: public:
explicit PanelLayout(const std::map<std::string,std::vector<std::unique_ptr<Panel>>>& categories, SharedResources& resources); explicit PanelLayout(const std::map<std::string,std::vector<std::shared_ptr<Panel>>>& categories, SharedResources& resources);
explicit PanelLayout(const std::vector<std::unique_ptr<Panel>>& panels, SharedResources& resources); explicit PanelLayout(const std::vector<std::shared_ptr<Panel>>& panels, SharedResources& resources);
static PanelLayout red_empty_layout(SharedResources& resources); static PanelLayout red_empty_layout(SharedResources& resources);
static PanelLayout title_sort(const Data::SongList& song_list, SharedResources& resources); static PanelLayout title_sort(const Data::SongList& song_list, SharedResources& resources);
private: private:

View File

@ -5,6 +5,7 @@
namespace MusicSelect { namespace MusicSelect {
void SubpagePanel::click(Ribbon&, const Data::Button&) { void SubpagePanel::click(Ribbon&, const Data::Button&) {
m_resources.options_state.push(*m_subpage); m_resources.options_state.push(*m_subpage);
m_resources.options_state.top().get().update();
} }
void SubpagePanel::draw(sf::RenderTarget& target, sf::RenderStates states) const { void SubpagePanel::draw(sf::RenderTarget& target, sf::RenderStates states) const {

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <memory>
#include <jbcoe/polymorphic_value.h> #include <jbcoe/polymorphic_value.h>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
@ -13,17 +15,17 @@ namespace MusicSelect {
public: public:
SubpagePanel( SubpagePanel(
SharedResources& resources, SharedResources& resources,
const jbcoe::polymorphic_value<OptionPage>& subpage, std::shared_ptr<OptionPage> subpage,
const std::string& name const std::string& name
) : ) :
Panel(resources), Panel(resources),
m_subpage(subpage), m_subpage(std::move(subpage)),
m_name(name) m_name(name)
{}; {};
void click(Ribbon& ribbon, const Data::Button& button) override; void click(Ribbon& ribbon, const Data::Button& button) override;
private: private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override; void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
jbcoe::polymorphic_value<OptionPage> m_subpage; std::shared_ptr<OptionPage> m_subpage;
std::string m_name; std::string m_name;
}; };
} }

View File

@ -77,7 +77,7 @@ namespace MusicSelect {
return (m_position + (Data::button_to_index(button) % 4)) % m_layout.size(); return (m_position + (Data::button_to_index(button) % 4)) % m_layout.size();
} }
std::unique_ptr<Panel>& Ribbon::get_panel_under_button(const Data::Button& button) { std::shared_ptr<Panel>& Ribbon::get_panel_under_button(const Data::Button& button) {
auto button_index = Data::button_to_index(button); auto button_index = Data::button_to_index(button);
return ( return (
m_layout m_layout
@ -117,8 +117,8 @@ namespace MusicSelect {
if (std::any_of( if (std::any_of(
column.begin(), column.begin(),
column.end(), column.end(),
[](const std::unique_ptr<Panel> panel) -> bool { [](const std::shared_ptr<Panel>& panel) -> bool {
return std::dynamic_pointer_cast<CategoryPanel>(panel) != nullptr; return std::dynamic_cast<CategoryPanel*>(panel.get()) != nullptr;
} }
)) { )) {
found = true; found = true;

View File

@ -41,7 +41,7 @@ namespace MusicSelect {
class Ribbon : public sf::Drawable, public sf::Transformable, public HoldsSharedResources, public Toolkit::Debuggable { class Ribbon : public sf::Drawable, public sf::Transformable, public HoldsSharedResources, public Toolkit::Debuggable {
public: public:
Ribbon(PanelLayout layout, SharedResources& t_resources); Ribbon(PanelLayout layout, SharedResources& t_resources);
std::unique_ptr<Panel>& get_panel_under_button(const Data::Button& button); std::shared_ptr<Panel>& get_panel_under_button(const Data::Button& button);
void click_on(const Data::Button& button); void click_on(const Data::Button& button);
void move_right(); void move_right();
void move_left(); void move_left();