MyGUI  3.2.0
MyGUI_SkinManager.cpp
Go to the documentation of this file.
00001 
00006 /*
00007     This file is part of MyGUI.
00008 
00009     MyGUI is free software: you can redistribute it and/or modify
00010     it under the terms of the GNU Lesser General Public License as published by
00011     the Free Software Foundation, either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     MyGUI is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 #include "MyGUI_Precompiled.h"
00023 #include "MyGUI_SkinManager.h"
00024 #include "MyGUI_LanguageManager.h"
00025 #include "MyGUI_ResourceSkin.h"
00026 #include "MyGUI_XmlDocument.h"
00027 #include "MyGUI_SubWidgetManager.h"
00028 #include "MyGUI_Gui.h"
00029 #include "MyGUI_DataManager.h"
00030 #include "MyGUI_FactoryManager.h"
00031 #include "MyGUI_IStateInfo.h"
00032 #include "MyGUI_LayoutManager.h"
00033 #include "MyGUI_BackwardCompatibility.h"
00034 
00035 namespace MyGUI
00036 {
00037 
00038     const std::string XML_TYPE("Skin");
00039     const std::string XML_TYPE_RESOURCE("Resource");
00040     const std::string RESOURCE_DEFAULT_NAME("Default");
00041 
00042     template <> SkinManager* Singleton<SkinManager>::msInstance = nullptr;
00043     template <> const char* Singleton<SkinManager>::mClassTypeName("SkinManager");
00044 
00045     SkinManager::SkinManager() :
00046         mIsInitialise(false)
00047     {
00048     }
00049 
00050     void SkinManager::initialise()
00051     {
00052         MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
00053         MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
00054 
00055         ResourceManager::getInstance().registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &SkinManager::_load);
00056         FactoryManager::getInstance().registerFactory<ResourceSkin>(XML_TYPE_RESOURCE);
00057 
00058         mDefaultName = "skin_Default";
00059         createDefault(mDefaultName);
00060 
00061         MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
00062         mIsInitialise = true;
00063     }
00064 
00065     void SkinManager::shutdown()
00066     {
00067         MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
00068         MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
00069 
00070         ResourceManager::getInstance().unregisterLoadXmlDelegate(XML_TYPE);
00071         FactoryManager::getInstance().unregisterFactory<ResourceSkin>(XML_TYPE_RESOURCE);
00072 
00073         MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
00074         mIsInitialise = false;
00075     }
00076 
00077     void SkinManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
00078     {
00079         // берем детей и крутимся, основной цикл со скинами
00080         xml::ElementEnumerator skin = _node->getElementEnumerator();
00081         while (skin.next(XML_TYPE))
00082         {
00083             /*std::string name = */skin->findAttribute("name");
00084             std::string type = skin->findAttribute("type");
00085             if (type.empty())
00086                 type = "ResourceSkin";
00087 
00088             IObject* object = FactoryManager::getInstance().createObject(XML_TYPE_RESOURCE, type);
00089             if (object != nullptr)
00090             {
00091                 ResourceSkin* data = object->castType<ResourceSkin>();
00092                 data->deserialization(skin.current(), _version);
00093 
00094                 ResourceManager::getInstance().addResource(data);
00095             }
00096         }
00097     }
00098 
00099     void SkinManager::createDefault(const std::string& _value)
00100     {
00101         xml::Document doc;
00102         xml::ElementPtr root = doc.createRoot("MyGUI");
00103         xml::ElementPtr newnode = root->createChild("Resource");
00104         newnode->addAttribute("type", ResourceSkin::getClassTypeName());
00105         newnode->addAttribute("name", _value);
00106 
00107         ResourceManager::getInstance().loadFromXmlNode(root, "", Version());
00108     }
00109 
00110     ResourceSkin* SkinManager::getByName(const std::string& _name) const
00111     {
00112         std::string skinName = BackwardCompatibility::getSkinRename(_name);
00113         IResource* result = nullptr;
00114         if (!skinName.empty() && skinName != RESOURCE_DEFAULT_NAME)
00115             result = ResourceManager::getInstance().getByName(skinName, false);
00116 
00117         if (result == nullptr)
00118         {
00119             result = ResourceManager::getInstance().getByName(mDefaultName, false);
00120             if (!skinName.empty() && skinName != RESOURCE_DEFAULT_NAME)
00121             {
00122                 MYGUI_LOG(Error, "Skin '" << skinName << "' not found. Replaced with default skin." << " [" << LayoutManager::getInstance().getCurrentLayout() << "]");
00123             }
00124         }
00125 
00126         return result ? result->castType<ResourceSkin>(false) : nullptr;
00127     }
00128 
00129     bool SkinManager::isExist(const std::string& _name) const
00130     {
00131         std::string skinName = BackwardCompatibility::getSkinRename(_name);
00132         IResource* result = ResourceManager::getInstance().getByName(skinName, false);
00133         return (result != nullptr) && (result->isType<ResourceSkin>());
00134     }
00135 
00136     void SkinManager::setDefaultSkin(const std::string& _value)
00137     {
00138         mDefaultName = _value;
00139     }
00140 
00141     const std::string SkinManager::getDefaultSkin() const
00142     {
00143         return mDefaultName;
00144     }
00145 
00146 } // namespace MyGUI