From 3d6adf9ade46675a9bddf01a60cd256f51683fc6 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Fri, 15 May 2020 18:21:24 +0200 Subject: [PATCH] Implemented a much more elegant handling of navigation sounds --- es-app/src/components/TextListComponent.h | 5 +- es-app/src/guis/GuiGamelistOptions.cpp | 3 + es-app/src/guis/GuiGamelistOptions.h | 4 +- es-app/src/views/SystemView.cpp | 17 ++--- es-app/src/views/SystemView.h | 3 - es-app/src/views/ViewController.cpp | 14 ++++ .../src/views/gamelist/GridGameListView.cpp | 2 +- .../views/gamelist/ISimpleGameListView.cpp | 11 +-- es-core/src/AudioManager.cpp | 2 +- es-core/src/Sound.cpp | 70 ++++++++++++++++++- es-core/src/Sound.h | 30 ++++++++ es-core/src/ThemeData.cpp | 22 +++--- 12 files changed, 139 insertions(+), 44 deletions(-) diff --git a/es-app/src/components/TextListComponent.h b/es-app/src/components/TextListComponent.h index a703e378e..272b80ff0 100644 --- a/es-app/src/components/TextListComponent.h +++ b/es-app/src/components/TextListComponent.h @@ -80,9 +80,8 @@ public: inline void setLineSpacing(float lineSpacing) { mLineSpacing = lineSpacing; } protected: - virtual void onScroll() { soundfile->play(); } + virtual void onScroll() { navigationsounds.playThemeNavigationSound(SCROLLSOUND); } virtual void onCursorChanged(const CursorState& state); - std::shared_ptr soundfile; private: int mMarqueeOffset; @@ -390,8 +389,6 @@ void TextListComponent::applyTheme(const std::shared_ptr& theme, c setFont(Font::getFromTheme(elem, properties, mFont)); const float selectorHeight = Math::max(mFont->getHeight(1.0), (float)mFont->getSize()) * mLineSpacing; setSelectorHeight(selectorHeight); - - soundfile = Sound::getFromTheme(theme, "navigationsounds", "scrollSound"); if(properties & ALIGNMENT) { diff --git a/es-app/src/guis/GuiGamelistOptions.cpp b/es-app/src/guis/GuiGamelistOptions.cpp index 95e7dddce..36d73534e 100644 --- a/es-app/src/guis/GuiGamelistOptions.cpp +++ b/es-app/src/guis/GuiGamelistOptions.cpp @@ -10,6 +10,7 @@ #include "FileSorts.h" #include "GuiMetaDataEd.h" #include "SystemData.h" +#include "Sound.h" GuiGamelistOptions::GuiGamelistOptions(Window* window, SystemData* system) : GuiComponent(window), mSystem(system), mMenu(window, "OPTIONS"), fromPlaceholder(false), mFiltersChanged(false) @@ -81,9 +82,11 @@ GuiGamelistOptions::GuiGamelistOptions(Window* window, SystemData* system) : Gui { if(mJumpToLetterList->getSelected() == FAV_CHAR) { + navigationsounds.playThemeNavigationSound(SCROLLSOUND); jumpToFirstFavorite(); return true; } + navigationsounds.playThemeNavigationSound(SCROLLSOUND); jumpToLetter(); return true; } diff --git a/es-app/src/guis/GuiGamelistOptions.h b/es-app/src/guis/GuiGamelistOptions.h index 0fc2ee735..05b788103 100644 --- a/es-app/src/guis/GuiGamelistOptions.h +++ b/es-app/src/guis/GuiGamelistOptions.h @@ -2,8 +2,6 @@ #ifndef ES_APP_GUIS_GUI_GAME_LIST_OPTIONS_H #define ES_APP_GUIS_GUI_GAME_LIST_OPTIONS_H -#define FAV_CHAR '*' - #include "components/MenuComponent.h" #include "components/OptionListComponent.h" #include "FileData.h" @@ -31,6 +29,8 @@ private: void findFirstFavorite(); void jumpToFirstFavorite(); + const char FAV_CHAR = '*'; + long firstFavorite = -1; MenuComponent mMenu; diff --git a/es-app/src/views/SystemView.cpp b/es-app/src/views/SystemView.cpp index e103ef6ab..c46b360d1 100644 --- a/es-app/src/views/SystemView.cpp +++ b/es-app/src/views/SystemView.cpp @@ -29,8 +29,6 @@ SystemView::SystemView(Window* window) : IList(wind void SystemView::populate() { mEntries.clear(); - mSelectSound = ""; -// mScrollSound = ""; for(auto it = SystemData::sSystemVector.cbegin(); it != SystemData::sSystemVector.cend(); it++) { @@ -127,8 +125,6 @@ void SystemView::populate() mWindow->pushGui(new GuiMsgBox(mWindow, "The selected UI mode has nothing to show,\n returning to UI mode: FULL", "OK", nullptr)); } } - - } void SystemView::goToSystem(SystemData* system, bool animate) @@ -159,13 +155,13 @@ bool SystemView::input(InputConfig* config, Input input) case VERTICAL_WHEEL: if (config->isMappedLike("up", input)) { - Sound::getFromTheme(theme, "navigationsounds", "systembrowseSound")->play(); + navigationsounds.playThemeNavigationSound(SYSTEMBROWSE); listInput(-1); return true; } if (config->isMappedLike("down", input)) { - Sound::getFromTheme(theme, "navigationsounds", "systembrowseSound")->play(); + navigationsounds.playThemeNavigationSound(SYSTEMBROWSE); listInput(1); return true; } @@ -175,13 +171,13 @@ bool SystemView::input(InputConfig* config, Input input) default: if (config->isMappedLike("left", input)) { - Sound::getFromTheme(theme, "navigationsounds", "systembrowseSound")->play(); + navigationsounds.playThemeNavigationSound(SYSTEMBROWSE); listInput(-1); return true; } if (config->isMappedLike("right", input)) { - Sound::getFromTheme(theme, "navigationsounds", "systembrowseSound")->play(); + navigationsounds.playThemeNavigationSound(SYSTEMBROWSE); listInput(1); return true; } @@ -190,16 +186,16 @@ bool SystemView::input(InputConfig* config, Input input) if(config->isMappedTo("a", input)) { - Sound::getFromTheme(theme, "navigationsounds", "selectSound")->play(); stopScrolling(); ViewController::get()->goToGameList(getSelected()); + navigationsounds.playThemeNavigationSound(SELECTSOUND); return true; } if (config->isMappedTo("x", input)) { // get random system // go to system - Sound::getFromTheme(theme, "navigationsounds", "systembrowseSound")->play(); + navigationsounds.playThemeNavigationSound(SYSTEMBROWSE); setCursor(SystemData::getRandomSystem()); return true; } @@ -247,7 +243,6 @@ void SystemView::onCursorChanged(const CursorState& /*state*/) if(abs(target - posMax - startPos) < dist) endPos = target - posMax; // loop around the start (max - 1 -> -1) - // animate mSystemInfo's opacity (fade out, wait, fade back in) cancelAnimation(1); diff --git a/es-app/src/views/SystemView.h b/es-app/src/views/SystemView.h index 8e77e744d..2d4c90033 100644 --- a/es-app/src/views/SystemView.h +++ b/es-app/src/views/SystemView.h @@ -62,9 +62,6 @@ public: std::vector getHelpPrompts() override; virtual HelpStyle getHelpStyle() override; - std::string mSelectSound; -// std::string mScrollSound; - protected: void onCursorChanged(const CursorState& state) override; diff --git a/es-app/src/views/ViewController.cpp b/es-app/src/views/ViewController.cpp index 73c5c05eb..b09e7fbaa 100644 --- a/es-app/src/views/ViewController.cpp +++ b/es-app/src/views/ViewController.cpp @@ -16,8 +16,10 @@ #include "Settings.h" #include "SystemData.h" #include "Window.h" +#include "Sound.h" ViewController* ViewController::sInstance = NULL; +NavigationSounds navigationsounds; ViewController* ViewController::get() { @@ -103,6 +105,7 @@ void ViewController::goToNextGameList() assert(mState.viewing == GAME_LIST); SystemData* system = getState().getSystem(); assert(system); + navigationsounds.playThemeNavigationSound(QUICKSYSSELECT); goToGameList(system->getNext()); } @@ -111,6 +114,7 @@ void ViewController::goToPrevGameList() assert(mState.viewing == GAME_LIST); SystemData* system = getState().getSystem(); assert(system); + navigationsounds.playThemeNavigationSound(QUICKSYSSELECT); goToGameList(system->getPrev()); } @@ -224,6 +228,11 @@ void ViewController::launch(FileData* game, Vector3f center) mLockInput = true; std::string transition_style = Settings::getInstance()->getString("TransitionStyle"); + + navigationsounds.playThemeNavigationSound(LAUNCHSOUND); + // let launch sound play to the end before launching game + while(navigationsounds.isPlayingThemeNavigationSound(LAUNCHSOUND)); + if(transition_style == "fade") { // fade out, launch game, fade back in @@ -452,6 +461,8 @@ void ViewController::preload() (*it)->getIndex()->resetFilters(); getGameListView(*it); } + // load navigation sounds + navigationsounds.loadThemeNavigationSounds(SystemData::sSystemVector[0]->getTheme()); } void ViewController::reloadGameListView(IGameListView* view, bool reloadTheme) @@ -523,6 +534,9 @@ void ViewController::reloadAll() goToSystemView(SystemData::sSystemVector.front()); } + // load navigation sounds + navigationsounds.loadThemeNavigationSounds(SystemData::sSystemVector[0]->getTheme()); + updateHelpPrompts(); } diff --git a/es-app/src/views/gamelist/GridGameListView.cpp b/es-app/src/views/gamelist/GridGameListView.cpp index b85debc51..637555986 100644 --- a/es-app/src/views/gamelist/GridGameListView.cpp +++ b/es-app/src/views/gamelist/GridGameListView.cpp @@ -149,7 +149,7 @@ bool GridGameListView::input(InputConfig* config, Input input) { if (input.value == 0 and (config->isMappedLike("left", input) || config->isMappedLike("right", input) || (config->isMappedLike("up", input)) || (config->isMappedLike("down", input)) )) - Sound::getFromTheme(getTheme(), "navigationsounds", "scrollSound")->play(); + navigationsounds.playThemeNavigationSound(SCROLLSOUND); if(config->isMappedLike("left", input) || config->isMappedLike("right", input)) return GuiComponent::input(config, input); diff --git a/es-app/src/views/gamelist/ISimpleGameListView.cpp b/es-app/src/views/gamelist/ISimpleGameListView.cpp index efb389c4e..747600112 100644 --- a/es-app/src/views/gamelist/ISimpleGameListView.cpp +++ b/es-app/src/views/gamelist/ISimpleGameListView.cpp @@ -87,9 +87,6 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) FileData* cursor = getCursor(); if(cursor->getType() == GAME) { - soundfile = Sound::getFromTheme(getTheme(), "navigationsounds", "launchSound"); - soundfile->play(); - while(soundfile->isPlaying()); launch(cursor); }else{ // it's a folder @@ -111,7 +108,7 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) setCursor(mCursorStack.top()); mCursorStack.pop(); }else{ - Sound::getFromTheme(getTheme(), "navigationsounds", "backSound")->play(); + navigationsounds.playThemeNavigationSound(BACKSOUND); onFocusLost(); SystemData* systemToView = getCursor()->getSystem(); if (systemToView->isCollection()) @@ -126,7 +123,6 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) { if(Settings::getInstance()->getBool("QuickSystemSelect")) { - Sound::getFromTheme(getTheme(), "navigationsounds", "quicksysselectSound")->play(); onFocusLost(); ViewController::get()->goToNextGameList(); return true; @@ -135,7 +131,6 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) { if(Settings::getInstance()->getBool("QuickSystemSelect")) { - Sound::getFromTheme(getTheme(), "navigationsounds", "quicksysselectSound")->play(); onFocusLost(); ViewController::get()->goToPrevGameList(); return true; @@ -145,7 +140,7 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) if (mRoot->getSystem()->isGameSystem()) { // go to random system game - Sound::getFromTheme(getTheme(), "navigationsounds", "scrollSound")->play(); + navigationsounds.playThemeNavigationSound(SCROLLSOUND); FileData* randomGame = getCursor()->getSystem()->getRandomGame(); if (randomGame) { @@ -157,7 +152,7 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) { if(mRoot->getSystem()->isGameSystem()) { - Sound::getFromTheme(getTheme(), "navigationsounds", "favoriteSound")->play(); + navigationsounds.playThemeNavigationSound(FAVORITESOUND); if(CollectionSystemManager::get()->toggleGameInCollection(getCursor())) { return true; diff --git a/es-core/src/AudioManager.cpp b/es-core/src/AudioManager.cpp index 1d0f8ac49..85f5a4baa 100644 --- a/es-core/src/AudioManager.cpp +++ b/es-core/src/AudioManager.cpp @@ -27,7 +27,7 @@ void AudioManager::mixAudio(void* /*unused*/, Uint8 *stream, int len) //calculate rest length of current sample Uint32 restLength = (sound->getLength() - sound->getPosition()); if (restLength > (Uint32)len) { - //if stream length is smaller than smaple lenght, clip it + //if stream length is smaller than sample length, clip it restLength = len; } //mix sample into stream diff --git a/es-core/src/Sound.cpp b/es-core/src/Sound.cpp index fdc38a073..f522d5baf 100644 --- a/es-core/src/Sound.cpp +++ b/es-core/src/Sound.cpp @@ -21,9 +21,6 @@ std::shared_ptr Sound::get(const std::string& path) std::shared_ptr Sound::getFromTheme(const std::shared_ptr& theme, const std::string& view, const std::string& element) { - if(!Settings::getInstance()->getBool("EnableSounds")) - return get(""); - LOG(LogInfo) << "Sound::getFromTheme() looking for [" << view << "." << element << "]"; const ThemeData::ThemeElement* elem = theme->getElement(view, element, "sound"); @@ -37,6 +34,73 @@ std::shared_ptr Sound::getFromTheme(const std::shared_ptr& the return get(elem->get("path")); } +void NavigationSounds::loadThemeNavigationSounds(const std::shared_ptr& theme) +{ + systembrowseSound = Sound::getFromTheme(theme, "all", "systembrowseSound"); + quicksysselectSound = Sound::getFromTheme(theme, "all", "quicksysselectSound"); + selectSound = Sound::getFromTheme(theme, "all", "selectSound"); + backSound = Sound::getFromTheme(theme, "all", "backSound"); + scrollSound = Sound::getFromTheme(theme, "all", "scrollSound"); + favoriteSound = Sound::getFromTheme(theme, "all", "favoriteSound"); + launchSound = Sound::getFromTheme(theme, "all", "launchSound"); +} + +void NavigationSounds::playThemeNavigationSound(NavigationSoundsID soundID) +{ + + switch(soundID) + { + case SYSTEMBROWSE: + navigationsounds.systembrowseSound->play(); + break; + case QUICKSYSSELECT: + navigationsounds.quicksysselectSound->play(); + break; + case SELECTSOUND: + navigationsounds.selectSound->play(); + break; + case BACKSOUND: + navigationsounds.backSound->play(); + break; + case SCROLLSOUND: + navigationsounds.scrollSound->play(); + break; + case FAVORITESOUND: + navigationsounds.favoriteSound->play(); + break; + case LAUNCHSOUND: + navigationsounds.launchSound->play(); + } +} + +bool NavigationSounds::isPlayingThemeNavigationSound(NavigationSoundsID soundID) +{ + switch(soundID) + { + case SYSTEMBROWSE: + return navigationsounds.systembrowseSound->isPlaying(); + break; + case QUICKSYSSELECT: + return navigationsounds.quicksysselectSound->isPlaying(); + break; + case SELECTSOUND: + return navigationsounds.selectSound->isPlaying(); + break; + case BACKSOUND: + return navigationsounds.backSound->isPlaying(); + break; + case SCROLLSOUND: + return navigationsounds.scrollSound->isPlaying(); + break; + case FAVORITESOUND: + return navigationsounds.favoriteSound->isPlaying(); + break; + case LAUNCHSOUND: + return navigationsounds.launchSound->isPlaying(); + } + return false; +} + Sound::Sound(const std::string & path) : mSampleData(NULL), mSamplePos(0), mSampleLength(0), playing(false) { loadFile(path); diff --git a/es-core/src/Sound.h b/es-core/src/Sound.h index d54688d8d..451e64ce3 100644 --- a/es-core/src/Sound.h +++ b/es-core/src/Sound.h @@ -43,4 +43,34 @@ private: static std::map< std::string, std::shared_ptr > sMap; }; +enum NavigationSoundsID +{ + SYSTEMBROWSE, + QUICKSYSSELECT, + SELECTSOUND, + BACKSOUND, + SCROLLSOUND, + FAVORITESOUND, + LAUNCHSOUND +}; + +class NavigationSounds +{ +public: + void loadThemeNavigationSounds(const std::shared_ptr& theme); + void playThemeNavigationSound(NavigationSoundsID soundID); + bool isPlayingThemeNavigationSound(NavigationSoundsID soundID); + +private: + std::shared_ptr systembrowseSound; + std::shared_ptr quicksysselectSound; + std::shared_ptr selectSound; + std::shared_ptr backSound; + std::shared_ptr scrollSound; + std::shared_ptr favoriteSound; + std::shared_ptr launchSound; +}; + +extern NavigationSounds navigationsounds; + #endif // ES_CORE_SOUND_H diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index df532892b..629a1be56 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -10,18 +10,10 @@ #include #include -std::vector ThemeData::sSupportedViews { { "navigationsounds" }, { "system" }, { "basic" }, { "detailed" }, { "grid" }, { "video" } }; -std::vector ThemeData::sSupportedFeatures { { "video" }, { "carousel" }, { "z-index" }, { "visible" } }; +std::vector ThemeData::sSupportedViews { { "all" }, { "system" }, { "basic" }, { "detailed" }, { "grid" }, { "video" } }; +std::vector ThemeData::sSupportedFeatures { { "navigationsounds" }, { "video" }, { "carousel" }, { "z-index" }, { "visible" } }; std::map> ThemeData::sElementMap { - { "navigationsounds", { - { "systembrowseSound", PATH }, - { "quicksysselectSound", PATH }, - { "selectSound", PATH }, - { "backSound", PATH }, - { "scrollSound", PATH }, - { "favoriteSound", PATH }, - { "launchSound", PATH } } }, { "image", { { "pos", NORMALIZED_PAIR }, { "size", NORMALIZED_PAIR }, @@ -94,7 +86,7 @@ std::map> The { "secondaryColor", COLOR }, { "fontPath", PATH }, { "fontSize", FLOAT }, - { "scrollSound", PATH }, + { "scrollSound", PATH }, // need to keep this for backwards compatibility with old themes { "alignment", STRING }, { "horizontalMargin", FLOAT }, { "forceUppercase", BOOLEAN }, @@ -150,6 +142,14 @@ std::map> The { "iconColor", COLOR }, { "fontPath", PATH }, { "fontSize", FLOAT } } }, + { "navigationsounds", { + { "systembrowseSound", PATH }, + { "quicksysselectSound", PATH }, + { "selectSound", PATH }, + { "backSound", PATH }, + { "scrollSound", PATH }, + { "favoriteSound", PATH }, + { "launchSound", PATH } } }, { "video", { { "pos", NORMALIZED_PAIR }, { "size", NORMALIZED_PAIR },