MyGUI  3.2.0
MyGUI_LayoutManager.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_LayoutManager.h"
00024 #include "MyGUI_ResourceManager.h"
00025 #include "MyGUI_FactoryManager.h"
00026 #include "MyGUI_WidgetManager.h"
00027 
00028 namespace MyGUI
00029 {
00030 
00031     const std::string XML_TYPE("Layout");
00032     const std::string XML_TYPE_RESOURCE("Resource");
00033 
00034     template <> LayoutManager* Singleton<LayoutManager>::msInstance = nullptr;
00035     template <> const char* Singleton<LayoutManager>::mClassTypeName("LayoutManager");
00036 
00037     LayoutManager::LayoutManager() :
00038         mIsInitialise(false)
00039     {
00040     }
00041 
00042     void LayoutManager::initialise()
00043     {
00044         MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
00045         MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
00046 
00047         ResourceManager::getInstance().registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &LayoutManager::_load);
00048         FactoryManager::getInstance().registerFactory<ResourceLayout>(XML_TYPE_RESOURCE);
00049 
00050         MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
00051         mIsInitialise = true;
00052     }
00053 
00054     void LayoutManager::shutdown()
00055     {
00056         MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
00057         MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
00058 
00059         ResourceManager::getInstance().unregisterLoadXmlDelegate(XML_TYPE);
00060         FactoryManager::getInstance().unregisterFactory<ResourceLayout>(XML_TYPE_RESOURCE);
00061 
00062         MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
00063         mIsInitialise = false;
00064     }
00065 
00066     void LayoutManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
00067     {
00068         ResourceLayout* resource = new ResourceLayout(_node, _file);
00069         ResourceManager::getInstance().addResource(resource);
00070     }
00071 
00072     VectorWidgetPtr LayoutManager::loadLayout(const std::string& _file, const std::string& _prefix, Widget* _parent)
00073     {
00074         mCurrentLayoutName = _file;
00075 
00076         ResourceLayout* resource = getByName(_file, false);
00077         if (!resource)
00078         {
00079             ResourceManager::getInstance().load(_file);
00080             resource = getByName(_file, false);
00081         }
00082 
00083         VectorWidgetPtr result;
00084         if (resource)
00085             result = resource->createLayout(_prefix, _parent);
00086         else
00087             MYGUI_LOG(Warning, "Layout '" << _file << "' couldn't be loaded");
00088 
00089         mCurrentLayoutName = "";
00090 
00091         return result;
00092     }
00093 
00094     void LayoutManager::unloadLayout(VectorWidgetPtr& _widgets)
00095     {
00096         WidgetManager::getInstance().destroyWidgets(_widgets);
00097     }
00098 
00099     ResourceLayout* LayoutManager::getByName(const std::string& _name, bool _throw) const
00100     {
00101         std::string skinName = BackwardCompatibility::getSkinRename(_name);
00102         IResource* result = ResourceManager::getInstance().getByName(skinName, false);
00103 
00104         if (result != nullptr)
00105         {
00106             ResourceLayout* resource = result->castType<ResourceLayout>(false);
00107             if (resource == nullptr)
00108             {
00109                 MYGUI_ASSERT(!_throw, "Resource '" << skinName << "' is not ResourceLayout type");
00110             }
00111             return resource;
00112         }
00113 
00114         MYGUI_ASSERT(!_throw, "ResourceLayout '" << skinName << "' not found");
00115         return nullptr;
00116     }
00117 
00118     const std::string& LayoutManager::getCurrentLayout() const
00119     {
00120         return mCurrentLayoutName;
00121     }
00122 
00123     bool LayoutManager::isExist(const std::string& _name) const
00124     {
00125         return getByName(_name, false) != nullptr;
00126     }
00127 
00128 } // namespace MyGUI