From 9381276fd3313d8ebde4718c4c05907ba69062da Mon Sep 17 00:00:00 2001 From: Kyzentun Date: Sun, 1 Mar 2015 15:40:16 -0700 Subject: [PATCH] Changed ScreenSelectMusic and Song to look for preview.ogg and use it for a sample if it exists instead of the music file. Sample length still comes from simfile. Changed foreach_ordered to work on tables with string and number keys. --- Docs/Changelog_sm5.txt | 15 +++++++++++---- Themes/_fallback/Scripts/01 IniFile.lua | 24 ++++++++++++++++++------ src/ScreenSelectMusic.cpp | 4 ++-- src/Song.cpp | 20 ++++++++++++++++++++ src/Song.h | 2 ++ 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/Docs/Changelog_sm5.txt b/Docs/Changelog_sm5.txt index bb334d3057..8411fd121c 100644 --- a/Docs/Changelog_sm5.txt +++ b/Docs/Changelog_sm5.txt @@ -4,22 +4,29 @@ The StepMania 5 Changelog covers all post-sm-ssc changes. For a list of changes from StepMania 4 alpha 5 to sm-ssc v1.2.5, see Changelog_sm-ssc.txt. ________________________________________________________________________________ +2015/03/01 +---------- +* [SelectMusic] Select Music now plays preview.ogg for a simfile if it exists + instead of using the song file for the sample music. The sample length + from the simfile is used and the sample start is treated as 0. [kyzentun] +* [foreach] foreach_ordered lua function now works on tables that have both + number and string keys. Number keys are iterated over first. [kyzentun] 2015/02/27 ---------- * [ActorFrame] Fixed RemoveChild and RemoveAllChildren lua functions to - delete the children instead of leaking memory. [kyzentun] + delete the children instead of leaking memory. [kyzentun] 2015/02/26 ---------- * [PaneDisplay] Changed to print an error when metrics or player number are - omitted instead of crashing. [kyzentun] + omitted instead of crashing. [kyzentun] * [Screen] Fixed crash when AddInputCallback is passed nil. [kyzentun] 2015/02/22 ---------- * [BitmapText] Place characters of right-to-left alphabets correctly. - (untested) [roothorick] + (untested) [roothorick] ================================================================================ StepMania 5.0.6 | 20150217 @@ -28,7 +35,7 @@ StepMania 5.0.6 | 20150217 2015/02/16 ---------- * [EditMode] Fixed mistake in TimingData that broke editing bpms. [kyzentun] - Fixed crash when deleting steps. [kyzentun] + Fixed crash when deleting steps. [kyzentun] * [InputMapper] Backslash can be mapped now. [kyzentun] ================================================================================ diff --git a/Themes/_fallback/Scripts/01 IniFile.lua b/Themes/_fallback/Scripts/01 IniFile.lua index bc042bee5d..c01743dd4d 100644 --- a/Themes/_fallback/Scripts/01 IniFile.lua +++ b/Themes/_fallback/Scripts/01 IniFile.lua @@ -6,14 +6,26 @@ Note that this is a namespace, not a class per se. -- TODO: move this into a more general section -- func takes a key and a value +function foreach_by_sorted_keys(tbl, keys, func) + table.sort(keys) + for _, key in ipairs(keys) do func(key, tbl[key]) end +end + function foreach_ordered( tbl, func ) - local keys = { } - for k,_ in pairs(tbl) do keys[#keys+1] = k end - - table.sort( keys ) - + local string_keys= {} + local number_keys= {} + -- First person to to use this on a table that uses something else as keys + -- gets to extend this function to cover more types. And a beating. -Kyz + for k,_ in pairs(tbl) do + if type(k) == "string" then + table.insert(string_keys, k) + elseif type(k) == "number" then + table.insert(number_keys, k) + end + end -- iterate in sorted order - for _,key in ipairs(keys) do func( key, tbl[key]) end + foreach_by_sorted_keys(tbl, number_keys, func) + foreach_by_sorted_keys(tbl, string_keys, func) end -- redeclared here for my sanity's sake diff --git a/src/ScreenSelectMusic.cpp b/src/ScreenSelectMusic.cpp index 6425f17dc7..985c019e5b 100644 --- a/src/ScreenSelectMusic.cpp +++ b/src/ScreenSelectMusic.cpp @@ -1796,9 +1796,9 @@ void ScreenSelectMusic::AfterMusicChange() case SampleMusicPreviewMode_Normal: case SampleMusicPreviewMode_LastSong: // fall through // play the sample music - m_sSampleMusicToPlay = pSong->GetMusicPath(); + m_sSampleMusicToPlay = pSong->GetPreviewMusicPath(); m_pSampleMusicTimingData = &pSong->m_SongTiming; - m_fSampleStartSeconds = pSong->m_fMusicSampleStartSeconds; + m_fSampleStartSeconds = pSong->GetPreviewStartSeconds(); m_fSampleLengthSeconds = pSong->m_fMusicSampleLengthSeconds; break; default: diff --git a/src/Song.cpp b/src/Song.cpp index 1ccdf2c8f2..343a61009c 100644 --- a/src/Song.cpp +++ b/src/Song.cpp @@ -1477,6 +1477,26 @@ RString Song::GetPreviewVidPath() const return GetSongAssetPath( m_sPreviewVidFile, m_sSongDir ); } +RString Song::GetPreviewMusicPath() const +{ + RString preview= GetSongDir() + "preview.ogg"; + if(FILEMAN->DoesFileExist(preview)) + { + return preview; + } + return GetMusicPath(); +} + +float Song::GetPreviewStartSeconds() const +{ + RString preview= GetSongDir() + "preview.ogg"; + if(FILEMAN->DoesFileExist(preview)) + { + return 0.0f; + } + return m_fMusicSampleStartSeconds; +} + RString Song::GetDisplayMainTitle() const { if(!PREFSMAN->m_bShowNativeLanguage) return GetTranslitMainTitle(); diff --git a/src/Song.h b/src/Song.h index d786cb1b90..76c1bd360d 100644 --- a/src/Song.h +++ b/src/Song.h @@ -257,6 +257,8 @@ public: RString GetBackgroundPath() const; RString GetCDTitlePath() const; RString GetPreviewVidPath() const; + RString GetPreviewMusicPath() const; + float GetPreviewStartSeconds() const; // For loading only: bool m_bHasMusic, m_bHasBanner, m_bHasBackground;