UiTextParser.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <functional>
  17. #include <string>
  18. #include <vector>
  19. #include <Overlay/OgreUTFString.h>
  20. #include <tinyxml.h>
  21. #include "UiTextChar.h"
  22. /**
  23. * Reads the markup of a text into the list of its characters.
  24. *
  25. * The elements it knows are: colour (value), pause_ok, pause (time, in frames of the game),
  26. * next_page, timer, variable (name), character (id), party (pos), include (name) and image
  27. * (sprite). The contents of the elements are read too.
  28. */
  29. class UiTextParser{
  30. public:
  31. /**
  32. * Name of the variable with the time of the timer.
  33. */
  34. static const char* const TIMER_VARIABLE;
  35. /**
  36. * What the parser needs to know to expand the elements of a text. All must be set.
  37. */
  38. struct Sources{
  39. /**
  40. * Retrieves the value of a variable.
  41. */
  42. std::function<Ogre::UTFString(const Ogre::String&)> variable;
  43. /**
  44. * Retrieves the name of a character, by ID.
  45. */
  46. std::function<std::string(int)> character_name;
  47. /**
  48. * Retrieves the name of a party member, by position.
  49. */
  50. std::function<std::string(int)> party_name;
  51. /**
  52. * Retrieves a text to include, by name. Null if there is none.
  53. */
  54. std::function<TiXmlNode*(const Ogre::String&)> text;
  55. /**
  56. * Creates the sprite of an image, and sets it and its position in the character.
  57. * Returns false if there is no such image.
  58. */
  59. std::function<bool(const Ogre::String&, TextChar&)> image;
  60. };
  61. /**
  62. * Constructor.
  63. *
  64. * @param[in] sources Where the contents of the elements come from.
  65. * @param[out] text The characters are added at the end of this list.
  66. */
  67. UiTextParser(const Sources& sources, std::vector<TextChar>& text);
  68. /**
  69. * Reads a node, its contents and the nodes that follow it.
  70. *
  71. * @param[in] node The first node. Nothing is read if it's null.
  72. * @param[in] colour Colour of the characters.
  73. */
  74. void Parse(TiXmlNode* node, const Ogre::ColourValue& colour);
  75. /**
  76. * Checks if any of the parsed text shows the timer.
  77. */
  78. bool UsesTimer() const{return uses_timer_;}
  79. private:
  80. /**
  81. * Adds the characters of an element, not including its contents.
  82. *
  83. * @param[in] element The element.
  84. * @param[in] colour Colour of the characters.
  85. * @return The colour of the contents of the element.
  86. */
  87. Ogre::ColourValue ParseElement(const TiXmlElement& element, const Ogre::ColourValue& colour);
  88. /**
  89. * Adds a variable: a character that marks where it is, followed by its value.
  90. */
  91. void AddVariable(const Ogre::String& name, const Ogre::ColourValue& colour);
  92. /**
  93. * Adds the characters of a name.
  94. */
  95. void AddName(const std::string& name, const Ogre::ColourValue& colour);
  96. /**
  97. * Adds the characters of a string.
  98. */
  99. void AddString(const Ogre::UTFString& string, const Ogre::ColourValue& colour);
  100. /**
  101. * Where the contents of the elements come from.
  102. */
  103. const Sources& sources_;
  104. /**
  105. * The list of characters.
  106. */
  107. std::vector<TextChar>& text_;
  108. /**
  109. * True if any text shows the timer.
  110. */
  111. bool uses_timer_;
  112. };