Changed the sound cache to be part of the Sound class instead of

ThemeData.
This commit is contained in:
Aloshi 2014-01-03 10:40:36 -06:00
parent 8a52866ca6
commit 8d1ac3087e
10 changed files with 65 additions and 34 deletions

View file

@ -2,6 +2,35 @@
#include "AudioManager.h"
#include "Log.h"
#include "Settings.h"
#include "ThemeData.h"
std::map< std::string, std::shared_ptr<Sound> > Sound::sMap;
std::shared_ptr<Sound> Sound::get(const std::string& path)
{
auto it = sMap.find(path);
if(it != sMap.end())
return it->second;
std::shared_ptr<Sound> sound = std::shared_ptr<Sound>(new Sound(path));
AudioManager::getInstance()->registerSound(sound);
sMap[path] = sound;
return sound;
}
std::shared_ptr<Sound> Sound::getFromTheme(const std::shared_ptr<ThemeData>& 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<std::string>("path"));
}
Sound::Sound(const std::string & path) : mSampleData(NULL), mSamplePos(0), mSampleLength(0), playing(false)
{

View file

@ -2,8 +2,11 @@
#define _SOUND_H_
#include <string>
#include <map>
#include <memory>
#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<Sound> get(const std::string& path);
static std::shared_ptr<Sound> getFromTheme(const std::shared_ptr<ThemeData>& 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<Sound> > sMap;
};
#endif

View file

@ -33,7 +33,8 @@ std::map< std::string, std::map<std::string, ThemeData::ElementPropertyType> > 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>& ThemeData::getDefault()
{
const ThemeElement* elem = getElement("common", elementName, "sound");
if(!elem)
return;
if(elem->has("path"))
static std::shared_ptr<ThemeData> theme = nullptr;
if(theme == nullptr)
{
const std::string path = elem->get<std::string>("path");
auto cacheIt = mSoundCache.find(path);
if(cacheIt != mSoundCache.end())
{
cacheIt->second->play();
return;
}
std::shared_ptr<Sound> sound = std::shared_ptr<Sound>(new Sound(path));
sound->play();
mSoundCache[path] = sound;
theme = std::shared_ptr<ThemeData>(new ThemeData());
theme->loadFile(getHomePath() + "/.emulationstation/es_theme_default.xml");
}
}
return theme;
}
std::vector<GuiComponent*> ThemeData::makeExtras(const std::shared_ptr<ThemeData>& theme, const std::string& view, Window* window)
{

View file

@ -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<GuiComponent*> makeExtras(const std::shared_ptr<ThemeData>& theme, const std::string& view, Window* window);
static const std::shared_ptr<ThemeData>& getDefault();
private:
static std::map< std::string, std::map<std::string, ElementPropertyType> > sElementMap;
@ -146,8 +146,6 @@ private:
void parseElement(const pugi::xml_node& elementNode, const std::map<std::string, ElementPropertyType>& typeMap, ThemeElement& element);
std::map<std::string, ThemeView> mViews;
std::map< std::string, std::shared_ptr<Sound> > mSoundCache;
};
// okay ideas for applying themes to GuiComponents:

View file

@ -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<ThemeData>();
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)

View file

@ -69,6 +69,7 @@ public:
inline void setSelectedColor(unsigned int color) { mSelectedColor = color; }
inline void setScrollSound(const std::shared_ptr<Sound>& sound) { mScrollSound = sound; }
inline void setColor(unsigned int id, unsigned int color) { mColors[id] = color; }
inline void setSound(const std::shared_ptr<Sound>& sound) { mScrollSound = sound; }
private:
static const int MARQUEE_DELAY = 900;
@ -429,6 +430,9 @@ void TextListComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme, c
}
setFont(Font::getFromTheme(elem, properties, mFont));
if(properties & SOUND && elem->has("scrollSound"))
setSound(Sound::get(elem->get<std::string>("scrollSound")));
}
#endif

View file

@ -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;
}

View file

@ -19,7 +19,7 @@ void BasicGameListView::onThemeChanged(const std::shared_ptr<ThemeData>& 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)

View file

@ -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)
{

View file

@ -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());