UiText.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <boost/test/unit_test.hpp>
  16. #include <tinyxml.h>
  17. #include "engine/ui/UiTextLayout.h"
  18. #include "engine/ui/UiTextParser.h"
  19. namespace{
  20. /**
  21. * Sources with fixed names, a variable "money" and an included text "greeting".
  22. */
  23. struct TestSources{
  24. TiXmlDocument included;
  25. UiTextParser::Sources sources;
  26. TestSources(){
  27. // As in the game, where the XML files set it: spaces and line feeds are kept.
  28. TiXmlBase::SetCondenseWhiteSpace(false);
  29. included.Parse("<text>Hi <colour value=\"1 0 0\">you</colour>!</text>");
  30. sources.variable = [](const Ogre::String& name){
  31. return Ogre::UTFString(name == "money" ? "150" : "");
  32. };
  33. sources.character_name = [](int id){return "Name" + std::to_string(id);};
  34. sources.party_name = [](int position){return "Party" + std::to_string(position);};
  35. sources.text = [this](const Ogre::String& name){
  36. return name == "greeting" ? included.RootElement() : nullptr;
  37. };
  38. sources.image = [](const Ogre::String& name, TextChar& character){
  39. character.sprite_y = 7;
  40. return name == "circle";
  41. };
  42. }
  43. };
  44. std::vector<TextChar> Parse(const std::string& markup, bool* timer = nullptr){
  45. TestSources test;
  46. TiXmlDocument document;
  47. document.Parse(("<container>" + markup + "</container>").c_str());
  48. std::vector<TextChar> text;
  49. UiTextParser parser(test.sources, text);
  50. parser.Parse(document.RootElement(), Ogre::ColourValue::White);
  51. if (timer != nullptr) *timer = parser.UsesTimer();
  52. return text;
  53. }
  54. /**
  55. * The visible letters of a text.
  56. */
  57. std::string Letters(const std::vector<TextChar>& text){
  58. std::string letters;
  59. for (const TextChar& character : text){
  60. if (character.char_code != 0) letters += static_cast<char>(character.char_code);
  61. }
  62. return letters;
  63. }
  64. UiCharData Glyph(const int code){
  65. UiCharData data = {};
  66. data.char_code = code;
  67. data.width = 8;
  68. data.height = 10;
  69. data.pre = 1;
  70. data.post = 1;
  71. return data;
  72. }
  73. }
  74. BOOST_AUTO_TEST_CASE(TestUiTextReadsEveryElement){
  75. // All the text is read, not only the first element of each level.
  76. const auto text = Parse("HP <colour value=\"1 0 0\">-5</colour> of <include name=\"greeting\" />.");
  77. BOOST_CHECK_EQUAL(Letters(text), "HP -5 of Hi you!.");
  78. BOOST_CHECK(text[0].colour == Ogre::ColourValue::White);
  79. BOOST_CHECK(text[3].colour == Ogre::ColourValue(1, 0, 0));
  80. BOOST_CHECK(text[5].colour == Ogre::ColourValue::White);
  81. }
  82. BOOST_AUTO_TEST_CASE(TestUiTextNamesAndVariables){
  83. BOOST_CHECK_EQUAL(Letters(Parse("<character id=\"2\"/>,<party pos=\"1\"/>")), "Name2,Party1");
  84. const auto text = Parse("<variable name=\"money\"/>G");
  85. BOOST_REQUIRE_EQUAL(text.size(), 5u);
  86. BOOST_CHECK(text[0].skip);
  87. BOOST_CHECK_EQUAL(text[0].variable, "money");
  88. BOOST_CHECK_EQUAL(text[0].variable_len, 3u);
  89. BOOST_CHECK_EQUAL(Letters(text), "150G");
  90. bool timer = false;
  91. BOOST_CHECK_EQUAL(Parse("<timer/>", &timer)[0].variable, UiTextParser::TIMER_VARIABLE);
  92. BOOST_CHECK(timer);
  93. }
  94. BOOST_AUTO_TEST_CASE(TestUiTextCommands){
  95. const auto text = Parse("a<pause_ok/><pause time=\"60\"/><next_page/><image sprite=\"circle\"/>");
  96. BOOST_REQUIRE_EQUAL(text.size(), 5u);
  97. BOOST_CHECK(text[1].pause_ok);
  98. BOOST_CHECK_EQUAL(text[2].pause_time, 2.0f);
  99. BOOST_CHECK(text[3].next_page);
  100. BOOST_CHECK_EQUAL(text[4].sprite_y, 7.0f);
  101. }
  102. BOOST_AUTO_TEST_CASE(TestUiTextIgnoresIncompleteElements){
  103. // These crashed the game.
  104. BOOST_CHECK_EQUAL(
  105. Letters(Parse(
  106. "<colour>a</colour><character/><party pos=\"x\"/><variable/><pause/><include/><image/>b"
  107. )), "ab"
  108. );
  109. BOOST_CHECK(Parse("<include name=\"nothing\"/><image sprite=\"square\"/>").empty());
  110. }
  111. BOOST_AUTO_TEST_CASE(TestUiTextLayoutWrapsAndStopsAtTheBottom){
  112. // An area of 25 x 20 on a screen of the reference height: two letters per line, two lines.
  113. const UiTextLayout::Area area{{0, 0}, {25, 20}, {1, 1}, 720, 0, 0, 0, 0};
  114. UiTextLayout layout(area, 0, 0, 10);
  115. UiTextLayout::Box box;
  116. BOOST_REQUIRE(layout.Place(Glyph('a'), box));
  117. BOOST_CHECK_EQUAL(box.left, 1);
  118. BOOST_CHECK_EQUAL(box.right, 9);
  119. BOOST_REQUIRE(layout.Place(Glyph('b'), box));
  120. BOOST_CHECK_EQUAL(box.left, 11);
  121. BOOST_CHECK_EQUAL(box.top, 0);
  122. // The third letter doesn't fit: it goes to the next line. The space before it is not
  123. // repeated there.
  124. BOOST_REQUIRE(layout.Place(Glyph('c'), box));
  125. BOOST_CHECK_EQUAL(box.left, 0);
  126. BOOST_CHECK_EQUAL(box.top, 10);
  127. layout.NewLine();
  128. BOOST_CHECK(!layout.Place(Glyph('d'), box));
  129. }
  130. BOOST_AUTO_TEST_CASE(TestUiTextLayoutWidthAndCorners){
  131. const std::vector<TextChar> text = Parse("ab\nabc");
  132. BOOST_CHECK_EQUAL(UiTextLayout::TextWidth(text, Glyph), 30);
  133. const auto corners = UiTextLayout::ScreenCorners(
  134. {10, 20, 30, 40}, {0, 0}, 0, {100, 100}
  135. );
  136. BOOST_CHECK_CLOSE(corners[0].x, -0.8f, 0.001f);
  137. BOOST_CHECK_CLOSE(corners[0].y, 0.6f, 0.001f);
  138. BOOST_CHECK_CLOSE(corners[2].x, -0.4f, 0.001f);
  139. }