MyGUI 3.5.3
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
16#include <pugixml.hpp>
17
18namespace MyGUI
19{
20
21 ResourceLayout::ResourceLayout(xml::ElementPtr _node, std::string_view _fileName)
22 {
23 // FIXME hardcoded version
24 deserialization(_node, Version(1, 0, 0));
25 mResourceName = _fileName;
26 }
27
28 void ResourceLayout::deserialization(xml::ElementPtr _node, Version _version)
29 {
30 Base::deserialization(_node, _version);
31
32 mLayoutData.clear();
33
34 for (auto widget : _node.node().children("Widget"))
35 mLayoutData.push_back(parseWidget(widget));
36 }
37
38 WidgetInfo ResourceLayout::parseWidget(xml::ElementPtr _widget)
39 {
40 WidgetInfo widgetInfo;
41
42 widgetInfo.type = _widget.node().attribute("type").value();
43 widgetInfo.skin = _widget.node().attribute("skin").value();
44 widgetInfo.layer = _widget.node().attribute("layer").value();
45
46 std::string_view tmp{_widget.node().attribute("align").value()};
47 if (!tmp.empty())
48 widgetInfo.align = Align::parse(tmp);
49
50 widgetInfo.name = _widget.node().attribute("name").value();
51
52 tmp = _widget.node().attribute("style").value();
53 if (!tmp.empty())
54 widgetInfo.style = WidgetStyle::parse(tmp);
55
56 tmp = _widget.node().attribute("position").value();
57 if (!tmp.empty())
58 {
59 widgetInfo.intCoord = IntCoord::parse(tmp);
60 widgetInfo.positionType = WidgetInfo::Pixels;
61 }
62 else
63 {
64 tmp = _widget.node().attribute("position_real").value();
65 if (!tmp.empty())
66 {
67 widgetInfo.floatCoord = FloatCoord::parse(tmp);
68 widgetInfo.positionType = WidgetInfo::Relative;
69 }
70 }
71
72 for (auto node : _widget.node())
73 {
74 if (node.name() == std::string_view("Widget"))
75 {
76 widgetInfo.childWidgetsInfo.push_back(parseWidget(node));
77 }
78 else if (node.name() == std::string_view("Property"))
79 {
80 widgetInfo.properties.emplace_back(node.attribute("key").value(), node.attribute("value").value());
81 }
82 else if (node.name() == std::string_view("UserString"))
83 {
84 std::string_view key = node.attribute("key").value();
85 std::string_view value = node.attribute("value").value();
86 mapSet(widgetInfo.userStrings, key, value);
87 }
88 else if (node.name() == std::string_view("Controller"))
89 {
90 ControllerInfo controllerInfo;
91 controllerInfo.type = node.attribute("type").value();
92
93 for (auto prop : node.children("Property"))
94 {
95 std::string_view key = prop.attribute("key").value();
96 std::string_view value = prop.attribute("value").value();
97 mapSet(controllerInfo.properties, key, value);
98 }
99
100 widgetInfo.controllers.push_back(controllerInfo);
101 }
102 }
103
104 return widgetInfo;
105 }
106
107 VectorWidgetPtr ResourceLayout::createLayout(std::string_view _prefix, Widget* _parent)
108 {
109 VectorWidgetPtr widgets;
110
111 for (auto& iter : mLayoutData)
112 {
113 Widget* widget = createWidget(iter, _prefix, _parent);
114 widgets.push_back(widget);
115 }
116
117 return widgets;
118 }
119
120 Widget* ResourceLayout::createWidget(
121 const WidgetInfo& _widgetInfo,
122 std::string_view _prefix,
123 Widget* _parent,
124 bool _template)
125 {
126 std::string widgetName;
127 WidgetStyle style = _widgetInfo.style;
128 std::string_view widgetLayer = _widgetInfo.layer;
129
130 if (!_widgetInfo.name.empty())
131 {
132 widgetName = _prefix;
133 widgetName += _widgetInfo.name;
134 }
135
136 if (_parent != nullptr && style != WidgetStyle::Popup)
137 widgetLayer = {};
138 if (_parent == nullptr && widgetLayer.empty())
139 {
140 MYGUI_LOG(
141 Warning,
142 "Root widget's layer is not specified, widget won't be visible. Specify layer or parent or attach it "
143 "to another widget after load."
144 << " [" << LayoutManager::getInstance().getCurrentLayout() << "]");
145 }
146
147 IntCoord coord;
148 if (_widgetInfo.positionType == WidgetInfo::Pixels)
149 coord = _widgetInfo.intCoord;
150 else if (_widgetInfo.positionType == WidgetInfo::Relative)
151 {
152 if (_parent == nullptr || style == WidgetStyle::Popup)
153 coord = CoordConverter::convertFromRelative(
154 _widgetInfo.floatCoord,
155 RenderManager::getInstance().getViewSize());
156 else
157 coord = CoordConverter::convertFromRelative(_widgetInfo.floatCoord, _parent->getClientCoord().size());
158 }
159
160 Widget* wid;
161 if (nullptr == _parent)
162 wid = Gui::getInstance().createWidgetT(
163 _widgetInfo.type,
164 _widgetInfo.skin,
165 coord,
166 _widgetInfo.align,
167 widgetLayer,
168 widgetName);
169 else if (_template)
170 wid = _parent->_createSkinWidget(
171 style,
172 _widgetInfo.type,
173 _widgetInfo.skin,
174 coord,
175 _widgetInfo.align,
176 widgetLayer,
177 widgetName);
178 else
179 wid = _parent->createWidgetT(
180 style,
181 _widgetInfo.type,
182 _widgetInfo.skin,
183 coord,
184 _widgetInfo.align,
185 widgetLayer,
186 widgetName);
187
188 for (const auto& property : _widgetInfo.properties)
189 {
190 wid->setProperty(property.first, property.second);
191 }
192
193 for (const auto& userString : _widgetInfo.userStrings)
194 {
195 wid->setUserString(userString.first, userString.second);
196 if (!_template)
197 LayoutManager::getInstance().eventAddUserString(wid, userString.first, userString.second);
198 }
199
200 for (const auto& iter : _widgetInfo.childWidgetsInfo)
201 {
202 createWidget(iter, _prefix, wid);
203 }
204
205 for (const auto& iter : _widgetInfo.controllers)
206 {
207 MyGUI::ControllerItem* item = MyGUI::ControllerManager::getInstance().createItem(iter.type);
208 if (item)
209 {
210 for (const auto& property : iter.properties)
211 {
212 item->setProperty(property.first, property.second);
213 }
214 MyGUI::ControllerManager::getInstance().addItem(wid, item);
215 }
216 else
217 {
218 MYGUI_LOG(Warning, "Controller '" << iter.type << "' not found");
219 }
220 }
221
222 LayoutManager::getInstance().eventCreateWidget(wid, _widgetInfo);
223
224 return wid;
225 }
226
227 const VectorWidgetInfo& ResourceLayout::getLayoutData() const
228 {
229 return mLayoutData;
230 }
231
232} // namespace MyGUI
#define MYGUI_LOG(level, text)
void mapSet(Map &map, std::string_view key, const Value &value)
std::vector< WidgetInfo > VectorWidgetInfo
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
std::vector< Widget * > VectorWidgetPtr
static TCoord< int > parse(std::string_view _value)