MyGUI  3.2.1
MyGUI_SkinManager.cpp
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #include "MyGUI_Precompiled.h"
00008 #include "MyGUI_SkinManager.h"
00009 #include "MyGUI_LanguageManager.h"
00010 #include "MyGUI_ResourceSkin.h"
00011 #include "MyGUI_XmlDocument.h"
00012 #include "MyGUI_SubWidgetManager.h"
00013 #include "MyGUI_Gui.h"
00014 #include "MyGUI_DataManager.h"
00015 #include "MyGUI_FactoryManager.h"
00016 #include "MyGUI_IStateInfo.h"
00017 #include "MyGUI_LayoutManager.h"
00018 #include "MyGUI_BackwardCompatibility.h"
00019 
00020 namespace MyGUI
00021 {
00022 
00023     template <> SkinManager* Singleton<SkinManager>::msInstance = nullptr;
00024     template <> const char* Singleton<SkinManager>::mClassTypeName = "SkinManager";
00025 
00026     SkinManager::SkinManager() :
00027         mIsInitialise(false),
00028         mXmlSkinTagName("Skin"),
00029         mXmlDefaultSkinValue("Default")
00030     {
00031     }
00032 
00033     void SkinManager::initialise()
00034     {
00035         MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
00036         MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
00037 
00038         ResourceManager::getInstance().registerLoadXmlDelegate(mXmlSkinTagName) = newDelegate(this, &SkinManager::_load);
00039 
00040         std::string resourceCategory = ResourceManager::getInstance().getCategoryName();
00041         FactoryManager::getInstance().registerFactory<ResourceSkin>(resourceCategory);
00042 
00043         mDefaultName = "skin_Default";
00044         createDefault(mDefaultName);
00045 
00046         MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
00047         mIsInitialise = true;
00048     }
00049 
00050     void SkinManager::shutdown()
00051     {
00052         MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
00053         MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
00054 
00055         ResourceManager::getInstance().unregisterLoadXmlDelegate(mXmlSkinTagName);
00056 
00057         std::string resourceCategory = ResourceManager::getInstance().getCategoryName();
00058         FactoryManager::getInstance().unregisterFactory<ResourceSkin>(resourceCategory);
00059 
00060         MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
00061         mIsInitialise = false;
00062     }
00063 
00064     void SkinManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
00065     {
00066 #ifndef MYGUI_DONT_USE_OBSOLETE
00067         loadOldSkinFormat(_node, _file, _version, mXmlSkinTagName);
00068 #endif // MYGUI_DONT_USE_OBSOLETE
00069     }
00070 
00071     void SkinManager::createDefault(const std::string& _value)
00072     {
00073         std::string resourceCategory = ResourceManager::getInstance().getCategoryName();
00074         ResourceSkin* skin = FactoryManager::getInstance().createObject<ResourceSkin>(resourceCategory);
00075 
00076         skin->setResourceName(_value);
00077         ResourceManager::getInstance().addResource(skin);
00078     }
00079 
00080     ResourceSkin* SkinManager::getByName(const std::string& _name) const
00081     {
00082         std::string skinName = BackwardCompatibility::getSkinRename(_name);
00083         IResource* result = nullptr;
00084         if (!skinName.empty() && skinName != mXmlDefaultSkinValue)
00085             result = ResourceManager::getInstance().getByName(skinName, false);
00086 
00087         if (result == nullptr)
00088         {
00089             result = ResourceManager::getInstance().getByName(mDefaultName, false);
00090             if (!skinName.empty() && skinName != mXmlDefaultSkinValue)
00091             {
00092                 MYGUI_LOG(Error, "Skin '" << skinName << "' not found. Replaced with default skin." << " [" << LayoutManager::getInstance().getCurrentLayout() << "]");
00093             }
00094         }
00095 
00096         return result ? result->castType<ResourceSkin>(false) : nullptr;
00097     }
00098 
00099     bool SkinManager::isExist(const std::string& _name) const
00100     {
00101         std::string skinName = BackwardCompatibility::getSkinRename(_name);
00102         IResource* result = ResourceManager::getInstance().getByName(skinName, false);
00103         return (result != nullptr) && (result->isType<ResourceSkin>());
00104     }
00105 
00106     void SkinManager::setDefaultSkin(const std::string& _value)
00107     {
00108         mDefaultName = _value;
00109     }
00110 
00111     const std::string SkinManager::getDefaultSkin() const
00112     {
00113         return mDefaultName;
00114     }
00115 
00116 } // namespace MyGUI