TextControls.cpp 14 KB

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