XmlTemplate.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : CXmlTemplate implementation.
  9. #include "EditorDefs.h"
  10. #include "XmlTemplate.h"
  11. //////////////////////////////////////////////////////////////////////////
  12. // CXmlTemplate implementation
  13. //////////////////////////////////////////////////////////////////////////
  14. //////////////////////////////////////////////////////////////////////////
  15. void CXmlTemplate::GetValues(XmlNodeRef& node, const XmlNodeRef& fromNode)
  16. {
  17. assert(node != 0 && fromNode != 0);
  18. if (!node)
  19. {
  20. gEnv->pLog->LogError("CXmlTemplate::GetValues invalid node. Possible problems with Editor folder.");
  21. return;
  22. }
  23. for (int i = 0; i < node->getChildCount(); i++)
  24. {
  25. XmlNodeRef prop = node->getChild(i);
  26. if (prop->getChildCount() == 0)
  27. {
  28. QString value;
  29. if (fromNode->getAttr(prop->getTag(), value))
  30. {
  31. prop->setAttr("Value", value.toUtf8().data());
  32. }
  33. }
  34. else
  35. {
  36. // Have childs.
  37. XmlNodeRef fromNodeChild = fromNode->findChild(prop->getTag());
  38. if (fromNodeChild)
  39. {
  40. CXmlTemplate::GetValues(prop, fromNodeChild);
  41. }
  42. }
  43. }
  44. }
  45. //////////////////////////////////////////////////////////////////////////
  46. void CXmlTemplate::SetValues(const XmlNodeRef& node, XmlNodeRef& toNode)
  47. {
  48. assert(node != 0 && toNode != 0);
  49. toNode->removeAllAttributes();
  50. toNode->removeAllChilds();
  51. assert(node);
  52. if (!node)
  53. {
  54. gEnv->pLog->LogError("CXmlTemplate::SetValues invalid node. Possible problems with Editor folder.");
  55. return;
  56. }
  57. for (int i = 0; i < node->getChildCount(); i++)
  58. {
  59. XmlNodeRef prop = node->getChild(i);
  60. if (prop)
  61. {
  62. if (prop->getChildCount() > 0)
  63. {
  64. XmlNodeRef childToNode = toNode->newChild(prop->getTag());
  65. if (childToNode)
  66. {
  67. CXmlTemplate::SetValues(prop, childToNode);
  68. }
  69. }
  70. else
  71. {
  72. QString value;
  73. prop->getAttr("Value", value);
  74. toNode->setAttr(prop->getTag(), value.toUtf8().data());
  75. }
  76. }
  77. else
  78. {
  79. assert(!"nullptr returned from node->GetChild()");
  80. }
  81. }
  82. }
  83. //////////////////////////////////////////////////////////////////////////
  84. bool CXmlTemplate::SetValues(const XmlNodeRef& node, XmlNodeRef& toNode, const XmlNodeRef& modifiedNode)
  85. {
  86. assert(node != 0 && toNode != 0 && modifiedNode != 0);
  87. for (int i = 0; i < node->getChildCount(); i++)
  88. {
  89. XmlNodeRef prop = node->getChild(i);
  90. if (prop)
  91. {
  92. if (prop->getChildCount() > 0)
  93. {
  94. XmlNodeRef childToNode = toNode->findChild(prop->getTag());
  95. if (childToNode)
  96. {
  97. if (CXmlTemplate::SetValues(prop, childToNode, modifiedNode))
  98. {
  99. return true;
  100. }
  101. }
  102. }
  103. else if (prop == modifiedNode)
  104. {
  105. QString value;
  106. prop->getAttr("Value", value);
  107. toNode->setAttr(prop->getTag(), value.toUtf8().data());
  108. return true;
  109. }
  110. }
  111. else
  112. {
  113. assert(!"nullptr returned from node->GetChild()");
  114. }
  115. }
  116. return false;
  117. }
  118. //////////////////////////////////////////////////////////////////////////
  119. void CXmlTemplate::AddParam(XmlNodeRef& templ, const char* sName, bool value)
  120. {
  121. XmlNodeRef param = templ->newChild(sName);
  122. param->setAttr("type", "Bool");
  123. param->setAttr("value", value);
  124. }
  125. //////////////////////////////////////////////////////////////////////////
  126. void CXmlTemplate::AddParam(XmlNodeRef& templ, const char* sName, int value, int min, int max)
  127. {
  128. XmlNodeRef param = templ->newChild(sName);
  129. param->setAttr("type", "Int");
  130. param->setAttr("value", value);
  131. param->setAttr("min", min);
  132. param->setAttr("max", max);
  133. }
  134. //////////////////////////////////////////////////////////////////////////
  135. void CXmlTemplate::AddParam(XmlNodeRef& templ, const char* sName, float value, float min, float max)
  136. {
  137. XmlNodeRef param = templ->newChild(sName);
  138. param->setAttr("type", "Float");
  139. param->setAttr("value", value);
  140. param->setAttr("min", min);
  141. param->setAttr("max", max);
  142. }
  143. //////////////////////////////////////////////////////////////////////////
  144. void CXmlTemplate::AddParam(XmlNodeRef& templ, const char* sName, const char* sValue)
  145. {
  146. XmlNodeRef param = templ->newChild(sName);
  147. param->setAttr("type", "String");
  148. param->setAttr("value", sValue);
  149. }
  150. //////////////////////////////////////////////////////////////////////////
  151. //
  152. // CXmlTemplateRegistry implementation
  153. //
  154. //////////////////////////////////////////////////////////////////////////
  155. //////////////////////////////////////////////////////////////////////////
  156. CXmlTemplateRegistry::CXmlTemplateRegistry()
  157. {}
  158. //////////////////////////////////////////////////////////////////////////
  159. void CXmlTemplateRegistry::LoadTemplates(const QString& path)
  160. {
  161. m_templates.Clear();
  162. QString dir = Path::AddPathSlash(path);
  163. IFileUtil::FileArray files;
  164. CFileUtil::ScanDirectory(dir, "*.xml", files, false);
  165. for (int k = 0; k < files.size(); k++)
  166. {
  167. XmlNodeRef child;
  168. // Construct the full filepath of the current file
  169. XmlNodeRef node = XmlHelpers::LoadXmlFromFile((dir + files[k].filename).toUtf8().data());
  170. if (node != nullptr && node->isTag("Templates"))
  171. {
  172. QString name;
  173. for (int i = 0; i < node->getChildCount(); i++)
  174. {
  175. child = node->getChild(i);
  176. AddTemplate(child->getTag(), child);
  177. }
  178. }
  179. }
  180. }
  181. //////////////////////////////////////////////////////////////////////////
  182. void CXmlTemplateRegistry::AddTemplate(const QString& name, XmlNodeRef& tmpl)
  183. {
  184. m_templates[name] = tmpl;
  185. }
  186. //////////////////////////////////////////////////////////////////////////
  187. XmlNodeRef CXmlTemplateRegistry::FindTemplate(const QString& name)
  188. {
  189. XmlNodeRef node;
  190. if (m_templates.Find(name, node))
  191. {
  192. return node;
  193. }
  194. return nullptr;
  195. }