TextControls.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * TextControls.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "TextControls.h"
  12. #include "Slider.h"
  13. #include "Images.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../GameEngine.h"
  16. #include "../GameInstance.h"
  17. #include "../windows/CMessage.h"
  18. #include "../windows/InfoWindows.h"
  19. #include "../adventureMap/CInGameConsole.h"
  20. #include "../eventsSDL/InputHandler.h"
  21. #include "../render/Canvas.h"
  22. #include "../render/Graphics.h"
  23. #include "../render/IFont.h"
  24. #include "../render/IRenderHandler.h"
  25. #include "../../lib/texts/TextOperations.h"
  26. #ifdef VCMI_ANDROID
  27. #include "lib/CAndroidVMHelper.h"
  28. #endif
  29. std::string CLabel::visibleText()
  30. {
  31. return text;
  32. }
  33. void CLabel::showAll(Canvas & to)
  34. {
  35. CIntObject::showAll(to);
  36. if(!visibleText().empty())
  37. blitLine(to, pos, visibleText());
  38. }
  39. CLabel::CLabel(int x, int y, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text, int maxWidth)
  40. : CTextContainer(Align, Font, Color), text(Text), maxWidth(maxWidth)
  41. {
  42. setRedrawParent(true);
  43. autoRedraw = true;
  44. pos.x += x;
  45. pos.y += y;
  46. pos.w = pos.h = 0;
  47. trimText();
  48. if(alignment == ETextAlignment::TOPLEFT) // causes issues for MIDDLE
  49. {
  50. const auto & fontPtr = ENGINE->renderHandler().loadFont(font);
  51. pos.w = fontPtr->getStringWidth(visibleText().c_str());
  52. pos.h = fontPtr->getLineHeight();
  53. }
  54. }
  55. Point CLabel::getBorderSize()
  56. {
  57. return Point(0, 0);
  58. }
  59. std::string CLabel::getText()
  60. {
  61. return text;
  62. }
  63. void CLabel::setAutoRedraw(bool value)
  64. {
  65. autoRedraw = value;
  66. }
  67. void CLabel::setText(const std::string & Txt)
  68. {
  69. assert(TextOperations::isValidUnicodeString(Txt));
  70. text = Txt;
  71. trimText();
  72. if(autoRedraw)
  73. {
  74. if(background || !parent)
  75. redraw();
  76. else
  77. parent->redraw();
  78. }
  79. }
  80. void CLabel::clear()
  81. {
  82. text.clear();
  83. if(autoRedraw)
  84. {
  85. if(background || !parent)
  86. redraw();
  87. else
  88. parent->redraw();
  89. }
  90. }
  91. void CLabel::setMaxWidth(int width)
  92. {
  93. maxWidth = width;
  94. }
  95. void CLabel::trimText()
  96. {
  97. if(maxWidth > 0)
  98. {
  99. const auto & fontPtr = ENGINE->renderHandler().loadFont(font);
  100. while (fontPtr->getStringWidth(visibleText().c_str()) > maxWidth)
  101. TextOperations::trimRightUnicode(text);
  102. }
  103. }
  104. void CLabel::setColor(const ColorRGBA & Color)
  105. {
  106. color = Color;
  107. if(autoRedraw)
  108. {
  109. if(background || !parent)
  110. redraw();
  111. else
  112. parent->redraw();
  113. }
  114. }
  115. size_t CLabel::getWidth()
  116. {
  117. const auto & fontPtr = ENGINE->renderHandler().loadFont(font);
  118. return fontPtr->getStringWidth(visibleText());
  119. }
  120. CMultiLineLabel::CMultiLineLabel(Rect position, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text) :
  121. CLabel(position.x, position.y, Font, Align, Color, Text),
  122. visibleSize(0, 0, position.w, position.h)
  123. {
  124. pos.w = position.w;
  125. pos.h = position.h;
  126. splitText(Text, true);
  127. }
  128. void CMultiLineLabel::setVisibleSize(Rect visibleSize, bool redrawElement)
  129. {
  130. this->visibleSize = visibleSize;
  131. if(redrawElement)
  132. redraw();
  133. }
  134. void CMultiLineLabel::scrollTextBy(int distance)
  135. {
  136. scrollTextTo(visibleSize.y + distance);
  137. }
  138. void CMultiLineLabel::scrollTextTo(int distance, bool redrawAfterScroll)
  139. {
  140. Rect size = visibleSize;
  141. size.y = distance;
  142. setVisibleSize(size, redrawAfterScroll);
  143. }
  144. void CMultiLineLabel::setText(const std::string & Txt)
  145. {
  146. splitText(Txt, false); //setText used below can handle redraw
  147. CLabel::setText(Txt);
  148. }
  149. std::vector<std::string> CMultiLineLabel::getLines()
  150. {
  151. return lines;
  152. }
  153. void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what)
  154. {
  155. const auto f = ENGINE->renderHandler().loadFont(font);
  156. Point where = destRect.topLeft();
  157. const std::string delimiters = "{}";
  158. auto delimitersCount = std::count_if(what.cbegin(), what.cend(), [&delimiters](char c)
  159. {
  160. return delimiters.find(c) != std::string::npos;
  161. });
  162. //We should count delimiters length from string to correct centering later.
  163. delimitersCount *= f->getStringWidth(delimiters)/2;
  164. std::smatch match;
  165. std::regex expr("\\{(.*?)\\|");
  166. std::string::const_iterator searchStart( what.cbegin() );
  167. while(std::regex_search(searchStart, what.cend(), match, expr))
  168. {
  169. std::string colorText = match[1].str();
  170. if(auto c = Colors::parseColor(colorText))
  171. delimitersCount += f->getStringWidth(colorText + "|");
  172. searchStart = match.suffix().first;
  173. }
  174. // input is rect in which given text should be placed
  175. // calculate proper position for top-left corner of the text
  176. if(alignment == ETextAlignment::TOPLEFT || alignment == ETextAlignment::CENTERLEFT || alignment == ETextAlignment::BOTTOMLEFT)
  177. where.x += getBorderSize().x;
  178. if(alignment == ETextAlignment::CENTER || alignment == ETextAlignment::TOPCENTER || alignment == ETextAlignment::BOTTOMCENTER)
  179. where.x += (destRect.w - (static_cast<int>(f->getStringWidth(what)) - delimitersCount)) / 2;
  180. if(alignment == ETextAlignment::TOPRIGHT || alignment == ETextAlignment::BOTTOMRIGHT || alignment == ETextAlignment::CENTERRIGHT)
  181. where.x += getBorderSize().x + destRect.w - (static_cast<int>(f->getStringWidth(what)) - delimitersCount);
  182. if(alignment == ETextAlignment::TOPLEFT || alignment == ETextAlignment::TOPCENTER || alignment == ETextAlignment::TOPRIGHT)
  183. where.y += getBorderSize().y;
  184. if(alignment == ETextAlignment::CENTERLEFT || alignment == ETextAlignment::CENTER || alignment == ETextAlignment::CENTERRIGHT)
  185. where.y += (destRect.h - static_cast<int>(f->getLineHeight())) / 2;
  186. if(alignment == ETextAlignment::BOTTOMLEFT || alignment == ETextAlignment::BOTTOMCENTER || alignment == ETextAlignment::BOTTOMRIGHT)
  187. where.y += getBorderSize().y + destRect.h - static_cast<int>(f->getLineHeight());
  188. size_t begin = 0;
  189. size_t currDelimiter = 0;
  190. do
  191. {
  192. size_t end = what.find_first_of(delimiters[currDelimiter % 2], begin);
  193. if(begin != end)
  194. {
  195. std::string toPrint = what.substr(begin, end - begin);
  196. if(currDelimiter % 2) // Enclosed in {} text - set to yellow or defined color
  197. {
  198. std::smatch match;
  199. std::regex expr("^(.*?)\\|");
  200. if(std::regex_search(toPrint, match, expr))
  201. {
  202. std::string colorText = match[1].str();
  203. if(auto color = Colors::parseColor(colorText))
  204. {
  205. toPrint = toPrint.substr(colorText.length() + 1, toPrint.length() - colorText.length());
  206. to.drawText(where, font, *color, ETextAlignment::TOPLEFT, toPrint);
  207. }
  208. else
  209. to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
  210. }
  211. else
  212. to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
  213. }
  214. else // Non-enclosed text, use default color
  215. to.drawText(where, font, color, ETextAlignment::TOPLEFT, toPrint);
  216. begin = end;
  217. where.x += (int)f->getStringWidth(toPrint);
  218. }
  219. currDelimiter++;
  220. } while(begin++ != std::string::npos);
  221. }
  222. CTextContainer::CTextContainer(ETextAlignment alignment, EFonts font, ColorRGBA color) :
  223. alignment(alignment),
  224. font(font),
  225. color(color)
  226. {
  227. }
  228. void CMultiLineLabel::showAll(Canvas & to)
  229. {
  230. CIntObject::showAll(to);
  231. const auto & fontPtr = ENGINE->renderHandler().loadFont(font);
  232. // calculate which lines should be visible
  233. int totalLines = static_cast<int>(lines.size());
  234. int beginLine = visibleSize.y;
  235. int endLine = getTextLocation().h + visibleSize.y;
  236. if(beginLine < 0)
  237. beginLine = 0;
  238. else
  239. beginLine /= fontPtr->getLineHeight();
  240. if(endLine < 0)
  241. endLine = 0;
  242. else
  243. endLine /= fontPtr->getLineHeight();
  244. endLine++;
  245. // and where they should be displayed
  246. Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * fontPtr->getLineHeight());
  247. Point lineSize = Point(getTextLocation().w, fontPtr->getLineHeight());
  248. CanvasClipRectGuard guard(to, getTextLocation()); // to properly trim text that is too big to fit
  249. for(int i = beginLine; i < std::min(totalLines, endLine); i++)
  250. {
  251. if(!lines[i].empty()) //non-empty line
  252. blitLine(to, Rect(lineStart, lineSize), lines[i]);
  253. lineStart.y += fontPtr->getLineHeight();
  254. }
  255. }
  256. void CMultiLineLabel::splitText(const std::string & Txt, bool redrawAfter)
  257. {
  258. lines.clear();
  259. const auto & fontPtr = ENGINE->renderHandler().loadFont(font);
  260. int lineHeight = fontPtr->getLineHeight();
  261. lines = CMessage::breakText(Txt, pos.w, font);
  262. textSize.y = lineHeight * (int)lines.size();
  263. textSize.x = 0;
  264. for(const std::string & line : lines)
  265. vstd::amax(textSize.x, fontPtr->getStringWidth(line.c_str()));
  266. if(redrawAfter)
  267. redraw();
  268. }
  269. Rect CMultiLineLabel::getTextLocation()
  270. {
  271. // this method is needed for vertical alignment alignment of text
  272. // when height of available text is smaller than height of widget
  273. // in this case - we should add proper offset to display text at required position
  274. if(pos.h <= textSize.y)
  275. return pos;
  276. const auto & fontPtr = ENGINE->renderHandler().loadFont(font);
  277. Point textSizeComputed(pos.w, fontPtr->getLineHeight() * lines.size()); //FIXME: how is this different from textSize member?
  278. Point textOffset(pos.w - textSizeComputed.x, pos.h - textSizeComputed.y);
  279. switch(alignment)
  280. {
  281. case ETextAlignment::TOPLEFT: return Rect(pos.topLeft(), textSizeComputed);
  282. case ETextAlignment::TOPCENTER: return Rect(pos.topLeft(), textSizeComputed);
  283. case ETextAlignment::CENTER: return Rect(pos.topLeft() + textOffset / 2, textSizeComputed);
  284. case ETextAlignment::CENTERRIGHT: return Rect(pos.topLeft() + Point(textOffset.x, textOffset.y / 2), textSizeComputed);
  285. case ETextAlignment::BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSizeComputed);
  286. }
  287. assert(0);
  288. return Rect();
  289. }
  290. CLabelGroup::CLabelGroup(EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  291. : font(Font), align(Align), color(Color)
  292. {
  293. }
  294. void CLabelGroup::add(int x, int y, const std::string & text)
  295. {
  296. OBJECT_CONSTRUCTION;
  297. labels.push_back(std::make_shared<CLabel>(x, y, font, align, color, text));
  298. }
  299. size_t CLabelGroup::currentSize() const
  300. {
  301. return labels.size();
  302. }
  303. CTextBox::CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font, ETextAlignment Align, const ColorRGBA & Color) :
  304. sliderStyle(SliderStyle),
  305. slider(nullptr)
  306. {
  307. OBJECT_CONSTRUCTION;
  308. label = std::make_shared<CMultiLineLabel>(rect, Font, Align, Color);
  309. setRedrawParent(true);
  310. pos.x += rect.x;
  311. pos.y += rect.y;
  312. pos.h = rect.h;
  313. pos.w = rect.w;
  314. assert(pos.w >= 40); //we need some space
  315. setText(Text);
  316. }
  317. void CTextBox::sliderMoved(int to)
  318. {
  319. label->scrollTextTo(to);
  320. }
  321. void CTextBox::trimToFit()
  322. {
  323. if (slider)
  324. return;
  325. pos.w = label->textSize.x;
  326. pos.h = label->textSize.y;
  327. label->pos.w = label->textSize.x;
  328. label->pos.h = label->textSize.y;
  329. }
  330. void CTextBox::resize(Point newSize)
  331. {
  332. pos.w = newSize.x;
  333. pos.h = newSize.y;
  334. label->pos.w = pos.w;
  335. label->pos.h = pos.h;
  336. slider.reset();
  337. setText(label->getText()); // force refresh
  338. }
  339. void CTextBox::setText(const std::string & text)
  340. {
  341. label->pos.w = pos.w; // reset to default before textSize.y check
  342. label->setText(text);
  343. if(label->textSize.y <= label->pos.h && slider)
  344. {
  345. // slider is no longer needed
  346. slider.reset();
  347. label->scrollTextTo(0);
  348. }
  349. else if(slider)
  350. {
  351. // decrease width again if slider still used
  352. label->pos.w = pos.w - 16;
  353. assert(label->pos.w > 0);
  354. label->setText(text);
  355. slider->setAmount(label->textSize.y);
  356. }
  357. else if(label->textSize.y > label->pos.h)
  358. {
  359. // create slider and update widget
  360. label->pos.w = pos.w - 16;
  361. assert(label->pos.w > 0);
  362. label->setText(text);
  363. const auto & fontPtr = ENGINE->renderHandler().loadFont(label->font);
  364. OBJECT_CONSTRUCTION;
  365. slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  366. label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
  367. slider->setScrollStep(fontPtr->getLineHeight());
  368. slider->setPanningStep(1);
  369. slider->setScrollBounds(pos - slider->pos.topLeft());
  370. }
  371. }
  372. void CGStatusBar::setEnteringMode(bool on)
  373. {
  374. consoleText.clear();
  375. if (on)
  376. {
  377. //assert(enteringText == false);
  378. alignment = ETextAlignment::TOPLEFT;
  379. ENGINE->input().startTextInput(pos);
  380. setText(consoleText);
  381. }
  382. else
  383. {
  384. //assert(enteringText == true);
  385. alignment = ETextAlignment::CENTER;
  386. ENGINE->input().stopTextInput();
  387. setText(hoverText);
  388. }
  389. enteringText = on;
  390. }
  391. void CGStatusBar::setEnteredText(const std::string & text)
  392. {
  393. assert(enteringText == true);
  394. consoleText = text;
  395. setText(text);
  396. }
  397. void CGStatusBar::write(const std::string & Text)
  398. {
  399. hoverText = Text;
  400. if (enteringText == false)
  401. setText(hoverText);
  402. }
  403. void CGStatusBar::clearIfMatching(const std::string & Text)
  404. {
  405. if (hoverText == Text)
  406. clear();
  407. }
  408. void CGStatusBar::clear()
  409. {
  410. write({});
  411. }
  412. CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  413. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "", background_->pos.w)
  414. , enteringText(false)
  415. {
  416. addUsedEvents(LCLICK);
  417. background = background_;
  418. addChild(background.get());
  419. pos = background->pos;
  420. getBorderSize();
  421. autoRedraw = false;
  422. }
  423. CGStatusBar::CGStatusBar(int x, int y)
  424. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  425. , enteringText(false)
  426. {
  427. // without background
  428. addUsedEvents(LCLICK);
  429. }
  430. CGStatusBar::CGStatusBar(int x, int y, const ImagePath & name, int maxw)
  431. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  432. , enteringText(false)
  433. {
  434. addUsedEvents(LCLICK);
  435. OBJECT_CONSTRUCTION;
  436. auto backgroundImage = std::make_shared<CPicture>(name);
  437. background = backgroundImage;
  438. pos = background->pos;
  439. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  440. {
  441. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  442. vstd::amin(pos.w, maxw);
  443. backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
  444. }
  445. autoRedraw = false;
  446. }
  447. CGStatusBar::~CGStatusBar()
  448. {
  449. assert(ENGINE->statusbar().get() != this);
  450. }
  451. void CGStatusBar::show(Canvas & to)
  452. {
  453. showAll(to);
  454. }
  455. void CGStatusBar::clickPressed(const Point & cursorPosition)
  456. {
  457. if(GAME->interface() && GAME->interface()->cingconsole->isActive())
  458. GAME->interface()->cingconsole->startEnteringText();
  459. }
  460. void CGStatusBar::activate()
  461. {
  462. ENGINE->setStatusbar(shared_from_this());
  463. CIntObject::activate();
  464. }
  465. void CGStatusBar::deactivate()
  466. {
  467. ENGINE->setStatusbar(nullptr);
  468. if (enteringText)
  469. GAME->interface()->cingconsole->endEnteringText(false);
  470. CIntObject::deactivate();
  471. }
  472. Point CGStatusBar::getBorderSize()
  473. {
  474. //Width of borders where text should not be printed
  475. static const Point borderSize(5, 1);
  476. switch(alignment)
  477. {
  478. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  479. case ETextAlignment::TOPCENTER: return Point(pos.w / 2, borderSize.y);
  480. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  481. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  482. }
  483. assert(0);
  484. return Point();
  485. }