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