1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/*!
@file
@author Albert Semenov
@date 07/2012
*/
#include "Precompiled.h"
#include "ImageExportSerializer.h"
#include "FactoryManager.h"
#include "DataManager.h"
#include "DataTypeManager.h"
#include "PropertyUtility.h"
namespace tools
{
FACTORY_ITEM_ATTRIBUTE(ImageExportSerializer)
ImageExportSerializer::ImageExportSerializer()
{
}
ImageExportSerializer::~ImageExportSerializer()
{
}
void ImageExportSerializer::serialization(pugi::xml_document& _doc)
{
pugi::xml_node root = _doc.append_child("MyGUI");
root.append_attribute("type").set_value("Resource");
root.append_attribute("version").set_value("1.1");
DataPtr data = DataManager::getInstance().getRoot();
for (Data::VectorData::const_iterator child = data->getChilds().begin(); child != data->getChilds().end(); child ++)
writeImage(root, (*child));
}
bool ImageExportSerializer::deserialization(pugi::xml_document& _doc)
{
if (_doc.select_single_node("MyGUI[@type=\"Resource\"]").node().empty())
return false;
pugi::xpath_node_set nodes = _doc.select_nodes("MyGUI/Resource[@type=\"ResourceImageSet\"]");
for (pugi::xpath_node_set::const_iterator node = nodes.begin(); node != nodes.end(); node ++)
parseImage((*node).node());
updateImageProperty(DataManager::getInstance().getRoot());
return true;
}
void ImageExportSerializer::parseImage(pugi::xml_node _node)
{
DataPtr data = Data::CreateInstance();
data->setType(DataTypeManager::getInstance().getType("Image"));
data->setPropertyValue("Name", _node.attribute("name").value());
DataManager::getInstance().getRoot()->addChild(data);
pugi::xpath_node_set nodes = _node.select_nodes("Group");
for (pugi::xpath_node_set::const_iterator node = nodes.begin(); node != nodes.end(); node ++)
parseGroup((*node).node(), data);
}
void ImageExportSerializer::parseGroup(pugi::xml_node _node, DataPtr _parent)
{
DataPtr data = Data::CreateInstance();
data->setType(DataTypeManager::getInstance().getType("Group"));
std::string value = _node.attribute("name").value();
if (value.empty())
value = "unnamed";
data->setPropertyValue("Name", value);
data->setPropertyValue("Texture", _node.attribute("texture").value());
MyGUI::IntSize size = MyGUI::IntSize::parse(_node.attribute("size").value());
data->setPropertyValue("Size", MyGUI::IntCoord(0, 0, size.width, size.height).print());
_parent->addChild(data);
pugi::xpath_node_set nodes = _node.select_nodes("Index");
for (pugi::xpath_node_set::const_iterator node = nodes.begin(); node != nodes.end(); node ++)
parseIndex((*node).node(), data);
}
void ImageExportSerializer::parseIndex(pugi::xml_node _node, DataPtr _parent)
{
DataPtr data = Data::CreateInstance();
data->setType(DataTypeManager::getInstance().getType("Index"));
std::string value = _node.attribute("name").value();
if (value.empty())
value = "unnamed";
data->setPropertyValue("Name", value);
data->setPropertyValue("Rate", _node.attribute("rate").value());
_parent->addChild(data);
pugi::xpath_node_set nodes = _node.select_nodes("Frame");
for (pugi::xpath_node_set::const_iterator node = nodes.begin(); node != nodes.end(); node ++)
parseFrame((*node).node(), data);
}
void ImageExportSerializer::parseFrame(pugi::xml_node _node, DataPtr _parent)
{
DataPtr data = Data::CreateInstance();
data->setType(DataTypeManager::getInstance().getType("Frame"));
data->setPropertyValue("Point", _node.attribute("point").value());
std::string value = _node.attribute("count").value();
if (value.empty())
value = "1";
data->setPropertyValue("Count", value);
_parent->addChild(data);
}
void ImageExportSerializer::writeImage(pugi::xml_node _parent, DataPtr _data)
{
pugi::xml_node node = _parent.append_child("Resource");
node.append_attribute("type").set_value("ResourceImageSet");
node.append_attribute("name").set_value(_data->getPropertyValue("Name").c_str());
for (Data::VectorData::const_iterator child = _data->getChilds().begin(); child != _data->getChilds().end(); child ++)
writeGroup(node, (*child));
}
void ImageExportSerializer::writeGroup(pugi::xml_node _parent, DataPtr _data)
{
pugi::xml_node node = _parent.append_child("Group");
node.append_attribute("name").set_value(_data->getPropertyValue("Name").c_str());
node.append_attribute("texture").set_value(_data->getPropertyValue("Texture").c_str());
node.append_attribute("size").set_value(MyGUI::IntCoord::parse(_data->getPropertyValue("Size")).size().print().c_str());
for (Data::VectorData::const_iterator child = _data->getChilds().begin(); child != _data->getChilds().end(); child ++)
writeIndex(node, (*child));
}
void ImageExportSerializer::writeIndex(pugi::xml_node _parent, DataPtr _data)
{
pugi::xml_node node = _parent.append_child("Index");
node.append_attribute("name").set_value(_data->getPropertyValue("Name").c_str());
std::string value = _data->getPropertyValue("Rate");
if (!value.empty())
node.append_attribute("rate").set_value(value.c_str());
for (Data::VectorData::const_iterator child = _data->getChilds().begin(); child != _data->getChilds().end(); child ++)
writeFrame(node, (*child));
}
void ImageExportSerializer::writeFrame(pugi::xml_node _parent, DataPtr _data)
{
pugi::xml_node node = _parent.append_child("Frame");
node.append_attribute("point").set_value(_data->getPropertyValue("Point").c_str());
size_t count = MyGUI::utility::parseValue<size_t>(_data->getPropertyValue("Count"));
if (count > 1)
node.append_attribute("count").set_value(MyGUI::utility::toString(count).c_str());
}
void ImageExportSerializer::updateImageProperty(DataPtr _data)
{
const Data::VectorData& childs = _data->getChilds();
for (Data::VectorData::const_iterator child = childs.begin(); child != childs.end(); child++)
{
bool unique = PropertyUtility::isUniqueName((*child), "Name");
(*child)->setPropertyValue("UniqueName", unique);
updateGroupProperty(*child);
}
}
void ImageExportSerializer::updateGroupProperty(DataPtr _data)
{
const Data::VectorData& childs = _data->getChilds();
for (Data::VectorData::const_iterator child = childs.begin(); child != childs.end(); child++)
{
bool unique = PropertyUtility::isUniqueName((*child), "Name");
(*child)->setPropertyValue("UniqueName", unique);
updateIndexProperty(*child);
}
}
void ImageExportSerializer::updateIndexProperty(DataPtr _data)
{
const Data::VectorData& childs = _data->getChilds();
for (Data::VectorData::const_iterator child = childs.begin(); child != childs.end(); child++)
{
bool unique = PropertyUtility::isUniqueName((*child), "Name");
(*child)->setPropertyValue("UniqueName", unique);
}
MyGUI::IntPoint point = getFirstFramePoint(_data);
MyGUI::IntSize size = _data->getPropertyValue<MyGUI::IntCoord>("Size").size();
MyGUI::IntCoord coord(point, size);
_data->setPropertyValue("Size", coord);
}
MyGUI::IntPoint ImageExportSerializer::getFirstFramePoint(DataPtr _data)
{
if (_data->getType()->getName() != "Group")
return MyGUI::IntPoint();
if (_data->getChilds().size() != 0)
{
DataPtr child = _data->getChildByIndex(0);
if (child->getChilds().size() != 0)
{
return child->getChildByIndex(0)->getPropertyValue<MyGUI::IntPoint>("Point");
}
}
return MyGUI::IntPoint();
}
}
|