MyGUI: MyGUI_ResourceLayout.cpp Source File

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"
8#include "MyGUI_ResourceLayout.h"
9#include "MyGUI_CoordConverter.h"
10#include "MyGUI_RenderManager.h"
11#include "MyGUI_ControllerManager.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
26 void ResourceLayout::deserialization(xml::ElementPtr _node, Version _version)
27 {
28 Base::deserialization(_node, _version);
29
30 mLayoutData.clear();
31
32 xml::ElementEnumerator widget = _node->getElementEnumerator();
33 while (widget.next("Widget"))
34 mLayoutData.push_back(parseWidget(widget));
35 }
36
37 WidgetInfo ResourceLayout::parseWidget(xml::ElementEnumerator& _widget)
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);
58 widgetInfo.positionType = WidgetInfo::Pixels;
59 }
60 else if (_widget->findAttribute("position_real", tmp))
61 {
62 widgetInfo.floatCoord = FloatCoord::parse(tmp);
63 widgetInfo.positionType = WidgetInfo::Relative;
64 }
65
66 // берем детей и крутимся
67 xml::ElementEnumerator node = _widget->getElementEnumerator();
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
89 xml::ElementEnumerator prop = node->getElementEnumerator();
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
117 Widget* ResourceLayout::createWidget(
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)
150 coord = CoordConverter::convertFromRelative(
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)
159 wid = Gui::getInstance().createWidgetT(
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 {
204 MyGUI::ControllerItem* item = MyGUI::ControllerManager::getInstance().createItem(iter.type);
205 if (item)
206 {
207 for (const auto& property : iter.properties)
208 {
209 item->setProperty(property.first, property.second);
210 }
211 MyGUI::ControllerManager::getInstance().addItem(wid, item);
212 }
213 else
214 {
215 MYGUI_LOG(Warning, "Controller '" << iter.type << "' not found");
216 }
217 }
218
219 LayoutManager::getInstance().eventCreateWidget(wid, _widgetInfo);
220
221 return wid;
222 }
223
224 const VectorWidgetInfo& ResourceLayout::getLayoutData() const
225 {
226 return mLayoutData;
227 }
228
229} // namespace MyGUI
MyGUI_ControllerManager.h
MyGUI_CoordConverter.h
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition MyGUI_Diagnostic.h:17
MyGUI_Gui.h
MyGUI_LayoutManager.h
MyGUI_Precompiled.h
MyGUI_RenderManager.h
MyGUI_ResourceLayout.h
MyGUI_Widget.h
MyGUI::ControllerItem
Definition MyGUI_ControllerItem.h:26
MyGUI::ControllerItem::setProperty
virtual void setProperty(std::string_view, std::string_view)
Definition MyGUI_ControllerItem.h:33
MyGUI::ControllerManager::createItem
ControllerItem * createItem(std::string_view _type)
Definition MyGUI_ControllerManager.cpp:71
MyGUI::ControllerManager::getInstance
static ControllerManager & getInstance()
MyGUI::ControllerManager::addItem
void addItem(Widget *_widget, ControllerItem *_item)
Definition MyGUI_ControllerManager.cpp:77
MyGUI::CoordConverter::convertFromRelative
static IntCoord convertFromRelative(const FloatCoord &_coord, const IntSize &_view)
Definition MyGUI_CoordConverter.h:34
MyGUI::Gui::getInstance
static Gui & getInstance()
Definition MyGUI_Gui.cpp:34
MyGUI::Gui::createWidgetT
Widget * createWidgetT(std::string_view _type, std::string_view _skin, const IntCoord &_coord, Align _align, std::string_view _layer, std::string_view _name={})
Definition MyGUI_Gui.cpp:275
MyGUI::IResource::mResourceName
std::string mResourceName
Definition MyGUI_IResource.h:54
MyGUI::LayoutManager::getInstance
static LayoutManager & getInstance()
MyGUI::LayoutManager::eventAddUserString
EventHandle_AddUserStringDelegate eventAddUserString
Definition MyGUI_LayoutManager.h:69
MyGUI::LayoutManager::eventCreateWidget
EventHandle_CreateWidgetDelegate eventCreateWidget
Definition MyGUI_LayoutManager.h:60
MyGUI::RenderManager::getInstance
static RenderManager & getInstance()
MyGUI::ResourceLayout::mLayoutData
VectorWidgetInfo mLayoutData
Definition MyGUI_ResourceLayout.h:45
MyGUI::ResourceLayout::deserialization
void deserialization(xml::ElementPtr _node, Version _version) override
Definition MyGUI_ResourceLayout.cpp:26
MyGUI::ResourceLayout::createWidget
Widget * createWidget(const WidgetInfo &_widgetInfo, std::string_view _prefix={}, Widget *_parent=nullptr, bool _template=false)
Definition MyGUI_ResourceLayout.cpp:117
MyGUI::ResourceLayout::ResourceLayout
ResourceLayout()=default
MyGUI::ResourceLayout::createLayout
VectorWidgetPtr createLayout(std::string_view _prefix={}, Widget *_parent=nullptr)
Definition MyGUI_ResourceLayout.cpp:104
MyGUI::ResourceLayout::parseWidget
WidgetInfo parseWidget(xml::ElementEnumerator &_widget)
Definition MyGUI_ResourceLayout.cpp:37
MyGUI::ResourceLayout::getLayoutData
const VectorWidgetInfo & getLayoutData() const
Definition MyGUI_ResourceLayout.cpp:224
MyGUI::UserData::setUserString
void setUserString(std::string_view _key, std::string_view _value)
Definition MyGUI_WidgetUserData.cpp:14
MyGUI::Version
Definition MyGUI_Version.h:18
MyGUI::Widget
widget description should be here.
Definition MyGUI_Widget.h:40
MyGUI::Widget::setProperty
void setProperty(std::string_view _key, std::string_view _value)
Definition MyGUI_Widget.cpp:1135
MyGUI::Widget::createWidgetT
Widget * createWidgetT(std::string_view _type, std::string_view _skin, const IntCoord &_coord, Align _align, std::string_view _name={})
Definition MyGUI_Widget.cpp:944
MyGUI::Widget::getClientCoord
IntCoord getClientCoord() const
Definition MyGUI_Widget.cpp:459
MyGUI::Widget::_createSkinWidget
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={})
Definition MyGUI_Widget.cpp:1206
MyGUI::xml::ElementEnumerator
Definition MyGUI_XmlDocument.h:121
MyGUI::xml::ElementEnumerator::next
bool next()
Definition MyGUI_XmlDocument.cpp:107
MyGUI::xml::Element::findAttribute
bool findAttribute(std::string_view _name, std::string &_value)
Definition MyGUI_XmlDocument.cpp:242
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition MyGUI_XmlDocument.cpp:348
MyGUI::xml::Element::getName
const std::string & getName() const
Definition MyGUI_XmlDocument.cpp:328
MyGUI::xml::ElementPtr
Element * ElementPtr
Definition MyGUI_XmlDocument.h:112
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::mapSet
void mapSet(Map &map, std::string_view key, const Value &value)
Definition MyGUI_Macros.h:36
MyGUI::VectorWidgetInfo
std::vector< WidgetInfo > VectorWidgetInfo
Definition MyGUI_LayoutData.h:55
MyGUI::IntCoord
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:36
MyGUI::VectorWidgetPtr
std::vector< Widget * > VectorWidgetPtr
Definition MyGUI_WidgetDefines.h:20
MyGUI::Align::parse
static Align parse(std::string_view _value)
Definition MyGUI_Align.h:127
MyGUI::ControllerInfo
Definition MyGUI_LayoutData.h:17
MyGUI::ControllerInfo::type
std::string type
Definition MyGUI_LayoutData.h:18
MyGUI::ControllerInfo::properties
MapString properties
Definition MyGUI_LayoutData.h:19
MyGUI::WidgetInfo
Definition MyGUI_LayoutData.h:23
MyGUI::WidgetInfo::layer
std::string layer
Definition MyGUI_LayoutData.h:42
MyGUI::WidgetInfo::floatCoord
FloatCoord floatCoord
Definition MyGUI_LayoutData.h:52
MyGUI::WidgetInfo::type
std::string type
Definition MyGUI_LayoutData.h:37
MyGUI::WidgetInfo::style
WidgetStyle style
Definition MyGUI_LayoutData.h:40
MyGUI::WidgetInfo::userStrings
MapString userStrings
Definition MyGUI_LayoutData.h:35
MyGUI::WidgetInfo::properties
VectorStringPairs properties
Definition MyGUI_LayoutData.h:34
MyGUI::WidgetInfo::name
std::string name
Definition MyGUI_LayoutData.h:41
MyGUI::WidgetInfo::intCoord
IntCoord intCoord
Definition MyGUI_LayoutData.h:51
MyGUI::WidgetInfo::positionType
PositionType positionType
Definition MyGUI_LayoutData.h:50
MyGUI::WidgetInfo::Pixels
@ Pixels
Definition MyGUI_LayoutData.h:46
MyGUI::WidgetInfo::Relative
@ Relative
Definition MyGUI_LayoutData.h:47
MyGUI::WidgetInfo::controllers
std::vector< ControllerInfo > controllers
Definition MyGUI_LayoutData.h:36
MyGUI::WidgetInfo::childWidgetsInfo
std::vector< WidgetInfo > childWidgetsInfo
Definition MyGUI_LayoutData.h:32
MyGUI::WidgetInfo::skin
std::string skin
Definition MyGUI_LayoutData.h:38
MyGUI::WidgetInfo::align
Align align
Definition MyGUI_LayoutData.h:39
MyGUI::WidgetStyle
Definition MyGUI_WidgetStyle.h:19
MyGUI::WidgetStyle::Popup
@ Popup
Definition MyGUI_WidgetStyle.h:23
MyGUI::WidgetStyle::parse
static WidgetStyle parse(std::string_view _value)
Definition MyGUI_WidgetStyle.h:38
MyGUI::types::TCoord< int >::parse
static TCoord< int > parse(std::string_view _value)
Definition MyGUI_TCoord.h:183
MyGUI::types::TCoord::size
TSize< T > size() const
Definition MyGUI_TCoord.h:165