MyGUI  3.2.0
MyGUI_ResourceLayout.cpp
Go to the documentation of this file.
1 
6 /*
7  This file is part of MyGUI.
8 
9  MyGUI is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  MyGUI is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
23 #include "MyGUI_ResourceLayout.h"
24 #include "MyGUI_CoordConverter.h"
25 #include "MyGUI_RenderManager.h"
27 #include "MyGUI_LayoutManager.h"
28 #include "MyGUI_Widget.h"
29 #include "MyGUI_Gui.h"
30 
31 namespace MyGUI
32 {
33 
35  {
36  }
37 
38  ResourceLayout::ResourceLayout(xml::ElementPtr _node, const std::string& _fileName)
39  {
40  // FIXME hardcoded version
41  deserialization(_node, Version(1, 0, 0));
42  mResourceName = _fileName;
43  }
44 
46  {
47  Base::deserialization(_node, _version);
48 
49  mLayoutData.clear();
50 
52  while (widget.next("Widget"))
53  mLayoutData.push_back(parseWidget(widget));
54  }
55 
57  {
58  WidgetInfo widgetInfo;
59 
60  std::string tmp;
61 
62  _widget->findAttribute("type", widgetInfo.type);
63  _widget->findAttribute("skin", widgetInfo.skin);
64  _widget->findAttribute("layer", widgetInfo.layer);
65 
66  if (_widget->findAttribute("align", tmp)) widgetInfo.align = Align::parse(tmp);
67 
68  _widget->findAttribute("name", widgetInfo.name);
69 
70  if (_widget->findAttribute("style", tmp)) widgetInfo.style = WidgetStyle::parse(tmp);
71 
72  IntCoord coord;
73  if (_widget->findAttribute("position", tmp))
74  {
75  widgetInfo.intCoord = IntCoord::parse(tmp);
76  widgetInfo.positionType = WidgetInfo::Pixels;
77  }
78  else if (_widget->findAttribute("position_real", tmp))
79  {
80  widgetInfo.floatCoord = FloatCoord::parse(tmp);
82  }
83 
84  // берем детей и крутимся
86  while (node.next())
87  {
88  if (node->getName() == "Widget")
89  {
90  widgetInfo.childWidgetsInfo.push_back(parseWidget(node));
91  }
92  else if (node->getName() == "Property")
93  {
94  widgetInfo.properties.push_back(PairString(node->findAttribute("key"), node->findAttribute("value")));
95  }
96  else if (node->getName() == "UserString")
97  {
98  widgetInfo.userStrings[node->findAttribute("key")] = node->findAttribute("value");
99  }
100  else if (node->getName() == "Controller")
101  {
102  ControllerInfo controllerInfo;
103  controllerInfo.type = node->findAttribute("type");
104 
106  while (prop.next("Property"))
107  controllerInfo.properties[prop->findAttribute("key")] = prop->findAttribute("value");
108 
109  widgetInfo.controllers.push_back(controllerInfo);
110  }
111  }
112 
113  return widgetInfo;
114  }
115 
116  VectorWidgetPtr ResourceLayout::createLayout(const std::string& _prefix, Widget* _parent)
117  {
118  VectorWidgetPtr widgets;
119 
120  for (VectorWidgetInfo::iterator iter = mLayoutData.begin(); iter != mLayoutData.end(); ++iter)
121  {
122  Widget* widget = createWidget(*iter, _prefix, _parent);
123  widgets.push_back(widget);
124  }
125 
126  return widgets;
127  }
128 
129  Widget* ResourceLayout::createWidget(const WidgetInfo& _widgetInfo, const std::string& _prefix, Widget* _parent, bool _template)
130  {
131  std::string widgetName = _widgetInfo.name;
132  WidgetStyle style = _widgetInfo.style;
133  std::string widgetLayer = _widgetInfo.layer;
134 
135  if (!widgetName.empty()) widgetName = _prefix + widgetName;
136 
137  if (_parent != nullptr && style != WidgetStyle::Popup) widgetLayer.clear();
138  if (_parent == nullptr && widgetLayer.empty())
139  {
140  MYGUI_LOG(Warning, "Root widget's layer is not specified, widget won't be visible. Specify layer or parent or attach it to another widget after load." << " [" << LayoutManager::getInstance().getCurrentLayout() << "]");
141  }
142 
143  IntCoord coord;
144  if (_widgetInfo.positionType == WidgetInfo::Pixels) coord = _widgetInfo.intCoord;
145  else if (_widgetInfo.positionType == WidgetInfo::Relative)
146  {
147  if (_parent == nullptr || style == WidgetStyle::Popup)
149  else
150  coord = CoordConverter::convertFromRelative(_widgetInfo.floatCoord, _parent->getClientCoord().size());
151  }
152 
153  Widget* wid;
154  if (nullptr == _parent)
155  wid = Gui::getInstance().createWidgetT(_widgetInfo.type, _widgetInfo.skin, coord, _widgetInfo.align, widgetLayer, widgetName);
156  else if (_template)
157  wid = _parent->_createSkinWidget(style, _widgetInfo.type, _widgetInfo.skin, coord, _widgetInfo.align, widgetLayer, widgetName);
158  else
159  wid = _parent->createWidgetT(style, _widgetInfo.type, _widgetInfo.skin, coord, _widgetInfo.align, widgetLayer, widgetName);
160 
161  for (VectorStringPairs::const_iterator iter = _widgetInfo.properties.begin(); iter != _widgetInfo.properties.end(); ++iter)
162  {
163  wid->setProperty(iter->first, iter->second);
164  }
165 
166  for (MapString::const_iterator iter = _widgetInfo.userStrings.begin(); iter != _widgetInfo.userStrings.end(); ++iter)
167  {
168  wid->setUserString(iter->first, iter->second);
169  if (!_template)
170  LayoutManager::getInstance().eventAddUserString(wid, iter->first, iter->second);
171  }
172 
173  for (VectorWidgetInfo::const_iterator iter = _widgetInfo.childWidgetsInfo.begin(); iter != _widgetInfo.childWidgetsInfo.end(); ++iter)
174  {
175  createWidget(*iter, _prefix, wid);
176  }
177 
178  for (std::vector<ControllerInfo>::const_iterator iter = _widgetInfo.controllers.begin(); iter != _widgetInfo.controllers.end(); ++iter)
179  {
181  if (item)
182  {
183  for (MapString::const_iterator iterProp = iter->properties.begin(); iterProp != iter->properties.end(); ++iterProp)
184  {
185  item->setProperty(iterProp->first, iterProp->second);
186  }
188  }
189  else
190  {
191  MYGUI_LOG(Warning, "Controller '" << iter->type << "' not found");
192  }
193  }
194 
195  return wid;
196  }
197 
199  {
200  return mLayoutData;
201  }
202 
203 } // namespace MyGUI
void addItem(Widget *_widget, ControllerItem *_item)
virtual void deserialization(xml::ElementPtr _node, Version _version)
std::vector< WidgetInfo > childWidgetsInfo
PositionType positionType
void setUserString(const std::string &_key, const std::string &_value)
static LayoutManager & getInstance()
TSize< T > size() const
Definition: MyGUI_TCoord.h:205
static WidgetStyle parse(const std::string &_value)
bool findAttribute(const std::string &_name, std::string &_value)
VectorWidgetInfo mLayoutData
static Align parse(const std::string &_value)
Definition: MyGUI_Align.h:142
static IntCoord convertFromRelative(const FloatCoord &_coord, const IntSize &_view)
virtual const IntSize & getViewSize() const =0
std::pair< std::string, std::string > PairString
Definition: MyGUI_Types.h:55
virtual void setProperty(const std::string &_key, const std::string &_value)
IntCoord getClientCoord()
#define MYGUI_LOG(level, text)
VectorWidgetPtr createLayout(const std::string &_prefix="", Widget *_parent=0)
std::vector< Widget * > VectorWidgetPtr
Widget * _createSkinWidget(WidgetStyle _style, const std::string &_type, const std::string &_skin, const IntCoord &_coord, Align _align, const std::string &_layer="", const std::string &_name="")
Widget * createWidgetT(const std::string &_type, const std::string &_skin, const IntCoord &_coord, Align _align, const std::string &_name="")
const VectorWidgetInfo & getLayoutData() const
EventHandle_AddUserStringDelegate eventAddUserString
std::string mResourceName
ElementEnumerator getElementEnumerator()
const std::string & getName() const
Widget * createWidgetT(const std::string &_type, const std::string &_skin, const IntCoord &_coord, Align _align, const std::string &_layer, const std::string &_name="")
Definition: MyGUI_Gui.cpp:303
static TCoord< int > parse(const std::string &_value)
Definition: MyGUI_TCoord.h:222
std::vector< WidgetInfo > VectorWidgetInfo
std::vector< ControllerInfo > controllers
WidgetInfo parseWidget(xml::ElementEnumerator &_widget)
void setProperty(const std::string &_key, const std::string &_value)
Widget * createWidget(const WidgetInfo &_widgetInfo, const std::string &_prefix="", Widget *_parent=0, bool _template=false)
ControllerItem * createItem(const std::string &_type)
VectorStringPairs properties