MyGUI_ResourceLayout.cpp
Go to the documentation of this file.
/*
* This source file is part of MyGUI. For the latest info, see http://mygui.info/
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/
#include "MyGUI_Precompiled.h"
#include "MyGUI_ResourceLayout.h"
#include "MyGUI_CoordConverter.h"
#include "MyGUI_RenderManager.h"
#include "MyGUI_ControllerManager.h"
#include "MyGUI_LayoutManager.h"
#include "MyGUI_Widget.h"
#include "MyGUI_Gui.h"
namespace MyGUI
{
ResourceLayout::ResourceLayout(xml::ElementPtr _node, std::string_view _fileName)
{
// FIXME hardcoded version
deserialization(_node, Version(1, 0, 0));
mResourceName = _fileName;
}
void ResourceLayout::deserialization(xml::ElementPtr _node, Version _version)
{
Base::deserialization(_node, _version);
mLayoutData.clear();
xml::ElementEnumerator widget = _node->getElementEnumerator();
while (widget.next("Widget"))
mLayoutData.push_back(parseWidget(widget));
}
WidgetInfo ResourceLayout::parseWidget(xml::ElementEnumerator& _widget)
{
WidgetInfo widgetInfo;
std::string tmp;
_widget->findAttribute("type", widgetInfo.type);
_widget->findAttribute("skin", widgetInfo.skin);
_widget->findAttribute("layer", widgetInfo.layer);
if (_widget->findAttribute("align", tmp))
widgetInfo.align = Align::parse(tmp);
_widget->findAttribute("name", widgetInfo.name);
if (_widget->findAttribute("style", tmp))
widgetInfo.style = WidgetStyle::parse(tmp);
if (_widget->findAttribute("position", tmp))
{
widgetInfo.intCoord = IntCoord::parse(tmp);
widgetInfo.positionType = WidgetInfo::Pixels;
}
else if (_widget->findAttribute("position_real", tmp))
{
widgetInfo.floatCoord = FloatCoord::parse(tmp);
widgetInfo.positionType = WidgetInfo::Relative;
}
// берем детей и крутимся
xml::ElementEnumerator node = _widget->getElementEnumerator();
while (node.next())
{
if (node->getName() == "Widget")
{
widgetInfo.childWidgetsInfo.push_back(parseWidget(node));
}
else if (node->getName() == "Property")
{
widgetInfo.properties.emplace_back(node->findAttribute("key"), node->findAttribute("value"));
}
else if (node->getName() == "UserString")
{
std::string_view key = node->findAttribute("key");
std::string_view value = node->findAttribute("value");
mapSet(widgetInfo.userStrings, key, value);
}
else if (node->getName() == "Controller")
{
ControllerInfo controllerInfo;
controllerInfo.type = node->findAttribute("type");
xml::ElementEnumerator prop = node->getElementEnumerator();
while (prop.next("Property"))
{
std::string_view key = prop->findAttribute("key");
std::string_view value = prop->findAttribute("value");
mapSet(controllerInfo.properties, key, value);
}
widgetInfo.controllers.push_back(controllerInfo);
}
}
return widgetInfo;
}
VectorWidgetPtr ResourceLayout::createLayout(std::string_view _prefix, Widget* _parent)
{
VectorWidgetPtr widgets;
for (auto& iter : mLayoutData)
{
Widget* widget = createWidget(iter, _prefix, _parent);
widgets.push_back(widget);
}
return widgets;
}
Widget* ResourceLayout::createWidget(
const WidgetInfo& _widgetInfo,
std::string_view _prefix,
Widget* _parent,
bool _template)
{
std::string widgetName;
WidgetStyle style = _widgetInfo.style;
std::string_view widgetLayer = _widgetInfo.layer;
if (!_widgetInfo.name.empty())
{
widgetName = _prefix;
widgetName += _widgetInfo.name;
}
if (_parent != nullptr && style != WidgetStyle::Popup)
widgetLayer = {};
if (_parent == nullptr && widgetLayer.empty())
{
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() << "]");
}
IntCoord coord;
if (_widgetInfo.positionType == WidgetInfo::Pixels)
coord = _widgetInfo.intCoord;
else if (_widgetInfo.positionType == WidgetInfo::Relative)
{
if (_parent == nullptr || style == WidgetStyle::Popup)
coord = CoordConverter::convertFromRelative(
_widgetInfo.floatCoord,
RenderManager::getInstance().getViewSize());
else
coord = CoordConverter::convertFromRelative(_widgetInfo.floatCoord, _parent->getClientCoord().size());
}
Widget* wid;
if (nullptr == _parent)
wid = Gui::getInstance().createWidgetT(
_widgetInfo.type,
_widgetInfo.skin,
coord,
_widgetInfo.align,
widgetLayer,
widgetName);
else if (_template)
wid = _parent->_createSkinWidget(
style,
_widgetInfo.type,
_widgetInfo.skin,
coord,
_widgetInfo.align,
widgetLayer,
widgetName);
else
wid = _parent->createWidgetT(
style,
_widgetInfo.type,
_widgetInfo.skin,
coord,
_widgetInfo.align,
widgetLayer,
widgetName);
for (const auto& property : _widgetInfo.properties)
{
wid->setProperty(property.first, property.second);
}
for (const auto& userString : _widgetInfo.userStrings)
{
wid->setUserString(userString.first, userString.second);
if (!_template)
LayoutManager::getInstance().eventAddUserString(wid, userString.first, userString.second);
}
for (const auto& iter : _widgetInfo.childWidgetsInfo)
{
createWidget(iter, _prefix, wid);
}
for (const auto& iter : _widgetInfo.controllers)
{
MyGUI::ControllerItem* item = MyGUI::ControllerManager::getInstance().createItem(iter.type);
if (item)
{
for (const auto& property : iter.properties)
{
item->setProperty(property.first, property.second);
}
MyGUI::ControllerManager::getInstance().addItem(wid, item);
}
else
{
MYGUI_LOG(Warning, "Controller '" << iter.type << "' not found");
}
}
LayoutManager::getInstance().eventCreateWidget(wid, _widgetInfo);
return wid;
}
const VectorWidgetInfo& ResourceLayout::getLayoutData() const
{
return mLayoutData;
}
} // 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