MyGUI  3.0.1
MyGUI_LayoutManager.cpp
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #include "MyGUI_Precompiled.h"
24 #include "MyGUI_ResourceManager.h"
25 #include "MyGUI_LayoutManager.h"
26 #include "MyGUI_SkinManager.h"
27 #include "MyGUI_WidgetManager.h"
28 #include "MyGUI_Widget.h"
29 #include "MyGUI_CoordConverter.h"
31 
32 namespace MyGUI
33 {
34 
35  const std::string XML_TYPE("Layout");
36 
37  MYGUI_INSTANCE_IMPLEMENT( LayoutManager )
38 
39  void LayoutManager::initialise()
40  {
41  MYGUI_ASSERT(!mIsInitialise, INSTANCE_TYPE_NAME << " initialised twice");
42  MYGUI_LOG(Info, "* Initialise: " << INSTANCE_TYPE_NAME);
43 
45  layoutPrefix = "";
46  layoutParent = nullptr;
47 
48  MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully initialized");
49  mIsInitialise = true;
50  }
51 
53  {
54  if (!mIsInitialise) return;
55  MYGUI_LOG(Info, "* Shutdown: " << INSTANCE_TYPE_NAME);
56 
57  ResourceManager::getInstance().unregisterLoadXmlDelegate(XML_TYPE);
58 
59  MYGUI_LOG(Info, INSTANCE_TYPE_NAME << " successfully shutdown");
60  mIsInitialise = false;
61  }
62 
63  VectorWidgetPtr& LayoutManager::load(const std::string& _file)
64  {
65  mVectorWidgetPtr.clear();
66  ResourceManager::getInstance()._loadImplement(_file, true, XML_TYPE, INSTANCE_TYPE_NAME);
67  return mVectorWidgetPtr;
68  }
69 
70  void LayoutManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
71  {
72 #if MYGUI_DEBUG_MODE == 1
73  MYGUI_LOG(Info, "load layout '" << _file << "'");
74 #endif
75  parseLayout(mVectorWidgetPtr, _node);
76  }
77 
78  VectorWidgetPtr& LayoutManager::loadLayout(const std::string& _file, const std::string& _prefix, Widget* _parent)
79  {
80  static VectorWidgetPtr widgets;
81  widgets.clear();
82 
83  layoutPrefix = _prefix;
84  layoutParent = _parent;
85  widgets = load(_file);
86  layoutPrefix = "";
87  layoutParent = nullptr;
88  return widgets;
89  }
90 
92  {
93  WidgetManager::getInstance().destroyWidgets(_widgets);
94  }
95 
96  void LayoutManager::parseLayout(VectorWidgetPtr& _widgets, xml::ElementPtr _root)
97  {
98  // берем детей и крутимся
100  while (widget.next("Widget")) parseWidget(_widgets, widget, layoutParent);
101  }
102 
103  void LayoutManager::parseWidget(VectorWidgetPtr& _widgets, xml::ElementEnumerator& _widget, Widget* _parent)
104  {
105  // парсим атрибуты виджета
106  std::string widgetType, widgetSkin, widgetName, widgetLayer, tmp;
107 
108  _widget->findAttribute("type", widgetType);
109  _widget->findAttribute("skin", widgetSkin);
110  _widget->findAttribute("layer", widgetLayer);
111 
112  Align align = Align::Default;
113  if (_widget->findAttribute("align", tmp)) align = Align::parse(tmp);
114 
115  _widget->findAttribute("name", widgetName);
116  if (!widgetName.empty()) widgetName = layoutPrefix + widgetName;
117 
118  WidgetStyle style = WidgetStyle::Child;
119  if (_widget->findAttribute("style", tmp)) style = WidgetStyle::parse(tmp);
120  if (_parent != nullptr && style != WidgetStyle::Popup) widgetLayer.clear();
121 
122  IntCoord coord;
123  if (_widget->findAttribute("position", tmp)) coord = IntCoord::parse(tmp);
124  else if (_widget->findAttribute("position_real", tmp))
125  {
126  if (_parent == nullptr || style == WidgetStyle::Popup)
128  else
129  coord = CoordConverter::convertFromRelative(FloatCoord::parse(tmp), _parent->getSize());
130  }
131 
132  Widget* wid;
133  if (nullptr == _parent)
134  wid = Gui::getInstance().createWidgetT(widgetType, widgetSkin, coord, align, widgetLayer, widgetName);
135  else
136  wid = _parent->createWidgetT(style, widgetType, widgetSkin, coord, align, widgetLayer, widgetName);
137 
138  if (layoutParent == _parent) _widgets.push_back(wid);
139 
140  // берем детей и крутимся
141  xml::ElementEnumerator node = _widget->getElementEnumerator();
142  while (node.next())
143  {
144  if (node->getName() == "Widget")
145  {
146  parseWidget(_widgets, node, wid);
147  }
148  else if (node->getName() == "Property")
149  {
150  wid->setProperty(node->findAttribute("key"), node->findAttribute("value"));
151  }
152  else if (node->getName() == "UserString")
153  {
154  wid->setUserString(node->findAttribute("key"), node->findAttribute("value"));
155  }
156  else if (node->getName() == "Controller")
157  {
158  const std::string& type = node->findAttribute("type");
160  if (item)
161  {
162  xml::ElementEnumerator prop = node->getElementEnumerator();
163  while (prop.next("Property"))
164  {
165  item->setProperty(prop->findAttribute("key"), prop->findAttribute("value"));
166  }
167  MyGUI::ControllerManager::getInstance().addItem(wid, item);
168  }
169  else
170  {
171  //LOG
172  }
173  }
174 
175  }
176  }
177 
178 } // namespace MyGUI