1
0
mirror of synced 2025-02-09 07:09:42 +01:00

Refactor dependancy on resources as HoldSharedResources class

This commit is contained in:
Stepland 2020-02-16 02:10:50 +01:00
parent 019a776432
commit b994459f74
9 changed files with 42 additions and 46 deletions

View File

@ -30,7 +30,7 @@ namespace Data {
float panel_position_y = 602.f / 1360.f; float panel_position_y = 602.f / 1360.f;
float panel_size = 160.f / 768.f; float panel_size = 160.f / 768.f;
float panel_spacing = (112.f / 3.f) / 768.f; float panel_spacing = (112.f / 3.f) / 768.f;
float panel_step() {return panel_size+panel_spacing;}; float panel_step() const {return panel_size+panel_spacing;};
float ribbon_x = 8.f / 768.f; float ribbon_x = 8.f / 768.f;
float ribbon_y = 602.f / 768.f; float ribbon_y = 602.f / 768.f;
float big_cover_size = 320.f / 768.f; float big_cover_size = 320.f / 768.f;

View File

@ -11,9 +11,14 @@
#include "SharedResources.hpp" #include "SharedResources.hpp"
namespace MusicSelect { namespace MusicSelect {
float Panel::get_size() const { Panel::Panel(SharedResources& resources) : HoldsSharedResources(resources) {
return m_resources.preferences.layout.panel_size*m_resources.preferences.screen.width;
} }
float Panel::get_size() const {
return get_panel_size();
}
void ColorPanel::draw(sf::RenderTarget& target, sf::RenderStates states) const { void ColorPanel::draw(sf::RenderTarget& target, sf::RenderStates states) const {
sf::RectangleShape panel{{get_size()*0.9f, get_size()*0.9f}}; sf::RectangleShape panel{{get_size()*0.9f, get_size()*0.9f}};
panel.setFillColor(m_color); panel.setFillColor(m_color);

View File

@ -13,18 +13,16 @@
namespace MusicSelect { namespace MusicSelect {
class Ribbon; class Ribbon;
class SharedResources;
// A Panel holds anything that can go under a button on the moving part // A Panel holds anything that can go under a button on the moving part
// of the music select screen, be it nothing, a category indicator, or a song // of the music select screen, be it nothing, a category indicator, or a song
class Panel : public sf::Drawable, public sf::Transformable { class Panel : public sf::Drawable, public sf::Transformable, public HoldsSharedResources {
public: public:
Panel(SharedResources& resources) : m_resources(resources) {}; explicit Panel(SharedResources& resources);
// What happens when you click on the panel // What happens when you click on the panel
virtual void click(Ribbon& ribbon, std::size_t from_button_index) = 0; virtual void click(Ribbon& ribbon, std::size_t from_button_index) = 0;
virtual ~Panel() = default; virtual ~Panel() = default;
protected: protected:
SharedResources& m_resources;
float get_size() const; float get_size() const;
}; };

View File

@ -64,12 +64,7 @@ namespace MusicSelect {
return clock.getElapsedTime() / m_time_factor > sf::milliseconds(300); return clock.getElapsedTime() / m_time_factor > sf::milliseconds(300);
} }
Ribbon::Ribbon(SharedResources& t_resources) : Ribbon::Ribbon(SharedResources& t_resources) : HoldsSharedResources(t_resources) {
m_layout(),
m_move_animation(),
m_resources(t_resources),
empty_song()
{
std::cout << "Loaded MusicSelect::Ribbon" << std::endl; std::cout << "Loaded MusicSelect::Ribbon" << std::endl;
} }
@ -273,19 +268,13 @@ namespace MusicSelect {
} }
ImGui::End(); ImGui::End();
} }
auto panel_step = (
(
m_resources.preferences.layout.panel_size +
m_resources.preferences.layout.panel_spacing
) * m_resources.preferences.screen.width
);
for (int column_offset = -1; column_offset <= 4; column_offset++) { for (int column_offset = -1; column_offset <= 4; column_offset++) {
std::size_t actual_column = (column_zero + column_offset + m_layout.size()) % m_layout.size(); std::size_t actual_column = (column_zero + column_offset + m_layout.size()) % m_layout.size();
for (int row = 0; row < 3; row++) { for (int row = 0; row < 3; row++) {
auto panel = m_layout.at(actual_column).at(row); auto panel = m_layout.at(actual_column).at(row);
panel->setPosition( panel->setPosition(
(static_cast<float>(relative_column_zero + column_offset) - float_position) * (panel_step), (static_cast<float>(relative_column_zero + column_offset) - float_position) * (get_panel_step()),
row * (panel_step) row * (get_panel_step())
); );
target.draw(*panel, states); target.draw(*panel, states);
} }
@ -293,17 +282,11 @@ namespace MusicSelect {
} }
void Ribbon::draw_without_animation(sf::RenderTarget &target, sf::RenderStates states) const { void Ribbon::draw_without_animation(sf::RenderTarget &target, sf::RenderStates states) const {
auto panel_step = (
(
m_resources.preferences.layout.panel_size +
m_resources.preferences.layout.panel_spacing
) * m_resources.preferences.screen.width
);
for (int column = -1; column <= 4; column++) { for (int column = -1; column <= 4; column++) {
int actual_column_index = (column + m_position + m_layout.size()) % m_layout.size(); int actual_column_index = (column + m_position + m_layout.size()) % m_layout.size();
for (int row = 0; row < 3; row++) { for (int row = 0; row < 3; row++) {
auto panel = m_layout.at(actual_column_index).at(row); auto panel = m_layout.at(actual_column_index).at(row);
panel->setPosition(column * (panel_step), row * (panel_step)); panel->setPosition(column * (get_panel_step()), row * (get_panel_step()));
target.draw(*panel, states); target.draw(*panel, states);
} }
} }

View File

@ -35,7 +35,7 @@ namespace MusicSelect {
// The Ribbon is the moving part of the Music Select Screen // The Ribbon is the moving part of the Music Select Screen
// It can be sorted in a number of ways // It can be sorted in a number of ways
class Ribbon final : public sf::Drawable, public sf::Transformable, public Toolkit::Debuggable { class Ribbon final : public sf::Drawable, public sf::Transformable, public HoldsSharedResources, public Toolkit::Debuggable {
public: public:
Ribbon(SharedResources& t_resources); Ribbon(SharedResources& t_resources);
void title_sort(const Data::SongList& song_list); void title_sort(const Data::SongList& song_list);
@ -58,7 +58,6 @@ namespace MusicSelect {
std::vector<std::array<std::shared_ptr<Panel>,3>> m_layout; std::vector<std::array<std::shared_ptr<Panel>,3>> m_layout;
std::size_t m_position = 0; std::size_t m_position = 0;
mutable std::optional<MoveAnimation> m_move_animation; mutable std::optional<MoveAnimation> m_move_animation;
SharedResources& m_resources;
float m_time_factor = 1.f; float m_time_factor = 1.f;
Data::Song empty_song; Data::Song empty_song;
}; };

View File

@ -2,6 +2,8 @@
#include <iostream> #include <iostream>
#include "Panel.hpp"
namespace MusicSelect { namespace MusicSelect {
SharedResources::SharedResources(Data::Preferences& p) : SharedResources::SharedResources(Data::Preferences& p) :
preferences(p), preferences(p),

View File

@ -10,8 +10,6 @@
#include "../../Data/Preferences.hpp" #include "../../Data/Preferences.hpp"
#include "../../Data/SongList.hpp" #include "../../Data/SongList.hpp"
#include "Panel.hpp"
namespace MusicSelect { namespace MusicSelect {
class SelectablePanel; class SelectablePanel;
@ -41,4 +39,17 @@ namespace MusicSelect {
sf::Color ADV_color = sf::Color{252,212,32}; sf::Color ADV_color = sf::Color{252,212,32};
sf::Color EXT_color = sf::Color{234,46,32}; sf::Color EXT_color = sf::Color{234,46,32};
}; };
struct HoldsSharedResources {
HoldsSharedResources(SharedResources& resources) : m_resources(resources) {};
float get_screen_width() const {return static_cast<float>(m_resources.preferences.screen.width);};
float get_screen_height() const {return static_cast<float>(m_resources.preferences.screen.height);};
float get_panel_size() const {return m_resources.preferences.layout.panel_size*get_screen_width();};
float get_panel_spacing() const {return m_resources.preferences.layout.panel_spacing*get_screen_width();};
float get_panel_step() const {return m_resources.preferences.layout.panel_step()*get_screen_width();};
float get_big_cover_x() const {return m_resources.preferences.layout.big_cover_x*get_screen_width();};
float get_big_cover_y() const {return m_resources.preferences.layout.big_cover_y*get_screen_width();};
protected:
SharedResources& m_resources;
};
} }

View File

@ -1,11 +1,14 @@
#include "SongInfo.hpp" #include "SongInfo.hpp"
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include "Panel.hpp"
namespace MusicSelect { namespace MusicSelect {
BigCover::BigCover(SharedResources& resources) : BigCover::BigCover(SharedResources& resources) :
m_resources(resources) HoldsSharedResources(resources)
{ {
m_cover_fallback.setSize({get_size(), get_size()}); m_cover_fallback.setSize({get_size(), get_size()});
m_cover_fallback.setFillColor(sf::Color::Transparent); m_cover_fallback.setFillColor(sf::Color::Transparent);
@ -45,7 +48,7 @@ namespace MusicSelect {
} }
SongInfo::SongInfo(SharedResources& resources) : SongInfo::SongInfo(SharedResources& resources) :
m_resources(resources), HoldsSharedResources(resources),
m_big_cover(resources) m_big_cover(resources)
{ {
m_big_cover.setOrigin( m_big_cover.setOrigin(
@ -79,7 +82,7 @@ namespace MusicSelect {
song_title, song_title,
m_resources.noto_sans_medium, m_resources.noto_sans_medium,
static_cast<unsigned int>( static_cast<unsigned int>(
0.026315789f*get_width() 0.026315789f*get_screen_width()
) )
}; };
auto song_title_bounds = song_title_label.getLocalBounds(); auto song_title_bounds = song_title_label.getLocalBounds();
@ -90,7 +93,7 @@ namespace MusicSelect {
auto cover_pos = m_big_cover.getPosition(); auto cover_pos = m_big_cover.getPosition();
song_title_label.setPosition( song_title_label.setPosition(
get_big_cover_x() - m_big_cover.get_size()/2.f, get_big_cover_x() - m_big_cover.get_size()/2.f,
get_big_cover_y() + m_big_cover.get_size() + 0.01f*get_width() get_big_cover_y() + m_big_cover.get_size() + 0.01f*get_screen_width()
); );
target.draw(song_title_label); target.draw(song_title_label);
} }
@ -100,7 +103,7 @@ namespace MusicSelect {
song_artist, song_artist,
m_resources.noto_sans_medium, m_resources.noto_sans_medium,
static_cast<unsigned int>( static_cast<unsigned int>(
0.02f*get_width() 0.02f*get_screen_width()
) )
}; };
song_artist_label.setStyle(sf::Text::Italic); song_artist_label.setStyle(sf::Text::Italic);
@ -113,7 +116,7 @@ namespace MusicSelect {
auto cover_pos = m_big_cover.getPosition(); auto cover_pos = m_big_cover.getPosition();
song_artist_label.setPosition( song_artist_label.setPosition(
get_big_cover_x() - m_big_cover.get_size()/2.f, get_big_cover_x() - m_big_cover.get_size()/2.f,
get_big_cover_y() + m_big_cover.get_size() + 0.05f*get_width() get_big_cover_y() + m_big_cover.get_size() + 0.05f*get_screen_width()
); );
target.draw(song_artist_label); target.draw(song_artist_label);
} }

View File

@ -9,28 +9,23 @@
namespace MusicSelect { namespace MusicSelect {
class BigCover : public sf::Drawable, public sf::Transformable { class BigCover : public sf::Drawable, public sf::Transformable, public HoldsSharedResources {
public: public:
BigCover(SharedResources& resources); BigCover(SharedResources& resources);
float get_size() const {return m_resources.preferences.layout.big_cover_size*m_resources.preferences.screen.width;}; float get_size() const {return m_resources.preferences.layout.big_cover_size*get_screen_width();};
private: private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override; void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
SharedResources& m_resources;
sf::RectangleShape m_cover_fallback; sf::RectangleShape m_cover_fallback;
const Toolkit::AffineTransform<float> m_seconds_to_alpha{0.0f, 0.3f, 0.f, 255.f}; const Toolkit::AffineTransform<float> m_seconds_to_alpha{0.0f, 0.3f, 0.f, 255.f};
}; };
// Displays the song info on the top part of the screen // Displays the song info on the top part of the screen
class SongInfo : public sf::Drawable, public sf::Transformable { class SongInfo : public sf::Drawable, public sf::Transformable, public HoldsSharedResources {
public: public:
SongInfo(SharedResources& resources); SongInfo(SharedResources& resources);
float get_width() const {return m_resources.preferences.screen.width;};
float get_big_cover_x() const {return m_resources.preferences.layout.big_cover_x*get_width();};
float get_big_cover_y() const {return m_resources.preferences.layout.big_cover_y*get_width();};
private: private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override; void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
void draw_song_title(sf::RenderTarget& target) const; void draw_song_title(sf::RenderTarget& target) const;
SharedResources& m_resources;
BigCover m_big_cover; BigCover m_big_cover;
}; };
} }