Play the music preview when a song panel is clicked
This commit is contained in:
parent
14ab20f1c0
commit
99c1227553
4
TODO.md
4
TODO.md
@ -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
|
||||
|
@ -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',
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include <cstddef>
|
||||
#include <set>
|
||||
|
||||
#include <memon.hpp>
|
||||
#include <memon/memon.hpp>
|
||||
|
||||
#include "Buttons.hpp"
|
||||
#include "Note.hpp"
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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");
|
||||
|
44
src/Screens/MusicSelect/MusicPreview.cpp
Normal file
44
src/Screens/MusicSelect/MusicPreview.cpp
Normal 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()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
28
src/Screens/MusicSelect/MusicPreview.hpp
Normal file
28
src/Screens/MusicSelect/MusicPreview.hpp
Normal 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;
|
||||
};
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user