1
0
mirror of synced 2025-02-02 12:27:20 +01:00

Refactoring

This commit is contained in:
Stepland 2020-03-07 22:44:09 +01:00
parent 3f9c482d25
commit de51fb4fa5
22 changed files with 199 additions and 215 deletions

View File

@ -38,6 +38,8 @@ sources = [
'src/Data/Song.cpp',
'src/Drawables/BlackFrame.hpp',
'src/Drawables/BlackFrame.cpp',
'src/Drawables/ButtonHighlight.hpp',
'src/Drawables/ButtonHighlight.cpp',
'src/Resources/TextureCache.cpp',
'src/Resources/TextureCache.hpp',
'src/Resources/Marker.cpp',
@ -45,8 +47,6 @@ sources = [
# 'src/Resources/CoverAtlas.hpp',
# 'src/Resources/CoverAtlas.cpp',
# 'src/Screens/Gameplay.hpp',
'src/Screens/MusicSelect/ButtonHighlight.hpp',
'src/Screens/MusicSelect/ButtonHighlight.cpp',
'src/Screens/MusicSelect/DensityGraph.hpp',
'src/Screens/MusicSelect/DensityGraph.cpp',
'src/Screens/MusicSelect/MusicPreview.hpp',

View File

@ -131,19 +131,19 @@ namespace Data {
};
struct HoldsPreferences {
HoldsPreferences(Preferences& t_preferences) : m_preferences(t_preferences) {};
float get_screen_width() const {return static_cast<float>(m_preferences.screen.width);};
float get_screen_height() const {return static_cast<float>(m_preferences.screen.height);};
float get_panel_size() const {return m_preferences.layout.panel_size*get_screen_width();};
float get_panel_spacing() const {return m_preferences.layout.panel_spacing*get_screen_width();};
float get_ribbon_x() const {return m_preferences.layout.ribbon_x*get_screen_width();};
float get_ribbon_y() const {return m_preferences.layout.ribbon_y*get_screen_width();};
float get_panel_step() const {return m_preferences.layout.panel_step()*get_screen_width();};
float get_big_cover_x() const {return m_preferences.layout.big_cover_x*get_screen_width();};
float get_big_cover_y() const {return m_preferences.layout.big_cover_y*get_screen_width();};
float get_big_level_x() const {return m_preferences.layout.big_level_x*get_screen_width();};
float get_big_level_y() const {return m_preferences.layout.big_level_y*get_screen_width();};
float get_upper_part_height() const {return m_preferences.layout.upper_part_height*get_screen_width();};
Preferences& m_preferences;
HoldsPreferences(Preferences& t_preferences) : preferences(t_preferences) {};
float get_screen_width() const {return static_cast<float>(preferences.screen.width);};
float get_screen_height() const {return static_cast<float>(preferences.screen.height);};
float get_panel_size() const {return preferences.layout.panel_size*get_screen_width();};
float get_panel_spacing() const {return preferences.layout.panel_spacing*get_screen_width();};
float get_ribbon_x() const {return preferences.layout.ribbon_x*get_screen_width();};
float get_ribbon_y() const {return preferences.layout.ribbon_y*get_screen_width();};
float get_panel_step() const {return preferences.layout.panel_step()*get_screen_width();};
float get_big_cover_x() const {return preferences.layout.big_cover_x*get_screen_width();};
float get_big_cover_y() const {return preferences.layout.big_cover_y*get_screen_width();};
float get_big_level_x() const {return preferences.layout.big_level_x*get_screen_width();};
float get_big_level_y() const {return preferences.layout.big_level_y*get_screen_width();};
float get_upper_part_height() const {return preferences.layout.upper_part_height*get_screen_width();};
Preferences& preferences;
};
}

View File

@ -1,8 +1,8 @@
#include "ButtonHighlight.hpp"
namespace MusicSelect {
ButtonHighlight::ButtonHighlight(SharedResources& resources) :
HoldsSharedResources(resources),
namespace Drawables {
ButtonHighlight::ButtonHighlight(Data::Preferences& t_preferences) :
Data::HoldsPreferences(t_preferences),
m_highlight(),
m_time_to_alpha(0.f, 0.25f, 255.f, 0.f)
{

View File

@ -5,15 +5,14 @@
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include "../../Data/Buttons.hpp"
#include "../../Toolkit/AffineTransform.hpp"
#include "../Data/Buttons.hpp"
#include "../Data/Preferences.hpp"
#include "../Toolkit/AffineTransform.hpp"
#include "SharedResources.hpp"
namespace MusicSelect {
class ButtonHighlight : public sf::Drawable, public sf::Transformable, public HoldsSharedResources {
namespace Drawables {
class ButtonHighlight : public sf::Drawable, public sf::Transformable, public Data::HoldsPreferences {
public:
ButtonHighlight(SharedResources& resources);
ButtonHighlight(Data::Preferences& t_preferences);
void button_pressed(Data::Button button);
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

View File

@ -19,11 +19,6 @@ int main(int, char const **) {
const std::string jujube_path = whereami::executable_dir();
Data::Preferences preferences{jujube_path};
auto markers = Resources::load_markers(jujube_path);
if (markers.find(preferences.options.marker) == markers.end()) {
preferences.options.marker = markers.begin()->first;
}
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow window{
@ -36,12 +31,11 @@ int main(int, char const **) {
settings
};
Data::SongList song_list{jujube_path};
MusicSelect::Screen music_select{
song_list,
preferences,
markers,
};
MusicSelect::SharedResources music_select_resources{preferences};
if (music_select_resources.markers.find(preferences.options.marker) == music_select_resources.markers.end()) {
preferences.options.marker = music_select_resources.markers.begin()->first;
}
MusicSelect::Screen music_select{song_list, music_select_resources};
music_select.select_chart(window);
/*
while (true) {

View File

@ -13,20 +13,13 @@
#include "Panels/Panel.hpp"
#include "PanelLayout.hpp"
MusicSelect::Screen::Screen(
const Data::SongList& t_song_list,
Data::Preferences& t_preferences,
const Resources::Markers& t_markers
) :
MusicSelect::Screen::Screen(const Data::SongList& t_song_list, SharedResources& t_resources) :
HoldsSharedResources(t_resources),
song_list(t_song_list),
resources(t_preferences, t_markers),
markers(t_markers),
ribbon(PanelLayout::title_sort(t_song_list, resources), resources),
song_info(resources),
button_highlight(resources),
main_option_page(resources),
black_frame(t_preferences),
key_mapping()
ribbon(PanelLayout::title_sort(t_song_list, t_resources), t_resources),
song_info(t_resources),
main_option_page(t_resources),
black_frame(t_resources.preferences)
{
panel_filter.setFillColor(sf::Color(0,0,0,128));
std::cout << "loaded MusicSelect::Screen" << std::endl;
@ -37,14 +30,8 @@ void MusicSelect::Screen::select_chart(sf::RenderWindow& window) {
ImGui::SFML::Init(window);
bool chart_selected = false;
sf::Clock imguiClock;
ribbon.setPosition(
resources.m_preferences.layout.ribbon_x*resources.m_preferences.screen.width,
resources.m_preferences.layout.ribbon_y*resources.m_preferences.screen.width
);
button_highlight.setPosition(
resources.m_preferences.layout.ribbon_x*resources.m_preferences.screen.width,
resources.m_preferences.layout.ribbon_y*resources.m_preferences.screen.width
);
ribbon.setPosition(get_ribbon_x(), get_ribbon_y());
resources.button_highlight.setPosition(get_ribbon_x(), get_ribbon_y());
panel_filter.setSize(sf::Vector2f{window.getSize()});
while ((not chart_selected) and window.isOpen()) {
sf::Event event;
@ -63,19 +50,13 @@ void MusicSelect::Screen::select_chart(sf::RenderWindow& window) {
case sf::Event::Resized:
// update the view to the new size of the window
window.setView(sf::View({0, 0, static_cast<float>(event.size.width), static_cast<float>(event.size.height)}));
resources.m_preferences.screen.height = event.size.height;
resources.m_preferences.screen.width = event.size.width;
ribbon.setPosition(
resources.m_preferences.layout.ribbon_x*resources.m_preferences.screen.width,
resources.m_preferences.layout.ribbon_y*resources.m_preferences.screen.width
);
button_highlight.setPosition(
resources.m_preferences.layout.ribbon_x*resources.m_preferences.screen.width,
resources.m_preferences.layout.ribbon_y*resources.m_preferences.screen.width
);
resources.preferences.screen.height = event.size.height;
resources.preferences.screen.width = event.size.width;
ribbon.setPosition(get_ribbon_x(), get_ribbon_y());
resources.button_highlight.setPosition(get_ribbon_x(), get_ribbon_y());
panel_filter.setSize(sf::Vector2f{window.getSize()});
if (not resources.options_state.empty()) {
resources.options_state.top().get().update();
resources.options_state.back().get().update();
}
break;
default:
@ -88,9 +69,9 @@ void MusicSelect::Screen::select_chart(sf::RenderWindow& window) {
window.draw(song_info);
if (not resources.options_state.empty()) {
window.draw(panel_filter);
window.draw(resources.options_state.top());
window.draw(resources.options_state.back());
}
window.draw(button_highlight);
window.draw(resources.button_highlight);
window.draw(black_frame);
ribbon.draw_debug();
draw_debug();
@ -107,11 +88,11 @@ void MusicSelect::Screen::draw_debug() {
if (ImGui::CollapsingHeader("Preferences")) {
if (ImGui::TreeNode("screen")) {
ImGui::TextUnformatted("width : "); ImGui::SameLine();
ImGui::Text("%s", std::to_string(resources.m_preferences.screen.width).c_str());
ImGui::Text("%s", std::to_string(resources.preferences.screen.width).c_str());
ImGui::TextUnformatted("height : "); ImGui::SameLine();
ImGui::Text("%s", std::to_string(resources.m_preferences.screen.height).c_str());
ImGui::Text("%s", std::to_string(resources.preferences.screen.height).c_str());
ImGui::TextUnformatted("fullscreen : "); ImGui::SameLine();
ImGui::Text("%s", resources.m_preferences.screen.fullscreen ? "true" : "false");
ImGui::Text("%s", resources.preferences.screen.fullscreen ? "true" : "false");
ImGui::TreePop();
}
if (ImGui::TreeNode("layout")) {
@ -120,10 +101,19 @@ void MusicSelect::Screen::draw_debug() {
}
if (ImGui::TreeNode("options")) {
ImGui::TextUnformatted("marker : "); ImGui::SameLine();
ImGui::Text("%s", resources.m_preferences.options.marker.c_str());
ImGui::Text("%s", resources.preferences.options.marker.c_str());
ImGui::TreePop();
}
}
if (ImGui::CollapsingHeader("Options Menu Stack")) {
if (resources.options_state.empty()) {
ImGui::TextUnformatted("- empty -");
} else {
for (auto &&i : resources.options_state) {
ImGui::Text("%s", typeid(i.get()).name());
}
}
}
}
ImGui::End();
}
@ -135,13 +125,13 @@ void MusicSelect::Screen::handle_key_press(const sf::Event::KeyEvent& key_event)
if (not resources.options_state.empty()) {
// Safety measure, pressing escape will alway pop the menu page
if (key_event.code == sf::Keyboard::Escape) {
resources.options_state.pop();
resources.options_state.pop_back();
if (not resources.options_state.empty()) {
resources.options_state.top().get().update();
resources.options_state.back().get().update();
}
output_used = true;
} else {
output_used = resources.options_state.top().get().handle_raw_input(key_event);
output_used = resources.options_state.back().get().handle_raw_input(key_event);
}
}
if (output_used) {
@ -169,12 +159,10 @@ void MusicSelect::Screen::handle_mouse_click(const sf::Event::MouseButtonEvent&
sf::Vector2i mouse_position{mouse_button_event.x, mouse_button_event.y};
sf::Vector2i ribbon_origin{
static_cast<int>(resources.get_ribbon_x()),
static_cast<int>(resources.get_ribbon_y())
static_cast<int>(get_ribbon_x()),
static_cast<int>(get_ribbon_y())
};
auto panels_area_size = static_cast<int>(
resources.get_panel_size() * 4.f + resources.get_panel_spacing() * 3.f
);
auto panels_area_size = static_cast<int>(get_panel_size() * 4.f + get_panel_spacing() * 3.f);
sf::IntRect panels_area{ribbon_origin, sf::Vector2i{panels_area_size, panels_area_size}};
if (not panels_area.contains(mouse_position)) {
return;
@ -195,7 +183,7 @@ void MusicSelect::Screen::handle_mouse_click(const sf::Event::MouseButtonEvent&
}
void MusicSelect::Screen::press_button(const Data::Button& button) {
button_highlight.button_pressed(button);
resources.button_highlight.button_pressed(button);
auto button_index = Data::button_to_index(button);
if (button_index < 12) {
ribbon.click_on(button);
@ -208,8 +196,15 @@ void MusicSelect::Screen::press_button(const Data::Button& button) {
ribbon.move_right();
break;
case Data::Button::B15: // Options Menu
resources.options_state.push(main_option_page);
resources.options_state.top().get().update();
if (resources.options_state.empty()) {
resources.options_state.push_back(main_option_page);
resources.options_state.back().get().update();
} else {
resources.options_state.pop_back();
if (not resources.options_state.empty()) {
resources.options_state.back().get().update();
}
}
break;
default:
break;

View File

@ -16,7 +16,6 @@
#include "Ribbon.hpp"
#include "SongInfo.hpp"
#include "SharedResources.hpp"
#include "ButtonHighlight.hpp"
#include "OptionPage.hpp"
namespace MusicSelect {
@ -24,12 +23,11 @@ namespace MusicSelect {
class SongPanel;
// The music select screen is created only once
// it loads a cache of available songs in the song_list attribute
class Screen : public Toolkit::Debuggable {
class Screen : public Toolkit::Debuggable, public HoldsSharedResources {
public:
Screen(
const Data::SongList& t_song_list,
Data::Preferences& t_preferences,
const Resources::Markers& t_markers
SharedResources& t_resources
);
void select_chart(sf::RenderWindow& window);
void draw_debug() override;
@ -37,14 +35,9 @@ namespace MusicSelect {
// Data
const Data::SongList song_list;
// Resources
SharedResources resources;
Resources::Markers markers;
// State
Ribbon ribbon;
SongInfo song_info;
ButtonHighlight button_highlight;
MainOptionPage main_option_page;
Drawables::BlackFrame black_frame;

View File

@ -14,19 +14,19 @@ namespace MusicSelect {
this->setPosition(get_ribbon_x(), get_ribbon_y());
}
RibbonPage::RibbonPage(const PanelLayout& layout, SharedResources& resources) :
OptionPage(resources),
m_ribbon(layout, resources)
RibbonPage::RibbonPage(const PanelLayout& layout, SharedResources& t_resources) :
OptionPage(t_resources),
m_ribbon(layout, t_resources)
{
}
bool MusicSelect::RibbonPage::handle_raw_input(const sf::Event::KeyEvent& key_event) {
auto button = m_preferences.key_mapping.key_to_button(key_event.code);
auto button = preferences.key_mapping.key_to_button(key_event.code);
if (not button) {
return false;
}
auto button_index = Data::button_to_index(*button);
if (button_index > 12) {
if (button_index > 13) {
return false;
}
button_click(*button);
@ -56,33 +56,33 @@ namespace MusicSelect {
target.draw(m_ribbon, states);
}
MainOptionPage::MainOptionPage(SharedResources& resources) :
RibbonPage(MainOptionPage::create_layout(resources), resources)
MainOptionPage::MainOptionPage(SharedResources& t_resources) :
RibbonPage(MainOptionPage::create_layout(t_resources), t_resources)
{
}
PanelLayout MainOptionPage::create_layout(SharedResources& resources) {
PanelLayout MainOptionPage::create_layout(SharedResources& t_resources) {
std::vector<std::shared_ptr<Panel>> subpages;
auto marker_select = std::make_shared<MarkerSelect>(resources);
subpages.emplace_back(std::make_shared<SubpagePanel>(resources, std::move(marker_select), "markers"));
return PanelLayout{subpages, resources};
auto marker_select = std::make_shared<MarkerSelect>(t_resources);
subpages.emplace_back(std::make_shared<SubpagePanel>(t_resources, std::move(marker_select), "markers"));
return PanelLayout{subpages, t_resources};
}
MarkerSelect::MarkerSelect(SharedResources& resources) :
RibbonPage(MarkerSelect::create_layout(resources), resources)
MarkerSelect::MarkerSelect(SharedResources& t_resources) :
RibbonPage(MarkerSelect::create_layout(t_resources), t_resources)
{
}
MarkerSelect::~MarkerSelect() {
m_resources.selected_marker.reset();
resources.selected_marker.reset();
}
PanelLayout MarkerSelect::create_layout(SharedResources& resources) {
PanelLayout MarkerSelect::create_layout(SharedResources& t_resources) {
std::vector<std::shared_ptr<Panel>> markers;
for (const auto &[name, marker] : resources.markers) {
markers.emplace_back(std::make_shared<MarkerPanel>(resources, marker));
for (const auto &[name, marker] : t_resources.markers) {
markers.emplace_back(std::make_shared<MarkerPanel>(t_resources, marker));
}
return PanelLayout{markers, resources};
return PanelLayout{markers, t_resources};
}
}

View File

@ -15,7 +15,7 @@ namespace MusicSelect {
class OptionPage : public sf::Drawable, public sf::Transformable, public HoldsSharedResources {
public:
OptionPage(SharedResources& resources) : HoldsSharedResources(resources) {update();};
OptionPage(SharedResources& t_resources) : HoldsSharedResources(t_resources) {update();};
// Returns true if input was used
virtual bool handle_raw_input(const sf::Event::KeyEvent& event) = 0;
virtual ~OptionPage() = default;
@ -24,7 +24,7 @@ namespace MusicSelect {
class RibbonPage : public OptionPage {
public:
RibbonPage(const PanelLayout& layout, SharedResources& resources);
RibbonPage(const PanelLayout& layout, SharedResources& t_resources);
bool handle_raw_input(const sf::Event::KeyEvent& event) override;
void button_click(const Data::Button& button);
private:
@ -34,16 +34,16 @@ namespace MusicSelect {
class MainOptionPage final : public RibbonPage {
public:
MainOptionPage(SharedResources& resources);
MainOptionPage(SharedResources& t_resources);
private:
static PanelLayout create_layout(SharedResources& resources);
static PanelLayout create_layout(SharedResources& t_resources);
};
class MarkerSelect final : public RibbonPage {
public:
MarkerSelect(SharedResources& resources);
MarkerSelect(SharedResources& t_resources);
~MarkerSelect();
private:
static PanelLayout create_layout(SharedResources& resources);
static PanelLayout create_layout(SharedResources& t_resources);
};
}

View File

@ -5,12 +5,12 @@
namespace MusicSelect {
PanelLayout::PanelLayout(
const std::map<std::string,std::vector<std::shared_ptr<Panel>>>& categories,
SharedResources& resources
SharedResources& t_resources
) {
for (auto &&[category, panels] : categories) {
if (not panels.empty()) {
std::vector<std::shared_ptr<Panel>> current_column;
current_column.emplace_back(std::make_shared<CategoryPanel>(resources, category));
current_column.emplace_back(std::make_shared<CategoryPanel>(t_resources, category));
for (auto& panel : panels) {
if (current_column.size() == 3) {
push_back({current_column[0],current_column[1],current_column[2]});
@ -20,18 +20,18 @@ namespace MusicSelect {
}
if (not current_column.empty()) {
while (current_column.size() < 3) {
current_column.emplace_back(std::make_shared<EmptyPanel>(resources));
current_column.emplace_back(std::make_shared<EmptyPanel>(t_resources));
}
push_back({current_column[0],current_column[1],current_column[2]});
}
}
}
fill_layout(resources);
fill_layout(t_resources);
}
PanelLayout::PanelLayout(
const std::vector<std::shared_ptr<Panel>>& panels,
SharedResources& resources
SharedResources& t_resources
) {
std::vector<std::shared_ptr<Panel>> current_column;
for (auto& panel : panels) {
@ -43,22 +43,22 @@ namespace MusicSelect {
}
if (not current_column.empty()) {
while (current_column.size() < 3) {
current_column.emplace_back(std::make_shared<EmptyPanel>(resources));
current_column.emplace_back(std::make_shared<EmptyPanel>(t_resources));
}
push_back({current_column[0],current_column[1],current_column[2]});
}
fill_layout(resources);
fill_layout(t_resources);
}
PanelLayout PanelLayout::red_empty_layout(SharedResources& resources) {
PanelLayout PanelLayout::red_empty_layout(SharedResources& t_resources) {
std::vector<std::shared_ptr<Panel>> panels;
for (size_t i = 0; i < 3*4; i++) {
panels.emplace_back(std::make_shared<ColoredMessagePanel>(resources, sf::Color::Red, "- EMPTY -"));
panels.emplace_back(std::make_shared<ColoredMessagePanel>(t_resources, sf::Color::Red, "- EMPTY -"));
}
return PanelLayout{panels, resources};
return PanelLayout{panels, t_resources};
}
PanelLayout PanelLayout::title_sort(const Data::SongList& song_list, SharedResources& resources) {
PanelLayout PanelLayout::title_sort(const Data::SongList& song_list, SharedResources& t_resources) {
std::vector<std::shared_ptr<const Data::Song>> songs;
for (auto &&song : song_list.songs) {
songs.push_back(song);
@ -75,27 +75,27 @@ namespace MusicSelect {
if ('A' <= letter and letter <= 'Z') {
categories
[std::string(1, letter)]
.emplace_back(std::make_shared<SongPanel>(resources, song));
.emplace_back(std::make_shared<SongPanel>(t_resources, song));
} else if ('a' <= letter and letter <= 'z') {
categories
[std::string(1, 'A' + (letter - 'a'))]
.emplace_back(std::make_shared<SongPanel>(resources, song));
.emplace_back(std::make_shared<SongPanel>(t_resources, song));
} else {
categories["?"].emplace_back(std::make_shared<SongPanel>(resources, song));
categories["?"].emplace_back(std::make_shared<SongPanel>(t_resources, song));
}
} else {
categories["?"].emplace_back(std::make_shared<SongPanel>(resources, song));
categories["?"].emplace_back(std::make_shared<SongPanel>(t_resources, song));
}
}
return PanelLayout{categories, resources};
return PanelLayout{categories, t_resources};
}
void PanelLayout::fill_layout(SharedResources& resources) {
void PanelLayout::fill_layout(SharedResources& t_resources) {
while (size() < 4) {
push_back({
std::make_shared<EmptyPanel>(resources),
std::make_shared<EmptyPanel>(resources),
std::make_shared<EmptyPanel>(resources)
std::make_shared<EmptyPanel>(t_resources),
std::make_shared<EmptyPanel>(t_resources),
std::make_shared<EmptyPanel>(t_resources)
});
}
}

View File

@ -14,14 +14,14 @@ namespace MusicSelect {
class PanelLayout : public std::vector<std::array<std::shared_ptr<Panel>,3>> {
public:
// Takes of map of category name and associated Panels, useful for all the sorted layouts
explicit PanelLayout(const std::map<std::string,std::vector<std::shared_ptr<Panel>>>& categories, SharedResources& resources);
explicit PanelLayout(const std::map<std::string,std::vector<std::shared_ptr<Panel>>>& categories, SharedResources& t_resources);
// Arranges all the panels in the vector in columns of three
explicit PanelLayout(const std::vector<std::shared_ptr<Panel>>& panels, SharedResources& resources);
explicit PanelLayout(const std::vector<std::shared_ptr<Panel>>& panels, SharedResources& t_resources);
// Stepmania-like empty layout with big red panels that say EMPTY
static PanelLayout red_empty_layout(SharedResources& resources);
static PanelLayout red_empty_layout(SharedResources& t_resources);
// Standard title sort with categories for each letter
static PanelLayout title_sort(const Data::SongList& song_list, SharedResources& resources);
static PanelLayout title_sort(const Data::SongList& song_list, SharedResources& t_resources);
private:
void fill_layout(SharedResources& resources);
void fill_layout(SharedResources& t_resources);
};
}

View File

@ -3,19 +3,19 @@
#include <cmath>
namespace MusicSelect {
MarkerPanel::MarkerPanel(SharedResources& resources, const Resources::Marker& marker) :
Panel(resources),
MarkerPanel::MarkerPanel(SharedResources& t_resources, const Resources::Marker& marker) :
Panel(t_resources),
m_marker(marker)
{
if (m_resources.m_preferences.options.marker == m_marker.m_metadata.name) {
if (resources.preferences.options.marker == m_marker.m_metadata.name) {
select();
}
}
void MarkerPanel::click(Ribbon&, const Data::Button&) {
if (selected) {
m_resources.selected_marker->last_click.restart();
m_resources.selected_marker->is_first_click = false;
resources.selected_marker->last_click.restart();
resources.selected_marker->is_first_click = false;
} else {
select();
}
@ -25,7 +25,7 @@ namespace MusicSelect {
states.transform *= getTransform();
float animation_time = 0.f;
if (selected) {
animation_time = std::fmod(m_resources.selected_marker->last_click.getElapsedTime().asSeconds(), 2.f) - 1.f;
animation_time = std::fmod(resources.selected_marker->last_click.getElapsedTime().asSeconds(), 2.f) - 1.f;
}
auto sprite = m_marker.get_sprite(Resources::MarkerAnimation::APPROACH, animation_time);
if (sprite) {
@ -36,11 +36,11 @@ namespace MusicSelect {
}
void MarkerPanel::select() {
if (m_resources.selected_marker) {
m_resources.selected_marker->obj.unselect();
if (resources.selected_marker) {
resources.selected_marker->obj.unselect();
}
m_resources.selected_marker.emplace(*this);
m_resources.m_preferences.options.marker = m_marker.m_metadata.name;
resources.selected_marker.emplace(*this);
resources.preferences.options.marker = m_marker.m_metadata.name;
this->selected = true;
}

View File

@ -10,7 +10,7 @@
namespace MusicSelect {
class MarkerPanel final : public Panel {
public:
MarkerPanel(SharedResources& resources, const Resources::Marker& marker);
MarkerPanel(SharedResources& t_resources, const Resources::Marker& marker);
void click(Ribbon&, const Data::Button&) override;
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;

View File

@ -11,7 +11,7 @@
#include "../SharedResources.hpp"
namespace MusicSelect {
Panel::Panel(SharedResources& resources) : HoldsSharedResources(resources) {
Panel::Panel(SharedResources& t_resources) : HoldsSharedResources(t_resources) {
}
@ -43,7 +43,7 @@ namespace MusicSelect {
sf::Text category_label{
"category",
m_resources.fallback_font.medium,
resources.fallback_font.medium,
static_cast<unsigned int>(get_size()*0.1f)
};
category_label.setFillColor(sf::Color::White);
@ -52,7 +52,7 @@ namespace MusicSelect {
sf::Text label_text{
m_label,
m_resources.fallback_font.black,
resources.fallback_font.black,
static_cast<unsigned int>(get_size()*0.7f)
};
label_text.setFillColor(sf::Color::White);
@ -74,23 +74,23 @@ namespace MusicSelect {
} else {
selected_chart = m_song->chart_levels.cbegin()->first;
}
m_resources.selected_panel->last_click.restart();
m_resources.selected_panel->is_first_click = false;
resources.selected_panel->last_click.restart();
resources.selected_panel->is_first_click = false;
} else {
// Look for the first chart with dif greater or equal to the last select one
// or else select the first chart
auto it = m_song->chart_levels.lower_bound(m_resources.get_last_selected_difficulty());
auto it = m_song->chart_levels.lower_bound(resources.get_last_selected_difficulty());
if (it != m_song->chart_levels.cend()) {
selected_chart = it->first;
} else {
selected_chart = m_song->chart_levels.cbegin()->first;
}
// The song was not selected before : first unselect the last one
if (m_resources.selected_panel.has_value()) {
m_resources.selected_panel->obj.unselect();
if (resources.selected_panel.has_value()) {
resources.selected_panel->obj.unselect();
}
m_resources.selected_panel.emplace(*this);
m_resources.music_preview.play(m_song->full_audio_path(), m_song->preview);
resources.selected_panel.emplace(*this);
resources.music_preview.play(m_song->full_audio_path(), m_song->preview);
}
}
@ -108,11 +108,11 @@ namespace MusicSelect {
void SongPanel::draw(sf::RenderTarget& target, sf::RenderStates states) const {
states.transform *= getTransform();
auto last_selected_chart = m_resources.get_last_selected_difficulty();
auto last_selected_chart = resources.get_last_selected_difficulty();
// We should gray out the panel if the currently selected difficulty doesn't exist for this song
bool should_be_grayed_out = m_song->chart_levels.find(last_selected_chart) == m_song->chart_levels.end();
if (m_song->cover) {
auto loaded_texture = m_resources.covers.async_get(m_song->folder/m_song->cover.value());
auto loaded_texture = resources.covers.async_get(m_song->folder/m_song->cover.value());
if (loaded_texture) {
sf::Sprite cover{*(loaded_texture->texture)};
auto alpha = static_cast<std::uint8_t>(
@ -135,7 +135,7 @@ namespace MusicSelect {
chart_dif_badge.setFillColor(sf::Color(128,128,128));
} else {
chart_dif_badge.setFillColor(
m_resources.get_chart_color(last_selected_chart)
resources.get_chart_color(last_selected_chart)
);
}
target.draw(chart_dif_badge, states);
@ -143,7 +143,7 @@ namespace MusicSelect {
auto dif = m_song->chart_levels.at(last_selected_chart);
sf::Text dif_label{
std::to_string(dif),
m_resources.fallback_font.black,
resources.fallback_font.black,
static_cast<unsigned int>(get_size()*0.15f)
};
dif_label.setFillColor(sf::Color::White);
@ -152,7 +152,7 @@ namespace MusicSelect {
target.draw(dif_label, states);
}
sf::Text song_title;
song_title.setFont(m_resources.fallback_font.medium);
song_title.setFont(resources.fallback_font.medium);
song_title.setString(sf::String::fromUtf8(m_song->title.begin(), m_song->title.end()));
song_title.setCharacterSize(static_cast<unsigned int>(0.06875f*get_size()));
song_title.setFillColor(sf::Color::White);
@ -182,7 +182,7 @@ namespace MusicSelect {
target.draw(frame, states);
sf::Text message;
message.setFont(m_resources.fallback_font.medium);
message.setFont(resources.fallback_font.medium);
message.setString(sf::String::fromUtf8(m_message.begin(), m_message.end()));
message.setCharacterSize(static_cast<unsigned int>(0.1f*get_size()));
message.setFillColor(m_color);

View File

@ -19,7 +19,7 @@ namespace MusicSelect {
// of the music select screen, be it nothing, a category indicator, or a song
class Panel : public sf::Drawable, public sf::Transformable, public HoldsSharedResources {
public:
explicit Panel(SharedResources& resources);
explicit Panel(SharedResources& t_resources);
// What happens when you click on the panel
virtual void click(Ribbon& ribbon, const Data::Button& button) = 0;
virtual ~Panel() = default;
@ -37,7 +37,7 @@ namespace MusicSelect {
class ColoredMessagePanel final : public Panel {
public:
ColoredMessagePanel(SharedResources& resources, const sf::Color& color, const std::string& message) : Panel(resources), m_color(color), m_message(message) {};
ColoredMessagePanel(SharedResources& t_resources, const sf::Color& color, const std::string& message) : Panel(t_resources), m_color(color), m_message(message) {};
void click(Ribbon&, const Data::Button&) override {return;};
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
@ -47,7 +47,7 @@ namespace MusicSelect {
class ColorPanel final : public Panel {
public:
ColorPanel(SharedResources& resources, const sf::Color& t_color) : Panel(resources), m_color(t_color) {};
ColorPanel(SharedResources& t_resources, const sf::Color& t_color) : Panel(t_resources), m_color(t_color) {};
void click(Ribbon&, const Data::Button&) override {return;};
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
@ -56,7 +56,7 @@ namespace MusicSelect {
class CategoryPanel final : public Panel {
public:
CategoryPanel(SharedResources& resources, const std::string& t_label) : Panel(resources), m_label(t_label) {};
CategoryPanel(SharedResources& t_resources, const std::string& t_label) : Panel(t_resources), m_label(t_label) {};
void click(Ribbon& ribbon, const Data::Button& button) override;
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
@ -73,7 +73,7 @@ namespace MusicSelect {
class SongPanel final : public SelectablePanel {
public:
explicit SongPanel(SharedResources& resources, const std::shared_ptr<const Data::Song>& t_song) : SelectablePanel(resources), m_song(t_song) {};
explicit SongPanel(SharedResources& t_resources, const std::shared_ptr<const Data::Song>& t_song) : SelectablePanel(t_resources), m_song(t_song) {};
void click(Ribbon& ribbon, const Data::Button& button) override;
void unselect() override;
std::optional<SongDifficulty> get_selected_difficulty() const override;

View File

@ -4,8 +4,8 @@
namespace MusicSelect {
void SubpagePanel::click(Ribbon&, const Data::Button&) {
m_resources.options_state.push(*m_subpage);
m_resources.options_state.top().get().update();
resources.options_state.push_back(*m_subpage);
resources.options_state.back().get().update();
}
void SubpagePanel::draw(sf::RenderTarget& target, sf::RenderStates states) const {
@ -19,7 +19,7 @@ namespace MusicSelect {
target.draw(frame, states);
sf::Text message;
message.setFont(m_resources.fallback_font.medium);
message.setFont(resources.fallback_font.medium);
message.setString(sf::String::fromUtf8(m_name.begin(), m_name.end()));
message.setCharacterSize(static_cast<unsigned int>(0.1f*get_size()));
message.setFillColor(sf::Color::White);

View File

@ -14,11 +14,11 @@ namespace MusicSelect {
class SubpagePanel final : public Panel {
public:
SubpagePanel(
SharedResources& resources,
SharedResources& t_resources,
std::shared_ptr<OptionPage> subpage,
const std::string& name
) :
Panel(resources),
Panel(t_resources),
m_subpage(subpage),
m_name(name)
{};

View File

@ -65,8 +65,8 @@ namespace MusicSelect {
return clock.getElapsedTime() / m_time_factor > sf::milliseconds(300);
}
Ribbon::Ribbon(PanelLayout layout, SharedResources& resources) :
HoldsSharedResources(resources),
Ribbon::Ribbon(PanelLayout layout, SharedResources& t_resources) :
HoldsSharedResources(t_resources),
m_layout(layout)
{
std::cout << "Loaded MusicSelect::Ribbon" << std::endl;

View File

@ -26,10 +26,11 @@ namespace MusicSelect {
}
}
SharedResources::SharedResources(Data::Preferences& p, const Resources::Markers& m) :
SharedResources::SharedResources(Data::Preferences& p) :
Data::HoldsPreferences(p),
fallback_font(p.jujube_path),
markers(m)
markers(Resources::load_markers(p.jujube_path)),
button_highlight(p)
{
covers.reserve(256);
std::cout << "Loaded MusicSelect::SharedResources" << std::endl;

View File

@ -10,10 +10,11 @@
#include <SFML/Graphics/Font.hpp>
#include <SFML/System.hpp>
#include "../../Resources/Marker.hpp"
#include "../../Resources/TextureCache.hpp"
#include "../../Data/Preferences.hpp"
#include "../../Data/Song.hpp"
#include "../../Drawables/ButtonHighlight.hpp"
#include "../../Resources/Marker.hpp"
#include "../../Resources/TextureCache.hpp"
#include "DensityGraph.hpp"
#include "MusicPreview.hpp"
@ -42,7 +43,7 @@ namespace MusicSelect {
// Holds everything that needs to be shared by all levels of the class hierarchy
struct SharedResources : public Data::HoldsPreferences {
SharedResources(Data::Preferences& p, const Resources::Markers& m);
SharedResources(Data::Preferences& p);
Textures::TextureCache covers;
@ -62,17 +63,18 @@ namespace MusicSelect {
MusicPreview music_preview;
std::stack<std::reference_wrapper<OptionPage>> options_state;
const Resources::Markers& markers;
std::vector<std::reference_wrapper<OptionPage>> options_state;
Resources::Markers markers;
std::optional<Timed<MarkerPanel>> selected_marker;
Drawables::ButtonHighlight button_highlight;
};
// Proxy for HoldsPreferences
struct HoldsSharedResources : public Data::HoldsPreferences {
HoldsSharedResources(SharedResources& resources) : Data::HoldsPreferences(resources.m_preferences), m_resources(resources) {};
HoldsSharedResources(SharedResources& t_resources) : Data::HoldsPreferences(t_resources.preferences), resources(t_resources) {};
protected:
SharedResources& m_resources;
SharedResources& resources;
};
}

View File

@ -12,8 +12,8 @@
namespace MusicSelect {
BigCover::BigCover(SharedResources& resources) :
HoldsSharedResources(resources)
BigCover::BigCover(SharedResources& t_resources) :
HoldsSharedResources(t_resources)
{
m_cover_fallback.setFillColor(sf::Color::Transparent);
m_cover_fallback.setOutlineThickness(1.f);
@ -24,7 +24,7 @@ namespace MusicSelect {
states.transform *= getTransform();
m_cover_fallback.setSize({get_size(), get_size()});
target.draw(m_cover_fallback, states);
auto selected_panel = m_resources.selected_panel;
auto selected_panel = resources.selected_panel;
if (not selected_panel.has_value()) {
return;
}
@ -36,7 +36,7 @@ namespace MusicSelect {
if (not cover_path.has_value()) {
return;
}
auto cover_texture = m_resources.covers.async_get(*cover_path);
auto cover_texture = resources.covers.async_get(*cover_path);
if (not cover_texture.has_value()) {
return;
}
@ -52,8 +52,8 @@ namespace MusicSelect {
target.draw(cover, states);
}
SongInfo::SongInfo(SharedResources& resources) :
HoldsSharedResources(resources),
SongInfo::SongInfo(SharedResources& t_resources) :
HoldsSharedResources(t_resources),
m_big_cover(resources)
{}
@ -69,7 +69,7 @@ namespace MusicSelect {
}
void SongInfo::draw_song_title(sf::RenderTarget& target, sf::RenderStates states) const {
auto selected_panel = m_resources.selected_panel;
auto selected_panel = resources.selected_panel;
if (not selected_panel.has_value()) {
return;
}
@ -81,7 +81,7 @@ namespace MusicSelect {
if (not song_title.empty()) {
sf::Text song_title_label{
sf::String::fromUtf8(song_title.begin(), song_title.end()),
m_resources.fallback_font.medium,
resources.fallback_font.medium,
static_cast<unsigned int>(
0.026315789f*get_screen_width()
)
@ -101,7 +101,7 @@ namespace MusicSelect {
if (not song_artist.empty()) {
sf::Text song_artist_label{
sf::String::fromUtf8(song_artist.begin(), song_artist.end()),
m_resources.fallback_font.medium,
resources.fallback_font.medium,
static_cast<unsigned int>(
0.02f*get_screen_width()
)
@ -122,7 +122,7 @@ namespace MusicSelect {
}
void SongInfo::draw_big_level(sf::RenderTarget& target, sf::RenderStates states) const {
auto selected_panel = m_resources.selected_panel;
auto selected_panel = resources.selected_panel;
if (not selected_panel.has_value()) {
return;
}
@ -132,7 +132,7 @@ namespace MusicSelect {
}
sf::Text level_label{
"LEVEL",
m_resources.fallback_font.light,
resources.fallback_font.light,
static_cast<unsigned int>(12.f/768.f*get_screen_width())
};
Toolkit::set_origin_normalized(level_label, 0.5f, 0.f);
@ -142,7 +142,7 @@ namespace MusicSelect {
sf::Text level_number_label{
std::to_string(selected_chart->song.chart_levels.at(selected_chart->difficulty)),
m_resources.fallback_font.black,
resources.fallback_font.black,
static_cast<unsigned int>(130.f/768.f*get_screen_width())
};
Toolkit::set_origin_normalized(level_number_label, 0.5f, 0.f);
@ -161,17 +161,17 @@ namespace MusicSelect {
sf::Text chart_label{
sf::String::fromUtf8(full_difficulty.begin(), full_difficulty.end()),
m_resources.fallback_font.medium,
resources.fallback_font.medium,
static_cast<unsigned int>(20.f/768.f*get_screen_width())
};
Toolkit::set_origin_normalized_no_position(chart_label, 0.5f, 0.f);
chart_label.setPosition(get_big_level_x(), get_big_level_y()+(145.f/768.f*get_screen_width()));
chart_label.setFillColor(m_resources.get_chart_color(selected_chart->difficulty));
chart_label.setFillColor(resources.get_chart_color(selected_chart->difficulty));
target.draw(chart_label, states);
}
void MusicSelect::SongInfo::draw_chart_list(sf::RenderTarget& target, sf::RenderStates states) const {
auto selected_panel = m_resources.selected_panel;
auto selected_panel = resources.selected_panel;
if (not selected_panel.has_value()) {
return;
}
@ -187,7 +187,7 @@ namespace MusicSelect {
for (auto &&[difficulty, level] : selected_chart->song.chart_levels) {
sf::CircleShape dif_badge{dif_badge_radius};
Toolkit::set_origin_normalized(dif_badge, 0.5f, 0.5f);
dif_badge.setFillColor(m_resources.get_chart_color(difficulty));
dif_badge.setFillColor(resources.get_chart_color(difficulty));
dif_badge.setPosition(dif_badge_x+dif_index*dif_badge_step, dif_badge_y);
target.draw(dif_badge, states);
if (difficulty == selected_chart->difficulty) {
@ -221,7 +221,7 @@ namespace MusicSelect {
line.setFillColor(sf::Color::White);
line.setPosition(get_screen_width()*0.5f,425.f/768.f*get_screen_width());
target.draw(line, states);
auto selected_panel = m_resources.selected_panel;
auto selected_panel = resources.selected_panel;
if (not selected_panel.has_value()) {
return;
}
@ -229,8 +229,8 @@ namespace MusicSelect {
if (not selected_difficulty.has_value()) {
return;
}
m_resources.density_graphs.load(*selected_difficulty);
auto densities = m_resources.density_graphs.get(*selected_difficulty);
resources.density_graphs.load(*selected_difficulty);
auto densities = resources.density_graphs.get(*selected_difficulty);
if (not densities.has_value()) {
return;
}

View File

@ -11,8 +11,8 @@ namespace MusicSelect {
class BigCover : public sf::Drawable, public sf::Transformable, public HoldsSharedResources {
public:
BigCover(SharedResources& resources);
float get_size() const {return m_preferences.layout.big_cover_size*get_screen_width();};
BigCover(SharedResources& t_resources);
float get_size() const {return preferences.layout.big_cover_size*get_screen_width();};
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
mutable sf::RectangleShape m_cover_fallback;
@ -22,7 +22,7 @@ namespace MusicSelect {
// Displays the song info on the top part of the screen
class SongInfo : public sf::Drawable, public sf::Transformable, public HoldsSharedResources {
public:
SongInfo(SharedResources& resources);
SongInfo(SharedResources& t_resources);
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
void draw_song_title(sf::RenderTarget& target, sf::RenderStates states) const;