2
0

TextControls.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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::CENTERLEFT: return Rect(pos.topLeft() + Point(0, textOffset.y / 2), textSizeComputed);
  285. case ETextAlignment::CENTERRIGHT: return Rect(pos.topLeft() + Point(textOffset.x, textOffset.y / 2), textSizeComputed);
  286. case ETextAlignment::BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSizeComputed);
  287. }
  288. assert(0);
  289. return Rect();
  290. }
  291. CLabelGroup::CLabelGroup(EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  292. : font(Font), align(Align), color(Color)
  293. {
  294. }
  295. void CLabelGroup::add(int x, int y, const std::string & text)
  296. {
  297. OBJECT_CONSTRUCTION;
  298. labels.push_back(std::make_shared<CLabel>(x, y, font, align, color, text));
  299. }
  300. size_t CLabelGroup::currentSize() const
  301. {
  302. return labels.size();
  303. }
  304. CTextBox::CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font, ETextAlignment Align, const ColorRGBA & Color) :
  305. sliderStyle(SliderStyle),
  306. slider(nullptr)
  307. {
  308. OBJECT_CONSTRUCTION;
  309. label = std::make_shared<CMultiLineLabel>(rect, Font, Align, Color);
  310. setRedrawParent(true);
  311. pos.x += rect.x;
  312. pos.y += rect.y;
  313. pos.h = rect.h;
  314. pos.w = rect.w;
  315. assert(pos.w >= 40); //we need some space
  316. setText(Text);
  317. }
  318. void CTextBox::sliderMoved(int to)
  319. {
  320. label->scrollTextTo(to);
  321. }
  322. void CTextBox::trimToFit()
  323. {
  324. if (slider)
  325. return;
  326. pos.w = label->textSize.x;
  327. pos.h = label->textSize.y;
  328. label->pos.w = label->textSize.x;
  329. label->pos.h = label->textSize.y;
  330. }
  331. void CTextBox::resize(Point newSize)
  332. {
  333. pos.w = newSize.x;
  334. pos.h = newSize.y;
  335. label->pos.w = pos.w;
  336. label->pos.h = pos.h;
  337. slider.reset();
  338. setText(label->getText()); // force refresh
  339. }
  340. void CTextBox::setText(const std::string & text)
  341. {
  342. label->pos.w = pos.w; // reset to default before textSize.y check
  343. label->setText(text);
  344. if(label->textSize.y <= label->pos.h && slider)
  345. {
  346. // slider is no longer needed
  347. slider.reset();
  348. label->scrollTextTo(0);
  349. }
  350. else if(slider)
  351. {
  352. // decrease width again if slider still used
  353. label->pos.w = pos.w - 16;
  354. assert(label->pos.w > 0);
  355. label->setText(text);
  356. slider->setAmount(label->textSize.y);
  357. }
  358. else if(label->textSize.y > label->pos.h)
  359. {
  360. // create slider and update widget
  361. label->pos.w = pos.w - 16;
  362. assert(label->pos.w > 0);
  363. label->setText(text);
  364. const auto & fontPtr = ENGINE->renderHandler().loadFont(label->font);
  365. OBJECT_CONSTRUCTION;
  366. slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  367. label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
  368. slider->setScrollStep(fontPtr->getLineHeight());
  369. slider->setPanningStep(1);
  370. slider->setScrollBounds(pos - slider->pos.topLeft());
  371. }
  372. }
  373. void CGStatusBar::setEnteringMode(bool on)
  374. {
  375. consoleText.clear();
  376. if (on)
  377. {
  378. //assert(enteringText == false);
  379. alignment = ETextAlignment::TOPLEFT;
  380. ENGINE->input().startTextInput(pos);
  381. setText(consoleText);
  382. }
  383. else
  384. {
  385. //assert(enteringText == true);
  386. alignment = ETextAlignment::CENTER;
  387. ENGINE->input().stopTextInput();
  388. setText(hoverText);
  389. }
  390. enteringText = on;
  391. }
  392. void CGStatusBar::setEnteredText(const std::string & text)
  393. {
  394. assert(enteringText == true);
  395. consoleText = text;
  396. setText(text);
  397. }
  398. void CGStatusBar::write(const std::string & Text)
  399. {
  400. hoverText = Text;
  401. if (enteringText == false)
  402. setText(hoverText);
  403. }
  404. void CGStatusBar::clearIfMatching(const std::string & Text)
  405. {
  406. if (hoverText == Text)
  407. clear();
  408. }
  409. void CGStatusBar::clear()
  410. {
  411. write({});
  412. }
  413. CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  414. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "", background_->pos.w)
  415. , enteringText(false)
  416. {
  417. addUsedEvents(LCLICK);
  418. background = background_;
  419. addChild(background.get());
  420. pos = background->pos;
  421. getBorderSize();
  422. autoRedraw = false;
  423. }
  424. CGStatusBar::CGStatusBar(int x, int y)
  425. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  426. , enteringText(false)
  427. {
  428. // without background
  429. addUsedEvents(LCLICK);
  430. }
  431. CGStatusBar::CGStatusBar(int x, int y, const ImagePath & name, int maxw)
  432. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  433. , enteringText(false)
  434. {
  435. addUsedEvents(LCLICK);
  436. OBJECT_CONSTRUCTION;
  437. auto backgroundImage = std::make_shared<CPicture>(name);
  438. background = backgroundImage;
  439. pos = background->pos;
  440. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  441. {
  442. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  443. vstd::amin(pos.w, maxw);
  444. backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
  445. }
  446. autoRedraw = false;
  447. }
  448. CGStatusBar::~CGStatusBar()
  449. {
  450. assert(ENGINE->statusbar().get() != this);
  451. }
  452. void CGStatusBar::show(Canvas & to)
  453. {
  454. showAll(to);
  455. }
  456. void CGStatusBar::clickPressed(const Point & cursorPosition)
  457. {
  458. if(GAME->interface() && GAME->interface()->cingconsole->isActive())
  459. GAME->interface()->cingconsole->startEnteringText();
  460. }
  461. void CGStatusBar::activate()
  462. {
  463. ENGINE->setStatusbar(shared_from_this());
  464. CIntObject::activate();
  465. }
  466. void CGStatusBar::deactivate()
  467. {
  468. ENGINE->setStatusbar(nullptr);
  469. if (enteringText)
  470. GAME->interface()->cingconsole->endEnteringText(false);
  471. CIntObject::deactivate();
  472. }
  473. Point CGStatusBar::getBorderSize()
  474. {
  475. //Width of borders where text should not be printed
  476. static const Point borderSize(5, 1);
  477. switch(alignment)
  478. {
  479. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  480. case ETextAlignment::TOPCENTER: return Point(pos.w / 2, borderSize.y);
  481. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  482. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  483. }
  484. assert(0);
  485. return Point();
  486. }