MyGUI 3.4.1
MyGUI_ResourceLayout.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
10#include "MyGUI_RenderManager.h"
12#include "MyGUI_LayoutManager.h"
13#include "MyGUI_Widget.h"
14#include "MyGUI_Gui.h"
15
16namespace MyGUI
17{
18
20 {
21 }
22
23 ResourceLayout::ResourceLayout(xml::ElementPtr _node, const std::string& _fileName)
24 {
25 // FIXME hardcoded version
26 deserialization(_node, Version(1, 0, 0));
27 mResourceName = _fileName;
28 }
29
31 {
32 Base::deserialization(_node, _version);
33
34 mLayoutData.clear();
35
37 while (widget.next("Widget"))
38 mLayoutData.push_back(parseWidget(widget));
39 }
40
42 {
43 WidgetInfo widgetInfo;
44
45 std::string tmp;
46
47 _widget->findAttribute("type", widgetInfo.type);
48 _widget->findAttribute("skin", widgetInfo.skin);
49 _widget->findAttribute("layer", widgetInfo.layer);
50
51 if (_widget->findAttribute("align", tmp)) widgetInfo.align = Align::parse(tmp);
52
53 _widget->findAttribute("name", widgetInfo.name);
54
55 if (_widget->findAttribute("style", tmp)) widgetInfo.style = WidgetStyle::parse(tmp);
56
57 IntCoord coord;
58 if (_widget->findAttribute("position", tmp))
59 {
60 widgetInfo.intCoord = IntCoord::parse(tmp);
62 }
63 else if (_widget->findAttribute("position_real", tmp))
64 {
65 widgetInfo.floatCoord = FloatCoord::parse(tmp);
67 }
68
69 // берем детей и крутимся
71 while (node.next())
72 {
73 if (node->getName() == "Widget")
74 {
75 widgetInfo.childWidgetsInfo.push_back(parseWidget(node));
76 }
77 else if (node->getName() == "Property")
78 {
79 widgetInfo.properties.push_back(PairString(node->findAttribute("key"), node->findAttribute("value")));
80 }
81 else if (node->getName() == "UserString")
82 {
83 widgetInfo.userStrings[node->findAttribute("key")] = node->findAttribute("value");
84 }
85 else if (node->getName() == "Controller")
86 {
87 ControllerInfo controllerInfo;
88 controllerInfo.type = node->findAttribute("type");
89
91 while (prop.next("Property"))
92 controllerInfo.properties[prop->findAttribute("key")] = prop->findAttribute("value");
93
94 widgetInfo.controllers.push_back(controllerInfo);
95 }
96 }
97
98 return widgetInfo;
99 }
100
101 VectorWidgetPtr ResourceLayout::createLayout(const std::string& _prefix, Widget* _parent)
102 {
103 VectorWidgetPtr widgets;
104
105 for (VectorWidgetInfo::iterator iter = mLayoutData.begin(); iter != mLayoutData.end(); ++iter)
106 {
107 Widget* widget = createWidget(*iter, _prefix, _parent);
108 widgets.push_back(widget);
109 }
110
111 return widgets;
112 }
113
114 Widget* ResourceLayout::createWidget(const WidgetInfo& _widgetInfo, const std::string& _prefix, Widget* _parent, bool _template)
115 {
116 std::string widgetName = _widgetInfo.name;
117 WidgetStyle style = _widgetInfo.style;
118 std::string widgetLayer = _widgetInfo.layer;
119
120 if (!widgetName.empty()) widgetName = _prefix + widgetName;
121
122 if (_parent != nullptr && style != WidgetStyle::Popup) widgetLayer.clear();
123 if (_parent == nullptr && widgetLayer.empty())
124 {
125 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() << "]");
126 }
127
128 IntCoord coord;
129 if (_widgetInfo.positionType == WidgetInfo::Pixels) coord = _widgetInfo.intCoord;
130 else if (_widgetInfo.positionType == WidgetInfo::Relative)
131 {
132 if (_parent == nullptr || style == WidgetStyle::Popup)
134 else
135 coord = CoordConverter::convertFromRelative(_widgetInfo.floatCoord, _parent->getClientCoord().size());
136 }
137
138 Widget* wid;
139 if (nullptr == _parent)
140 wid = Gui::getInstance().createWidgetT(_widgetInfo.type, _widgetInfo.skin, coord, _widgetInfo.align, widgetLayer, widgetName);
141 else if (_template)
142 wid = _parent->_createSkinWidget(style, _widgetInfo.type, _widgetInfo.skin, coord, _widgetInfo.align, widgetLayer, widgetName);
143 else
144 wid = _parent->createWidgetT(style, _widgetInfo.type, _widgetInfo.skin, coord, _widgetInfo.align, widgetLayer, widgetName);
145
146 for (VectorStringPairs::const_iterator iter = _widgetInfo.properties.begin(); iter != _widgetInfo.properties.end(); ++iter)
147 {
148 wid->setProperty(iter->first, iter->second);
149 }
150
151 for (MapString::const_iterator iter = _widgetInfo.userStrings.begin(); iter != _widgetInfo.userStrings.end(); ++iter)
152 {
153 wid->setUserString(iter->first, iter->second);
154 if (!_template)
155 LayoutManager::getInstance().eventAddUserString(wid, iter->first, iter->second);
156 }
157
158 for (VectorWidgetInfo::const_iterator iter = _widgetInfo.childWidgetsInfo.begin(); iter != _widgetInfo.childWidgetsInfo.end(); ++iter)
159 {
160 createWidget(*iter, _prefix, wid);
161 }
162
163 for (std::vector<ControllerInfo>::const_iterator iter = _widgetInfo.controllers.begin(); iter != _widgetInfo.controllers.end(); ++iter)
164 {
166 if (item)
167 {
168 for (MapString::const_iterator iterProp = iter->properties.begin(); iterProp != iter->properties.end(); ++iterProp)
169 {
170 item->setProperty(iterProp->first, iterProp->second);
171 }
173 }
174 else
175 {
176 MYGUI_LOG(Warning, "Controller '" << iter->type << "' not found");
177 }
178 }
179
181
182 return wid;
183 }
184
186 {
187 return mLayoutData;
188 }
189
190} // namespace MyGUI
#define MYGUI_LOG(level, text)
virtual void setProperty(const std::string &_key, const std::string &_value)
ControllerItem * createItem(const std::string &_type)
static ControllerManager & getInstance()
void addItem(Widget *_widget, ControllerItem *_item)
static IntCoord convertFromRelative(const FloatCoord &_coord, const IntSize &_view)
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:284
static Gui & getInstance()
std::string mResourceName
static LayoutManager & getInstance()
EventHandle_AddUserStringDelegate eventAddUserString
EventHandle_CreateWidgetDelegate eventCreateWidget
virtual const IntSize & getViewSize() const =0
static RenderManager & getInstance()
VectorWidgetInfo mLayoutData
void deserialization(xml::ElementPtr _node, Version _version) override
WidgetInfo parseWidget(xml::ElementEnumerator &_widget)
const VectorWidgetInfo & getLayoutData() const
Widget * createWidget(const WidgetInfo &_widgetInfo, const std::string &_prefix="", Widget *_parent=nullptr, bool _template=false)
VectorWidgetPtr createLayout(const std::string &_prefix="", Widget *_parent=nullptr)
void setUserString(const std::string &_key, const std::string &_value)
widget description should be here.
Definition: MyGUI_Widget.h:37
Widget * createWidgetT(const std::string &_type, const std::string &_skin, const IntCoord &_coord, Align _align, const std::string &_name="")
void setProperty(const std::string &_key, const std::string &_value)
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="")
IntCoord getClientCoord() const
ElementEnumerator getElementEnumerator()
const std::string & getName() const
bool findAttribute(const std::string &_name, std::string &_value)
std::pair< std::string, std::string > PairString
Definition: MyGUI_Types.h:41
std::vector< WidgetInfo > VectorWidgetInfo
std::vector< Widget * > VectorWidgetPtr
static Align parse(const std::string &_value)
Definition: MyGUI_Align.h:127
VectorStringPairs properties
PositionType positionType
std::vector< ControllerInfo > controllers
std::vector< WidgetInfo > childWidgetsInfo
static WidgetStyle parse(const std::string &_value)
TSize< T > size() const
Definition: MyGUI_TCoord.h:190
static TCoord< int > parse(const std::string &_value)
Definition: MyGUI_TCoord.h:207