MyGUI 3.0.1

MyGUI_SkinManager.cpp

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