TextControls.cpp 14 KB

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