From 989ba9a8f9c772696774d596e2ca73a3c8f876e6 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Sat, 17 Nov 2012 12:39:49 -0600 Subject: [PATCH] Heavily refactored how theming data is accessed. --- changelog.txt | 1 + src/components/GuiGameList.cpp | 55 ++++++------ src/components/GuiTheme.cpp | 151 +++++++++++++++++---------------- src/components/GuiTheme.h | 42 +++------ 4 files changed, 117 insertions(+), 132 deletions(-) diff --git a/changelog.txt b/changelog.txt index 2b206f4d3..29fcf443f 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,6 @@ November 17 -Fixed default game image resizing if none is defined. +-Heavily refactored theming. You shouldn't notice any changes, but if something broke, let me know! November 14 -Added Exit command to the menu. diff --git a/src/components/GuiGameList.cpp b/src/components/GuiGameList.cpp index 7fbb7991a..8e65dbcf2 100644 --- a/src/components/GuiGameList.cpp +++ b/src/components/GuiGameList.cpp @@ -5,9 +5,6 @@ #include "GuiFastSelect.h" #include -//this is just a default value; the true value is in mTheme->getListOffsetX(); -const float GuiGameList::sInfoWidth = 0.5; - GuiGameList::GuiGameList(bool useDetail) { @@ -21,10 +18,10 @@ GuiGameList::GuiGameList(bool useDetail) //Those with smaller displays may prefer the older view. if(mDetailed) { - mList = new GuiList(Renderer::getScreenWidth() * sInfoWidth, Renderer::getDefaultFont(Renderer::LARGE)->getHeight() + 2, Renderer::getDefaultFont(Renderer::MEDIUM)); + mList = new GuiList(Renderer::getScreenWidth() * mTheme->getFloat("listOffsetX"), Renderer::getDefaultFont(Renderer::LARGE)->getHeight() + 2, Renderer::getDefaultFont(Renderer::MEDIUM)); - mScreenshot = new GuiImage(Renderer::getScreenWidth() * mTheme->getGameImageOffsetX(), Renderer::getScreenHeight() * mTheme->getGameImageOffsetY(), "", mTheme->getGameImageWidth(), mTheme->getGameImageHeight(), false); - mScreenshot->setOrigin(mTheme->getGameImageOriginX(), mTheme->getGameImageOriginY()); + mScreenshot = new GuiImage(Renderer::getScreenWidth() * mTheme->getFloat("gameImageOffsetX"), Renderer::getScreenHeight() * mTheme->getFloat("gameImageOffsetY"), "", mTheme->getFloat("gameImageWidth"), mTheme->getFloat("gameImageHeight"), false); + mScreenshot->setOrigin(mTheme->getFloat("gameImageOriginX"), mTheme->getFloat("gameImageOriginY")); //addChild(mScreenshot); //the animation renders the screenshot @@ -93,14 +90,14 @@ void GuiGameList::onRender() mTheme->render(); //header - if(!mTheme->getHeaderHidden()) + if(!mTheme->getBool("hideHeader")) Renderer::drawCenteredText(mSystem->getName(), 0, 1, 0xFF0000FF, Renderer::getDefaultFont(Renderer::LARGE)); if(mDetailed) { //divider - if(!mTheme->getDividersHidden()) - Renderer::drawRect(Renderer::getScreenWidth() * mTheme->getListOffsetX() - 4, Renderer::getDefaultFont(Renderer::LARGE)->getHeight() + 2, 8, Renderer::getScreenHeight(), 0x0000FFFF); + if(!mTheme->getBool("hideDividers")) + Renderer::drawRect(Renderer::getScreenWidth() * mTheme->getFloat("listOffsetX") - 4, Renderer::getDefaultFont(Renderer::LARGE)->getHeight() + 2, 8, Renderer::getScreenHeight(), 0x0000FFFF); //if we're not scrolling and we have selected a non-folder if(!mList->isScrolling() && mList->getSelectedObject() && !mList->getSelectedObject()->isFolder()) @@ -109,7 +106,7 @@ void GuiGameList::onRender() std::string desc = game->getDescription(); if(!desc.empty()) - Renderer::drawWrappedText(desc, Renderer::getScreenWidth() * 0.03, mScreenshot->getOffsetY() + mScreenshot->getHeight() + 12, Renderer::getScreenWidth() * (mTheme->getListOffsetX() - 0.03), mTheme->getDescColor(), mTheme->getDescriptionFont()); + Renderer::drawWrappedText(desc, Renderer::getScreenWidth() * 0.03, mScreenshot->getOffsetY() + mScreenshot->getHeight() + 12, Renderer::getScreenWidth() * (mTheme->getFloat("listOffsetX") - 0.03), mTheme->getColor("description"), mTheme->getDescriptionFont()); } } } @@ -121,7 +118,7 @@ void GuiGameList::onInput(InputManager::InputButton button, bool keyDown) if(!keyDown) { //play select sound - mTheme->getMenuSelectSound()->play(); + mTheme->getSound("menuSelect")->play(); FileData* file = mList->getSelectedObject(); if(file->isFolder()) //if you selected a folder, add this directory to the stack, and use the selected one @@ -133,7 +130,7 @@ void GuiGameList::onInput(InputManager::InputButton button, bool keyDown) mList->stopScrolling(); //wait for the sound to finish or we'll never hear it... - while(mTheme->getMenuSelectSound()->isPlaying()); + while(mTheme->getSound("menuSelect")->isPlaying()); mSystem->launchGame((GameData*)file); } @@ -149,7 +146,7 @@ void GuiGameList::onInput(InputManager::InputButton button, bool keyDown) updateDetailData(); //play the back sound - mTheme->getMenuBackSound()->play(); + mTheme->getSound("menuBack")->play(); } //only allow switching systems if more than one exists (otherwise it'll reset your position when you switch and it's annoying) @@ -174,7 +171,7 @@ void GuiGameList::onInput(InputManager::InputButton button, bool keyDown) //open the fast select menu if(button == InputManager::SELECT && keyDown) { - new GuiFastSelect(this, mList, mList->getSelectedObject()->getName()[0], mTheme->getBoxData(), mTheme->getFastSelectColor(), mTheme->getMenuScrollSound()); + new GuiFastSelect(this, mList, mList->getSelectedObject()->getName()[0], mTheme->getBoxData(), mTheme->getColor("fastSelect"), mTheme->getSound("menuScroll")); } if(mDetailed) @@ -201,9 +198,9 @@ void GuiGameList::updateList() FileData* file = mFolder->getFile(i); if(file->isFolder()) - mList->addObject(file->getName(), file, mTheme->getSecondaryColor()); + mList->addObject(file->getName(), file, mTheme->getColor("secondary")); else - mList->addObject(file->getName(), file, mTheme->getPrimaryColor()); + mList->addObject(file->getName(), file, mTheme->getColor("primary")); } } @@ -223,24 +220,24 @@ void GuiGameList::updateTheme() else mTheme->readXML(""); //clears any current theme - mList->setSelectorColor(mTheme->getSelectorColor()); - mList->setSelectedTextColor(mTheme->getSelectedTextColor()); - mList->setScrollSound(mTheme->getMenuScrollSound()); + mList->setSelectorColor(mTheme->getColor("selector")); + mList->setSelectedTextColor(mTheme->getColor("selected")); + mList->setScrollSound(mTheme->getSound("menuScroll")); //fonts mList->setFont(mTheme->getListFont()); if(mDetailed) { - mList->setCentered(mTheme->getListCentered()); + mList->setCentered(mTheme->getBool("listCentered")); - mList->setOffsetX(mTheme->getListOffsetX() * Renderer::getScreenWidth()); - mList->setTextOffsetX(mTheme->getListTextOffsetX() * Renderer::getScreenWidth()); + mList->setOffsetX(mTheme->getFloat("listOffsetX") * Renderer::getScreenWidth()); + mList->setTextOffsetX(mTheme->getFloat("listTextOffsetX") * Renderer::getScreenWidth()); - mScreenshot->setOffsetX(mTheme->getGameImageOffsetX() * Renderer::getScreenWidth()); - mScreenshot->setOffsetY(mTheme->getGameImageOffsetY() * Renderer::getScreenHeight()); - mScreenshot->setOrigin(mTheme->getGameImageOriginX(), mTheme->getGameImageOriginY()); - mScreenshot->setResize(mTheme->getGameImageWidth(), mTheme->getGameImageHeight(), false); + mScreenshot->setOffsetX(mTheme->getFloat("gameImageOffsetX") * Renderer::getScreenWidth()); + mScreenshot->setOffsetY(mTheme->getFloat("gameImageOffsetY") * Renderer::getScreenHeight()); + mScreenshot->setOrigin(mTheme->getFloat("gameImageOriginX"), mTheme->getFloat("gameImageOriginY")); + mScreenshot->setResize(mTheme->getFloat("gameImageWidth"), mTheme->getFloat("gameImageHeight"), false); } } @@ -251,10 +248,10 @@ void GuiGameList::updateDetailData() if(mList->getSelectedObject() && !mList->getSelectedObject()->isFolder()) { - mScreenshot->setOffset((mTheme->getGameImageOffsetX() - 0.05) * Renderer::getScreenWidth(), mTheme->getGameImageOffsetY() * Renderer::getScreenHeight()); + mScreenshot->setOffset((mTheme->getFloat("gameImageOffsetX") - 0.05) * Renderer::getScreenWidth(), mTheme->getFloat("gameImageOffsetY") * Renderer::getScreenHeight()); if(((GameData*)mList->getSelectedObject())->getImagePath().empty()) - mScreenshot->setImage(mTheme->getImageNotFoundPath()); + mScreenshot->setImage(mTheme->getString("imageNotFoundPath")); else mScreenshot->setImage(((GameData*)mList->getSelectedObject())->getImagePath()); @@ -277,7 +274,7 @@ void GuiGameList::clearDetailData() void GuiGameList::onPause() { mList->stopScrolling(); - mTheme->getMenuOpenSound()->play(); + mTheme->getSound("menuOpenSound")->play(); InputManager::unregisterComponent(this); } diff --git a/src/components/GuiTheme.cpp b/src/components/GuiTheme.cpp index 2af0f4e1a..48e701ac1 100644 --- a/src/components/GuiTheme.cpp +++ b/src/components/GuiTheme.cpp @@ -7,35 +7,36 @@ #include #include "../Renderer.h" -unsigned int GuiTheme::getPrimaryColor() { return mListPrimaryColor; } -unsigned int GuiTheme::getSecondaryColor() { return mListSecondaryColor; } -unsigned int GuiTheme::getSelectorColor() { return mListSelectorColor; } -unsigned int GuiTheme::getDescColor() { return mDescColor; } -unsigned int GuiTheme::getFastSelectColor() { return mFastSelectColor; } -bool GuiTheme::getHeaderHidden() { return mHideHeader; } -bool GuiTheme::getDividersHidden() { return mHideDividers; } -bool GuiTheme::getListCentered() { return mListCentered; } -float GuiTheme::getListOffsetX() { return mListOffsetX; } -float GuiTheme::getListTextOffsetX() { return mListTextOffsetX; } +//really, this should all be refactored into "getColor("attribName")" and such, +//but it's a little late now. -unsigned int GuiTheme::getSelectedTextColor() { return mListSelectedColor; } +unsigned int GuiTheme::getColor(std::string name) +{ + return mColorMap[name]; +} + +bool GuiTheme::getBool(std::string name) +{ + return mBoolMap[name]; +} + +float GuiTheme::getFloat(std::string name) +{ + return mFloatMap[name]; +} + +Sound* GuiTheme::getSound(std::string name) +{ + return mSoundMap[name]; +} + +std::string GuiTheme::getString(std::string name) +{ + return mStringMap[name]; +} GuiBoxData GuiTheme::getBoxData() { return mBoxData; } -Sound* GuiTheme::getMenuScrollSound() { return &mMenuScrollSound; } -Sound* GuiTheme::getMenuSelectSound() { return &mMenuSelectSound; } -Sound* GuiTheme::getMenuBackSound() { return &mMenuBackSound; } -Sound* GuiTheme::getMenuOpenSound() { return &mMenuOpenSound; } - -float GuiTheme::getGameImageOffsetX() { return mGameImageOffsetX; } -float GuiTheme::getGameImageOffsetY() { return mGameImageOffsetY; } -float GuiTheme::getGameImageWidth() { return mGameImageWidth; } -float GuiTheme::getGameImageHeight() { return mGameImageHeight; } -float GuiTheme::getGameImageOriginX() { return mGameImageOriginX; } -float GuiTheme::getGameImageOriginY() { return mGameImageOriginY; } - -std::string GuiTheme::getImageNotFoundPath() { return mImageNotFoundPath; } - Font* GuiTheme::getListFont() { if(mListFont == NULL) @@ -54,6 +55,11 @@ Font* GuiTheme::getDescriptionFont() GuiTheme::GuiTheme(std::string path) { + mSoundMap["menuScroll"] = new Sound(); + mSoundMap["menuSelect"] = new Sound(); + mSoundMap["menuBack"] = new Sound(); + mSoundMap["menuOpen"] = new Sound(); + mListFont = NULL; mDescFont = NULL; @@ -70,25 +76,32 @@ GuiTheme::~GuiTheme() void GuiTheme::setDefaults() { - mListPrimaryColor = 0x0000FFFF; - mListSecondaryColor = 0x00FF00FF; - mListSelectorColor = 0x000000FF; - mListSelectedColor = 0xFF0000FF; - mDescColor = 0x0000FFFF; - mFastSelectColor = 0xFF0000FF; - mHideHeader = false; - mHideDividers = false; - mListCentered = true; + mColorMap["primary"] = 0x0000FFFF; + mColorMap["secondary"] = 0x00FF00FF; + mColorMap["selector"] = 0x000000FF; + mColorMap["selected"] = 0xFF0000FF; + mColorMap["description"] = 0x0000FFFF; + mColorMap["fastSelect"] = 0xFF0000FF; - mListOffsetX = 0.5; - mListTextOffsetX = 0.005; + mBoolMap["hideHeader"] = false; + mBoolMap["hideDividers"] = false; + mBoolMap["listCentered"] = true; - mGameImageOriginX = 0.5; - mGameImageOriginY = 0; - mGameImageOffsetX = mListOffsetX / 2; - mGameImageOffsetY = (float)Renderer::getDefaultFont(Renderer::LARGE)->getHeight() / Renderer::getScreenHeight(); - mGameImageWidth = mListOffsetX; - mGameImageHeight = 0; + mFloatMap["listOffsetX"] = 0.5; + mFloatMap["listTextOffsetX"] = 0.005; + mFloatMap["gameImageOriginX"] = 0.5; + mFloatMap["gameImageOriginY"] = 0; + mFloatMap["gameImageOffsetX"] = mFloatMap["listOffsetX"] / 2; + mFloatMap["gameImageOffsetY"] = (float)Renderer::getDefaultFont(Renderer::LARGE)->getHeight() / (float)Renderer::getScreenHeight(); + mFloatMap["gameImageWidth"] = mFloatMap["listOffsetX"]; + mFloatMap["gameImageHeight"] = 0; + + mSoundMap["menuScroll"]->loadFile(""); + mSoundMap["menuSelect"]->loadFile(""); + mSoundMap["menuBack"]->loadFile(""); + mSoundMap["menuOpen"]->loadFile(""); + + mStringMap["imageNotFoundPath"] = ""; mBoxData.backgroundPath = ""; mBoxData.backgroundTiled = false; @@ -98,13 +111,6 @@ void GuiTheme::setDefaults() mBoxData.verticalTiled = false; mBoxData.cornerPath = ""; - mMenuScrollSound.loadFile(""); - mMenuSelectSound.loadFile(""); - mMenuBackSound.loadFile(""); - mMenuOpenSound.loadFile(""); - - mImageNotFoundPath = ""; - if(mListFont != NULL) { delete mListFont; @@ -160,14 +166,15 @@ void GuiTheme::readXML(std::string path) pugi::xml_node root = doc.child("theme"); //load non-component theme stuff - mListPrimaryColor = resolveColor(root.child("listPrimaryColor").text().get(), mListPrimaryColor); - mListSecondaryColor = resolveColor(root.child("listSecondaryColor").text().get(), mListSecondaryColor); - mListSelectorColor = resolveColor(root.child("listSelectorColor").text().get(), mListSelectorColor); - mListSelectedColor = resolveColor(root.child("listSelectedColor").text().get(), mListPrimaryColor); - mDescColor = resolveColor(root.child("descColor").text().get(), mDescColor); - mFastSelectColor = resolveColor(root.child("fastSelectColor").text().get(), mFastSelectColor); - mHideHeader = root.child("hideHeader"); - mHideDividers = root.child("hideDividers"); + mColorMap["primary"] = resolveColor(root.child("listPrimaryColor").text().get(), mColorMap["primary"]); + mColorMap["secondary"] = resolveColor(root.child("listSecondaryColor").text().get(), mColorMap["secondary"]); + mColorMap["selector"] = resolveColor(root.child("listSelectorColor").text().get(), mColorMap["selector"]); + mColorMap["selected"] = resolveColor(root.child("listSelectedColor").text().get(), mColorMap["primary"]); + mColorMap["description"] = resolveColor(root.child("descColor").text().get(), mColorMap["description"]); + mColorMap["fastSelect"] = resolveColor(root.child("fastSelectColor").text().get(), mColorMap["fastSelect"]); + + mBoolMap["hideHeader"] = root.child("hideHeader"); + mBoolMap["hideDividers"] = root.child("hideDividers"); //GuiBox theming data mBoxData.backgroundPath = expandPath(root.child("boxBackground").text().get()); @@ -179,9 +186,9 @@ void GuiTheme::readXML(std::string path) mBoxData.cornerPath = expandPath(root.child("boxCorner").text().get()); //list stuff - mListCentered = !root.child("listLeftAlign"); - mListOffsetX = strToFloat(root.child("listOffsetX").text().get(), mListOffsetX); - mListTextOffsetX = strToFloat(root.child("listTextOffsetX").text().get(), mListTextOffsetX); + mBoolMap["listCentered"] = !root.child("listLeftAlign"); + mFloatMap["listOffsetX"] = strToFloat(root.child("listOffsetX").text().get(), mFloatMap["listOffsetX"]); + mFloatMap["listTextOffsetX"] = strToFloat(root.child("listTextOffsetX").text().get(), mFloatMap["listTextOffsetX"]); //game image stuff std::string artPos = root.child("gameImagePos").text().get(); @@ -193,20 +200,20 @@ void GuiTheme::readXML(std::string path) splitString(artDim, ' ', &artWidth, &artHeight); splitString(artOrigin, ' ', &artOriginX, &artOriginY); - mGameImageOffsetX = resolveExp(artPosX, mGameImageOffsetX); - mGameImageOffsetY = resolveExp(artPosY, mGameImageOffsetY); - mGameImageWidth = resolveExp(artWidth, mGameImageWidth); - mGameImageHeight = resolveExp(artHeight, mGameImageHeight); - mGameImageOriginX = resolveExp(artOriginX, mGameImageOriginX); - mGameImageOriginY = resolveExp(artOriginY, mGameImageOriginY); + mFloatMap["gameImageOffsetX"] = resolveExp(artPosX, mFloatMap["gameImageOffsetX"]); + mFloatMap["gameImageOffsetY"] = resolveExp(artPosY, mFloatMap["gameImageOffsetY"]); + mFloatMap["gameImageWidth"] = resolveExp(artWidth, mFloatMap["gameImageWidth"]); + mFloatMap["gameImageHeight"] = resolveExp(artHeight, mFloatMap["gameImageHeight"]); + mFloatMap["gameImageOriginX"] = resolveExp(artOriginX, mFloatMap["gameImageOriginX"]); + mFloatMap["gameImageOriginY"] = resolveExp(artOriginY, mFloatMap["gameImageOriginY"]); - mImageNotFoundPath = expandPath(root.child("gameImageNotFound").text().get()); + mStringMap["imageNotFoundPath"] = expandPath(root.child("gameImageNotFound").text().get()); //sounds - mMenuScrollSound.loadFile(expandPath(root.child("menuScrollSound").text().get())); - mMenuSelectSound.loadFile(expandPath(root.child("menuSelectSound").text().get())); - mMenuBackSound.loadFile(expandPath(root.child("menuBackSound").text().get())); - mMenuOpenSound.loadFile(expandPath(root.child("menuOpenSound").text().get())); + mSoundMap["menuScroll"]->loadFile(expandPath(root.child("menuScrollSound").text().get())); + mSoundMap["menuSelect"]->loadFile(expandPath(root.child("menuSelectSound").text().get())); + mSoundMap["menuBack"]->loadFile(expandPath(root.child("menuBackSound").text().get())); + mSoundMap["menuOpen"]->loadFile(expandPath(root.child("menuOpenSound").text().get())); //fonts mListFont = resolveFont(root.child("listFont"), Font::getDefaultPath(), Renderer::getDefaultFont(Renderer::MEDIUM)->getSize()); @@ -305,7 +312,7 @@ float GuiTheme::resolveExp(std::string str, float defaultVal) //set variables exp.setVariable("headerHeight", Renderer::getDefaultFont(Renderer::LARGE)->getHeight() / Renderer::getScreenHeight()); - exp.setVariable("infoWidth", mListOffsetX); + exp.setVariable("infoWidth", mFloatMap["listOffsetX"]); return exp.eval(); } diff --git a/src/components/GuiTheme.h b/src/components/GuiTheme.h index b1a18bc38..514289f6f 100644 --- a/src/components/GuiTheme.h +++ b/src/components/GuiTheme.h @@ -16,34 +16,13 @@ public: void readXML(std::string path); - unsigned int getPrimaryColor(); - unsigned int getSecondaryColor(); - unsigned int getSelectorColor(); - unsigned int getSelectedTextColor(); - unsigned int getDescColor(); - unsigned int getFastSelectColor(); - bool getHeaderHidden(); - bool getDividersHidden(); - bool getListCentered(); - - float getListOffsetX(); - float getListTextOffsetX(); - - float getGameImageOffsetX(); - float getGameImageOffsetY(); - float getGameImageWidth(); - float getGameImageHeight(); - float getGameImageOriginX(); - float getGameImageOriginY(); - GuiBoxData getBoxData(); - Sound* getMenuScrollSound(); - Sound* getMenuSelectSound(); - Sound* getMenuBackSound(); - Sound* getMenuOpenSound(); - - std::string getImageNotFoundPath(); + unsigned int getColor(std::string name); + bool getBool(std::string name); + float getFloat(std::string name); + Sound* getSound(std::string name); + std::string getString(std::string name); Font* getListFont(); Font* getDescriptionFont(); @@ -64,12 +43,13 @@ private: std::vector mComponentVector; std::string mPath; - unsigned int mListPrimaryColor, mListSecondaryColor, mListSelectorColor, mListSelectedColor, mDescColor, mFastSelectColor; - bool mHideHeader, mHideDividers, mListCentered; - float mListOffsetX, mListTextOffsetX, mGameImageOffsetX, mGameImageOffsetY, mGameImageWidth, mGameImageHeight, mGameImageOriginX, mGameImageOriginY; + std::map mColorMap; + std::map mBoolMap; + std::map mFloatMap; + std::map mSoundMap; + std::map mStringMap; + GuiBoxData mBoxData; - Sound mMenuScrollSound, mMenuSelectSound, mMenuBackSound, mMenuOpenSound; - std::string mImageNotFoundPath; Font* mListFont; Font* mDescFont; };