MyGUI: MyGUI_ResourceLayout.cpp Source File

MyGUI 3.4.2

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

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()
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
30 void ResourceLayout::deserialization(xml::ElementPtr _node, Version _version)
31 {
32 Base::deserialization(_node, _version);
33
34 mLayoutData.clear();
35
36 xml::ElementEnumerator widget = _node->getElementEnumerator();
37 while (widget.next("Widget"))
38 mLayoutData.push_back(parseWidget(widget));
39 }
40
41 WidgetInfo ResourceLayout::parseWidget(xml::ElementEnumerator& _widget)
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);
61 widgetInfo.positionType = WidgetInfo::Pixels;
62 }
63 else if (_widget->findAttribute("position_real", tmp))
64 {
65 widgetInfo.floatCoord = FloatCoord::parse(tmp);
66 widgetInfo.positionType = WidgetInfo::Relative;
67 }
68
69 // берем детей и крутимся
70 xml::ElementEnumerator node = _widget->getElementEnumerator();
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
90 xml::ElementEnumerator prop = node->getElementEnumerator();
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)
133 coord = CoordConverter::convertFromRelative(_widgetInfo.floatCoord, RenderManager::getInstance().getViewSize());
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 {
165 MyGUI::ControllerItem* item = MyGUI::ControllerManager::getInstance().createItem(iter->type);
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 }
172 MyGUI::ControllerManager::getInstance().addItem(wid, item);
173 }
174 else
175 {
176 MYGUI_LOG(Warning, "Controller '" << iter->type << "' not found");
177 }
178 }
179
180 LayoutManager::getInstance().eventCreateWidget(wid, _widgetInfo);
181
182 return wid;
183 }
184
185 const VectorWidgetInfo& ResourceLayout::getLayoutData() const
186 {
187 return mLayoutData;
188 }
189
190} // 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:27
MyGUI::ControllerItem::setProperty
virtual void setProperty(const std::string &, const std::string &)
Definition MyGUI_ControllerItem.h:36
MyGUI::ControllerManager::createItem
ControllerItem * createItem(const std::string &_type)
Definition MyGUI_ControllerManager.cpp:72
MyGUI::ControllerManager::getInstance
static ControllerManager & getInstance()
MyGUI::ControllerManager::addItem
void addItem(Widget *_widget, ControllerItem *_item)
Definition MyGUI_ControllerManager.cpp:78
MyGUI::CoordConverter::convertFromRelative
static IntCoord convertFromRelative(const FloatCoord &_coord, const IntSize &_view)
Definition MyGUI_CoordConverter.h:33
MyGUI::Gui::createWidgetT
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
MyGUI::Gui::getInstance
static Gui & getInstance()
Definition MyGUI_Gui.cpp:34
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:66
MyGUI::LayoutManager::eventCreateWidget
EventHandle_CreateWidgetDelegate eventCreateWidget
Definition MyGUI_LayoutManager.h:57
MyGUI::RenderManager::getViewSize
virtual const IntSize & getViewSize() const =0
MyGUI::RenderManager::getInstance
static RenderManager & getInstance()
MyGUI::ResourceLayout::mLayoutData
VectorWidgetInfo mLayoutData
Definition MyGUI_ResourceLayout.h:43
MyGUI::ResourceLayout::deserialization
void deserialization(xml::ElementPtr _node, Version _version) override
Definition MyGUI_ResourceLayout.cpp:30
MyGUI::ResourceLayout::ResourceLayout
ResourceLayout()
Definition MyGUI_ResourceLayout.cpp:19
MyGUI::ResourceLayout::parseWidget
WidgetInfo parseWidget(xml::ElementEnumerator &_widget)
Definition MyGUI_ResourceLayout.cpp:41
MyGUI::ResourceLayout::getLayoutData
const VectorWidgetInfo & getLayoutData() const
Definition MyGUI_ResourceLayout.cpp:185
MyGUI::ResourceLayout::createWidget
Widget * createWidget(const WidgetInfo &_widgetInfo, const std::string &_prefix="", Widget *_parent=nullptr, bool _template=false)
Definition MyGUI_ResourceLayout.cpp:114
MyGUI::ResourceLayout::createLayout
VectorWidgetPtr createLayout(const std::string &_prefix="", Widget *_parent=nullptr)
Definition MyGUI_ResourceLayout.cpp:101
MyGUI::UserData::setUserString
void setUserString(const std::string &_key, const std::string &_value)
Definition MyGUI_WidgetUserData.cpp:14
MyGUI::Version
Definition MyGUI_Version.h:18
MyGUI::Widget
widget description should be here.
Definition MyGUI_Widget.h:37
MyGUI::Widget::createWidgetT
Widget * createWidgetT(const std::string &_type, const std::string &_skin, const IntCoord &_coord, Align _align, const std::string &_name="")
Definition MyGUI_Widget.cpp:909
MyGUI::Widget::setProperty
void setProperty(const std::string &_key, const std::string &_value)
Definition MyGUI_Widget.cpp:1071
MyGUI::Widget::_createSkinWidget
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="")
Definition MyGUI_Widget.cpp:1139
MyGUI::Widget::getClientCoord
IntCoord getClientCoord() const
Definition MyGUI_Widget.cpp:427
MyGUI::xml::ElementEnumerator
Definition MyGUI_XmlDocument.h:115
MyGUI::xml::ElementEnumerator::next
bool next()
Definition MyGUI_XmlDocument.cpp:100
MyGUI::xml::Element
Definition MyGUI_XmlDocument.h:159
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition MyGUI_XmlDocument.cpp:352
MyGUI::xml::Element::getName
const std::string & getName() const
Definition MyGUI_XmlDocument.cpp:332
MyGUI::xml::Element::findAttribute
bool findAttribute(const std::string &_name, std::string &_value)
Definition MyGUI_XmlDocument.cpp:246
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::PairString
std::pair< std::string, std::string > PairString
Definition MyGUI_Types.h:42
MyGUI::VectorWidgetInfo
std::vector< WidgetInfo > VectorWidgetInfo
Definition MyGUI_LayoutData.h:51
MyGUI::VectorWidgetPtr
std::vector< Widget * > VectorWidgetPtr
Definition MyGUI_WidgetDefines.h:20
MyGUI::Align::parse
static Align parse(const std::string &_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:43
MyGUI::WidgetInfo::floatCoord
FloatCoord floatCoord
Definition MyGUI_LayoutData.h:48
MyGUI::WidgetInfo::type
std::string type
Definition MyGUI_LayoutData.h:38
MyGUI::WidgetInfo::style
WidgetStyle style
Definition MyGUI_LayoutData.h:41
MyGUI::WidgetInfo::userStrings
MapString userStrings
Definition MyGUI_LayoutData.h:36
MyGUI::WidgetInfo::properties
VectorStringPairs properties
Definition MyGUI_LayoutData.h:35
MyGUI::WidgetInfo::name
std::string name
Definition MyGUI_LayoutData.h:42
MyGUI::WidgetInfo::intCoord
IntCoord intCoord
Definition MyGUI_LayoutData.h:47
MyGUI::WidgetInfo::positionType
PositionType positionType
Definition MyGUI_LayoutData.h:46
MyGUI::WidgetInfo::Pixels
@ Pixels
Definition MyGUI_LayoutData.h:45
MyGUI::WidgetInfo::Relative
@ Relative
Definition MyGUI_LayoutData.h:45
MyGUI::WidgetInfo::controllers
std::vector< ControllerInfo > controllers
Definition MyGUI_LayoutData.h:37
MyGUI::WidgetInfo::childWidgetsInfo
std::vector< WidgetInfo > childWidgetsInfo
Definition MyGUI_LayoutData.h:33
MyGUI::WidgetInfo::skin
std::string skin
Definition MyGUI_LayoutData.h:39
MyGUI::WidgetInfo::align
Align align
Definition MyGUI_LayoutData.h:40
MyGUI::WidgetStyle
Definition MyGUI_WidgetStyle.h:19
MyGUI::WidgetStyle::parse
static WidgetStyle parse(const std::string &_value)
Definition MyGUI_WidgetStyle.h:38
MyGUI::WidgetStyle::Popup
@ Popup
Definition MyGUI_WidgetStyle.h:23
MyGUI::types::TCoord< int >
MyGUI::types::TCoord::size
TSize< T > size() const
Definition MyGUI_TCoord.h:190
MyGUI::types::TCoord< int >::parse
static TCoord< int > parse(const std::string &_value)
Definition MyGUI_TCoord.h:207