TextControls.cpp 14 KB

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