TextControls.cpp 14 KB

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