1
0
mirror of synced 2025-02-08 22:59:41 +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 <iostream>
#include <memory>
#include <vector>
#include "Ribbon.hpp"
@ -17,7 +18,6 @@ namespace MusicSelect {
OptionPage(resources),
m_ribbon(layout, resources)
{
update();
}
void RibbonPage::click(const Data::Button& button) {
@ -46,21 +46,19 @@ namespace MusicSelect {
MainOptionPage::MainOptionPage(SharedResources& resources) :
RibbonPage(MainOptionPage::create_layout(resources), resources)
{
update();
}
PanelLayout MainOptionPage::create_layout(SharedResources& resources) {
std::vector<std::unique_ptr<Panel>> subpages;
jbcoe::polymorphic_value<OptionPage> marker_select{MarkerSelect{resources}};
subpages.emplace_back(SubpagePanel{resources, marker_select, "markers"});
std::vector<std::shared_ptr<Panel>> subpages;
auto marker_select = std::make_shared<MarkerSelect>(MarkerSelect{resources});
subpages.emplace_back(std::make_shared<SubpagePanel>(resources, marker_select, "markers"));
return PanelLayout{subpages, resources};
}
MarkerSelect::MarkerSelect(SharedResources& resources) :
RibbonPage(MarkerSelect::create_layout(resources), resources)
{
update();
}
MarkerSelect::~MarkerSelect() {
@ -68,9 +66,9 @@ namespace MusicSelect {
}
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) {
markers.emplace_back(MarkerPanel{resources, marker});
markers.emplace_back(std::make_shared<MarkerPanel>(resources, marker));
}
return PanelLayout{markers, resources};
}

View File

@ -14,7 +14,7 @@ namespace MusicSelect {
class OptionPage : public sf::Drawable, public sf::Transformable, public HoldsSharedResources {
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
// 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

View File

@ -4,13 +4,13 @@
namespace MusicSelect {
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
) {
for (auto &&[category, panels] : categories) {
if (not panels.empty()) {
std::vector<std::unique_ptr<Panel>> current_column;
current_column.emplace_back(CategoryPanel{resources, category});
std::vector<std::shared_ptr<Panel>> current_column;
current_column.emplace_back(std::make_shared<CategoryPanel>(resources, category));
for (auto& panel : panels) {
if (current_column.size() == 3) {
push_back({
@ -20,11 +20,11 @@ namespace MusicSelect {
});
current_column.clear();
}
current_column.push_back(std::move(panel));
current_column.push_back(panel);
}
if (not current_column.empty()) {
while (current_column.size() < 3) {
current_column.emplace_back(EmptyPanel{resources});
current_column.emplace_back(std::make_shared<EmptyPanel>(resources));
}
push_back({
std::move(current_column[0]),
@ -38,10 +38,10 @@ namespace MusicSelect {
}
PanelLayout::PanelLayout(
const std::vector<std::unique_ptr<Panel>>& panels,
const std::vector<std::shared_ptr<Panel>>& panels,
SharedResources& resources
) {
std::vector<std::unique_ptr<Panel>> current_column;
std::vector<std::shared_ptr<Panel>> current_column;
for (auto& panel : panels) {
if (current_column.size() == 3) {
push_back({
@ -51,11 +51,11 @@ namespace MusicSelect {
});
current_column.clear();
}
current_column.push_back(std::move(panel));
current_column.push_back(panel);
}
if (not current_column.empty()) {
while (current_column.size() < 3) {
current_column.emplace_back(EmptyPanel{resources});
current_column.emplace_back(std::make_shared<EmptyPanel>(resources));
}
push_back({
std::move(current_column[0]),
@ -67,9 +67,9 @@ namespace MusicSelect {
}
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++) {
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};
}
@ -84,38 +84,34 @@ namespace MusicSelect {
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::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) {
if (song->title.size() > 0) {
char letter = song->title[0];
if ('A' <= letter and letter <= 'Z') {
categories
[std::string(1, letter)]
.emplace_back(SongPanel{resources, song});
.emplace_back(std::make_shared<SongPanel>(resources, song));
} else if ('a' <= letter and letter <= 'z') {
categories
[std::string(1, 'A' + (letter - 'a'))]
.emplace_back(SongPanel{resources, song});
.emplace_back(std::make_shared<SongPanel>(resources, song));
} else {
categories["?"].emplace_back(SongPanel{resources, song});
categories["?"].emplace_back(std::make_shared<SongPanel>(resources, song));
}
} else {
categories["?"].emplace_back(SongPanel{resources, song});
categories["?"].emplace_back(std::make_shared<SongPanel>(resources, song));
}
}
return PanelLayout{categories, resources};
}
void PanelLayout::PanelLayout::push_vector(const std::vector<std::unique_ptr<Panel>>& panels) {
}
void PanelLayout::fill_layout(SharedResources& resources) {
while (size() < 4) {
push_back({
std::unique_ptr<Panel>{EmptyPanel{resources}},
std::unique_ptr<Panel>{EmptyPanel{resources}},
std::unique_ptr<Panel>{EmptyPanel{resources}}
std::make_shared<EmptyPanel>(resources),
std::make_shared<EmptyPanel>(resources),
std::make_shared<EmptyPanel>(resources)
});
}
}

View File

@ -11,10 +11,10 @@ namespace MusicSelect {
class Panel;
// 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:
explicit PanelLayout(const std::map<std::string,std::vector<std::unique_ptr<Panel>>>& categories, SharedResources& resources);
explicit PanelLayout(const std::vector<std::unique_ptr<Panel>>& panels, SharedResources& resources);
explicit PanelLayout(const std::map<std::string,std::vector<std::shared_ptr<Panel>>>& categories, SharedResources& resources);
explicit PanelLayout(const std::vector<std::shared_ptr<Panel>>& panels, SharedResources& resources);
static PanelLayout red_empty_layout(SharedResources& resources);
static PanelLayout title_sort(const Data::SongList& song_list, SharedResources& resources);
private:

View File

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

View File

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

View File

@ -77,7 +77,7 @@ namespace MusicSelect {
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);
return (
m_layout
@ -117,8 +117,8 @@ namespace MusicSelect {
if (std::any_of(
column.begin(),
column.end(),
[](const std::unique_ptr<Panel> panel) -> bool {
return std::dynamic_pointer_cast<CategoryPanel>(panel) != nullptr;
[](const std::shared_ptr<Panel>& panel) -> bool {
return std::dynamic_cast<CategoryPanel*>(panel.get()) != nullptr;
}
)) {
found = true;

View File

@ -41,7 +41,7 @@ namespace MusicSelect {
class Ribbon : public sf::Drawable, public sf::Transformable, public HoldsSharedResources, public Toolkit::Debuggable {
public:
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 move_right();
void move_left();