RichText.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Taken from: https://github.com/skyrpex/RichText
  2. #ifndef UTIL3DS_RICHTEXT_HPP
  3. #define UTIL3DS_RICHTEXT_HPP
  4. //////////////////////////////////////////////////////////////////////////
  5. // Headers
  6. //////////////////////////////////////////////////////////////////////////
  7. #include <vector>
  8. #include <cpp3ds/Graphics/Drawable.hpp>
  9. #include <cpp3ds/Graphics/Color.hpp>
  10. #include <cpp3ds/Graphics/Text.hpp>
  11. #include <cpp3ds/System/Vector2.hpp>
  12. #include "TweenObjects.hpp"
  13. namespace util3ds
  14. {
  15. class RichText : public cpp3ds::Drawable, public TweenTransformable<cpp3ds::Transformable>
  16. {
  17. public:
  18. //////////////////////////////////////////////////////////////////////////
  19. // Nested class that represents a single line
  20. //////////////////////////////////////////////////////////////////////////
  21. class Line : public cpp3ds::Transformable, public cpp3ds::Drawable
  22. {
  23. public:
  24. //////////////////////////////////////////////////////////////////////
  25. // Set character size
  26. //////////////////////////////////////////////////////////////////////
  27. void setCharacterSize(unsigned int size);
  28. //////////////////////////////////////////////////////////////////////
  29. // Set font
  30. //////////////////////////////////////////////////////////////////////
  31. void setFont(const cpp3ds::Font &font);
  32. //////////////////////////////////////////////////////////////////////
  33. // Get texts
  34. //////////////////////////////////////////////////////////////////////
  35. std::vector<cpp3ds::Text> &getTexts() const;
  36. //////////////////////////////////////////////////////////////////////
  37. // Append text
  38. //////////////////////////////////////////////////////////////////////
  39. void appendText(cpp3ds::Text text);
  40. //////////////////////////////////////////////////////////////////////
  41. // Get local bounds
  42. //////////////////////////////////////////////////////////////////////
  43. cpp3ds::FloatRect getLocalBounds() const;
  44. //////////////////////////////////////////////////////////////////////
  45. // Get global bounds
  46. //////////////////////////////////////////////////////////////////////
  47. cpp3ds::FloatRect getGlobalBounds() const;
  48. void setOpacity(cpp3ds::Uint8 alpha);
  49. cpp3ds::Uint8 getOpacity() const;
  50. protected:
  51. //////////////////////////////////////////////////////////////////////
  52. // Draw
  53. //////////////////////////////////////////////////////////////////////
  54. void draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const override;
  55. private:
  56. //////////////////////////////////////////////////////////////////////
  57. // Update geometry
  58. //////////////////////////////////////////////////////////////////////
  59. void updateGeometry() const;
  60. //////////////////////////////////////////////////////////////////////
  61. // Update geometry for a given text
  62. //////////////////////////////////////////////////////////////////////
  63. void updateTextAndGeometry(cpp3ds::Text &text) const;
  64. //////////////////////////////////////////////////////////////////////
  65. // Member data
  66. //////////////////////////////////////////////////////////////////////
  67. mutable std::vector<cpp3ds::Text> m_texts; ///< List of texts
  68. mutable cpp3ds::FloatRect m_bounds; ///< Local bounds
  69. };
  70. //////////////////////////////////////////////////////////////////////////
  71. // Constructor
  72. //////////////////////////////////////////////////////////////////////////
  73. RichText();
  74. //////////////////////////////////////////////////////////////////////////
  75. // Constructor
  76. //////////////////////////////////////////////////////////////////////////
  77. RichText(const cpp3ds::Font &font);
  78. //////////////////////////////////////////////////////////////////////////
  79. // Operators
  80. //////////////////////////////////////////////////////////////////////////
  81. RichText & operator << (const cpp3ds::Color &color);
  82. RichText & operator << (cpp3ds::Text::Style style);
  83. RichText & operator << (const cpp3ds::String &string);
  84. //////////////////////////////////////////////////////////////////////////
  85. // Set character size
  86. //////////////////////////////////////////////////////////////////////////
  87. void setCharacterSize(unsigned int size);
  88. //////////////////////////////////////////////////////////////////////////
  89. // Set font
  90. //////////////////////////////////////////////////////////////////////////
  91. void setFont(const cpp3ds::Font &font);
  92. void setOpacity(cpp3ds::Uint8 alpha);
  93. cpp3ds::Uint8 getOpacity() const;
  94. void useSystemFont();
  95. //////////////////////////////////////////////////////////////////////////
  96. // Clear
  97. //////////////////////////////////////////////////////////////////////////
  98. void clear();
  99. //////////////////////////////////////////////////////////////////////////
  100. // Get text list
  101. //////////////////////////////////////////////////////////////////////////
  102. const std::vector<Line> &getLines() const;
  103. //////////////////////////////////////////////////////////////////////////
  104. // Get character size
  105. //////////////////////////////////////////////////////////////////////////
  106. unsigned int getCharacterSize() const;
  107. //////////////////////////////////////////////////////////////////////////
  108. // Get font
  109. //////////////////////////////////////////////////////////////////////////
  110. const cpp3ds::Font *getFont() const;
  111. //////////////////////////////////////////////////////////////////////////
  112. // Get local bounds
  113. //////////////////////////////////////////////////////////////////////////
  114. cpp3ds::FloatRect getLocalBounds() const;
  115. //////////////////////////////////////////////////////////////////////////
  116. // Get global bounds
  117. //////////////////////////////////////////////////////////////////////////
  118. cpp3ds::FloatRect getGlobalBounds() const;
  119. protected:
  120. //////////////////////////////////////////////////////////////////////////
  121. // Render
  122. //////////////////////////////////////////////////////////////////////////
  123. void draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const override;
  124. private:
  125. //////////////////////////////////////////////////////////////////////////
  126. // Delegate constructor
  127. //////////////////////////////////////////////////////////////////////////
  128. RichText(const cpp3ds::Font *font);
  129. //////////////////////////////////////////////////////////////////////////
  130. // Creates a cpp3ds::Text instance using the current styles
  131. //////////////////////////////////////////////////////////////////////////
  132. cpp3ds::Text createText(const cpp3ds::String &string) const;
  133. //////////////////////////////////////////////////////////////////////////
  134. // Update geometry
  135. //////////////////////////////////////////////////////////////////////////
  136. void updateGeometry() const;
  137. //////////////////////////////////////////////////////////////////////////
  138. // Member data
  139. //////////////////////////////////////////////////////////////////////////
  140. mutable std::vector<Line> m_lines; ///< List of lines
  141. const cpp3ds::Font *m_font; ///< Font
  142. unsigned int m_characterSize; ///< Character size
  143. mutable cpp3ds::FloatRect m_bounds; ///< Local bounds
  144. cpp3ds::Color m_currentColor; ///< Last used color
  145. cpp3ds::Text::Style m_currentStyle; ///< Last style used
  146. bool m_useSystemFont;
  147. };
  148. } // namespace util3ds
  149. #endif // UTIL3DS_RICHTEXT_HPP