MyGUI 3.2.0

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

- File List
- Globals

MyGUI_ResourceLayout.cpp
Go to the documentation of this file.
1
6 /*
7 This file is part of MyGUI.
8
9 MyGUI is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 MyGUI is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
23 #include "MyGUI_ResourceLayout.h"
24 #include "MyGUI_CoordConverter.h"
25 #include "MyGUI_RenderManager.h"
26 #include "MyGUI_ControllerManager.h"
27 #include "MyGUI_LayoutManager.h"
28 #include "MyGUI_Widget.h"
29 #include "MyGUI_Gui.h"
30
31 namespace MyGUI
32 {
33
34 ResourceLayout::ResourceLayout()
35 {
36 }
37
38 ResourceLayout::ResourceLayout(xml::ElementPtr _node, const std::string& _fileName)
39 {
40 // FIXME hardcoded version
41 deserialization(_node, Version(1, 0, 0));
42 mResourceName = _fileName;
43 }
44
45 void ResourceLayout::deserialization(xml::ElementPtr _node, Version _version)
46 {
47 Base::deserialization(_node, _version);
48
49 mLayoutData.clear();
50
51 xml::ElementEnumerator widget = _node->getElementEnumerator();
52 while (widget.next("Widget"))
53 mLayoutData.push_back(parseWidget(widget));
54 }
55
56 WidgetInfo ResourceLayout::parseWidget(xml::ElementEnumerator& _widget)
57 {
58 WidgetInfo widgetInfo;
59
60 std::string tmp;
61
62 _widget->findAttribute("type", widgetInfo.type);
63 _widget->findAttribute("skin", widgetInfo.skin);
64 _widget->findAttribute("layer", widgetInfo.layer);
65
66 if (_widget->findAttribute("align", tmp)) widgetInfo.align = Align::parse(tmp);
67
68 _widget->findAttribute("name", widgetInfo.name);
69
70 if (_widget->findAttribute("style", tmp)) widgetInfo.style = WidgetStyle::parse(tmp);
71
72 IntCoord coord;
73 if (_widget->findAttribute("position", tmp))
74 {
75 widgetInfo.intCoord = IntCoord::parse(tmp);
76 widgetInfo.positionType = WidgetInfo::Pixels;
77 }
78 else if (_widget->findAttribute("position_real", tmp))
79 {
80 widgetInfo.floatCoord = FloatCoord::parse(tmp);
81 widgetInfo.positionType = WidgetInfo::Relative;
82 }
83
84 // берем детей и крутимся
85 xml::ElementEnumerator node = _widget->getElementEnumerator();
86 while (node.next())
87 {
88 if (node->getName() == "Widget")
89 {
90 widgetInfo.childWidgetsInfo.push_back(parseWidget(node));
91 }
92 else if (node->getName() == "Property")
93 {
94 widgetInfo.properties.push_back(PairString(node->findAttribute("key"), node->findAttribute("value")));
95 }
96 else if (node->getName() == "UserString")
97 {
98 widgetInfo.userStrings[node->findAttribute("key")] = node->findAttribute("value");
99 }
100 else if (node->getName() == "Controller")
101 {
102 ControllerInfo controllerInfo;
103 controllerInfo.type = node->findAttribute("type");
104
105 xml::ElementEnumerator prop = node->getElementEnumerator();
106 while (prop.next("Property"))
107 controllerInfo.properties[prop->findAttribute("key")] = prop->findAttribute("value");
108
109 widgetInfo.controllers.push_back(controllerInfo);
110 }
111 }
112
113 return widgetInfo;
114 }
115
116 VectorWidgetPtr ResourceLayout::createLayout(const std::string& _prefix, Widget* _parent)
117 {
118 VectorWidgetPtr widgets;
119
120 for (VectorWidgetInfo::iterator iter = mLayoutData.begin(); iter != mLayoutData.end(); ++iter)
121 {
122 Widget* widget = createWidget(*iter, _prefix, _parent);
123 widgets.push_back(widget);
124 }
125
126 return widgets;
127 }
128
129 Widget* ResourceLayout::createWidget(const WidgetInfo& _widgetInfo, const std::string& _prefix, Widget* _parent, bool _template)
130 {
131 std::string widgetName = _widgetInfo.name;
132 WidgetStyle style = _widgetInfo.style;
133 std::string widgetLayer = _widgetInfo.layer;
134
135 if (!widgetName.empty()) widgetName = _prefix + widgetName;
136
137 if (_parent != nullptr && style != WidgetStyle::Popup) widgetLayer.clear();
138 if (_parent == nullptr && widgetLayer.empty())
139 {
140 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() << "]");
141 }
142
143 IntCoord coord;
144 if (_widgetInfo.positionType == WidgetInfo::Pixels) coord = _widgetInfo.intCoord;
145 else if (_widgetInfo.positionType == WidgetInfo::Relative)
146 {
147 if (_parent == nullptr || style == WidgetStyle::Popup)
148 coord = CoordConverter::convertFromRelative(_widgetInfo.floatCoord, RenderManager::getInstance().getViewSize());
149 else
150 coord = CoordConverter::convertFromRelative(_widgetInfo.floatCoord, _parent->getClientCoord().size());
151 }
152
153 Widget* wid;
154 if (nullptr == _parent)
155 wid = Gui::getInstance().createWidgetT(_widgetInfo.type, _widgetInfo.skin, coord, _widgetInfo.align, widgetLayer, widgetName);
156 else if (_template)
157 wid = _parent->_createSkinWidget(style, _widgetInfo.type, _widgetInfo.skin, coord, _widgetInfo.align, widgetLayer, widgetName);
158 else
159 wid = _parent->createWidgetT(style, _widgetInfo.type, _widgetInfo.skin, coord, _widgetInfo.align, widgetLayer, widgetName);
160
161 for (VectorStringPairs::const_iterator iter = _widgetInfo.properties.begin(); iter != _widgetInfo.properties.end(); ++iter)
162 {
163 wid->setProperty(iter->first, iter->second);
164 }
165
166 for (MapString::const_iterator iter = _widgetInfo.userStrings.begin(); iter != _widgetInfo.userStrings.end(); ++iter)
167 {
168 wid->setUserString(iter->first, iter->second);
169 if (!_template)
170 LayoutManager::getInstance().eventAddUserString(wid, iter->first, iter->second);
171 }
172
173 for (VectorWidgetInfo::const_iterator iter = _widgetInfo.childWidgetsInfo.begin(); iter != _widgetInfo.childWidgetsInfo.end(); ++iter)
174 {
175 createWidget(*iter, _prefix, wid);
176 }
177
178 for (std::vector<ControllerInfo>::const_iterator iter = _widgetInfo.controllers.begin(); iter != _widgetInfo.controllers.end(); ++iter)
179 {
180 MyGUI::ControllerItem* item = MyGUI::ControllerManager::getInstance().createItem(iter->type);
181 if (item)
182 {
183 for (MapString::const_iterator iterProp = iter->properties.begin(); iterProp != iter->properties.end(); ++iterProp)
184 {
185 item->setProperty(iterProp->first, iterProp->second);
186 }
187 MyGUI::ControllerManager::getInstance().addItem(wid, item);
188 }
189 else
190 {
191 MYGUI_LOG(Warning, "Controller '" << iter->type << "' not found");
192 }
193 }
194
195 return wid;
196 }
197
198 const VectorWidgetInfo& ResourceLayout::getLayoutData() const
199 {
200 return mLayoutData;
201 }
202
203 } // namespace MyGUI
MyGUI::ControllerManager::addItem
void addItem(Widget *_widget, ControllerItem *_item)
Definition: MyGUI_ControllerManager.cpp:93
MyGUI::types::TCoord< int >
MyGUI::Widget
Definition: MyGUI_Widget.h:41
MyGUI::WidgetInfo::align
Align align
Definition: MyGUI_LayoutData.h:55
MyGUI::ResourceLayout::deserialization
virtual void deserialization(xml::ElementPtr _node, Version _version)
Definition: MyGUI_ResourceLayout.cpp:45
MyGUI::WidgetInfo::childWidgetsInfo
std::vector< WidgetInfo > childWidgetsInfo
Definition: MyGUI_LayoutData.h:48
MyGUI_Precompiled.h
MyGUI::WidgetInfo::positionType
PositionType positionType
Definition: MyGUI_LayoutData.h:61
MyGUI::UserData::setUserString
void setUserString(const std::string &_key, const std::string &_value)
Definition: MyGUI_WidgetUserData.cpp:37
MyGUI::Singleton< LayoutManager >::getInstance
static LayoutManager & getInstance()
Definition: MyGUI_Singleton.h:53
MyGUI::types::TCoord::size
TSize< T > size() const
Definition: MyGUI_TCoord.h:205
MyGUI::WidgetStyle::parse
static WidgetStyle parse(const std::string &_value)
Definition: MyGUI_WidgetStyle.h:51
MyGUI::xml::Element::findAttribute
bool findAttribute(const std::string &_name, std::string &_value)
Definition: MyGUI_XmlDocument.cpp:261
MyGUI::ResourceLayout::mLayoutData
VectorWidgetInfo mLayoutData
Definition: MyGUI_ResourceLayout.h:58
MyGUI::Align::parse
static Align parse(const std::string &_value)
Definition: MyGUI_Align.h:142
MyGUI::CoordConverter::convertFromRelative
static IntCoord convertFromRelative(const FloatCoord &_coord, const IntSize &_view)
Definition: MyGUI_CoordConverter.h:49
MyGUI::WidgetInfo::style
WidgetStyle style
Definition: MyGUI_LayoutData.h:56
MyGUI::WidgetInfo::intCoord
IntCoord intCoord
Definition: MyGUI_LayoutData.h:62
MyGUI::RenderManager::getViewSize
virtual const IntSize & getViewSize() const =0
MyGUI::WidgetInfo::Relative
Definition: MyGUI_LayoutData.h:60
MyGUI::PairString
std::pair< std::string, std::string > PairString
Definition: MyGUI_Types.h:55
MyGUI::ControllerItem::setProperty
virtual void setProperty(const std::string &_key, const std::string &_value)
Definition: MyGUI_ControllerItem.h:45
MyGUI::Widget::getClientCoord
IntCoord getClientCoord()
Definition: MyGUI_Widget.cpp:442
MyGUI::xml::ElementEnumerator
Definition: MyGUI_XmlDocument.h:124
MyGUI::ControllerInfo
Definition: MyGUI_LayoutData.h:31
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition: MyGUI_Diagnostic.h:38
MyGUI_LayoutManager.h
MyGUI_RenderManager.h
MyGUI::WidgetInfo::skin
std::string skin
Definition: MyGUI_LayoutData.h:54
MyGUI_CoordConverter.h
MyGUI::ControllerInfo::type
std::string type
Definition: MyGUI_LayoutData.h:33
MyGUI::xml::ElementEnumerator::next
bool next()
Definition: MyGUI_XmlDocument.cpp:115
MyGUI::ResourceLayout::createLayout
VectorWidgetPtr createLayout(const std::string &_prefix="", Widget *_parent=0)
Definition: MyGUI_ResourceLayout.cpp:116
MyGUI_ControllerManager.h
MyGUI::ResourceLayout::ResourceLayout
ResourceLayout()
Definition: MyGUI_ResourceLayout.cpp:34
MyGUI::VectorWidgetPtr
std::vector< Widget * > VectorWidgetPtr
Definition: MyGUI_WidgetDefines.h:33
MyGUI::WidgetInfo
Definition: MyGUI_LayoutData.h:37
MyGUI::xml::Element
Definition: MyGUI_XmlDocument.h:168
MyGUI::WidgetInfo::layer
std::string layer
Definition: MyGUI_LayoutData.h:58
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:1137
MyGUI::Version
Definition: MyGUI_Version.h:32
MyGUI::WidgetInfo::floatCoord
FloatCoord floatCoord
Definition: MyGUI_LayoutData.h:63
MyGUI_ResourceLayout.h
MyGUI::WidgetInfo::userStrings
MapString userStrings
Definition: MyGUI_LayoutData.h:51
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:912
MyGUI::ResourceLayout::getLayoutData
const VectorWidgetInfo & getLayoutData() const
Definition: MyGUI_ResourceLayout.cpp:198
MyGUI::WidgetStyle
Definition: MyGUI_WidgetStyle.h:31
MyGUI::LayoutManager::eventAddUserString
EventHandle_AddUserStringDelegate eventAddUserString
Definition: MyGUI_LayoutManager.h:72
MyGUI::IResource::mResourceName
std::string mResourceName
Definition: MyGUI_IResource.h:67
MyGUI::WidgetInfo::type
std::string type
Definition: MyGUI_LayoutData.h:53
MyGUI_Widget.h
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition: MyGUI_XmlDocument.cpp:367
MyGUI::ControllerItem
Definition: MyGUI_ControllerItem.h:34
MyGUI::xml::Element::getName
const std::string & getName() const
Definition: MyGUI_XmlDocument.cpp:347
MyGUI_Gui.h
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:303
MyGUI::types::TCoord< int >::parse
static TCoord< int > parse(const std::string &_value)
Definition: MyGUI_TCoord.h:222
MyGUI::WidgetStyle::Popup
Definition: MyGUI_WidgetStyle.h:36
MyGUI::VectorWidgetInfo
std::vector< WidgetInfo > VectorWidgetInfo
Definition: MyGUI_LayoutData.h:66
MyGUI::WidgetInfo::controllers
std::vector< ControllerInfo > controllers
Definition: MyGUI_LayoutData.h:52
MyGUI::ResourceLayout::parseWidget
WidgetInfo parseWidget(xml::ElementEnumerator &_widget)
Definition: MyGUI_ResourceLayout.cpp:56
MyGUI::Widget::setProperty
void setProperty(const std::string &_key, const std::string &_value)
Definition: MyGUI_Widget.cpp:1073
MyGUI::WidgetInfo::name
std::string name
Definition: MyGUI_LayoutData.h:57
MyGUI::ControllerInfo::properties
MapString properties
Definition: MyGUI_LayoutData.h:34
MyGUI::ResourceLayout::createWidget
Widget * createWidget(const WidgetInfo &_widgetInfo, const std::string &_prefix="", Widget *_parent=0, bool _template=false)
Definition: MyGUI_ResourceLayout.cpp:129
MyGUI::ControllerManager::createItem
ControllerItem * createItem(const std::string &_type)
Definition: MyGUI_ControllerManager.cpp:87
MyGUI::WidgetInfo::Pixels
Definition: MyGUI_LayoutData.h:60
MyGUI::WidgetInfo::properties
VectorStringPairs properties
Definition: MyGUI_LayoutData.h:50