RichText.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Taken from: https://github.com/skyrpex/RichText
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // Headers
  4. ////////////////////////////////////////////////////////////////////////////////
  5. #include "RichText.hpp"
  6. #include <cpp3ds/Graphics/Font.hpp>
  7. #include <cpp3ds/Graphics/Rect.hpp>
  8. #include <cpp3ds/Graphics/RenderTarget.hpp>
  9. #include <cpp3ds/System/String.hpp>
  10. namespace util3ds
  11. {
  12. ////////////////////////////////////////////////////////////////////////////////
  13. void RichText::Line::setCharacterSize(unsigned int size)
  14. {
  15. for (cpp3ds::Text &text : m_texts)
  16. text.setCharacterSize(size);
  17. updateGeometry();
  18. }
  19. ////////////////////////////////////////////////////////////////////////////////
  20. void RichText::Line::setFont(const cpp3ds::Font &font)
  21. {
  22. for (cpp3ds::Text &text : m_texts)
  23. text.setFont(font);
  24. updateGeometry();
  25. }
  26. ////////////////////////////////////////////////////////////////////////////////
  27. std::vector<cpp3ds::Text> &RichText::Line::getTexts() const
  28. {
  29. return m_texts;
  30. }
  31. ////////////////////////////////////////////////////////////////////////////////
  32. void RichText::Line::appendText(cpp3ds::Text text)
  33. {
  34. // Set text offset
  35. updateTextAndGeometry(text);
  36. // Push back
  37. m_texts.push_back(std::move(text));
  38. }
  39. ////////////////////////////////////////////////////////////////////////////////
  40. cpp3ds::FloatRect RichText::Line::getLocalBounds() const
  41. {
  42. return m_bounds;
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////
  45. cpp3ds::FloatRect RichText::Line::getGlobalBounds() const
  46. {
  47. return getTransform().transformRect(getLocalBounds());
  48. }
  49. ////////////////////////////////////////////////////////////////////////////////
  50. void RichText::Line::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  51. {
  52. states.transform *= getTransform();
  53. for (const cpp3ds::Text &text : m_texts)
  54. target.draw(text, states);
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////
  57. void RichText::Line::updateGeometry() const
  58. {
  59. m_bounds = cpp3ds::FloatRect();
  60. for (cpp3ds::Text &text : m_texts)
  61. updateTextAndGeometry(text);
  62. }
  63. ////////////////////////////////////////////////////////////////////////////////
  64. void RichText::Line::updateTextAndGeometry(cpp3ds::Text &text) const
  65. {
  66. // Set text offset
  67. text.setPosition(m_bounds.width, 0.f);
  68. // Update bounds
  69. int lineSpacing = text.getFont()->getLineSpacing(text.getCharacterSize());
  70. m_bounds.height = std::max(m_bounds.height, static_cast<float>(lineSpacing));
  71. m_bounds.width += text.getGlobalBounds().width;
  72. }
  73. ////////////////////////////////////////////////////////////////////////////////
  74. RichText::RichText()
  75. : RichText(nullptr)
  76. {
  77. }
  78. ////////////////////////////////////////////////////////////////////////////////
  79. RichText::RichText(const cpp3ds::Font &font)
  80. : RichText(&font)
  81. {
  82. }
  83. ////////////////////////////////////////////////////////////////////////////////
  84. RichText & RichText::operator << (const cpp3ds::Color &color)
  85. {
  86. m_currentColor = color;
  87. return *this;
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////
  90. RichText & RichText::operator << (cpp3ds::Text::Style style)
  91. {
  92. m_currentStyle = style;
  93. return *this;
  94. }
  95. ////////////////////////////////////////////////////////////////////////////////
  96. std::vector<cpp3ds::String> explode(const cpp3ds::String &string, cpp3ds::Uint32 delimiter) {
  97. if (string.isEmpty())
  98. return std::vector<cpp3ds::String>();
  99. // For each character in the string
  100. std::vector<cpp3ds::String> result;
  101. cpp3ds::String buffer;
  102. for (cpp3ds::Uint32 character : string) {
  103. // If we've hit the delimiter character
  104. if (character == delimiter) {
  105. // Add them to the result vector
  106. result.push_back(buffer);
  107. buffer.clear();
  108. } else {
  109. // Accumulate the next character into the sequence
  110. buffer += character;
  111. }
  112. }
  113. // Add to the result if buffer still has contents or if the last character is a delimiter
  114. if (!buffer.isEmpty() || string[string.getSize() - 1] == delimiter)
  115. result.push_back(buffer);
  116. return result;
  117. }
  118. ////////////////////////////////////////////////////////////////////////////////
  119. RichText & RichText::operator << (const cpp3ds::String &string)
  120. {
  121. // Maybe skip
  122. if (string.isEmpty())
  123. return *this;
  124. // Explode into substrings
  125. std::vector<cpp3ds::String> subStrings = explode(string, '\n');
  126. // Append first substring using the last line
  127. auto it = subStrings.begin();
  128. if (it != subStrings.end()) {
  129. // If there isn't any line, just create it
  130. if (m_lines.empty())
  131. m_lines.resize(1);
  132. // Remove last line's height
  133. Line &line = m_lines.back();
  134. m_bounds.height -= line.getGlobalBounds().height;
  135. // Append text
  136. line.appendText(createText(*it));
  137. // Update bounds
  138. m_bounds.height += line.getGlobalBounds().height;
  139. m_bounds.width = std::max(m_bounds.width, line.getGlobalBounds().width);
  140. }
  141. // Append the rest of substrings as new lines
  142. while (++it != subStrings.end()) {
  143. Line line;
  144. line.setPosition(0.f, m_bounds.height);
  145. line.appendText(createText(*it));
  146. m_lines.push_back(std::move(line));
  147. // Update bounds
  148. m_bounds.height += line.getGlobalBounds().height;
  149. m_bounds.width = std::max(m_bounds.width, line.getGlobalBounds().width);
  150. }
  151. // Return
  152. return *this;
  153. }
  154. ////////////////////////////////////////////////////////////////////////////////
  155. void RichText::setCharacterSize(unsigned int size)
  156. {
  157. // Maybe skip
  158. if (m_characterSize == size)
  159. return;
  160. // Update character size
  161. m_characterSize = size;
  162. // Set texts character size
  163. for (Line &line : m_lines)
  164. line.setCharacterSize(size);
  165. updateGeometry();
  166. }
  167. ////////////////////////////////////////////////////////////////////////////////
  168. void RichText::setFont(const cpp3ds::Font &font)
  169. {
  170. // Maybe skip
  171. if (m_font == &font)
  172. return;
  173. // Update font
  174. m_font = &font;
  175. // Set texts font
  176. for (Line &line : m_lines)
  177. line.setFont(font);
  178. updateGeometry();
  179. }
  180. ////////////////////////////////////////////////////////////////////////////////
  181. void RichText::clear()
  182. {
  183. // Clear texts
  184. m_lines.clear();
  185. // Reset bounds
  186. m_bounds = cpp3ds::FloatRect();
  187. }
  188. ////////////////////////////////////////////////////////////////////////////////
  189. void RichText::useSystemFont()
  190. {
  191. m_useSystemFont = true;
  192. for (auto& line : m_lines)
  193. for (auto& text : line.getTexts())
  194. text.useSystemFont();
  195. }
  196. ////////////////////////////////////////////////////////////////////////////////
  197. const std::vector<RichText::Line> &RichText::getLines() const
  198. {
  199. return m_lines;
  200. }
  201. ////////////////////////////////////////////////////////////////////////////////
  202. unsigned int RichText::getCharacterSize() const
  203. {
  204. return m_characterSize;
  205. }
  206. ////////////////////////////////////////////////////////////////////////////////
  207. const cpp3ds::Font *RichText::getFont() const
  208. {
  209. return m_font;
  210. }
  211. ////////////////////////////////////////////////////////////
  212. cpp3ds::FloatRect RichText::getLocalBounds() const
  213. {
  214. return m_bounds;
  215. }
  216. ////////////////////////////////////////////////////////////
  217. cpp3ds::FloatRect RichText::getGlobalBounds() const
  218. {
  219. return getTransform().transformRect(getLocalBounds());
  220. }
  221. ////////////////////////////////////////////////////////////////////////////////
  222. void RichText::draw(cpp3ds::RenderTarget& target, cpp3ds::RenderStates states) const
  223. {
  224. states.transform *= getTransform();
  225. for (const Line &line : m_lines)
  226. target.draw(line, states);
  227. }
  228. ////////////////////////////////////////////////////////////////////////////////
  229. RichText::RichText(const cpp3ds::Font *font)
  230. : m_font(font),
  231. m_characterSize(30),
  232. m_currentColor(cpp3ds::Color::White),
  233. m_currentStyle(cpp3ds::Text::Regular),
  234. m_useSystemFont(false)
  235. {
  236. }
  237. ////////////////////////////////////////////////////////////////////////////////
  238. cpp3ds::Text RichText::createText(const cpp3ds::String &string) const
  239. {
  240. cpp3ds::Text text;
  241. text.setString(string);
  242. text.setFillColor(m_currentColor);
  243. text.setStyle(m_currentStyle);
  244. text.setCharacterSize(m_characterSize);
  245. if (m_useSystemFont)
  246. text.useSystemFont();
  247. else if (m_font)
  248. text.setFont(*m_font);
  249. return text;
  250. }
  251. ////////////////////////////////////////////////////////////////////////////////
  252. void RichText::updateGeometry() const
  253. {
  254. m_bounds = cpp3ds::FloatRect();
  255. for (Line &line : m_lines) {
  256. line.setPosition(0.f, m_bounds.height);
  257. m_bounds.height += line.getGlobalBounds().height;
  258. m_bounds.width = std::max(m_bounds.width, line.getGlobalBounds().width);
  259. }
  260. }
  261. void RichText::setOpacity(cpp3ds::Uint8 alpha)
  262. {
  263. for (auto& line : m_lines)
  264. line.setOpacity(alpha);
  265. }
  266. cpp3ds::Uint8 RichText::getOpacity() const
  267. {
  268. if (m_lines.empty())
  269. return 0;
  270. return m_lines.front().getOpacity();
  271. }
  272. void RichText::Line::setOpacity(cpp3ds::Uint8 alpha)
  273. {
  274. for (auto& text : m_texts) {
  275. cpp3ds::Color color = text.getFillColor();
  276. color.a = alpha;
  277. text.setFillColor(color);
  278. }
  279. }
  280. cpp3ds::Uint8 RichText::Line::getOpacity() const
  281. {
  282. if (m_texts.empty())
  283. return 0;
  284. return m_texts.front().getFillColor().a;
  285. }
  286. } // namespace util3ds