QGearsBackground2DFileXMLSerializer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include "common/TypeDefine.h"
  17. #include "data/QGearsXMLSerializer.h"
  18. #include "QGearsBackground2DFile.h"
  19. namespace QGears{
  20. /**
  21. * Handles the serialization of 2D background XML files.
  22. */
  23. class Background2DFileXMLSerializer : public XMLSerializer{
  24. public:
  25. /**
  26. * Constructor.
  27. */
  28. Background2DFileXMLSerializer();
  29. /**
  30. * Destructor.
  31. */
  32. virtual ~Background2DFileXMLSerializer();
  33. /**
  34. * Imports a 2D background XML file.
  35. *
  36. * @param stream[in] The contents of the background file.
  37. * @param dest[out] The formed background file.
  38. */
  39. virtual void ImportBackground2DFile(
  40. Ogre::DataStreamPtr &stream, Background2DFile *dest
  41. );
  42. protected:
  43. /**
  44. * Reads a file header and sets the instance data.
  45. *
  46. * @param stream[in] The contents of the HRC file.
  47. */
  48. virtual void ReadHeader(TiXmlNode *node);
  49. /**
  50. * Reads an XMl node attribute as tile blending information.
  51. *
  52. * @param node[in] The XML node.
  53. * @param attribute[in] The name of the attribute to read.
  54. * @param dest[out] The value of the specified attribute. If the
  55. * attribute doesn't exists, the value of def will be set here.
  56. * @return True if the attribute was actually read, false if it
  57. * didn't exist and the default value was loaded into dest.
  58. */
  59. virtual bool ReadAttribute(
  60. TiXmlNode &node, const String &attribute, Blending &dest,
  61. const Blending &def
  62. );
  63. using XMLSerializer::ReadAttribute;
  64. /**
  65. * Reads an XML node as a tile.
  66. *
  67. * @param node[in] The XML node to read.
  68. * @param dest[out] The formed tile data.
  69. */
  70. virtual void readObject(TiXmlNode &node, Tile &dest);
  71. /**
  72. * Reads an XML node as an animation.
  73. *
  74. * @param node[in] The XML node to read.
  75. * @param dest[out] The formed animation data.
  76. */
  77. virtual void readObject(TiXmlNode &node, Animation &dest);
  78. /**
  79. * Reads an XML node as a key frame.
  80. *
  81. * @param node[in] The XML node to read.
  82. * @param dest[out] The formed key frame data.
  83. */
  84. virtual void readObject(TiXmlNode &node, KeyFrame &dest);
  85. /**
  86. * Alpha-type blending key.
  87. */
  88. static const String BLENDING_ALPHA;
  89. /**
  90. * Add-type blending key.
  91. */
  92. static const String BLENDING_ADD;
  93. /**
  94. * Substract-type blending key.
  95. */
  96. static const String BLENDING_SUBTRACT;
  97. /**
  98. * Reads a XML node as a vector.
  99. *
  100. * @param node[in] The XML node to read.
  101. * @param tag[in] XML tag to read.
  102. * @param dest[out] The vector data will be loaded here.
  103. */
  104. template<typename ValueType> void ReadVector(
  105. TiXmlNode &node, const String &tag, std::vector<ValueType> &dest
  106. ){
  107. dest.clear();
  108. TiXmlNode* child(node.FirstChild());
  109. while (child != NULL){
  110. if (
  111. child->Type() == TiXmlNode::TINYXML_ELEMENT
  112. && child->ValueStr() == tag
  113. ){
  114. ValueType in_tmp;
  115. readObject(*child, in_tmp);
  116. dest.push_back(in_tmp);
  117. }
  118. child = child->NextSibling();
  119. }
  120. }
  121. /**
  122. * Reads a XML node as a map.
  123. *
  124. * @param node[in] The XML node to read.
  125. * @param tag[in] XML tag to read.
  126. * @param key_attribute[in] @todo Understand and document.
  127. * @param dest[out] The map data will be loaded here.
  128. */
  129. template<typename KeyType, typename ValueType> void ReadMap(
  130. TiXmlNode& node, const String& tag, const String& key_attribute,
  131. std::map<KeyType, ValueType> &dest
  132. ){
  133. dest.clear();
  134. TiXmlNode* child(node.FirstChild());
  135. while (child != NULL){
  136. if (
  137. child->Type() == TiXmlNode::TINYXML_ELEMENT
  138. && child->ValueStr() == tag
  139. ){
  140. KeyType key;
  141. if (ReadAttribute(*child, key_attribute, key)){
  142. ValueType in_tmp;
  143. readObject(*child, in_tmp);
  144. dest[key] = in_tmp;
  145. }
  146. }
  147. child = child->NextSibling();
  148. }
  149. }
  150. };
  151. }