guiHyperText.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /*
  2. Minetest
  3. Copyright (C) 2019 EvicenceBKidscode / Pierre-Yves Rollo <dev@pyrollo.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "IGUIEnvironment.h"
  17. #include "IGUIElement.h"
  18. #include "guiScrollBar.h"
  19. #include "IGUIFont.h"
  20. #include <vector>
  21. #include <list>
  22. #include <unordered_map>
  23. using namespace irr::gui;
  24. #include "client/fontengine.h"
  25. #include <SColor.h>
  26. #include "client/tile.h"
  27. #include "IVideoDriver.h"
  28. #include "client/client.h"
  29. #include "client/renderingengine.h"
  30. #include "hud.h"
  31. #include "guiHyperText.h"
  32. #include "util/string.h"
  33. bool check_color(const std::string &str)
  34. {
  35. irr::video::SColor color;
  36. return parseColorString(str, color, false);
  37. }
  38. bool check_integer(const std::string &str)
  39. {
  40. if (str.empty())
  41. return false;
  42. char *endptr = nullptr;
  43. strtol(str.c_str(), &endptr, 10);
  44. return *endptr == '\0';
  45. }
  46. // -----------------------------------------------------------------------------
  47. // ParsedText - A text parser
  48. void ParsedText::Element::setStyle(StyleList &style)
  49. {
  50. this->underline = is_yes(style["underline"]);
  51. video::SColor color;
  52. if (parseColorString(style["color"], color, false))
  53. this->color = color;
  54. if (parseColorString(style["hovercolor"], color, false))
  55. this->hovercolor = color;
  56. unsigned int font_size = std::atoi(style["fontsize"].c_str());
  57. FontMode font_mode = FM_Standard;
  58. if (style["fontstyle"] == "mono")
  59. font_mode = FM_Mono;
  60. // TODO: find a way to check font validity
  61. // Build a new fontengine ?
  62. this->font =
  63. #if USE_FREETYPE
  64. (gui::CGUITTFont *)
  65. #endif
  66. g_fontengine->getFont(font_size, font_mode,
  67. is_yes(style["bold"]), is_yes(style["italic"]));
  68. if (!this->font)
  69. printf("No font found ! Size=%d, mode=%d, bold=%s, italic=%s\n",
  70. font_size, font_mode, style["bold"].c_str(),
  71. style["italic"].c_str());
  72. }
  73. void ParsedText::Paragraph::setStyle(StyleList &style)
  74. {
  75. if (style["halign"] == "center")
  76. this->halign = HALIGN_CENTER;
  77. else if (style["halign"] == "right")
  78. this->halign = HALIGN_RIGHT;
  79. else if (style["halign"] == "justify")
  80. this->halign = HALIGN_JUSTIFY;
  81. else
  82. this->halign = HALIGN_LEFT;
  83. }
  84. ParsedText::ParsedText(const wchar_t *text)
  85. {
  86. // Default style
  87. m_root_tag.name = "root";
  88. m_root_tag.style["fontsize"] = "16";
  89. m_root_tag.style["fontstyle"] = "normal";
  90. m_root_tag.style["bold"] = "false";
  91. m_root_tag.style["italic"] = "false";
  92. m_root_tag.style["underline"] = "false";
  93. m_root_tag.style["halign"] = "left";
  94. m_root_tag.style["color"] = "#EEEEEE";
  95. m_root_tag.style["hovercolor"] = m_root_tag.style["color"];
  96. m_tags.push_back(&m_root_tag);
  97. m_active_tags.push_front(&m_root_tag);
  98. m_style = m_root_tag.style;
  99. // Default simple tags definitions
  100. StyleList style;
  101. style["hovercolor"] = "#FF0000";
  102. style["color"] = "#0000FF";
  103. style["underline"] = "true";
  104. m_elementtags["action"] = style;
  105. style.clear();
  106. style["bold"] = "true";
  107. m_elementtags["b"] = style;
  108. style.clear();
  109. style["italic"] = "true";
  110. m_elementtags["i"] = style;
  111. style.clear();
  112. style["underline"] = "true";
  113. m_elementtags["u"] = style;
  114. style.clear();
  115. style["fontstyle"] = "mono";
  116. m_elementtags["mono"] = style;
  117. style.clear();
  118. style["fontsize"] = m_root_tag.style["fontsize"];
  119. m_elementtags["normal"] = style;
  120. style.clear();
  121. style["fontsize"] = "24";
  122. m_elementtags["big"] = style;
  123. style.clear();
  124. style["fontsize"] = "36";
  125. m_elementtags["bigger"] = style;
  126. style.clear();
  127. style["halign"] = "center";
  128. m_paragraphtags["center"] = style;
  129. style.clear();
  130. style["halign"] = "justify";
  131. m_paragraphtags["justify"] = style;
  132. style.clear();
  133. style["halign"] = "left";
  134. m_paragraphtags["left"] = style;
  135. style.clear();
  136. style["halign"] = "right";
  137. m_paragraphtags["right"] = style;
  138. style.clear();
  139. m_element = NULL;
  140. m_paragraph = NULL;
  141. parse(text);
  142. }
  143. ParsedText::~ParsedText()
  144. {
  145. for (auto &tag : m_tags)
  146. delete tag;
  147. }
  148. void ParsedText::parse(const wchar_t *text)
  149. {
  150. wchar_t c;
  151. u32 cursor = 0;
  152. bool escape = false;
  153. while ((c = text[cursor]) != L'\0') {
  154. cursor++;
  155. if (c == L'\r') { // Mac or Windows breaks
  156. if (text[cursor] == L'\n')
  157. cursor++;
  158. // If text has begun, don't skip empty line
  159. if (m_paragraph) {
  160. endParagraph();
  161. enterElement(ELEMENT_SEPARATOR);
  162. }
  163. escape = false;
  164. continue;
  165. }
  166. if (c == L'\n') { // Unix breaks
  167. // If text has begun, don't skip empty line
  168. if (m_paragraph) {
  169. endParagraph();
  170. enterElement(ELEMENT_SEPARATOR);
  171. }
  172. escape = false;
  173. continue;
  174. }
  175. if (escape) {
  176. escape = false;
  177. pushChar(c);
  178. continue;
  179. }
  180. if (c == L'\\') {
  181. escape = true;
  182. continue;
  183. }
  184. // Tag check
  185. if (c == L'<') {
  186. u32 newcursor = parseTag(text, cursor);
  187. if (newcursor > 0) {
  188. cursor = newcursor;
  189. continue;
  190. }
  191. }
  192. // Default behavior
  193. pushChar(c);
  194. }
  195. endParagraph();
  196. }
  197. void ParsedText::endElement()
  198. {
  199. m_element = NULL;
  200. }
  201. void ParsedText::endParagraph()
  202. {
  203. if (!m_paragraph)
  204. return;
  205. endElement();
  206. m_paragraph = NULL;
  207. }
  208. void ParsedText::enterParagraph()
  209. {
  210. if (!m_paragraph) {
  211. m_paragraphs.emplace_back();
  212. m_paragraph = &m_paragraphs.back();
  213. m_paragraph->setStyle(m_style);
  214. }
  215. }
  216. void ParsedText::enterElement(ElementType type)
  217. {
  218. enterParagraph();
  219. if (!m_element || m_element->type != type) {
  220. m_paragraph->elements.emplace_back();
  221. m_element = &m_paragraph->elements.back();
  222. m_element->type = type;
  223. m_element->tags = m_active_tags;
  224. m_element->setStyle(m_style);
  225. }
  226. }
  227. void ParsedText::pushChar(wchar_t c)
  228. {
  229. // New word if needed
  230. if (c == L' ' || c == L'\t')
  231. enterElement(ELEMENT_SEPARATOR);
  232. else
  233. enterElement(ELEMENT_TEXT);
  234. m_element->text += c;
  235. }
  236. ParsedText::Tag *ParsedText::newTag(const std::string &name, const AttrsList &attrs)
  237. {
  238. endElement();
  239. Tag *newtag = new Tag();
  240. newtag->name = name;
  241. newtag->attrs = attrs;
  242. m_tags.push_back(newtag);
  243. return newtag;
  244. }
  245. ParsedText::Tag *ParsedText::openTag(const std::string &name, const AttrsList &attrs)
  246. {
  247. Tag *newtag = newTag(name, attrs);
  248. m_active_tags.push_front(newtag);
  249. return newtag;
  250. }
  251. bool ParsedText::closeTag(const std::string &name)
  252. {
  253. bool found = false;
  254. for (auto id = m_active_tags.begin(); id != m_active_tags.end(); ++id)
  255. if ((*id)->name == name) {
  256. m_active_tags.erase(id);
  257. found = true;
  258. break;
  259. }
  260. return found;
  261. }
  262. void ParsedText::parseGenericStyleAttr(
  263. const std::string &name, const std::string &value, StyleList &style)
  264. {
  265. // Color styles
  266. if (name == "color" || name == "hovercolor") {
  267. if (check_color(value))
  268. style[name] = value;
  269. // Boolean styles
  270. } else if (name == "bold" || name == "italic" || name == "underline") {
  271. style[name] = is_yes(value);
  272. } else if (name == "size") {
  273. if (check_integer(value))
  274. style["fontsize"] = value;
  275. } else if (name == "font") {
  276. if (value == "mono" || value == "normal")
  277. style["fontstyle"] = value;
  278. }
  279. }
  280. void ParsedText::parseStyles(const AttrsList &attrs, StyleList &style)
  281. {
  282. for (auto const &attr : attrs)
  283. parseGenericStyleAttr(attr.first, attr.second, style);
  284. }
  285. void ParsedText::globalTag(const AttrsList &attrs)
  286. {
  287. for (const auto &attr : attrs) {
  288. // Only page level style
  289. if (attr.first == "margin") {
  290. if (check_integer(attr.second))
  291. margin = stoi(attr.second.c_str());
  292. } else if (attr.first == "valign") {
  293. if (attr.second == "top")
  294. valign = ParsedText::VALIGN_TOP;
  295. else if (attr.second == "bottom")
  296. valign = ParsedText::VALIGN_BOTTOM;
  297. else if (attr.second == "middle")
  298. valign = ParsedText::VALIGN_MIDDLE;
  299. } else if (attr.first == "background") {
  300. irr::video::SColor color;
  301. if (attr.second == "none") {
  302. background_type = BACKGROUND_NONE;
  303. } else if (parseColorString(attr.second, color, false)) {
  304. background_type = BACKGROUND_COLOR;
  305. background_color = color;
  306. }
  307. // Inheriting styles
  308. } else if (attr.first == "halign") {
  309. if (attr.second == "left" || attr.second == "center" ||
  310. attr.second == "right" ||
  311. attr.second == "justify")
  312. m_root_tag.style["halign"] = attr.second;
  313. // Generic default styles
  314. } else {
  315. parseGenericStyleAttr(attr.first, attr.second, m_root_tag.style);
  316. }
  317. }
  318. }
  319. u32 ParsedText::parseTag(const wchar_t *text, u32 cursor)
  320. {
  321. // Tag name
  322. bool end = false;
  323. std::string name = "";
  324. wchar_t c = text[cursor];
  325. if (c == L'/') {
  326. end = true;
  327. c = text[++cursor];
  328. if (c == L'\0')
  329. return 0;
  330. }
  331. while (c != ' ' && c != '>') {
  332. name += c;
  333. c = text[++cursor];
  334. if (c == L'\0')
  335. return 0;
  336. }
  337. // Tag attributes
  338. AttrsList attrs;
  339. while (c != L'>') {
  340. std::string attr_name = "";
  341. std::string attr_val = "";
  342. while (c == ' ') {
  343. c = text[++cursor];
  344. if (c == L'\0' || c == L'=')
  345. return 0;
  346. }
  347. while (c != L' ' && c != L'=') {
  348. attr_name += (char)c;
  349. c = text[++cursor];
  350. if (c == L'\0' || c == L'>')
  351. return 0;
  352. }
  353. while (c == L' ') {
  354. c = text[++cursor];
  355. if (c == L'\0' || c == L'>')
  356. return 0;
  357. }
  358. if (c != L'=')
  359. return 0;
  360. c = text[++cursor];
  361. if (c == L'\0')
  362. return 0;
  363. while (c != L'>' && c != L' ') {
  364. attr_val += (char)c;
  365. c = text[++cursor];
  366. if (c == L'\0')
  367. return 0;
  368. }
  369. attrs[attr_name] = attr_val;
  370. }
  371. ++cursor; // Last ">"
  372. // Tag specific processing
  373. StyleList style;
  374. if (name == "global") {
  375. if (end)
  376. return 0;
  377. globalTag(attrs);
  378. } else if (name == "style") {
  379. if (end) {
  380. closeTag(name);
  381. } else {
  382. parseStyles(attrs, style);
  383. openTag(name, attrs)->style = style;
  384. }
  385. endElement();
  386. } else if (name == "img" || name == "item") {
  387. if (end)
  388. return 0;
  389. // Name is a required attribute
  390. if (!attrs.count("name"))
  391. return 0;
  392. // Rotate attribute is only for <item>
  393. if (attrs.count("rotate") && name != "item")
  394. return 0;
  395. // Angle attribute is only for <item>
  396. if (attrs.count("angle") && name != "item")
  397. return 0;
  398. // Ok, element can be created
  399. newTag(name, attrs);
  400. if (name == "img")
  401. enterElement(ELEMENT_IMAGE);
  402. else
  403. enterElement(ELEMENT_ITEM);
  404. m_element->text = strtostrw(attrs["name"]);
  405. if (attrs.count("float")) {
  406. if (attrs["float"] == "left")
  407. m_element->floating = FLOAT_LEFT;
  408. if (attrs["float"] == "right")
  409. m_element->floating = FLOAT_RIGHT;
  410. }
  411. if (attrs.count("width")) {
  412. int width = stoi(attrs["width"]);
  413. if (width > 0)
  414. m_element->dim.Width = width;
  415. }
  416. if (attrs.count("height")) {
  417. int height = stoi(attrs["height"]);
  418. if (height > 0)
  419. m_element->dim.Height = height;
  420. }
  421. if (attrs.count("angle")) {
  422. std::string str = attrs["angle"];
  423. std::vector<std::string> parts = split(str, ',');
  424. if (parts.size() == 3) {
  425. m_element->angle = v3s16(
  426. rangelim(stoi(parts[0]), -180, 180),
  427. rangelim(stoi(parts[1]), -180, 180),
  428. rangelim(stoi(parts[2]), -180, 180));
  429. m_element->rotation = v3s16(0, 0, 0);
  430. }
  431. }
  432. if (attrs.count("rotate")) {
  433. if (attrs["rotate"] == "yes") {
  434. m_element->rotation = v3s16(0, 100, 0);
  435. } else {
  436. std::string str = attrs["rotate"];
  437. std::vector<std::string> parts = split(str, ',');
  438. if (parts.size() == 3) {
  439. m_element->rotation = v3s16 (
  440. rangelim(stoi(parts[0]), -1000, 1000),
  441. rangelim(stoi(parts[1]), -1000, 1000),
  442. rangelim(stoi(parts[2]), -1000, 1000));
  443. }
  444. }
  445. }
  446. endElement();
  447. } else if (name == "tag") {
  448. // Required attributes
  449. if (!attrs.count("name"))
  450. return 0;
  451. StyleList tagstyle;
  452. parseStyles(attrs, tagstyle);
  453. if (is_yes(attrs["paragraph"]))
  454. m_paragraphtags[attrs["name"]] = tagstyle;
  455. else
  456. m_elementtags[attrs["name"]] = tagstyle;
  457. } else if (name == "action") {
  458. if (end) {
  459. closeTag(name);
  460. } else {
  461. if (!attrs.count("name"))
  462. return 0;
  463. openTag(name, attrs)->style = m_elementtags["action"];
  464. }
  465. } else if (m_elementtags.count(name)) {
  466. if (end) {
  467. closeTag(name);
  468. } else {
  469. openTag(name, attrs)->style = m_elementtags[name];
  470. }
  471. endElement();
  472. } else if (m_paragraphtags.count(name)) {
  473. if (end) {
  474. closeTag(name);
  475. } else {
  476. openTag(name, attrs)->style = m_paragraphtags[name];
  477. }
  478. endParagraph();
  479. } else
  480. return 0; // Unknown tag
  481. // Update styles accordingly
  482. m_style.clear();
  483. for (auto tag = m_active_tags.crbegin(); tag != m_active_tags.crend(); ++tag)
  484. for (const auto &prop : (*tag)->style)
  485. m_style[prop.first] = prop.second;
  486. return cursor;
  487. }
  488. // -----------------------------------------------------------------------------
  489. // Text Drawer
  490. TextDrawer::TextDrawer(const wchar_t *text, Client *client,
  491. gui::IGUIEnvironment *environment, ISimpleTextureSource *tsrc) :
  492. m_text(text),
  493. m_client(client), m_environment(environment)
  494. {
  495. // Size all elements
  496. for (auto &p : m_text.m_paragraphs) {
  497. for (auto &e : p.elements) {
  498. switch (e.type) {
  499. case ParsedText::ELEMENT_SEPARATOR:
  500. case ParsedText::ELEMENT_TEXT:
  501. if (e.font) {
  502. e.dim.Width = e.font->getDimension(e.text.c_str()).Width;
  503. e.dim.Height = e.font->getDimension(L"Yy").Height;
  504. #if USE_FREETYPE
  505. e.baseline = e.dim.Height - 1 - e.font->getAscender()/64;
  506. #endif
  507. } else {
  508. e.dim = {0, 0};
  509. }
  510. break;
  511. case ParsedText::ELEMENT_IMAGE:
  512. case ParsedText::ELEMENT_ITEM:
  513. // Resize only non sized items
  514. if (e.dim.Height != 0 && e.dim.Width != 0)
  515. break;
  516. // Default image and item size
  517. core::dimension2d<u32> dim(80, 80);
  518. if (e.type == ParsedText::ELEMENT_IMAGE) {
  519. video::ITexture *texture =
  520. m_client->getTextureSource()->
  521. getTexture(strwtostr(e.text));
  522. if (texture)
  523. dim = texture->getOriginalSize();
  524. }
  525. if (e.dim.Height == 0)
  526. if (e.dim.Width == 0)
  527. e.dim = dim;
  528. else
  529. e.dim.Height = dim.Height * e.dim.Width /
  530. dim.Width;
  531. else
  532. e.dim.Width = dim.Width * e.dim.Height /
  533. dim.Height;
  534. break;
  535. }
  536. }
  537. }
  538. }
  539. // Get element at given coordinates. Coordinates are inner coordinates (starting
  540. // at 0,0).
  541. ParsedText::Element *TextDrawer::getElementAt(core::position2d<s32> pos)
  542. {
  543. pos.Y -= m_voffset;
  544. for (auto &p : m_text.m_paragraphs) {
  545. for (auto &el : p.elements) {
  546. core::rect<s32> rect(el.pos, el.dim);
  547. if (rect.isPointInside(pos))
  548. return &el;
  549. }
  550. }
  551. return 0;
  552. }
  553. /*
  554. This function places all elements according to given width. Elements have
  555. been previously sized by constructor and will be later drawed by draw.
  556. It may be called each time width changes and resulting height can be
  557. retrieved using getHeight. See GUIHyperText constructor, it uses it once to
  558. test if text fits in window and eventually another time if width is reduced
  559. m_floatingbecause of scrollbar added.
  560. */
  561. void TextDrawer::place(const core::rect<s32> &dest_rect)
  562. {
  563. m_floating.clear();
  564. s32 y = 0;
  565. s32 ymargin = m_text.margin;
  566. // Iterator used :
  567. // p - Current paragraph, walked only once
  568. // el - Current element, walked only once
  569. // e and f - local element and floating operators
  570. for (auto &p : m_text.m_paragraphs) {
  571. // Find and place floating stuff in paragraph
  572. for (auto e = p.elements.begin(); e != p.elements.end(); ++e) {
  573. if (e->floating != ParsedText::FLOAT_NONE) {
  574. if (y)
  575. e->pos.Y = y + std::max(ymargin, e->margin);
  576. else
  577. e->pos.Y = ymargin;
  578. if (e->floating == ParsedText::FLOAT_LEFT)
  579. e->pos.X = m_text.margin;
  580. if (e->floating == ParsedText::FLOAT_RIGHT)
  581. e->pos.X = dest_rect.getWidth() - e->dim.Width -
  582. m_text.margin;
  583. RectWithMargin floating;
  584. floating.rect = core::rect<s32>(e->pos, e->dim);
  585. floating.margin = e->margin;
  586. m_floating.push_back(floating);
  587. }
  588. }
  589. if (y)
  590. y = y + std::max(ymargin, p.margin);
  591. ymargin = p.margin;
  592. // Place non floating stuff
  593. std::vector<ParsedText::Element>::iterator el = p.elements.begin();
  594. while (el != p.elements.end()) {
  595. // Determine line width and y pos
  596. s32 left, right;
  597. s32 nexty = y;
  598. do {
  599. y = nexty;
  600. nexty = 0;
  601. // Inner left & right
  602. left = m_text.margin;
  603. right = dest_rect.getWidth() - m_text.margin;
  604. for (const auto &f : m_floating) {
  605. // Does floating rect intersect paragraph y line?
  606. if (f.rect.UpperLeftCorner.Y - f.margin <= y &&
  607. f.rect.LowerRightCorner.Y + f.margin >= y) {
  608. // Next Y to try if no room left
  609. if (!nexty || f.rect.LowerRightCorner.Y +
  610. std::max(f.margin, p.margin) < nexty) {
  611. nexty = f.rect.LowerRightCorner.Y +
  612. std::max(f.margin, p.margin) + 1;
  613. }
  614. if (f.rect.UpperLeftCorner.X - f.margin <= left &&
  615. f.rect.LowerRightCorner.X + f.margin < right) {
  616. // float on left
  617. if (f.rect.LowerRightCorner.X +
  618. std::max(f.margin, p.margin) > left) {
  619. left = f.rect.LowerRightCorner.X +
  620. std::max(f.margin, p.margin);
  621. }
  622. } else if (f.rect.LowerRightCorner.X + f.margin >= right &&
  623. f.rect.UpperLeftCorner.X - f.margin > left) {
  624. // float on right
  625. if (f.rect.UpperLeftCorner.X -
  626. std::max(f.margin, p.margin) < right)
  627. right = f.rect.UpperLeftCorner.X -
  628. std::max(f.margin, p.margin);
  629. } else if (f.rect.UpperLeftCorner.X - f.margin <= left &&
  630. f.rect.LowerRightCorner.X + f.margin >= right) {
  631. // float taking all space
  632. left = right;
  633. }
  634. else
  635. { // float in the middle -- should not occure yet, see that later
  636. }
  637. }
  638. }
  639. } while (nexty && right <= left);
  640. u32 linewidth = right - left;
  641. float x = left;
  642. u32 charsheight = 0;
  643. u32 charswidth = 0;
  644. u32 wordcount = 0;
  645. // Skip begining of line separators but include them in height
  646. // computation.
  647. while (el != p.elements.end() &&
  648. el->type == ParsedText::ELEMENT_SEPARATOR) {
  649. if (el->floating == ParsedText::FLOAT_NONE) {
  650. el->drawwidth = 0;
  651. if (charsheight < el->dim.Height)
  652. charsheight = el->dim.Height;
  653. }
  654. el++;
  655. }
  656. std::vector<ParsedText::Element>::iterator linestart = el;
  657. std::vector<ParsedText::Element>::iterator lineend = p.elements.end();
  658. // First pass, find elements fitting into line
  659. // (or at least one element)
  660. while (el != p.elements.end() && (charswidth == 0 ||
  661. charswidth + el->dim.Width <= linewidth)) {
  662. if (el->floating == ParsedText::FLOAT_NONE) {
  663. if (el->type != ParsedText::ELEMENT_SEPARATOR) {
  664. lineend = el;
  665. wordcount++;
  666. }
  667. charswidth += el->dim.Width;
  668. if (charsheight < el->dim.Height)
  669. charsheight = el->dim.Height;
  670. }
  671. el++;
  672. }
  673. // Empty line, nothing to place only go down line height
  674. if (lineend == p.elements.end()) {
  675. y += charsheight;
  676. continue;
  677. }
  678. // Point to the first position outside line (may be end())
  679. lineend++;
  680. // Second pass, compute printable line width and adjustments
  681. charswidth = 0;
  682. s32 top = 0;
  683. s32 bottom = 0;
  684. for (auto e = linestart; e != lineend; ++e) {
  685. if (e->floating == ParsedText::FLOAT_NONE) {
  686. charswidth += e->dim.Width;
  687. if (top < (s32)e->dim.Height - e->baseline)
  688. top = e->dim.Height - e->baseline;
  689. if (bottom < e->baseline)
  690. bottom = e->baseline;
  691. }
  692. }
  693. float extraspace = 0.f;
  694. switch (p.halign) {
  695. case ParsedText::HALIGN_CENTER:
  696. x += (linewidth - charswidth) / 2.f;
  697. break;
  698. case ParsedText::HALIGN_JUSTIFY:
  699. if (wordcount > 1 && // Justification only if at least two words
  700. !(lineend == p.elements.end())) // Don't justify last line
  701. extraspace = ((float)(linewidth - charswidth)) / (wordcount - 1);
  702. break;
  703. case ParsedText::HALIGN_RIGHT:
  704. x += linewidth - charswidth;
  705. break;
  706. case ParsedText::HALIGN_LEFT:
  707. break;
  708. }
  709. // Third pass, actually place everything
  710. for (auto e = linestart; e != lineend; ++e) {
  711. if (e->floating != ParsedText::FLOAT_NONE)
  712. continue;
  713. e->pos.X = x;
  714. e->pos.Y = y;
  715. switch (e->type) {
  716. case ParsedText::ELEMENT_TEXT:
  717. case ParsedText::ELEMENT_SEPARATOR:
  718. e->pos.X = x;
  719. // Align char baselines
  720. e->pos.Y = y + top + e->baseline - e->dim.Height;
  721. x += e->dim.Width;
  722. if (e->type == ParsedText::ELEMENT_SEPARATOR)
  723. x += extraspace;
  724. break;
  725. case ParsedText::ELEMENT_IMAGE:
  726. case ParsedText::ELEMENT_ITEM:
  727. x += e->dim.Width;
  728. break;
  729. }
  730. // Draw width for separator can be different than element
  731. // width. This will be important for char effects like
  732. // underline.
  733. e->drawwidth = x - e->pos.X;
  734. }
  735. y += charsheight;
  736. } // Elements (actually lines)
  737. } // Paragraph
  738. // Check if float goes under paragraph
  739. for (const auto &f : m_floating) {
  740. if (f.rect.LowerRightCorner.Y >= y)
  741. y = f.rect.LowerRightCorner.Y;
  742. }
  743. m_height = y + m_text.margin;
  744. // Compute vertical offset according to vertical alignment
  745. if (m_height < dest_rect.getHeight())
  746. switch (m_text.valign) {
  747. case ParsedText::VALIGN_BOTTOM:
  748. m_voffset = dest_rect.getHeight() - m_height;
  749. break;
  750. case ParsedText::VALIGN_MIDDLE:
  751. m_voffset = (dest_rect.getHeight() - m_height) / 2;
  752. break;
  753. case ParsedText::VALIGN_TOP:
  754. default:
  755. m_voffset = 0;
  756. }
  757. else
  758. m_voffset = 0;
  759. }
  760. // Draw text in a rectangle with a given offset. Items are actually placed in
  761. // relative (to upper left corner) coordinates.
  762. void TextDrawer::draw(const core::rect<s32> &dest_rect,
  763. const core::position2d<s32> &dest_offset)
  764. {
  765. irr::video::IVideoDriver *driver = m_environment->getVideoDriver();
  766. core::position2d<s32> offset = dest_rect.UpperLeftCorner + dest_offset;
  767. offset.Y += m_voffset;
  768. if (m_text.background_type == ParsedText::BACKGROUND_COLOR)
  769. driver->draw2DRectangle(m_text.background_color, dest_rect);
  770. for (auto &p : m_text.m_paragraphs) {
  771. for (auto &el : p.elements) {
  772. core::rect<s32> rect(el.pos + offset, el.dim);
  773. if (!rect.isRectCollided(dest_rect))
  774. continue;
  775. switch (el.type) {
  776. case ParsedText::ELEMENT_SEPARATOR:
  777. case ParsedText::ELEMENT_TEXT: {
  778. irr::video::SColor color = el.color;
  779. for (auto tag : el.tags)
  780. if (&(*tag) == m_hovertag)
  781. color = el.hovercolor;
  782. if (!el.font)
  783. break;
  784. if (el.type == ParsedText::ELEMENT_TEXT)
  785. el.font->draw(el.text, rect, color, false, true,
  786. &dest_rect);
  787. if (el.underline && el.drawwidth) {
  788. s32 linepos = el.pos.Y + offset.Y +
  789. el.dim.Height - (el.baseline >> 1);
  790. core::rect<s32> linerect(el.pos.X + offset.X,
  791. linepos - (el.baseline >> 3) - 1,
  792. el.pos.X + offset.X + el.drawwidth,
  793. linepos + (el.baseline >> 3));
  794. driver->draw2DRectangle(color, linerect, &dest_rect);
  795. }
  796. } break;
  797. case ParsedText::ELEMENT_IMAGE: {
  798. video::ITexture *texture =
  799. m_client->getTextureSource()->getTexture(
  800. strwtostr(el.text));
  801. if (texture != 0)
  802. m_environment->getVideoDriver()->draw2DImage(
  803. texture, rect,
  804. irr::core::rect<s32>(
  805. core::position2d<s32>(0, 0),
  806. texture->getOriginalSize()),
  807. &dest_rect, 0, true);
  808. } break;
  809. case ParsedText::ELEMENT_ITEM: {
  810. IItemDefManager *idef = m_client->idef();
  811. ItemStack item;
  812. item.deSerialize(strwtostr(el.text), idef);
  813. drawItemStack(
  814. m_environment->getVideoDriver(),
  815. g_fontengine->getFont(), item, rect, &dest_rect,
  816. m_client, IT_ROT_OTHER, el.angle, el.rotation
  817. );
  818. } break;
  819. }
  820. }
  821. }
  822. }
  823. // -----------------------------------------------------------------------------
  824. // GUIHyperText - The formated text area formspec item
  825. //! constructor
  826. GUIHyperText::GUIHyperText(const wchar_t *text, IGUIEnvironment *environment,
  827. IGUIElement *parent, s32 id, const core::rect<s32> &rectangle,
  828. Client *client, ISimpleTextureSource *tsrc) :
  829. IGUIElement(EGUIET_ELEMENT, environment, parent, id, rectangle),
  830. m_client(client), m_vscrollbar(nullptr),
  831. m_drawer(text, client, environment, tsrc), m_text_scrollpos(0, 0)
  832. {
  833. #ifdef _DEBUG
  834. setDebugName("GUIHyperText");
  835. #endif
  836. IGUISkin *skin = 0;
  837. if (Environment)
  838. skin = Environment->getSkin();
  839. m_scrollbar_width = skin ? skin->getSize(gui::EGDS_SCROLLBAR_SIZE) : 16;
  840. core::rect<s32> rect = irr::core::rect<s32>(
  841. RelativeRect.getWidth() - m_scrollbar_width, 0,
  842. RelativeRect.getWidth(), RelativeRect.getHeight());
  843. m_vscrollbar = new GUIScrollBar(Environment, this, -1, rect, false, true);
  844. m_vscrollbar->setVisible(false);
  845. }
  846. //! destructor
  847. GUIHyperText::~GUIHyperText()
  848. {
  849. m_vscrollbar->remove();
  850. }
  851. ParsedText::Element *GUIHyperText::getElementAt(s32 X, s32 Y)
  852. {
  853. core::position2d<s32> pos{X, Y};
  854. pos -= m_display_text_rect.UpperLeftCorner;
  855. pos -= m_text_scrollpos;
  856. return m_drawer.getElementAt(pos);
  857. }
  858. void GUIHyperText::checkHover(s32 X, s32 Y)
  859. {
  860. m_drawer.m_hovertag = nullptr;
  861. if (AbsoluteRect.isPointInside(core::position2d<s32>(X, Y))) {
  862. ParsedText::Element *element = getElementAt(X, Y);
  863. if (element) {
  864. for (auto &tag : element->tags) {
  865. if (tag->name == "action") {
  866. m_drawer.m_hovertag = tag;
  867. break;
  868. }
  869. }
  870. }
  871. }
  872. if (m_drawer.m_hovertag)
  873. RenderingEngine::get_raw_device()->getCursorControl()->setActiveIcon(
  874. gui::ECI_HAND);
  875. else
  876. RenderingEngine::get_raw_device()->getCursorControl()->setActiveIcon(
  877. gui::ECI_NORMAL);
  878. }
  879. bool GUIHyperText::OnEvent(const SEvent &event)
  880. {
  881. // Scroll bar
  882. if (event.EventType == EET_GUI_EVENT &&
  883. event.GUIEvent.EventType == EGET_SCROLL_BAR_CHANGED &&
  884. event.GUIEvent.Caller == m_vscrollbar) {
  885. m_text_scrollpos.Y = -m_vscrollbar->getPos();
  886. }
  887. // Reset hover if element left
  888. if (event.EventType == EET_GUI_EVENT &&
  889. event.GUIEvent.EventType == EGET_ELEMENT_LEFT) {
  890. m_drawer.m_hovertag = nullptr;
  891. RenderingEngine::get_raw_device()->getCursorControl()->setActiveIcon(
  892. gui::ECI_NORMAL);
  893. }
  894. if (event.EventType == EET_MOUSE_INPUT_EVENT) {
  895. if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
  896. checkHover(event.MouseInput.X, event.MouseInput.Y);
  897. if (event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
  898. m_vscrollbar->setPos(m_vscrollbar->getPos() -
  899. event.MouseInput.Wheel * m_vscrollbar->getSmallStep());
  900. m_text_scrollpos.Y = -m_vscrollbar->getPos();
  901. m_drawer.draw(m_display_text_rect, m_text_scrollpos);
  902. checkHover(event.MouseInput.X, event.MouseInput.Y);
  903. } else if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
  904. ParsedText::Element *element = getElementAt(
  905. event.MouseInput.X, event.MouseInput.Y);
  906. if (element) {
  907. for (auto &tag : element->tags) {
  908. if (tag->name == "action") {
  909. Text = core::stringw(L"action:") +
  910. strtostrw(tag->attrs["name"]);
  911. if (Parent) {
  912. SEvent newEvent;
  913. newEvent.EventType = EET_GUI_EVENT;
  914. newEvent.GUIEvent.Caller = this;
  915. newEvent.GUIEvent.Element = 0;
  916. newEvent.GUIEvent.EventType = EGET_BUTTON_CLICKED;
  917. Parent->OnEvent(newEvent);
  918. }
  919. break;
  920. }
  921. }
  922. }
  923. }
  924. }
  925. return IGUIElement::OnEvent(event);
  926. }
  927. //! draws the element and its children
  928. void GUIHyperText::draw()
  929. {
  930. if (!IsVisible)
  931. return;
  932. // Text
  933. m_display_text_rect = AbsoluteRect;
  934. m_drawer.place(m_display_text_rect);
  935. // Show scrollbar if text overflow
  936. if (m_drawer.getHeight() > m_display_text_rect.getHeight()) {
  937. m_vscrollbar->setSmallStep(m_display_text_rect.getHeight() * 0.1f);
  938. m_vscrollbar->setLargeStep(m_display_text_rect.getHeight() * 0.5f);
  939. m_vscrollbar->setMax(m_drawer.getHeight() - m_display_text_rect.getHeight());
  940. m_vscrollbar->setVisible(true);
  941. m_vscrollbar->setPageSize(s32(m_drawer.getHeight()));
  942. core::rect<s32> smaller_rect = m_display_text_rect;
  943. smaller_rect.LowerRightCorner.X -= m_scrollbar_width;
  944. m_drawer.place(smaller_rect);
  945. } else {
  946. m_vscrollbar->setMax(0);
  947. m_vscrollbar->setPos(0);
  948. m_vscrollbar->setVisible(false);
  949. }
  950. m_drawer.draw(m_display_text_rect, m_text_scrollpos);
  951. // draw children
  952. IGUIElement::draw();
  953. }