TextHandler.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 <OgreString.h>
  17. #include <OgreSingleton.h>
  18. #include <tinyxml.h>
  19. /**
  20. * The text handler.
  21. */
  22. class TextHandler : public Ogre::Singleton<TextHandler>{
  23. public:
  24. /**
  25. * Constructor.
  26. */
  27. TextHandler();
  28. /**
  29. * Destructor.
  30. */
  31. virtual ~TextHandler();
  32. /**
  33. * Initializes the commands for the text handler.
  34. */
  35. void InitCmd();
  36. /**
  37. * Loads texts for a specific field.
  38. *
  39. * @param[in] field_name Name of the field.
  40. */
  41. void LoadFieldText(const std::string field_name);
  42. /**
  43. * Sets the language for texts.
  44. *
  45. * @param[in] language Language identifier. Must be the name of one of the folders in the
  46. * data/text/.
  47. */
  48. void SetLanguage(const Ogre::String& language);
  49. /**
  50. * Retrieves the currently set language.
  51. *
  52. * @return The current language.
  53. */
  54. const Ogre::String& GetLanguage();
  55. /**
  56. * Adds text.
  57. *
  58. * @param[in] name Text name or identifier.
  59. * @param[in] node Text node in an XML file.
  60. */
  61. void AddText(const Ogre::String& name, TiXmlNode* node);
  62. /**
  63. * Adds a text dialog.
  64. *
  65. * @param[in] name Text name or identifier.
  66. * @param[in] node Text node in an XML file.
  67. * @param[in] width Text width.
  68. * @param[in] height Text height.
  69. */
  70. void AddDialog(
  71. const Ogre::String& name, TiXmlNode* node, const float width, const float height
  72. );
  73. /**
  74. * Retrieves a text by name.
  75. *
  76. * @param[in] name Text name or identifier.
  77. * @return Text node in an XML file, or NULL if there is none.
  78. */
  79. TiXmlNode* GetText(const Ogre::String& name) const;
  80. /**
  81. * Retrieves a dialog and it's dimensions by name.
  82. *
  83. * @param[in] name Text name or identifier.
  84. * @param[out] width The text width will be saved here. It won't change if the dialog is
  85. * not found.
  86. * @param[out] height The text height will be saved here. It won't change if the dialog is
  87. * not found.
  88. * @return Text node in an XML file, or NULL if there is none.
  89. */
  90. TiXmlNode* GetDialog(const Ogre::String& name, float &width, float& height) const;
  91. /**
  92. * Gets the plain text of a dialog node. The tags that don't hold text, such as the colour,
  93. * are skipped (their content is kept), and the character tags are replaced by the name of
  94. * the character.
  95. *
  96. * @param[in] node The node of the dialog.
  97. * @param[in] character_name Gives the name of a character from its ID.
  98. * @return The text.
  99. */
  100. static std::string ExtractText(
  101. const TiXmlNode* node, const std::function<std::string(int)>& character_name
  102. );
  103. /**
  104. * Retrieves a dialog text by name.
  105. *
  106. * @param[in] name Text name or identifier.
  107. * @return Dialog text, an empty string if there is none by the specified ID.
  108. */
  109. std::string GetDialogText(const std::string name);
  110. /**
  111. * Deletes all texts in the handler.
  112. */
  113. void UnloadTexts();
  114. /**
  115. * Retrieves a character name from it's ID.
  116. *
  117. * @param[in] id ID of the character.
  118. * @return The name of the character. If the character doesn't exist, "UNKNOWN_CHAR_" + id
  119. * will be returned.
  120. */
  121. std::string GetCharacterName(int id);
  122. /**
  123. * Retrieves a character name from it's party position.
  124. *
  125. * @param[in] position Position in the party[1-3].
  126. * @return The name of the character in the party specified position. If an invalid
  127. * position is supplied, "UNKNOWN_PARTY_POS_" + id will be returned. If the position is
  128. * correct, but there is no character on it, an empty string will be returned.
  129. */
  130. std::string GetPartyCharacterName(int position);
  131. /**
  132. * Sets a character name.
  133. *
  134. * @param[in] id Character ID.
  135. * @param[in] name Character name.
  136. */
  137. void SetCharacterName(int id, const char* name);
  138. /**
  139. * Sets the party member IDs.
  140. *
  141. * @param[in] char_1 First member id.
  142. * @param[in] char_2 Second member id. -1 if not set.
  143. * @param[in] char_3 Third member id. -1 if not set.
  144. */
  145. void SetParty(int char_1, int char_2, int char_3);
  146. /**
  147. * Retrieves the party member IDs.
  148. *
  149. * @return The list of party member IDs.
  150. */
  151. std::vector<int> GetParty();
  152. private:
  153. /**
  154. * The language for the texts.
  155. */
  156. Ogre::String language_;
  157. /**
  158. * A text.
  159. */
  160. struct Text{
  161. /**
  162. * Name or identifier of the text.
  163. */
  164. Ogre::String name;
  165. /**
  166. * Text node in an XML file.
  167. */
  168. TiXmlNode* node;
  169. };
  170. /**
  171. * The list of texts in the handler.
  172. */
  173. std::vector<Text> texts_;
  174. /**
  175. * A dialog.
  176. */
  177. struct Dialog{
  178. /**
  179. * Name or identifier of the text.
  180. */
  181. Ogre::String name;
  182. /**
  183. * Text node in an XML file.
  184. */
  185. TiXmlNode* node;
  186. /**
  187. * The text width.
  188. */
  189. float width;
  190. /**
  191. * The text height.
  192. */
  193. float height;
  194. };
  195. /**
  196. * The list of dialogs in the handler.
  197. */
  198. std::vector<Dialog> dialogs_;
  199. /**
  200. * Character names, indexed by character IDs.
  201. *
  202. * Sometimes, character names must appear in texts. This must be refreshed on engine start
  203. * and when a character name changes.
  204. */
  205. std::string character_names_[11];
  206. /**
  207. * Current party formation.
  208. *
  209. * Sometimes, the name of a character in the party must appear in texts. This holds the
  210. * current party member IDs (or -1 if the party is not full). It must be refreshed every
  211. * time the party changes.
  212. */
  213. int current_party_[3];
  214. };