Utilites.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <OgreColourValue.h>
  17. #include <OgreMatrix4.h>
  18. #include <OgreString.h>
  19. #include <OgreStringVector.h>
  20. #include <Ogre.h>
  21. #include <Overlay/OgreUTFString.h>
  22. #include <OIS/OIS.h>
  23. #include <tinyxml.h>
  24. // TODO: All the methods defined here that refer to XML files are implemented
  25. // as methods of XmlFile. Can they be deleted from here?
  26. /**
  27. * Retrieves a boolean from an XMl tag.
  28. *
  29. * @param node[in] The XML node.
  30. * @param tag[in] The name of the tag.
  31. * @param def[in] Default value, in case the tag is not found or it's not a
  32. * boolean value.
  33. * @return Boolean value of the tag. If it's not found or it's not a boolean,
  34. * def is returned.
  35. */
  36. bool GetBool(TiXmlNode* node, const Ogre::String& tag, bool def = false);
  37. /**
  38. * Retrieves an integer from an XMl tag.
  39. *
  40. * @param node[in] The XML node.
  41. * @param tag[in] The name of the tag.
  42. * @param def[in] Default value, in case the tag is not found or it's not a
  43. * numeric value.
  44. * @return Integer value of the tag. If it's not found or it's not a number,
  45. * def is returned.
  46. */
  47. int GetInt(TiXmlNode* node, const Ogre::String& tag, int def = 0);
  48. /**
  49. * Retrieves a decimal from an XMl tag.
  50. *
  51. * @param node[in] The XML node.
  52. * @param tag[in] The name of the tag.
  53. * @param def[in] Default value, in case the tag is not found or it's not a
  54. * numeric value.
  55. * @return Floating value of the tag. If it's not found or it's not a number,
  56. * def is returned.
  57. */
  58. float GetFloat(TiXmlNode* node, const Ogre::String& tag, float def = 0.0f);
  59. /**
  60. * Retrieves a string from an XMl tag.
  61. *
  62. * @param node[in] The XML node.
  63. * @param tag[in] The name of the tag.
  64. * @param def[in] Default value, in case the tag is not found.
  65. * @return String value of the tag. If it's not found, def is returned.
  66. */
  67. const Ogre::String GetString(
  68. TiXmlNode* node, const Ogre::String& tag, const Ogre::String& def = ""
  69. );
  70. /**
  71. * Retrieves a string from an XMl tag.
  72. *
  73. * @param node[in] The XML node.
  74. * @param tag[in] The name of the tag.
  75. * @param def[in] Default value, in case the tag is not found.
  76. * @return String value of the tag, in UFT8. If not found, def is returned.
  77. */
  78. const Ogre::UTFString GetUTFString(
  79. TiXmlNode* node, const Ogre::String& tag, const Ogre::UTFString& def = ""
  80. );
  81. /**
  82. * Retrieves a 2-dimensional vector from an XMl tag.
  83. *
  84. * @param node[in] The XML node.
  85. * @param tag[in] The name of the tag.
  86. * @param def[in] Default value, in case the tag is not found.
  87. * @return Vector in the tag. If it's not found, def is returned.
  88. */
  89. const Ogre::Vector2 GetVector2(
  90. TiXmlNode* node, const Ogre::String& tag,
  91. const Ogre::Vector2& def = Ogre::Vector2::ZERO
  92. );
  93. /**
  94. * Retrieves a 3-dimensional vector from an XMl tag.
  95. *
  96. * @param node[in] The XML node.
  97. * @param tag[in] The name of the tag.
  98. * @param def[in] Default value, in case the tag is not found.
  99. * @return Vector in the tag. If it's not found, def is returned.
  100. */
  101. const Ogre::Vector3 GetVector3(
  102. TiXmlNode* node, const Ogre::String& tag,
  103. const Ogre::Vector3& def = Ogre::Vector3::ZERO
  104. );
  105. /**
  106. * Retrieves a 4-dimensional vector from an XMl tag.
  107. *
  108. * @param node[in] The XML node.
  109. * @param tag[in] The name of the tag.
  110. * @param def[in] Default value, in case the tag is not found.
  111. * @return Vector in the tag. If it's not found, def is returned.
  112. */
  113. const Ogre::Vector4 GetVector4(
  114. TiXmlNode* node, const Ogre::String& tag,
  115. const Ogre::Vector4& def = Ogre::Vector4::ZERO
  116. );
  117. /**
  118. * Retrieves a 4-dimensional matrix from an XMl tag.
  119. *
  120. * @param node[in] The XML node.
  121. * @param tag[in] The name of the tag.
  122. * @param def[in] Default value, in case the tag is not found.
  123. * @return Martix in the tag. If it's not found, def is returned.
  124. */
  125. const Ogre::Matrix4 GetMatrix4(
  126. TiXmlNode* node, const Ogre::String& tag,
  127. const Ogre::Matrix4& def = Ogre::Matrix4::IDENTITY
  128. );
  129. /**
  130. * Retrieves a quaternion from an XMl tag.
  131. *
  132. * @param node[in] The XML node.
  133. * @param tag[in] The name of the tag.
  134. * @param def[in] Default value, in case the tag is not found.
  135. * @return quaternion in the tag. If it's not found, def is returned.
  136. */
  137. const Ogre::Quaternion GetQuaternion(
  138. TiXmlNode* node, const Ogre::String& tag,
  139. const Ogre::Quaternion& def = Ogre::Quaternion::IDENTITY
  140. );
  141. /**
  142. * Retrieves a colour from an XMl tag.
  143. *
  144. * @param node[in] The XML node.
  145. * @param tag[in] The name of the tag.
  146. * @param def[in] Default value, in case the tag is not found.
  147. * @return Colour in the tag. If it's not found, def is returned.
  148. */
  149. const Ogre::ColourValue GetColourValue(
  150. TiXmlNode* node, const Ogre::String& tag,
  151. const Ogre::ColourValue& def = Ogre::ColourValue::ZERO
  152. );
  153. /**
  154. * Parses percentage strings
  155. *
  156. * Accepts strings in the formats "A" "A%" "A%B", where A and B are real
  157. * values.
  158. *
  159. * @param value_percent[out] Percent value
  160. * @param value[out] String numeric value.
  161. * @param string[in] Input string.
  162. * @example "80.4%" -> value = 0, percent_value = 80.4
  163. * @example "80.4%20.6" -> value = 20.6, percent_value = 80.4
  164. * @example "80.4" -> value = 80.4, percent_value = 0
  165. *
  166. */
  167. void ParsePercent(
  168. float& value_percent, float& value, const Ogre::String& string
  169. );
  170. /**
  171. * Parses a keyframe time string.
  172. *
  173. * It acceps input string of numeric values and numeric values followed by a
  174. * percentage sign
  175. *
  176. * @param length[in] Keyframe duration, used only for percentages.
  177. * @param string[in] Input string
  178. * @return Keyframe time.
  179. * @example "7.2" -> 7.2
  180. * @example "7.2%" -> length * 7.2
  181. */
  182. float ParseKeyFrameTime(const float length, const Ogre::String& string);
  183. /**
  184. * Creates a name.
  185. *
  186. * @prefix[in] Name prefix.
  187. * @return PREFIX + "0".
  188. * @todo This seems so simple, am I missing something?
  189. */
  190. const Ogre::String CreateAutoName(const Ogre::String prefix);
  191. /**
  192. * Obtains a name from a key code.
  193. *
  194. * @param key[in] Key code.
  195. * @return Human readable name assigned to the key code. "UNASSIGNED" if there
  196. * is no name for the key code.
  197. */
  198. Ogre::String KeyToString(OIS::KeyCode key);
  199. /**
  200. * Obtains a key code from a name.
  201. *
  202. * @param str[in] Key name.
  203. * @return Key code assigned to the name. OIS::KC_UNASSIGNED if there is no
  204. * keycode with the specified name.
  205. */
  206. OIS::KeyCode StringToKey(const Ogre::String& str);
  207. /**
  208. * Tokenizes a string.
  209. *
  210. * @param str[in] Input string.
  211. * @param delimiters[in] Token delimiters. They will be stripped from the
  212. * string.
  213. * @param delimiters_preserve[in] More token delimiters. They will be returned
  214. * as individual tokens.
  215. * @param quote[in] @todo Understand and document.
  216. * @param esc[in] Accepted escape characters. Escaped characters won't be
  217. * considered delimiters of any kind.
  218. * @return Tokens of the original string.
  219. */
  220. Ogre::StringVector StringTokenise(
  221. const Ogre::String& str, const Ogre::String& delimiters = "\t\n ",
  222. const Ogre::String& delimiters_preserve = "",
  223. const Ogre::String& quote = "\"", const Ogre::String& esc = "\\"
  224. );