TextControls.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. const std::string delimiters = "{}";
  154. int CTextContainer::getDelimitersWidth(EFonts font, std::string text)
  155. {
  156. const auto f = ENGINE->renderHandler().loadFont(font);
  157. auto delimitersWidth = std::count_if(text.cbegin(), text.cend(), [](char c)
  158. {
  159. return delimiters.find(c) != std::string::npos;
  160. });
  161. //We should count delimiters length from string to correct centering later.
  162. delimitersWidth *= f->getStringWidth(delimiters)/2;
  163. std::smatch match;
  164. std::regex expr("\\{(.*?)\\|");
  165. std::string::const_iterator searchStart( text.cbegin() );
  166. while(std::regex_search(searchStart, text.cend(), match, expr))
  167. {
  168. std::string colorText = match[1].str();
  169. if(auto c = Colors::parseColor(colorText))
  170. delimitersWidth += f->getStringWidth(colorText + "|");
  171. searchStart = match.suffix().first;
  172. }
  173. return delimitersWidth;
  174. }
  175. void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what)
  176. {
  177. const auto f = ENGINE->renderHandler().loadFont(font);
  178. Point where = destRect.topLeft();
  179. int delimitersWidth = getDelimitersWidth(font, what);
  180. // input is rect in which given text should be placed
  181. // calculate proper position for top-left corner of the text
  182. if(alignment == ETextAlignment::TOPLEFT || alignment == ETextAlignment::CENTERLEFT || alignment == ETextAlignment::BOTTOMLEFT)
  183. where.x += getBorderSize().x;
  184. if(alignment == ETextAlignment::CENTER || alignment == ETextAlignment::TOPCENTER || alignment == ETextAlignment::BOTTOMCENTER)
  185. where.x += (destRect.w - (static_cast<int>(f->getStringWidth(what)) - delimitersWidth)) / 2;
  186. if(alignment == ETextAlignment::TOPRIGHT || alignment == ETextAlignment::BOTTOMRIGHT || alignment == ETextAlignment::CENTERRIGHT)
  187. where.x += getBorderSize().x + destRect.w - (static_cast<int>(f->getStringWidth(what)) - delimitersWidth);
  188. if(alignment == ETextAlignment::TOPLEFT || alignment == ETextAlignment::TOPCENTER || alignment == ETextAlignment::TOPRIGHT)
  189. where.y += getBorderSize().y;
  190. if(alignment == ETextAlignment::CENTERLEFT || alignment == ETextAlignment::CENTER || alignment == ETextAlignment::CENTERRIGHT)
  191. where.y += (destRect.h - static_cast<int>(f->getLineHeight())) / 2;
  192. if(alignment == ETextAlignment::BOTTOMLEFT || alignment == ETextAlignment::BOTTOMCENTER || alignment == ETextAlignment::BOTTOMRIGHT)
  193. where.y += getBorderSize().y + destRect.h - static_cast<int>(f->getLineHeight());
  194. size_t begin = 0;
  195. size_t currDelimiter = 0;
  196. do
  197. {
  198. size_t end = what.find_first_of(delimiters[currDelimiter % 2], begin);
  199. if(begin != end)
  200. {
  201. std::string toPrint = what.substr(begin, end - begin);
  202. if(currDelimiter % 2) // Enclosed in {} text - set to yellow or defined color
  203. {
  204. std::smatch match;
  205. std::regex expr("^(.*?)\\|");
  206. if(std::regex_search(toPrint, match, expr))
  207. {
  208. std::string colorText = match[1].str();
  209. if(auto color = Colors::parseColor(colorText))
  210. {
  211. toPrint = toPrint.substr(colorText.length() + 1, toPrint.length() - colorText.length());
  212. to.drawText(where, font, *color, ETextAlignment::TOPLEFT, toPrint);
  213. }
  214. else
  215. to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
  216. }
  217. else
  218. to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
  219. }
  220. else // Non-enclosed text, use default color
  221. to.drawText(where, font, color, ETextAlignment::TOPLEFT, toPrint);
  222. begin = end;
  223. where.x += (int)f->getStringWidth(toPrint);
  224. }
  225. currDelimiter++;
  226. } while(begin++ != std::string::npos);
  227. }
  228. CTextContainer::CTextContainer(ETextAlignment alignment, EFonts font, ColorRGBA color) :
  229. alignment(alignment),
  230. font(font),
  231. color(color)
  232. {
  233. }
  234. void CMultiLineLabel::showAll(Canvas & to)
  235. {
  236. CIntObject::showAll(to);
  237. const auto & fontPtr = ENGINE->renderHandler().loadFont(font);
  238. // calculate which lines should be visible
  239. int totalLines = static_cast<int>(lines.size());
  240. int beginLine = visibleSize.y;
  241. int endLine = getTextLocation().h + visibleSize.y;
  242. if(beginLine < 0)
  243. beginLine = 0;
  244. else
  245. beginLine /= fontPtr->getLineHeight();
  246. if(endLine < 0)
  247. endLine = 0;
  248. else
  249. endLine /= fontPtr->getLineHeight();
  250. endLine++;
  251. // and where they should be displayed
  252. Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * fontPtr->getLineHeight());
  253. Point lineSize = Point(getTextLocation().w, fontPtr->getLineHeight());
  254. CanvasClipRectGuard guard(to, getTextLocation()); // to properly trim text that is too big to fit
  255. for(int i = beginLine; i < std::min(totalLines, endLine); i++)
  256. {
  257. if(!lines[i].empty()) //non-empty line
  258. blitLine(to, Rect(lineStart, lineSize), lines[i]);
  259. lineStart.y += fontPtr->getLineHeight();
  260. }
  261. }
  262. void CMultiLineLabel::splitText(const std::string & Txt, bool redrawAfter)
  263. {
  264. lines.clear();
  265. const auto & fontPtr = ENGINE->renderHandler().loadFont(font);
  266. int lineHeight = fontPtr->getLineHeight();
  267. lines = CMessage::breakText(Txt, pos.w, font);
  268. textSize.y = lineHeight * (int)lines.size();
  269. textSize.x = 0;
  270. for(const std::string & line : lines)
  271. vstd::amax(textSize.x, fontPtr->getStringWidth(line.c_str()));
  272. if(redrawAfter)
  273. redraw();
  274. }
  275. Rect CMultiLineLabel::getTextLocation()
  276. {
  277. // this method is needed for vertical alignment alignment of text
  278. // when height of available text is smaller than height of widget
  279. // in this case - we should add proper offset to display text at required position
  280. if(pos.h <= textSize.y)
  281. return pos;
  282. const auto & fontPtr = ENGINE->renderHandler().loadFont(font);
  283. Point textSizeComputed(pos.w, fontPtr->getLineHeight() * lines.size()); //FIXME: how is this different from textSize member?
  284. Point textOffset(pos.w - textSizeComputed.x, pos.h - textSizeComputed.y);
  285. switch(alignment)
  286. {
  287. case ETextAlignment::TOPLEFT: return Rect(pos.topLeft(), textSizeComputed);
  288. case ETextAlignment::TOPCENTER: return Rect(pos.topLeft(), textSizeComputed);
  289. case ETextAlignment::CENTER: return Rect(pos.topLeft() + textOffset / 2, textSizeComputed);
  290. case ETextAlignment::CENTERLEFT: return Rect(pos.topLeft() + Point(0, textOffset.y / 2), textSizeComputed);
  291. case ETextAlignment::CENTERRIGHT: return Rect(pos.topLeft() + Point(textOffset.x, textOffset.y / 2), textSizeComputed);
  292. case ETextAlignment::BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSizeComputed);
  293. }
  294. assert(0);
  295. return Rect();
  296. }
  297. CLabelGroup::CLabelGroup(EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  298. : font(Font), align(Align), color(Color)
  299. {
  300. }
  301. void CLabelGroup::add(int x, int y, const std::string & text)
  302. {
  303. OBJECT_CONSTRUCTION;
  304. labels.push_back(std::make_shared<CLabel>(x, y, font, align, color, text));
  305. }
  306. size_t CLabelGroup::currentSize() const
  307. {
  308. return labels.size();
  309. }
  310. CTextBox::CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font, ETextAlignment Align, const ColorRGBA & Color) :
  311. sliderStyle(SliderStyle),
  312. slider(nullptr)
  313. {
  314. OBJECT_CONSTRUCTION;
  315. label = std::make_shared<CMultiLineLabel>(rect, Font, Align, Color);
  316. setRedrawParent(true);
  317. pos.x += rect.x;
  318. pos.y += rect.y;
  319. pos.h = rect.h;
  320. pos.w = rect.w;
  321. assert(pos.w >= 40); //we need some space
  322. setText(Text);
  323. }
  324. void CTextBox::sliderMoved(int to)
  325. {
  326. label->scrollTextTo(to);
  327. }
  328. void CTextBox::trimToFit()
  329. {
  330. if (slider)
  331. return;
  332. pos.w = label->textSize.x;
  333. pos.h = label->textSize.y;
  334. label->pos.w = label->textSize.x;
  335. label->pos.h = label->textSize.y;
  336. }
  337. void CTextBox::resize(Point newSize)
  338. {
  339. pos.w = newSize.x;
  340. pos.h = newSize.y;
  341. label->pos.w = pos.w;
  342. label->pos.h = pos.h;
  343. slider.reset();
  344. setText(label->getText()); // force refresh
  345. }
  346. void CTextBox::setText(const std::string & text)
  347. {
  348. label->pos.w = pos.w; // reset to default before textSize.y check
  349. label->setText(text);
  350. if(label->textSize.y <= label->pos.h && slider)
  351. {
  352. // slider is no longer needed
  353. slider.reset();
  354. label->scrollTextTo(0);
  355. }
  356. else if(slider)
  357. {
  358. // decrease width again if slider still used
  359. label->pos.w = pos.w - 16;
  360. assert(label->pos.w > 0);
  361. label->setText(text);
  362. slider->setAmount(label->textSize.y);
  363. }
  364. else if(label->textSize.y > label->pos.h)
  365. {
  366. // create slider and update widget
  367. label->pos.w = pos.w - 16;
  368. assert(label->pos.w > 0);
  369. label->setText(text);
  370. const auto & fontPtr = ENGINE->renderHandler().loadFont(label->font);
  371. OBJECT_CONSTRUCTION;
  372. slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  373. label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
  374. slider->setScrollStep(fontPtr->getLineHeight());
  375. slider->setPanningStep(1);
  376. slider->setScrollBounds(pos - slider->pos.topLeft());
  377. }
  378. }
  379. void CGStatusBar::setEnteringMode(bool on)
  380. {
  381. consoleText.clear();
  382. if (on)
  383. {
  384. //assert(enteringText == false);
  385. alignment = ETextAlignment::TOPLEFT;
  386. ENGINE->input().startTextInput(pos);
  387. setText(consoleText);
  388. }
  389. else
  390. {
  391. //assert(enteringText == true);
  392. alignment = ETextAlignment::CENTER;
  393. ENGINE->input().stopTextInput();
  394. setText(hoverText);
  395. }
  396. enteringText = on;
  397. }
  398. void CGStatusBar::setEnteredText(const std::string & text)
  399. {
  400. assert(enteringText == true);
  401. consoleText = text;
  402. setText(text);
  403. }
  404. void CGStatusBar::write(const std::string & Text)
  405. {
  406. hoverText = Text;
  407. if (enteringText == false)
  408. setText(hoverText);
  409. }
  410. void CGStatusBar::clearIfMatching(const std::string & Text)
  411. {
  412. if (hoverText == Text)
  413. clear();
  414. }
  415. void CGStatusBar::clear()
  416. {
  417. write({});
  418. }
  419. CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  420. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "", background_->pos.w)
  421. , enteringText(false)
  422. {
  423. addUsedEvents(LCLICK);
  424. background = background_;
  425. addChild(background.get());
  426. pos = background->pos;
  427. getBorderSize();
  428. autoRedraw = false;
  429. }
  430. CGStatusBar::CGStatusBar(int x, int y)
  431. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  432. , enteringText(false)
  433. {
  434. // without background
  435. addUsedEvents(LCLICK);
  436. }
  437. CGStatusBar::CGStatusBar(int x, int y, const ImagePath & name, int maxw)
  438. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  439. , enteringText(false)
  440. {
  441. addUsedEvents(LCLICK);
  442. OBJECT_CONSTRUCTION;
  443. auto backgroundImage = std::make_shared<CPicture>(name);
  444. background = backgroundImage;
  445. pos = background->pos;
  446. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  447. {
  448. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  449. vstd::amin(pos.w, maxw);
  450. backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
  451. }
  452. autoRedraw = false;
  453. }
  454. CGStatusBar::~CGStatusBar()
  455. {
  456. assert(ENGINE->statusbar().get() != this);
  457. }
  458. void CGStatusBar::show(Canvas & to)
  459. {
  460. showAll(to);
  461. }
  462. void CGStatusBar::clickPressed(const Point & cursorPosition)
  463. {
  464. if(GAME->interface() && GAME->interface()->cingconsole->isActive())
  465. GAME->interface()->cingconsole->startEnteringText();
  466. }
  467. void CGStatusBar::activate()
  468. {
  469. ENGINE->setStatusbar(shared_from_this());
  470. CIntObject::activate();
  471. }
  472. void CGStatusBar::deactivate()
  473. {
  474. ENGINE->setStatusbar(nullptr);
  475. if (enteringText)
  476. GAME->interface()->cingconsole->endEnteringText(false);
  477. CIntObject::deactivate();
  478. }
  479. Point CGStatusBar::getBorderSize()
  480. {
  481. //Width of borders where text should not be printed
  482. static const Point borderSize(5, 1);
  483. switch(alignment)
  484. {
  485. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  486. case ETextAlignment::TOPCENTER: return Point(pos.w / 2, borderSize.y);
  487. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  488. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  489. }
  490. assert(0);
  491. return Point();
  492. }