From 8d1ac3087e37debf73d86c00971485cd701ee561 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Fri, 3 Jan 2014 10:40:36 -0600 Subject: [PATCH] Changed the sound cache to be part of the Sound class instead of ThemeData. --- src/Sound.cpp | 29 ++++++++++++++++++++++ src/Sound.h | 11 +++++++- src/ThemeData.cpp | 28 +++++++-------------- src/ThemeData.h | 6 ++--- src/components/GuiMenu.cpp | 10 +++++--- src/components/TextListComponent.h | 4 +++ src/views/ViewController.cpp | 3 --- src/views/gamelist/BasicGameListView.cpp | 2 +- src/views/gamelist/IGameListView.cpp | 2 +- src/views/gamelist/ISimpleGameListView.cpp | 4 ++- 10 files changed, 65 insertions(+), 34 deletions(-) diff --git a/src/Sound.cpp b/src/Sound.cpp index 64bf9c86b..cf540091f 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -2,6 +2,35 @@ #include "AudioManager.h" #include "Log.h" #include "Settings.h" +#include "ThemeData.h" + +std::map< std::string, std::shared_ptr > Sound::sMap; + +std::shared_ptr Sound::get(const std::string& path) +{ + auto it = sMap.find(path); + if(it != sMap.end()) + return it->second; + + std::shared_ptr sound = std::shared_ptr(new Sound(path)); + AudioManager::getInstance()->registerSound(sound); + sMap[path] = sound; + return sound; +} + +std::shared_ptr Sound::getFromTheme(const std::shared_ptr& theme, const std::string& view, const std::string& element) +{ + LOG(LogInfo) << " req sound [" << view << "." << element << "]"; + + const ThemeData::ThemeElement* elem = theme->getElement(view, element, "sound"); + if(!elem || !elem->has("path")) + { + LOG(LogInfo) << " (missing)"; + return get(""); + } + + return get(elem->get("path")); +} Sound::Sound(const std::string & path) : mSampleData(NULL), mSamplePos(0), mSampleLength(0), playing(false) { diff --git a/src/Sound.h b/src/Sound.h index 51cac2f9f..2eb430ce0 100644 --- a/src/Sound.h +++ b/src/Sound.h @@ -2,8 +2,11 @@ #define _SOUND_H_ #include +#include +#include #include "SDL_audio.h" +class ThemeData; class Sound { @@ -15,7 +18,9 @@ class Sound bool playing; public: - Sound(const std::string & path = ""); + static std::shared_ptr get(const std::string& path); + static std::shared_ptr getFromTheme(const std::shared_ptr& theme, const std::string& view, const std::string& elem); + ~Sound(); void init(); @@ -32,6 +37,10 @@ public: void setPosition(Uint32 newPosition); Uint32 getLength() const; Uint32 getLengthMS() const; + +private: + Sound(const std::string & path = ""); + static std::map< std::string, std::shared_ptr > sMap; }; #endif diff --git a/src/ThemeData.cpp b/src/ThemeData.cpp index 9d26d0aed..32696e952 100644 --- a/src/ThemeData.cpp +++ b/src/ThemeData.cpp @@ -33,7 +33,8 @@ std::map< std::string, std::map > T ("primaryColor", COLOR) ("secondaryColor", COLOR) ("fontPath", PATH) - ("fontSize", FLOAT)) + ("fontSize", FLOAT) + ("scrollSound", PATH)) ("container", boost::assign::map_list_of ("pos", NORMALIZED_PAIR) ("size", NORMALIZED_PAIR)) @@ -300,28 +301,17 @@ const ThemeData::ThemeElement* ThemeData::getElement(const std::string& view, co return &elemIt->second; } -void ThemeData::playSound(const std::string& elementName) +const std::shared_ptr& ThemeData::getDefault() { - const ThemeElement* elem = getElement("common", elementName, "sound"); - if(!elem) - return; - - if(elem->has("path")) + static std::shared_ptr theme = nullptr; + if(theme == nullptr) { - const std::string path = elem->get("path"); - auto cacheIt = mSoundCache.find(path); - if(cacheIt != mSoundCache.end()) - { - cacheIt->second->play(); - return; - } - - std::shared_ptr sound = std::shared_ptr(new Sound(path)); - sound->play(); - mSoundCache[path] = sound; + theme = std::shared_ptr(new ThemeData()); + theme->loadFile(getHomePath() + "/.emulationstation/es_theme_default.xml"); } -} + return theme; +} std::vector ThemeData::makeExtras(const std::shared_ptr& theme, const std::string& view, Window* window) { diff --git a/src/ThemeData.h b/src/ThemeData.h index 9f8a24a0f..561dcf910 100644 --- a/src/ThemeData.h +++ b/src/ThemeData.h @@ -127,13 +127,13 @@ public: void renderExtras(const std::string& view, Window* window, const Eigen::Affine3f& transform); - void playSound(const std::string& name); - // If expectedType is an empty string, will do no type checking. const ThemeElement* getElement(const std::string& view, const std::string& element, const std::string& expectedType) const; static std::vector makeExtras(const std::shared_ptr& theme, const std::string& view, Window* window); + static const std::shared_ptr& getDefault(); + private: static std::map< std::string, std::map > sElementMap; @@ -146,8 +146,6 @@ private: void parseElement(const pugi::xml_node& elementNode, const std::map& typeMap, ThemeElement& element); std::map mViews; - - std::map< std::string, std::shared_ptr > mSoundCache; }; // okay ideas for applying themes to GuiComponents: diff --git a/src/components/GuiMenu.cpp b/src/components/GuiMenu.cpp index e766c4810..696162ced 100644 --- a/src/components/GuiMenu.cpp +++ b/src/components/GuiMenu.cpp @@ -35,19 +35,21 @@ GuiMenu::GuiMenu(Window* window) : GuiComponent(window), mBackground(window, ":/ mList.setPosition(mSize.x() * 0.175f, mSize.y() * 0.05f); mList.setSize(mSize.x() * 0.65f, mSize.y() * 0.9f); + mTheme = ThemeData::getDefault(); + using namespace ThemeFlags; mBackground.applyTheme(mTheme, "menu", "background", PATH); mBackground.fitTo(Eigen::Vector2f(mList.getSize().x(), mSize.y()), Eigen::Vector3f(mList.getPosition().x(), 0, 0)); addChild(&mBackground); - mTheme = std::make_shared(); - mList.setFont(Font::get((int)(0.09f * Renderer::getScreenHeight()))); - mList.applyTheme(mTheme, "menu", "menulist", FONT_PATH | COLOR); + mList.applyTheme(mTheme, "menu", "menulist", FONT_PATH | COLOR | SOUND); mList.setSelectorColor(0xBBBBBBFF); mList.setColor(0, 0x0000FFFF); mList.setColor(1, 0xFF0000FF); + Sound::getFromTheme(mTheme, "menu", "menuOpen")->play(); + addChild(&mList); } @@ -57,7 +59,7 @@ bool GuiMenu::input(InputConfig* config, Input input) { if(config->isMappedTo("b", input) || config->isMappedTo("menu", input)) { - mTheme->playSound("menuCloseSound"); + Sound::getFromTheme(mTheme, "menu", "menuClose")->play(); delete this; return true; }else if(config->isMappedTo("a", input) && mList.getList().size() > 0) diff --git a/src/components/TextListComponent.h b/src/components/TextListComponent.h index d074d852b..97e55e4ff 100644 --- a/src/components/TextListComponent.h +++ b/src/components/TextListComponent.h @@ -69,6 +69,7 @@ public: inline void setSelectedColor(unsigned int color) { mSelectedColor = color; } inline void setScrollSound(const std::shared_ptr& sound) { mScrollSound = sound; } inline void setColor(unsigned int id, unsigned int color) { mColors[id] = color; } + inline void setSound(const std::shared_ptr& sound) { mScrollSound = sound; } private: static const int MARQUEE_DELAY = 900; @@ -429,6 +430,9 @@ void TextListComponent::applyTheme(const std::shared_ptr& theme, c } setFont(Font::getFromTheme(elem, properties, mFont)); + + if(properties & SOUND && elem->has("scrollSound")) + setSound(Sound::get(elem->get("scrollSound"))); } #endif diff --git a/src/views/ViewController.cpp b/src/views/ViewController.cpp index fc84b8e94..7f6bcf922 100644 --- a/src/views/ViewController.cpp +++ b/src/views/ViewController.cpp @@ -83,8 +83,6 @@ void ViewController::launch(FileData* game, Eigen::Vector3f center) return; } - game->getSystem()->getTheme()->playSound("gameSelectSound"); - Eigen::Affine3f origCamera = mCamera; origCamera.translation() = -mCurrentView->getPosition(); @@ -165,7 +163,6 @@ bool ViewController::input(InputConfig* config, Input input) { // open menu mWindow->pushGui(new GuiMenu(mWindow)); - ThemeData().playSound("menuOpenSound"); return true; } diff --git a/src/views/gamelist/BasicGameListView.cpp b/src/views/gamelist/BasicGameListView.cpp index dfa670767..d7fbf6bfd 100644 --- a/src/views/gamelist/BasicGameListView.cpp +++ b/src/views/gamelist/BasicGameListView.cpp @@ -19,7 +19,7 @@ void BasicGameListView::onThemeChanged(const std::shared_ptr& theme) { ISimpleGameListView::onThemeChanged(theme); using namespace ThemeFlags; - mList.applyTheme(theme, getName(), "gamelist", POSITION | ThemeFlags::SIZE | COLOR | SOUND | FONT_PATH | FONT_SIZE); + mList.applyTheme(theme, getName(), "gamelist", ALL); } void BasicGameListView::onFileChanged(FileData* file, FileChangeType change) diff --git a/src/views/gamelist/IGameListView.cpp b/src/views/gamelist/IGameListView.cpp index 26e1fabd4..096db00ce 100644 --- a/src/views/gamelist/IGameListView.cpp +++ b/src/views/gamelist/IGameListView.cpp @@ -21,7 +21,7 @@ bool IGameListView::input(InputConfig* config, Input input) onFileChanged(file, FILE_REMOVED); //tell the view delete file; //free it })); - mTheme->playSound("menuOpenSound"); + Sound::getFromTheme(mTheme, getName(), "menuOpen")->play(); return true; }else if(config->isMappedTo("select", input) && input.value != 0) { diff --git a/src/views/gamelist/ISimpleGameListView.cpp b/src/views/gamelist/ISimpleGameListView.cpp index 448d415f0..3ef3cbcd2 100644 --- a/src/views/gamelist/ISimpleGameListView.cpp +++ b/src/views/gamelist/ISimpleGameListView.cpp @@ -2,6 +2,7 @@ #include "../../ThemeData.h" #include "../../Window.h" #include "../ViewController.h" +#include "../../Sound.h" ISimpleGameListView::ISimpleGameListView(Window* window, FileData* root) : IGameListView(window, root), mHeaderText(window), mHeaderImage(window), mBackground(window), mThemeExtras(window) @@ -57,6 +58,7 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) FileData* cursor = getCursor(); if(cursor->getType() == GAME) { + Sound::getFromTheme(getTheme(), getName(), "launch")->play(); launch(cursor); }else{ // it's a folder @@ -75,7 +77,7 @@ bool ISimpleGameListView::input(InputConfig* config, Input input) populateList(mCursorStack.top()->getParent()->getChildren()); setCursor(mCursorStack.top()); mCursorStack.pop(); - getTheme()->playSound("backSound"); + Sound::getFromTheme(getTheme(), getName(), "back")->play(); }else{ onFocusLost(); mWindow->getViewController()->goToSystemView(getCursor()->getSystem());