1
0
mirror of synced 2024-11-14 10:37:40 +01:00

Play the music preview when a song panel is clicked

This commit is contained in:
Stepland 2020-02-25 23:47:29 +01:00
parent 14ab20f1c0
commit 99c1227553
11 changed files with 97 additions and 6 deletions

View File

@ -28,6 +28,8 @@
- Density graph
- format-agnostic chart class
- Black frame
- Sound
- Music Sample
### Misc
- Handling Resolution changes
@ -45,7 +47,7 @@
### Music Select Screen
- Sound
- Music Sample
- Sound Effects
- Fullscreen handling
- Song Panel click
- animation

View File

@ -45,6 +45,8 @@ sources = [
'src/Screens/MusicSelect/ButtonHighlight.cpp',
'src/Screens/MusicSelect/DensityGraph.hpp',
'src/Screens/MusicSelect/DensityGraph.cpp',
'src/Screens/MusicSelect/MusicPreview.hpp',
'src/Screens/MusicSelect/MusicPreview.cpp',
'src/Screens/MusicSelect/MusicSelect.hpp',
'src/Screens/MusicSelect/MusicSelect.cpp',
'src/Screens/MusicSelect/Panel.hpp',

View File

@ -3,7 +3,7 @@
#include <cstddef>
#include <set>
#include <memon.hpp>
#include <memon/memon.hpp>
#include "Buttons.hpp"
#include "Note.hpp"

View File

@ -5,7 +5,7 @@
#include <iostream>
#include <list>
#include <memon.hpp>
#include <memon/memon.hpp>
namespace fs = ghc::filesystem;
@ -115,6 +115,12 @@ namespace Data {
if (not m.music_path.empty()) {
this->audio.emplace(m.music_path);
}
if (m.preview) {
this->preview.emplace(
sf::seconds(m.preview->position),
sf::seconds(m.preview->duration)
);
}
for (const auto& [difficulty, chart] : m.charts) {
this->chart_levels[difficulty] = chart.level;
}

View File

@ -1,6 +1,5 @@
#pragma once
#include <ghc/filesystem.hpp>
#include <iterator>
#include <list>
#include <map>
@ -10,6 +9,9 @@
#include <variant>
#include <unordered_map>
#include <ghc/filesystem.hpp>
#include <SFML/Audio.hpp>
#include "Chart.hpp"
namespace fs = ghc::filesystem;
@ -33,6 +35,7 @@ namespace Data {
std::optional<fs::path> cover;
// Path the the audio file
std::optional<fs::path> audio;
std::optional<sf::Music::TimeSpan> preview;
// Mapping from chart difficulty (BSC, ADV, EXT ...) to the numeric level,
std::map<std::string, unsigned int, cmp_dif_name> chart_levels;

View File

@ -44,10 +44,10 @@ namespace MusicSelect {
}
DensityGraph compute_density_graph_from_struct(const SongDifficulty& sd) {
return compute_density_graph_from_song(sd.song, sd.difficulty);
return compute_density_graph_from_song_difficulty(sd.song, sd.difficulty);
}
DensityGraph compute_density_graph_from_song(const Data::Song& song, const std::string& difficulty) {
DensityGraph compute_density_graph_from_song_difficulty(const Data::Song& song, const std::string& difficulty) {
auto c = song.get_chart(difficulty);
if (not c.has_value()) {
throw std::invalid_argument("Song "+song.title+" has no "+difficulty+" chart");

View File

@ -0,0 +1,44 @@
#include "MusicPreview.hpp"
#include <iostream>
namespace MusicSelect {
void MusicPreview::play(std::optional<fs::path> music_path, std::optional<sf::Music::TimeSpan> loop) {
m_music_loop.emplace();
if (not music_path.has_value()) {
return;
}
if (not m_music_loop->music.openFromFile(music_path->string())) {
std::cerr << "Could not load " << music_path->string() << std::endl;
return;
}
if (not loop.has_value()) {
if (m_music_loop->music.getDuration() < sf::seconds(30)) {
loop.emplace(sf::seconds(0.f), m_music_loop->music.getDuration());
} else {
loop.emplace(sf::seconds(15.f), sf::seconds(10.f));
}
}
m_music_loop->music.setLoopPoints(*loop);
m_music_loop->loop = m_music_loop->music.getLoopPoints();
m_music_loop->music.setLoop(true);
m_music_loop->music.setPlayingOffset(loop->offset);
auto end = m_music_loop->loop.offset + m_music_loop->loop.length;
m_music_loop->fade_out = Toolkit::AffineTransform<float>{
(end - sf::seconds(2)).asSeconds(), end.asSeconds(),
100.f, 0.f
};
m_music_loop->music.play();
}
void MusicPreview::update() {
if (m_music_loop) {
m_music_loop->music.setVolume(
m_music_loop->fade_out.clampedTransform(
m_music_loop->music.getPlayingOffset().asSeconds()
)
);
}
}
}

View File

@ -0,0 +1,28 @@
#pragma once
#include <memory>
#include <optional>
#include <ghc/filesystem.hpp>
#include <SFML/Audio.hpp>
#include "../../Toolkit/AffineTransform.hpp"
namespace fs = ghc::filesystem;
namespace MusicSelect {
struct MusicLoop {
sf::Music music;
sf::Music::TimeSpan loop;
Toolkit::AffineTransform<float> fade_out = {0.f, 1.f, 0.f, 1.f}; // placeholder value
};
class MusicPreview {
public:
MusicPreview() = default;
void play(std::optional<fs::path> music_path, std::optional<sf::Music::TimeSpan> loop);
void update();
private:
std::optional<MusicLoop> m_music_loop;
};
}

View File

@ -76,6 +76,7 @@ void MusicSelect::Screen::select_chart(sf::RenderWindow& window) {
window.draw(black_frame);
ImGui::SFML::Render(window);
window.display();
resources.music_preview.update();
}
ImGui::SFML::Shutdown();
}

View File

@ -91,6 +91,7 @@ namespace MusicSelect {
m_resources.selected_panel->panel.unselect();
}
m_resources.selected_panel.emplace(TimedSelectedPanel{*this});
m_resources.music_preview.play(m_song->full_audio_path(), m_song->preview);
}
}

View File

@ -11,6 +11,7 @@
#include "../../Data/Song.hpp"
#include "DensityGraph.hpp"
#include "MusicPreview.hpp"
namespace MusicSelect {
@ -24,6 +25,7 @@ namespace MusicSelect {
bool is_first_click = true;
};
// Holds everything that needs to be shared by all levels of the class hierarchy
struct SharedResources : public Data::HoldsPreferences {
SharedResources(Data::Preferences& p);
@ -43,6 +45,8 @@ namespace MusicSelect {
sf::Color ADV_color = sf::Color{252,212,32};
sf::Color EXT_color = sf::Color{234,46,32};
sf::Color get_chart_color(const std::string& chart);
MusicPreview music_preview;
};
// Proxy for HoldsPreferences