TextControls.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. #include "StdInc.h"
  2. #include "TextControls.h"
  3. #include "Buttons.h"
  4. #include "Images.h"
  5. #include "../CMessage.h"
  6. #include "../gui/CGuiHandler.h"
  7. #include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff
  8. /*
  9. * TextControls.cpp, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. std::string CLabel::visibleText()
  18. {
  19. return text;
  20. }
  21. void CLabel::showAll(SDL_Surface * to)
  22. {
  23. CIntObject::showAll(to);
  24. if(!visibleText().empty())
  25. blitLine(to, pos, visibleText());
  26. }
  27. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::WHITE*/, const std::string &Text /*= ""*/)
  28. :CTextContainer(Align, Font, Color), text(Text)
  29. {
  30. type |= REDRAW_PARENT;
  31. autoRedraw = true;
  32. pos.x += x;
  33. pos.y += y;
  34. pos.w = pos.h = 0;
  35. bg = nullptr;
  36. if (alignment == TOPLEFT) // causes issues for MIDDLE
  37. {
  38. pos.w = graphics->fonts[font]->getStringWidth(visibleText().c_str());
  39. pos.h = graphics->fonts[font]->getLineHeight();
  40. }
  41. }
  42. Point CLabel::getBorderSize()
  43. {
  44. return Point(0, 0);
  45. }
  46. std::string CLabel::getText()
  47. {
  48. return text;
  49. }
  50. void CLabel::setText(const std::string &Txt)
  51. {
  52. text = Txt;
  53. if(autoRedraw)
  54. {
  55. if(bg || !parent)
  56. redraw();
  57. else
  58. parent->redraw();
  59. }
  60. }
  61. CMultiLineLabel::CMultiLineLabel(Rect position, EFonts Font, EAlignment Align, const SDL_Color &Color, const std::string &Text):
  62. CLabel(position.x, position.y, Font, Align, Color, Text),
  63. visibleSize(0, 0, position.w, position.h)
  64. {
  65. pos.w = position.w;
  66. pos.h = position.h;
  67. splitText(Text);
  68. }
  69. void CMultiLineLabel::setVisibleSize(Rect visibleSize)
  70. {
  71. this->visibleSize = visibleSize;
  72. redraw();
  73. }
  74. void CMultiLineLabel::scrollTextBy(int distance)
  75. {
  76. scrollTextTo(visibleSize.y + distance);
  77. }
  78. void CMultiLineLabel::scrollTextTo(int distance)
  79. {
  80. Rect size = visibleSize;
  81. size.y = distance;
  82. setVisibleSize(size);
  83. }
  84. void CMultiLineLabel::setText(const std::string &Txt)
  85. {
  86. splitText(Txt);
  87. CLabel::setText(Txt);
  88. }
  89. void CTextContainer::blitLine(SDL_Surface *to, Rect destRect, std::string what)
  90. {
  91. const IFont * f = graphics->fonts[font];
  92. Point where = destRect.topLeft();
  93. // input is rect in which given text should be placed
  94. // calculate proper position for top-left corner of the text
  95. if (alignment == TOPLEFT)
  96. {
  97. where.x += getBorderSize().x;
  98. where.y += getBorderSize().y;
  99. }
  100. if (alignment == CENTER)
  101. {
  102. where.x += (int(destRect.w) - int(f->getStringWidth(what))) / 2;
  103. where.y += (int(destRect.h) - int(f->getLineHeight())) / 2;
  104. }
  105. if (alignment == BOTTOMRIGHT)
  106. {
  107. where.x += getBorderSize().x + destRect.w - f->getStringWidth(what);
  108. where.y += getBorderSize().y + destRect.h - f->getLineHeight();
  109. }
  110. size_t begin = 0;
  111. std::string delimeters = "{}";
  112. size_t currDelimeter = 0;
  113. do
  114. {
  115. size_t end = what.find_first_of(delimeters[currDelimeter % 2], begin);
  116. if (begin != end)
  117. {
  118. std::string toPrint = what.substr(begin, end - begin);
  119. if (currDelimeter % 2) // Enclosed in {} text - set to yellow
  120. f->renderTextLeft(to, toPrint, Colors::YELLOW, where);
  121. else // Non-enclosed text, use default color
  122. f->renderTextLeft(to, toPrint, color, where);
  123. begin = end;
  124. where.x += f->getStringWidth(toPrint);
  125. }
  126. currDelimeter++;
  127. }
  128. while (begin++ != std::string::npos);
  129. }
  130. CTextContainer::CTextContainer(EAlignment alignment, EFonts font, SDL_Color color):
  131. alignment(alignment),
  132. font(font),
  133. color(color)
  134. {}
  135. void CMultiLineLabel::showAll(SDL_Surface * to)
  136. {
  137. CIntObject::showAll(to);
  138. const IFont * f = graphics->fonts[font];
  139. // calculate which lines should be visible
  140. int totalLines = lines.size();
  141. int beginLine = visibleSize.y;
  142. int endLine = getTextLocation().h + visibleSize.y;
  143. if (beginLine < 0)
  144. beginLine = 0;
  145. else
  146. beginLine /= f->getLineHeight();
  147. if (endLine < 0)
  148. endLine = 0;
  149. else
  150. endLine /= f->getLineHeight();
  151. endLine++;
  152. // and where they should be displayed
  153. Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * f->getLineHeight());
  154. Point lineSize = Point(getTextLocation().w, f->getLineHeight());
  155. CSDL_Ext::CClipRectGuard guard(to, getTextLocation()); // to properly trim text that is too big to fit
  156. for (int i = beginLine; i < std::min(totalLines, endLine); i++)
  157. {
  158. if (!lines[i].empty()) //non-empty line
  159. blitLine(to, Rect(lineStart, lineSize), lines[i]);
  160. lineStart.y += f->getLineHeight();
  161. }
  162. }
  163. void CMultiLineLabel::splitText(const std::string &Txt)
  164. {
  165. lines.clear();
  166. const IFont * f = graphics->fonts[font];
  167. int lineHeight = f->getLineHeight();
  168. lines = CMessage::breakText(Txt, pos.w, font);
  169. textSize.y = lineHeight * lines.size();
  170. textSize.x = 0;
  171. for(const std::string &line : lines)
  172. vstd::amax( textSize.x, f->getStringWidth(line.c_str()));
  173. redraw();
  174. }
  175. Rect CMultiLineLabel::getTextLocation()
  176. {
  177. // this method is needed for vertical alignment alignment of text
  178. // when height of available text is smaller than height of widget
  179. // in this case - we should add proper offset to display text at required position
  180. if (pos.h <= textSize.y)
  181. return pos;
  182. Point textSize(pos.w, graphics->fonts[font]->getLineHeight() * lines.size());
  183. Point textOffset(pos.w - textSize.x, pos.h - textSize.y);
  184. switch(alignment)
  185. {
  186. case TOPLEFT: return Rect(pos.topLeft(), textSize);
  187. case CENTER: return Rect(pos.topLeft() + textOffset / 2, textSize);
  188. case BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSize);
  189. }
  190. assert(0);
  191. return Rect();
  192. }
  193. CLabelGroup::CLabelGroup(EFonts Font, EAlignment Align, const SDL_Color &Color):
  194. font(Font), align(Align), color(Color)
  195. {}
  196. void CLabelGroup::add(int x, int y, const std::string &text)
  197. {
  198. OBJ_CONSTRUCTION_CAPTURING_ALL;
  199. new CLabel(x, y, font, align, color, text);
  200. }
  201. CTextBox::CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= TOPLEFT*/, const SDL_Color &Color /*= Colors::WHITE*/):
  202. sliderStyle(SliderStyle),
  203. slider(nullptr)
  204. {
  205. OBJ_CONSTRUCTION_CAPTURING_ALL;
  206. label = new CMultiLineLabel(rect, Font, Align, Color);
  207. type |= REDRAW_PARENT;
  208. pos.x += rect.x;
  209. pos.y += rect.y;
  210. pos.h = rect.h;
  211. pos.w = rect.w;
  212. assert(pos.w >= 40); //we need some space
  213. setText(Text);
  214. }
  215. void CTextBox::sliderMoved(int to)
  216. {
  217. label->scrollTextTo(to);
  218. }
  219. void CTextBox::resize(Point newSize)
  220. {
  221. pos.w = newSize.x;
  222. pos.h = newSize.y;
  223. label->pos.w = pos.w;
  224. label->pos.h = pos.h;
  225. if (slider)
  226. vstd::clear_pointer(slider); // will be recreated if needed later
  227. setText(label->getText()); // force refresh
  228. }
  229. void CTextBox::setText(const std::string &text)
  230. {
  231. label->pos.w = pos.w; // reset to default before textSize.y check
  232. label->setText(text);
  233. if(label->textSize.y <= label->pos.h && slider)
  234. {
  235. // slider is no longer needed
  236. vstd::clear_pointer(slider);
  237. }
  238. else if(slider)
  239. {
  240. // decrease width again if slider still used
  241. label->pos.w = pos.w - 32;
  242. label->setText(text);
  243. }
  244. else if(label->textSize.y > label->pos.h)
  245. {
  246. // create slider and update widget
  247. label->pos.w = pos.w - 32;
  248. label->setText(text);
  249. OBJ_CONSTRUCTION_CAPTURING_ALL;
  250. slider = new CSlider(Point(pos.w - 32, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  251. label->pos.h, label->textSize.y, 0, false, CSlider::EStyle(sliderStyle));
  252. slider->setScrollStep(graphics->fonts[label->font]->getLineHeight());
  253. }
  254. }
  255. void CGStatusBar::setText(const std::string & Text)
  256. {
  257. if(!textLock)
  258. CLabel::setText(Text);
  259. }
  260. void CGStatusBar::clear()
  261. {
  262. setText("");
  263. }
  264. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= Colors::WHITE*/)
  265. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  266. {
  267. init();
  268. bg = BG;
  269. addChild(bg);
  270. pos = bg->pos;
  271. getBorderSize();
  272. textLock = false;
  273. }
  274. CGStatusBar::CGStatusBar(int x, int y, std::string name/*="ADROLLVR.bmp"*/, int maxw/*=-1*/)
  275. : CLabel(x, y, FONT_SMALL, CENTER)
  276. {
  277. OBJ_CONSTRUCTION_CAPTURING_ALL;
  278. init();
  279. bg = new CPicture(name);
  280. pos = bg->pos;
  281. if((unsigned int)maxw < pos.w)
  282. {
  283. vstd::amin(pos.w, maxw);
  284. bg->srcRect = new Rect(0, 0, maxw, pos.h);
  285. }
  286. textLock = false;
  287. }
  288. CGStatusBar::~CGStatusBar()
  289. {
  290. GH.statusbar = oldStatusBar;
  291. }
  292. void CGStatusBar::show(SDL_Surface * to)
  293. {
  294. showAll(to);
  295. }
  296. void CGStatusBar::init()
  297. {
  298. oldStatusBar = GH.statusbar;
  299. GH.statusbar = this;
  300. }
  301. Point CGStatusBar::getBorderSize()
  302. {
  303. //Width of borders where text should not be printed
  304. static const Point borderSize(5,1);
  305. switch(alignment)
  306. {
  307. case TOPLEFT: return Point(borderSize.x, borderSize.y);
  308. case CENTER: return Point(pos.w/2, pos.h/2);
  309. case BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  310. }
  311. assert(0);
  312. return Point();
  313. }
  314. void CGStatusBar::lock(bool shouldLock)
  315. {
  316. textLock = shouldLock;
  317. }
  318. CTextInput::CTextInput(const Rect &Pos, EFonts font, const CFunctionList<void(const std::string &)> &CB):
  319. CLabel(Pos.x, Pos.y, font, CENTER),
  320. cb(CB)
  321. {
  322. type |= REDRAW_PARENT;
  323. focus = false;
  324. pos.h = Pos.h;
  325. pos.w = Pos.w;
  326. captureAllKeys = true;
  327. bg = nullptr;
  328. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  329. giveFocus();
  330. }
  331. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  332. :cb(CB)
  333. {
  334. focus = false;
  335. pos += Pos;
  336. captureAllKeys = true;
  337. OBJ_CONSTRUCTION;
  338. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  339. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  340. giveFocus();
  341. }
  342. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  343. {
  344. focus = false;
  345. pos += Pos;
  346. captureAllKeys = true;
  347. OBJ_CONSTRUCTION;
  348. bg = new CPicture(Pos, 0, true);
  349. Rect hlp = Pos;
  350. if(srf)
  351. CSDL_Ext::blitSurface(srf, &hlp, *bg, nullptr);
  352. else
  353. SDL_FillRect(*bg, nullptr, 0);
  354. pos.w = bg->pos.w;
  355. pos.h = bg->pos.h;
  356. bg->pos = pos;
  357. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  358. giveFocus();
  359. }
  360. void CTextInput::focusGot()
  361. {
  362. CSDL_Ext::startTextInput(&pos);
  363. }
  364. void CTextInput::focusLost()
  365. {
  366. CSDL_Ext::stopTextInput();
  367. }
  368. std::string CTextInput::visibleText()
  369. {
  370. return focus ? text + newText + "_" : text;
  371. }
  372. void CTextInput::clickLeft( tribool down, bool previousState )
  373. {
  374. if(down && !focus)
  375. giveFocus();
  376. }
  377. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  378. {
  379. if(!focus || key.state != SDL_PRESSED)
  380. return;
  381. if(key.keysym.sym == SDLK_TAB)
  382. {
  383. moveFocus();
  384. GH.breakEventHandling();
  385. return;
  386. }
  387. bool redrawNeeded = false;
  388. #ifdef VCMI_SDL1
  389. std::string oldText = text;
  390. #endif // 0
  391. switch(key.keysym.sym)
  392. {
  393. case SDLK_DELETE: // have index > ' ' so it won't be filtered out by default section
  394. return;
  395. case SDLK_BACKSPACE:
  396. if(!newText.empty())
  397. {
  398. Unicode::trimRight(newText);
  399. redrawNeeded = true;
  400. }
  401. else if(!text.empty())
  402. {
  403. Unicode::trimRight(text);
  404. redrawNeeded = true;
  405. }
  406. break;
  407. default:
  408. #ifdef VCMI_SDL1
  409. if (key.keysym.unicode < ' ')
  410. return;
  411. else
  412. {
  413. text += key.keysym.unicode; //TODO 16-/>8
  414. redrawNeeded = true;
  415. }
  416. #endif // 0
  417. break;
  418. }
  419. #ifdef VCMI_SDL1
  420. filters(text, oldText);
  421. #endif // 0
  422. if (redrawNeeded)
  423. {
  424. redraw();
  425. cb(text);
  426. }
  427. }
  428. void CTextInput::setText( const std::string &nText, bool callCb )
  429. {
  430. CLabel::setText(nText);
  431. if(callCb)
  432. cb(text);
  433. }
  434. bool CTextInput::captureThisEvent(const SDL_KeyboardEvent & key)
  435. {
  436. if(key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_KP_ENTER)
  437. return false;
  438. #ifdef VCMI_SDL1
  439. //this should allow all non-printable keys to go through (for example arrows)
  440. if (key.keysym.unicode < ' ')
  441. return false;
  442. return true;
  443. #else
  444. return false;
  445. #endif
  446. }
  447. #ifndef VCMI_SDL1
  448. void CTextInput::textInputed(const SDL_TextInputEvent & event)
  449. {
  450. if(!focus)
  451. return;
  452. std::string oldText = text;
  453. text += event.text;
  454. filters(text,oldText);
  455. if (text != oldText)
  456. {
  457. redraw();
  458. cb(text);
  459. }
  460. newText = "";
  461. }
  462. void CTextInput::textEdited(const SDL_TextEditingEvent & event)
  463. {
  464. if(!focus)
  465. return;
  466. newText = event.text;
  467. redraw();
  468. cb(text+newText);
  469. }
  470. #endif
  471. void CTextInput::filenameFilter(std::string & text, const std::string &)
  472. {
  473. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  474. size_t pos;
  475. while ((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  476. text.erase(pos, 1);
  477. }
  478. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  479. {
  480. assert(minValue < maxValue);
  481. if (text.empty())
  482. text = "0";
  483. size_t pos = 0;
  484. if (text[0] == '-') //allow '-' sign as first symbol only
  485. pos++;
  486. while (pos < text.size())
  487. {
  488. if (text[pos] < '0' || text[pos] > '9')
  489. {
  490. text = oldText;
  491. return; //new text is not number.
  492. }
  493. pos++;
  494. }
  495. try
  496. {
  497. int value = boost::lexical_cast<int>(text);
  498. if (value < minValue)
  499. text = boost::lexical_cast<std::string>(minValue);
  500. else if (value > maxValue)
  501. text = boost::lexical_cast<std::string>(maxValue);
  502. }
  503. catch(boost::bad_lexical_cast &)
  504. {
  505. //Should never happen. Unless I missed some cases
  506. logGlobal->warnStream() << "Warning: failed to convert "<< text << " to number!";
  507. text = oldText;
  508. }
  509. }
  510. CFocusable::CFocusable()
  511. {
  512. focusables.push_back(this);
  513. }
  514. CFocusable::~CFocusable()
  515. {
  516. if(inputWithFocus == this)
  517. {
  518. focusLost();
  519. inputWithFocus = nullptr;
  520. }
  521. focusables -= this;
  522. }
  523. void CFocusable::giveFocus()
  524. {
  525. if(inputWithFocus)
  526. {
  527. inputWithFocus->focus = false;
  528. inputWithFocus->focusLost();
  529. inputWithFocus->redraw();
  530. }
  531. focus = true;
  532. inputWithFocus = this;
  533. focusGot();
  534. redraw();
  535. }
  536. void CFocusable::moveFocus()
  537. {
  538. auto i = vstd::find(focusables, this),
  539. ourIt = i;
  540. for(i++; i != ourIt; i++)
  541. {
  542. if(i == focusables.end())
  543. i = focusables.begin();
  544. if((*i)->active)
  545. {
  546. (*i)->giveFocus();
  547. break;;
  548. }
  549. }
  550. }