2
0

TextControls.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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->setText(text);
  232. if (label->textSize.y <= label->pos.h && slider)
  233. {
  234. // slider is no longer needed
  235. vstd::clear_pointer(slider);
  236. label->pos.w = pos.w;
  237. label->setText(text);
  238. }
  239. else if (label->textSize.y > label->pos.h && !slider)
  240. {
  241. // create slider and update widget
  242. label->pos.w = pos.w - 32;
  243. label->setText(text);
  244. OBJ_CONSTRUCTION_CAPTURING_ALL;
  245. slider = new CSlider(pos.w - 32, 0, pos.h, boost::bind(&CTextBox::sliderMoved, this, _1),
  246. label->pos.h, label->textSize.y, 0, false, sliderStyle);
  247. slider->scrollStep = graphics->fonts[label->font]->getLineHeight();
  248. }
  249. }
  250. void CGStatusBar::setText(const std::string & Text)
  251. {
  252. if(!textLock)
  253. CLabel::setText(Text);
  254. }
  255. void CGStatusBar::clear()
  256. {
  257. setText("");
  258. }
  259. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= Colors::WHITE*/)
  260. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  261. {
  262. init();
  263. bg = BG;
  264. addChild(bg);
  265. pos = bg->pos;
  266. getBorderSize();
  267. textLock = false;
  268. }
  269. CGStatusBar::CGStatusBar(int x, int y, std::string name/*="ADROLLVR.bmp"*/, int maxw/*=-1*/)
  270. : CLabel(x, y, FONT_SMALL, CENTER)
  271. {
  272. OBJ_CONSTRUCTION_CAPTURING_ALL;
  273. init();
  274. bg = new CPicture(name);
  275. pos = bg->pos;
  276. if((unsigned int)maxw < pos.w)
  277. {
  278. vstd::amin(pos.w, maxw);
  279. bg->srcRect = new Rect(0, 0, maxw, pos.h);
  280. }
  281. textLock = false;
  282. }
  283. CGStatusBar::~CGStatusBar()
  284. {
  285. GH.statusbar = oldStatusBar;
  286. }
  287. void CGStatusBar::show(SDL_Surface * to)
  288. {
  289. showAll(to);
  290. }
  291. void CGStatusBar::init()
  292. {
  293. oldStatusBar = GH.statusbar;
  294. GH.statusbar = this;
  295. }
  296. Point CGStatusBar::getBorderSize()
  297. {
  298. //Width of borders where text should not be printed
  299. static const Point borderSize(5,1);
  300. switch(alignment)
  301. {
  302. case TOPLEFT: return Point(borderSize.x, borderSize.y);
  303. case CENTER: return Point(pos.w/2, pos.h/2);
  304. case BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  305. }
  306. assert(0);
  307. return Point();
  308. }
  309. void CGStatusBar::lock(bool shouldLock)
  310. {
  311. textLock = shouldLock;
  312. }
  313. CTextInput::CTextInput(const Rect &Pos, EFonts font, const CFunctionList<void(const std::string &)> &CB):
  314. CLabel(Pos.x, Pos.y, font, CENTER),
  315. cb(CB)
  316. {
  317. type |= REDRAW_PARENT;
  318. focus = false;
  319. pos.h = Pos.h;
  320. pos.w = Pos.w;
  321. captureAllKeys = true;
  322. bg = nullptr;
  323. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  324. giveFocus();
  325. }
  326. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  327. :cb(CB)
  328. {
  329. focus = false;
  330. pos += Pos;
  331. captureAllKeys = true;
  332. OBJ_CONSTRUCTION;
  333. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  334. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  335. giveFocus();
  336. }
  337. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  338. {
  339. focus = false;
  340. pos += Pos;
  341. captureAllKeys = true;
  342. OBJ_CONSTRUCTION;
  343. bg = new CPicture(Pos, 0, true);
  344. Rect hlp = Pos;
  345. if(srf)
  346. CSDL_Ext::blitSurface(srf, &hlp, *bg, nullptr);
  347. else
  348. SDL_FillRect(*bg, nullptr, 0);
  349. pos.w = bg->pos.w;
  350. pos.h = bg->pos.h;
  351. bg->pos = pos;
  352. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  353. giveFocus();
  354. }
  355. void CTextInput::focusGot()
  356. {
  357. CSDL_Ext::startTextInput(&pos);
  358. }
  359. void CTextInput::focusLost()
  360. {
  361. CSDL_Ext::stopTextInput();
  362. }
  363. std::string CTextInput::visibleText()
  364. {
  365. return focus ? text + newText + "_" : text;
  366. }
  367. void CTextInput::clickLeft( tribool down, bool previousState )
  368. {
  369. if(down && !focus)
  370. giveFocus();
  371. }
  372. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  373. {
  374. if(!focus || key.state != SDL_PRESSED)
  375. return;
  376. if(key.keysym.sym == SDLK_TAB)
  377. {
  378. moveFocus();
  379. GH.breakEventHandling();
  380. return;
  381. }
  382. bool redrawNeeded = false;
  383. #ifdef VCMI_SDL1
  384. std::string oldText = text;
  385. #endif // 0
  386. switch(key.keysym.sym)
  387. {
  388. case SDLK_DELETE: // have index > ' ' so it won't be filtered out by default section
  389. return;
  390. case SDLK_BACKSPACE:
  391. if(!newText.empty())
  392. {
  393. Unicode::trimRight(newText);
  394. redrawNeeded = true;
  395. }
  396. else if(!text.empty())
  397. {
  398. Unicode::trimRight(text);
  399. redrawNeeded = true;
  400. }
  401. break;
  402. default:
  403. #ifdef VCMI_SDL1
  404. if (key.keysym.unicode < ' ')
  405. return;
  406. else
  407. {
  408. text += key.keysym.unicode; //TODO 16-/>8
  409. redrawNeeded = true;
  410. }
  411. #endif // 0
  412. break;
  413. }
  414. #ifdef VCMI_SDL1
  415. filters(text, oldText);
  416. #endif // 0
  417. if (redrawNeeded)
  418. {
  419. redraw();
  420. cb(text);
  421. }
  422. }
  423. void CTextInput::setText( const std::string &nText, bool callCb )
  424. {
  425. CLabel::setText(nText);
  426. if(callCb)
  427. cb(text);
  428. }
  429. bool CTextInput::captureThisEvent(const SDL_KeyboardEvent & key)
  430. {
  431. if(key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_KP_ENTER)
  432. return false;
  433. #ifdef VCMI_SDL1
  434. //this should allow all non-printable keys to go through (for example arrows)
  435. if (key.keysym.unicode < ' ')
  436. return false;
  437. return true;
  438. #else
  439. return false;
  440. #endif
  441. }
  442. #ifndef VCMI_SDL1
  443. void CTextInput::textInputed(const SDL_TextInputEvent & event)
  444. {
  445. if(!focus)
  446. return;
  447. std::string oldText = text;
  448. text += event.text;
  449. filters(text,oldText);
  450. if (text != oldText)
  451. {
  452. redraw();
  453. cb(text);
  454. }
  455. newText = "";
  456. }
  457. void CTextInput::textEdited(const SDL_TextEditingEvent & event)
  458. {
  459. if(!focus)
  460. return;
  461. newText = event.text;
  462. redraw();
  463. cb(text+newText);
  464. }
  465. #endif
  466. void CTextInput::filenameFilter(std::string & text, const std::string &)
  467. {
  468. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  469. size_t pos;
  470. while ((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  471. text.erase(pos, 1);
  472. }
  473. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  474. {
  475. assert(minValue < maxValue);
  476. if (text.empty())
  477. text = "0";
  478. size_t pos = 0;
  479. if (text[0] == '-') //allow '-' sign as first symbol only
  480. pos++;
  481. while (pos < text.size())
  482. {
  483. if (text[pos] < '0' || text[pos] > '9')
  484. {
  485. text = oldText;
  486. return; //new text is not number.
  487. }
  488. pos++;
  489. }
  490. try
  491. {
  492. int value = boost::lexical_cast<int>(text);
  493. if (value < minValue)
  494. text = boost::lexical_cast<std::string>(minValue);
  495. else if (value > maxValue)
  496. text = boost::lexical_cast<std::string>(maxValue);
  497. }
  498. catch(boost::bad_lexical_cast &)
  499. {
  500. //Should never happen. Unless I missed some cases
  501. logGlobal->warnStream() << "Warning: failed to convert "<< text << " to number!";
  502. text = oldText;
  503. }
  504. }
  505. CFocusable::CFocusable()
  506. {
  507. focusables.push_back(this);
  508. }
  509. CFocusable::~CFocusable()
  510. {
  511. if(inputWithFocus == this)
  512. {
  513. focusLost();
  514. inputWithFocus = nullptr;
  515. }
  516. focusables -= this;
  517. }
  518. void CFocusable::giveFocus()
  519. {
  520. if(inputWithFocus)
  521. {
  522. inputWithFocus->focus = false;
  523. inputWithFocus->focusLost();
  524. inputWithFocus->redraw();
  525. }
  526. focus = true;
  527. inputWithFocus = this;
  528. focusGot();
  529. redraw();
  530. }
  531. void CFocusable::moveFocus()
  532. {
  533. auto i = vstd::find(focusables, this),
  534. ourIt = i;
  535. for(i++; i != ourIt; i++)
  536. {
  537. if(i == focusables.end())
  538. i = focusables.begin();
  539. if((*i)->active)
  540. {
  541. (*i)->giveFocus();
  542. break;;
  543. }
  544. }
  545. }