Utilities.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #include <OgreStringConverter.h>
  16. #include "common/Utilities.h"
  17. bool GetBool(TiXmlNode* node, const Ogre::String& tag, bool def){
  18. bool ret = def;
  19. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  20. const std::string* string = node->ToElement()->Attribute(tag);
  21. if (string != NULL) ret = Ogre::StringConverter::parseBool(*string);
  22. }
  23. return ret;
  24. }
  25. int GetInt(TiXmlNode* node, const Ogre::String& tag, int def){
  26. int ret = def;
  27. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  28. const std::string* string = node->ToElement()->Attribute(tag);
  29. if (string != NULL) ret = Ogre::StringConverter::parseInt(*string);
  30. }
  31. return ret;
  32. }
  33. float GetFloat(TiXmlNode* node, const Ogre::String& tag, float def){
  34. float ret = def;
  35. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  36. const std::string* string = node->ToElement()->Attribute(tag);
  37. if (string != NULL) ret = Ogre::StringConverter::parseReal(*string);
  38. }
  39. return ret;
  40. }
  41. const Ogre::String GetString(
  42. TiXmlNode* node, const Ogre::String& tag, const Ogre::String& def
  43. ){
  44. Ogre::String ret = def;
  45. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  46. const std::string* string = node->ToElement()->Attribute(tag);
  47. if (string != NULL) ret = *string;
  48. }
  49. return ret;
  50. }
  51. const Ogre::UTFString GetUTFString(
  52. TiXmlNode* node, const Ogre::String& tag, const Ogre::UTFString& def
  53. ){
  54. Ogre::UTFString ret = def;
  55. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  56. const std::string* string = node->ToElement()->Attribute(tag);
  57. if (string != NULL) ret = (*string).c_str();
  58. }
  59. return ret;
  60. }
  61. const Ogre::Vector2 GetVector2(TiXmlNode* node, const Ogre::String& tag, const Ogre::Vector2& def){
  62. Ogre::Vector2 ret = def;
  63. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  64. const std::string* string = node->ToElement()->Attribute(tag);
  65. if (string != NULL) ret = Ogre::StringConverter::parseVector2(*string);
  66. }
  67. return ret;
  68. }
  69. const Ogre::Vector3 GetVector3(TiXmlNode* node, const Ogre::String& tag, const Ogre::Vector3& def){
  70. Ogre::Vector3 ret = def;
  71. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  72. const std::string* string = node->ToElement()->Attribute(tag);
  73. if (string != NULL) ret = Ogre::StringConverter::parseVector3(*string);
  74. }
  75. return ret;
  76. }
  77. const Ogre::Vector4 GetVector4(TiXmlNode* node, const Ogre::String& tag, const Ogre::Vector4& def){
  78. Ogre::Vector4 ret = def;
  79. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  80. const std::string* string = node->ToElement()->Attribute(tag);
  81. if (string != NULL) ret = Ogre::StringConverter::parseVector4(*string);
  82. }
  83. return ret;
  84. }
  85. const Ogre::Matrix4 GetMatrix4(TiXmlNode* node, const Ogre::String& tag, const Ogre::Matrix4& def){
  86. Ogre::Matrix4 ret = def;
  87. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  88. const std::string* string = node->ToElement()->Attribute(tag);
  89. if (string != NULL) ret = Ogre::StringConverter::parseMatrix4(*string);
  90. }
  91. return ret;
  92. }
  93. const Ogre::Quaternion GetQuaternion(
  94. TiXmlNode* node, const Ogre::String& tag, const Ogre::Quaternion& def
  95. ){
  96. Ogre::Quaternion ret = def;
  97. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  98. const std::string* string = node->ToElement()->Attribute(tag);
  99. if (string != NULL) ret = Ogre::StringConverter::parseQuaternion(*string);
  100. }
  101. return ret;
  102. }
  103. const Ogre::ColourValue GetColourValue(
  104. TiXmlNode* node, const Ogre::String& tag, const Ogre::ColourValue& def
  105. ){
  106. Ogre::ColourValue ret = def;
  107. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  108. const std::string* string = node->ToElement()->Attribute(tag);
  109. if (string != NULL) ret = Ogre::StringConverter::parseColourValue(*string);
  110. }
  111. return ret;
  112. }
  113. void ParsePercent(float& value_percent, float& value, const Ogre::String& string){
  114. if (string.at(string.size() - 1) == '%'){
  115. value_percent = Ogre::StringConverter::parseReal(string.substr(0, string.size() - 1));
  116. value = 0;
  117. }
  118. else{
  119. Ogre::StringVector param = Ogre::StringUtil::split(string, "%");
  120. if (param.size() > 1){
  121. value_percent = Ogre::StringConverter::parseReal(param[0]);
  122. value = Ogre::StringConverter::parseReal(param[1]);
  123. }
  124. else{
  125. value_percent = 0;
  126. value = Ogre::StringConverter::parseReal(string);
  127. }
  128. }
  129. }
  130. float ParseKeyFrameTime(const float length, const Ogre::String& string){
  131. float res = 0;
  132. if (string.at(string.size() - 1) == '%')
  133. res = length * Ogre::StringConverter::parseReal(string.substr(0, string.size() - 1)) / 100;
  134. else res = Ogre::StringConverter::parseReal(string);
  135. return res;
  136. }
  137. /**
  138. * Relates key codes and their names.
  139. */
  140. struct KeyName {
  141. /**
  142. * Name assigned to the key code.
  143. */
  144. Ogre::String name;
  145. /**
  146. * The key code.
  147. */
  148. OIS::KeyCode key;
  149. };
  150. /**
  151. * Define names for keyboard keys.
  152. */
  153. KeyName key_names[] = {
  154. {"Enter", OIS::KC_RETURN},
  155. {"Escape", OIS::KC_ESCAPE},
  156. {"Space", OIS::KC_SPACE},
  157. {"LShift", OIS::KC_LSHIFT},
  158. {"RShift", OIS::KC_RSHIFT},
  159. {"LCtrl", OIS::KC_LCONTROL},
  160. {"RCtrl", OIS::KC_RCONTROL},
  161. {"LAlt", OIS::KC_LMENU},
  162. {"RAlt", OIS::KC_RMENU},
  163. {"0", OIS::KC_0},
  164. {"1", OIS::KC_1},
  165. {"2", OIS::KC_2},
  166. {"3", OIS::KC_3},
  167. {"4", OIS::KC_4},
  168. {"5", OIS::KC_5},
  169. {"6", OIS::KC_6},
  170. {"7", OIS::KC_7},
  171. {"8", OIS::KC_8},
  172. {"9", OIS::KC_9},
  173. {"A", OIS::KC_A},
  174. {"B", OIS::KC_B},
  175. {"C", OIS::KC_C},
  176. {"D", OIS::KC_D},
  177. {"E", OIS::KC_E},
  178. {"F", OIS::KC_F},
  179. {"G", OIS::KC_G},
  180. {"H", OIS::KC_H},
  181. {"I", OIS::KC_I},
  182. {"J", OIS::KC_J},
  183. {"K", OIS::KC_K},
  184. {"L", OIS::KC_L},
  185. {"M", OIS::KC_M},
  186. {"N", OIS::KC_N},
  187. {"O", OIS::KC_O},
  188. {"P", OIS::KC_P},
  189. {"Q", OIS::KC_Q},
  190. {"R", OIS::KC_R},
  191. {"S", OIS::KC_S},
  192. {"T", OIS::KC_T},
  193. {"U", OIS::KC_U},
  194. {"V", OIS::KC_V},
  195. {"W", OIS::KC_W},
  196. {"X", OIS::KC_X},
  197. {"Y", OIS::KC_Y},
  198. {"Z", OIS::KC_Z},
  199. {"Left", OIS::KC_LEFT},
  200. {"Up", OIS::KC_UP},
  201. {"Down", OIS::KC_DOWN},
  202. {"Right", OIS::KC_RIGHT},
  203. {"Num0", OIS::KC_NUMPAD0},
  204. {"Num1", OIS::KC_NUMPAD1},
  205. {"Num2", OIS::KC_NUMPAD2},
  206. {"Num3", OIS::KC_NUMPAD3},
  207. {"Num4", OIS::KC_NUMPAD4},
  208. {"Num5", OIS::KC_NUMPAD5},
  209. {"Num6", OIS::KC_NUMPAD6},
  210. {"Num7", OIS::KC_NUMPAD7},
  211. {"Num8", OIS::KC_NUMPAD8},
  212. {"Num9", OIS::KC_NUMPAD9},
  213. {"NumLock", OIS::KC_NUMLOCK},
  214. {"NumDiv", OIS::KC_DIVIDE},
  215. {"NumMult", OIS::KC_MULTIPLY},
  216. {"NumSub", OIS::KC_SUBTRACT},
  217. {"NumAdd", OIS::KC_ADD},
  218. {"NumEnter", OIS::KC_NUMPADENTER},
  219. {"NumDec", OIS::KC_DECIMAL},
  220. {"F1", OIS::KC_F1},
  221. {"F2", OIS::KC_F2},
  222. {"F3", OIS::KC_F3},
  223. {"F4", OIS::KC_F4},
  224. {"F5", OIS::KC_F5},
  225. {"F6", OIS::KC_F6},
  226. {"F7", OIS::KC_F7},
  227. {"F8", OIS::KC_F8},
  228. {"F9", OIS::KC_F9},
  229. {"F10", OIS::KC_F10},
  230. {"F11", OIS::KC_F11},
  231. {"F12", OIS::KC_F12},
  232. {"PGUP", OIS::KC_PGUP},
  233. {"PGDN", OIS::KC_PGDOWN},
  234. {"BackSpace", OIS::KC_BACK},
  235. {"UNASSIGNED", OIS::KC_UNASSIGNED},
  236. };
  237. Ogre::String KeyToString(OIS::KeyCode key){
  238. // Find string by key
  239. for (KeyName* kn = key_names; kn->name != "UNASSIGNED"; ++ kn)
  240. if (kn->key == key) return kn->name;
  241. return "UNASSIGNED";
  242. }
  243. OIS::KeyCode StringToKey(const Ogre::String& str){
  244. if (str == "") return OIS::KC_UNASSIGNED;
  245. // find key by string
  246. for (KeyName* kn = key_names; kn->name != "UNASSIGNED"; ++ kn)
  247. if (kn->name == str) return kn->key;
  248. return OIS::KC_UNASSIGNED;
  249. }
  250. Ogre::StringVector StringTokenise(
  251. const Ogre::String& str, const Ogre::String& delimiters, const Ogre::String& delimiters_preserve,
  252. const Ogre::String& quote, const Ogre::String& esc
  253. ){
  254. Ogre::StringVector ret;
  255. Ogre::String::size_type pos = 0; // Current position (char) in the string.
  256. char current_quote = 0; // The char of the currently open quote.
  257. bool quoted = false; // Indicator for an open quote.
  258. Ogre::String token; // String buffer for the token.
  259. bool token_complete = false; // Indicates if the current token is ready to be added to the
  260. // result vector.
  261. Ogre::String::size_type len = str.length(); // Length of the input string.
  262. // For every char in the input string.
  263. while (len > pos){
  264. char ch; // Buffer for the current character.
  265. char delimiter; // The buffer for the delimiter char which will be added to the tokens
  266. //if the delimiter is preserved.
  267. // Get the character of the string and reset the delimiter buffer.
  268. ch = str.at(pos);
  269. delimiter = 0;
  270. // Assume ch isn't a delimiter.
  271. bool add_char = true;
  272. // Check ...
  273. // ... if the delimiter is an escaped character.
  274. bool escaped = false; // Indicates if the next char is escaped.
  275. if (esc.empty() == false){ // Checks if esc-chars are provided.
  276. if (esc.find_first_of(ch) != std::string::npos){
  277. // Get the escaped char
  278. ++ pos;
  279. if (pos < len){ // If there are more chars left...
  280. // ... get the next one...
  281. ch = str.at(pos);
  282. // ... and add the escaped character to the token.
  283. add_char = true;
  284. }
  285. else{ // Can't get any more characters
  286. // Don't add the esc-char.
  287. add_char = false;
  288. }
  289. // Ignore the remaining delimiter checks
  290. escaped = true;
  291. }
  292. }
  293. // ... if the delimiter is a quote...
  294. if (quote.empty() == false && escaped == false){
  295. // ... if quote chars are provided and the char isn't escaped...
  296. if (quote.find_first_of(ch) != std::string::npos){
  297. // If not quoted, set to open quote and set the quote char.
  298. if (quoted == false){
  299. quoted = true;
  300. current_quote = ch;
  301. // Don't add the quote-char to the token.
  302. add_char = false;
  303. }
  304. else{ // If quote is open already.
  305. // Check if it is the matching character to close it.
  306. if (current_quote == ch){
  307. // Close quote and reset the quote character
  308. quoted = false;
  309. current_quote = 0;
  310. // Don't add the quote-char to the token
  311. add_char = false;
  312. }
  313. }
  314. }
  315. }
  316. // If the delimiter isn't preserved.
  317. if (delimiters.empty() == false && escaped == false && quoted == false){
  318. // If a delimiter is provided and the char isn't protected by quote
  319. // or escape char.
  320. if (delimiters.find_first_of(ch) != std::string::npos){
  321. // If the char is a delimiter and the token string isn't empty
  322. // the token is complete.
  323. if (token.empty() == false) token_complete = true;
  324. // Don't add the delimiter to the token.
  325. add_char = false;
  326. }
  327. }
  328. // If the delimiter is preserved, add it as a token.
  329. bool add_delimiter = false;
  330. if (delimiters_preserve.empty() == false && escaped == false && quoted == false){
  331. // If a delimiter which will be preserved is provided and the char
  332. // isn't protected by quote or escape char.
  333. if (delimiters_preserve.find_first_of(ch) != std::string::npos){
  334. // If the char is a delimiter and the token string isn't empty
  335. // the token is complete.
  336. if (token.empty() == false) token_complete = true;
  337. // Don't add the delimiter to the token.
  338. add_char = false;
  339. // add the delimiter
  340. delimiter = ch;
  341. add_delimiter = true;
  342. }
  343. }
  344. // Add the character to the token.
  345. if (add_char == true) token.push_back(ch);
  346. // Add the token if it is complete.
  347. if (token_complete == true && token.empty() == false){
  348. // Add the token string.
  349. ret.push_back(token);
  350. // Clear the contents.
  351. token.clear();
  352. // Build the next token.
  353. token_complete = false;
  354. }
  355. // Add the delimiter
  356. if (add_delimiter == true){
  357. // The next token is the delimiter.
  358. Ogre::String delim_token;
  359. delim_token.push_back(delimiter);
  360. ret.push_back(delim_token);
  361. }
  362. // Repeat for the next character
  363. ++ pos;
  364. }
  365. // Add the final token
  366. if (token.empty() == false) ret.push_back(token);
  367. return ret;
  368. }