• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

MyGUI_LayoutManager.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_Common.h"
00025 #include "MyGUI_ResourceManager.h"
00026 #include "MyGUI_LayoutManager.h"
00027 #include "MyGUI_SkinManager.h"
00028 #include "MyGUI_WidgetManager.h"
00029 #include "MyGUI_Widget.h"
00030 #include "MyGUI_CoordConverter.h"
00031 #include "MyGUI_ControllerManager.h"
00032 
00033 namespace MyGUI
00034 {
00035 
00036     const std::string XML_TYPE("Layout");
00037 
00038     MYGUI_INSTANCE_IMPLEMENT(LayoutManager);
00039 
00040     void LayoutManager::initialise()
00041     {
00042         MYGUI_ASSERT(false == mIsInitialise, INSTANCE_TYPE_NAME << " initialised twice");
00043         MYGUI_LOG(Info, "* Initialise: " << INSTANCE_TYPE_NAME);
00044 
00045         ResourceManager::getInstance().registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &LayoutManager::_load);
00046         layoutPrefix = "";
00047         layoutParent = nullptr;
00048 
00049         MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully initialized");
00050         mIsInitialise = true;
00051     }
00052 
00053     void LayoutManager::shutdown()
00054     {
00055         if (false == mIsInitialise) return;
00056         MYGUI_LOG(Info, "* Shutdown: " << INSTANCE_TYPE_NAME);
00057 
00058         ResourceManager::getInstance().unregisterLoadXmlDelegate(XML_TYPE);
00059 
00060         MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully shutdown");
00061         mIsInitialise = false;
00062     }
00063 
00064     VectorWidgetPtr& LayoutManager::load(const std::string& _file)
00065     {
00066         mVectorWidgetPtr.clear();
00067         ResourceManager::getInstance()._loadImplement(_file, true, XML_TYPE, INSTANCE_TYPE_NAME);
00068         return mVectorWidgetPtr;
00069     }
00070 
00071     void LayoutManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
00072     {
00073 #if MYGUI_DEBUG_MODE == 1
00074         MYGUI_LOG(Info, "load layout '" << _file << "'");
00075 #endif
00076         parseLayout(mVectorWidgetPtr, _node);
00077     }
00078 
00079     VectorWidgetPtr& LayoutManager::loadLayout(const std::string& _file, const std::string& _prefix, WidgetPtr _parent/*, const std::string& _group*/)
00080     {
00081         static VectorWidgetPtr widgets;
00082         widgets.clear();
00083 
00084         layoutPrefix = _prefix;
00085         layoutParent = _parent;
00086         widgets = load(_file);
00087         layoutPrefix = "";
00088         layoutParent = nullptr;
00089         return widgets;
00090     }
00091 
00092     void LayoutManager::unloadLayout(VectorWidgetPtr& _widgets)
00093     {
00094         WidgetManager::getInstance().destroyWidgets(_widgets);
00095     }
00096 
00097     void LayoutManager::parseLayout(VectorWidgetPtr& _widgets, xml::ElementPtr _root)
00098     {
00099         // берем детей и крутимся
00100         xml::ElementEnumerator widget = _root->getElementEnumerator();
00101         while (widget.next("Widget")) parseWidget(_widgets, widget, layoutParent);
00102     }
00103 
00104     void LayoutManager::parseWidget(VectorWidgetPtr& _widgets, xml::ElementEnumerator& _widget, WidgetPtr _parent)
00105     {
00106         // парсим атрибуты виджета
00107         std::string widgetType, widgetSkin, widgetName, widgetLayer, tmp;
00108 
00109         _widget->findAttribute("type", widgetType);
00110         _widget->findAttribute("skin", widgetSkin);
00111         _widget->findAttribute("layer", widgetLayer);
00112 
00113         Align align = Align::Default;
00114         if (_widget->findAttribute("align", tmp)) align = Align::parse(tmp);
00115 
00116         _widget->findAttribute("name", widgetName);
00117         if (!widgetName.empty()) widgetName = layoutPrefix + widgetName;
00118 
00119         WidgetStyle style = WidgetStyle::Child;
00120         if (_widget->findAttribute("style", tmp)) style = WidgetStyle::parse(tmp);
00121         if (_parent != nullptr && style != WidgetStyle::Popup) widgetLayer.clear();
00122 
00123         IntCoord coord;
00124         if (_widget->findAttribute("position", tmp)) coord = IntCoord::parse(tmp);
00125         else if (_widget->findAttribute("position_real", tmp))
00126         {
00127             if (_parent == nullptr || style == WidgetStyle::Popup)
00128                 coord = CoordConverter::convertFromRelative(FloatCoord::parse(tmp), Gui::getInstance().getViewSize());
00129             else
00130                 coord = CoordConverter::convertFromRelative(FloatCoord::parse(tmp), _parent->getSize());
00131         }
00132 
00133         WidgetPtr wid;
00134         if (nullptr == _parent)
00135             wid = Gui::getInstance().createWidgetT(widgetType, widgetSkin, coord, align, widgetLayer, widgetName);
00136         else
00137             wid = _parent->createWidgetT(style, widgetType, widgetSkin, coord, align, widgetLayer, widgetName);
00138 
00139         if (layoutParent == _parent) _widgets.push_back(wid);
00140 
00141         // берем детей и крутимся
00142         xml::ElementEnumerator node = _widget->getElementEnumerator();
00143         while (node.next())
00144         {
00145             if (node->getName() == "Widget")
00146             {
00147                 parseWidget(_widgets, node, wid);
00148             }
00149             else if (node->getName() == "Property")
00150             {
00151                 wid->setProperty(node->findAttribute("key"), node->findAttribute("value"));
00152             }
00153             else if (node->getName() == "UserString")
00154             {
00155                 wid->setUserString(node->findAttribute("key"), node->findAttribute("value"));
00156             }
00157             else if (node->getName() == "Controller")
00158             {
00159                 const std::string& type = node->findAttribute("type");
00160                 MyGUI::ControllerItem* item = MyGUI::ControllerManager::getInstance().createItem(type);
00161                 if (item)
00162                 {
00163                     xml::ElementEnumerator prop = node->getElementEnumerator();
00164                     while (prop.next("Property"))
00165                     {
00166                         item->setProperty(prop->findAttribute("key"), prop->findAttribute("value"));
00167                     }
00168                     MyGUI::ControllerManager::getInstance().addItem(wid, item);
00169                 }
00170                 else
00171                 {
00172                     //LOG
00173                 }
00174             }
00175 
00176         }
00177     }
00178 
00179 } // namespace MyGUI

Generated on Sun Jan 30 2011 for MyGUI by  doxygen 1.7.1