TextControls.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 "../gui/Shortcut.h"
  17. #include "../windows/CMessage.h"
  18. #include "../adventureMap/CInGameConsole.h"
  19. #include "../renderSDL/SDL_Extensions.h"
  20. #include "../render/Canvas.h"
  21. #include "../../lib/TextOperations.h"
  22. #ifdef VCMI_ANDROID
  23. #include "lib/CAndroidVMHelper.h"
  24. #endif
  25. std::list<CFocusable*> CFocusable::focusables;
  26. CFocusable * CFocusable::inputWithFocus;
  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 SDL_Color & Color, const std::string & Text)
  38. : CTextContainer(Align, Font, Color), text(Text)
  39. {
  40. setRedrawParent(true);
  41. autoRedraw = true;
  42. pos.x += x;
  43. pos.y += y;
  44. pos.w = pos.h = 0;
  45. if(alignment == ETextAlignment::TOPLEFT) // causes issues for MIDDLE
  46. {
  47. pos.w = (int)graphics->fonts[font]->getStringWidth(visibleText().c_str());
  48. pos.h = (int)graphics->fonts[font]->getLineHeight();
  49. }
  50. }
  51. Point CLabel::getBorderSize()
  52. {
  53. return Point(0, 0);
  54. }
  55. std::string CLabel::getText()
  56. {
  57. return text;
  58. }
  59. void CLabel::setAutoRedraw(bool value)
  60. {
  61. autoRedraw = value;
  62. }
  63. void CLabel::setText(const std::string & Txt)
  64. {
  65. text = Txt;
  66. if(autoRedraw)
  67. {
  68. if(background || !parent)
  69. redraw();
  70. else
  71. parent->redraw();
  72. }
  73. }
  74. void CLabel::setColor(const SDL_Color & Color)
  75. {
  76. color = Color;
  77. if(autoRedraw)
  78. {
  79. if(background || !parent)
  80. redraw();
  81. else
  82. parent->redraw();
  83. }
  84. }
  85. size_t CLabel::getWidth()
  86. {
  87. return graphics->fonts[font]->getStringWidth(visibleText());;
  88. }
  89. CMultiLineLabel::CMultiLineLabel(Rect position, EFonts Font, ETextAlignment Align, const SDL_Color & Color, const std::string & Text) :
  90. CLabel(position.x, position.y, Font, Align, Color, Text),
  91. visibleSize(0, 0, position.w, position.h)
  92. {
  93. pos.w = position.w;
  94. pos.h = position.h;
  95. splitText(Text, true);
  96. }
  97. void CMultiLineLabel::setVisibleSize(Rect visibleSize, bool redrawElement)
  98. {
  99. this->visibleSize = visibleSize;
  100. if(redrawElement)
  101. redraw();
  102. }
  103. void CMultiLineLabel::scrollTextBy(int distance)
  104. {
  105. scrollTextTo(visibleSize.y + distance);
  106. }
  107. void CMultiLineLabel::scrollTextTo(int distance, bool redrawAfterScroll)
  108. {
  109. Rect size = visibleSize;
  110. size.y = distance;
  111. setVisibleSize(size, redrawAfterScroll);
  112. }
  113. void CMultiLineLabel::setText(const std::string & Txt)
  114. {
  115. splitText(Txt, false); //setText used below can handle redraw
  116. CLabel::setText(Txt);
  117. }
  118. void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what)
  119. {
  120. const auto f = graphics->fonts[font];
  121. Point where = destRect.topLeft();
  122. const std::string delimeters = "{}";
  123. auto delimitersCount = std::count_if(what.cbegin(), what.cend(), [&delimeters](char c)
  124. {
  125. return delimeters.find(c) != std::string::npos;
  126. });
  127. //We should count delimiters length from string to correct centering later.
  128. delimitersCount *= f->getStringWidth(delimeters)/2;
  129. // input is rect in which given text should be placed
  130. // calculate proper position for top-left corner of the text
  131. if(alignment == ETextAlignment::TOPLEFT)
  132. {
  133. where.x += getBorderSize().x;
  134. where.y += getBorderSize().y;
  135. }
  136. if(alignment == ETextAlignment::TOPCENTER)
  137. {
  138. where.x += (int(destRect.w) - int(f->getStringWidth(what) - delimitersCount)) / 2;
  139. where.y += getBorderSize().y;
  140. }
  141. if(alignment == ETextAlignment::CENTER)
  142. {
  143. where.x += (int(destRect.w) - int(f->getStringWidth(what) - delimitersCount)) / 2;
  144. where.y += (int(destRect.h) - int(f->getLineHeight())) / 2;
  145. }
  146. if(alignment == ETextAlignment::BOTTOMRIGHT)
  147. {
  148. where.x += getBorderSize().x + destRect.w - ((int)f->getStringWidth(what) - delimitersCount);
  149. where.y += getBorderSize().y + destRect.h - (int)f->getLineHeight();
  150. }
  151. size_t begin = 0;
  152. size_t currDelimeter = 0;
  153. do
  154. {
  155. size_t end = what.find_first_of(delimeters[currDelimeter % 2], begin);
  156. if(begin != end)
  157. {
  158. std::string toPrint = what.substr(begin, end - begin);
  159. if(currDelimeter % 2) // Enclosed in {} text - set to yellow
  160. to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
  161. else // Non-enclosed text, use default color
  162. to.drawText(where, font, color, ETextAlignment::TOPLEFT, toPrint);
  163. begin = end;
  164. where.x += (int)f->getStringWidth(toPrint);
  165. }
  166. currDelimeter++;
  167. } while(begin++ != std::string::npos);
  168. }
  169. CTextContainer::CTextContainer(ETextAlignment alignment, EFonts font, SDL_Color color) :
  170. alignment(alignment),
  171. font(font),
  172. color(color)
  173. {
  174. }
  175. void CMultiLineLabel::showAll(Canvas & to)
  176. {
  177. CIntObject::showAll(to);
  178. const auto f = graphics->fonts[font];
  179. // calculate which lines should be visible
  180. int totalLines = static_cast<int>(lines.size());
  181. int beginLine = visibleSize.y;
  182. int endLine = getTextLocation().h + visibleSize.y;
  183. if(beginLine < 0)
  184. beginLine = 0;
  185. else
  186. beginLine /= (int)f->getLineHeight();
  187. if(endLine < 0)
  188. endLine = 0;
  189. else
  190. endLine /= (int)f->getLineHeight();
  191. endLine++;
  192. // and where they should be displayed
  193. Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * (int)f->getLineHeight());
  194. Point lineSize = Point(getTextLocation().w, (int)f->getLineHeight());
  195. CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), getTextLocation()); // to properly trim text that is too big to fit
  196. for(int i = beginLine; i < std::min(totalLines, endLine); i++)
  197. {
  198. if(!lines[i].empty()) //non-empty line
  199. blitLine(to, Rect(lineStart, lineSize), lines[i]);
  200. lineStart.y += (int)f->getLineHeight();
  201. }
  202. }
  203. void CMultiLineLabel::splitText(const std::string & Txt, bool redrawAfter)
  204. {
  205. lines.clear();
  206. const auto f = graphics->fonts[font];
  207. int lineHeight = static_cast<int>(f->getLineHeight());
  208. lines = CMessage::breakText(Txt, pos.w, font);
  209. textSize.y = lineHeight * (int)lines.size();
  210. textSize.x = 0;
  211. for(const std::string & line : lines)
  212. vstd::amax(textSize.x, f->getStringWidth(line.c_str()));
  213. if(redrawAfter)
  214. redraw();
  215. }
  216. Rect CMultiLineLabel::getTextLocation()
  217. {
  218. // this method is needed for vertical alignment alignment of text
  219. // when height of available text is smaller than height of widget
  220. // in this case - we should add proper offset to display text at required position
  221. if(pos.h <= textSize.y)
  222. return pos;
  223. Point textSize(pos.w, (int)graphics->fonts[font]->getLineHeight() * (int)lines.size());
  224. Point textOffset(pos.w - textSize.x, pos.h - textSize.y);
  225. switch(alignment)
  226. {
  227. case ETextAlignment::TOPLEFT: return Rect(pos.topLeft(), textSize);
  228. case ETextAlignment::TOPCENTER: return Rect(pos.topLeft(), textSize);
  229. case ETextAlignment::CENTER: return Rect(pos.topLeft() + textOffset / 2, textSize);
  230. case ETextAlignment::BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSize);
  231. }
  232. assert(0);
  233. return Rect();
  234. }
  235. CLabelGroup::CLabelGroup(EFonts Font, ETextAlignment Align, const SDL_Color & Color)
  236. : font(Font), align(Align), color(Color)
  237. {
  238. defActions = 255 - DISPOSE;
  239. }
  240. void CLabelGroup::add(int x, int y, const std::string & text)
  241. {
  242. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  243. labels.push_back(std::make_shared<CLabel>(x, y, font, align, color, text));
  244. }
  245. size_t CLabelGroup::currentSize() const
  246. {
  247. return labels.size();
  248. }
  249. CTextBox::CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font, ETextAlignment Align, const SDL_Color & Color) :
  250. sliderStyle(SliderStyle),
  251. slider(nullptr)
  252. {
  253. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  254. label = std::make_shared<CMultiLineLabel>(rect, Font, Align, Color);
  255. setRedrawParent(true);
  256. pos.x += rect.x;
  257. pos.y += rect.y;
  258. pos.h = rect.h;
  259. pos.w = rect.w;
  260. assert(pos.w >= 40); //we need some space
  261. setText(Text);
  262. }
  263. void CTextBox::sliderMoved(int to)
  264. {
  265. label->scrollTextTo(to);
  266. }
  267. void CTextBox::resize(Point newSize)
  268. {
  269. pos.w = newSize.x;
  270. pos.h = newSize.y;
  271. label->pos.w = pos.w;
  272. label->pos.h = pos.h;
  273. slider.reset();
  274. setText(label->getText()); // force refresh
  275. }
  276. void CTextBox::setText(const std::string & text)
  277. {
  278. label->pos.w = pos.w; // reset to default before textSize.y check
  279. label->setText(text);
  280. if(label->textSize.y <= label->pos.h && slider)
  281. {
  282. // slider is no longer needed
  283. slider.reset();
  284. }
  285. else if(slider)
  286. {
  287. // decrease width again if slider still used
  288. label->pos.w = pos.w - 32;
  289. label->setText(text);
  290. slider->setAmount(label->textSize.y);
  291. }
  292. else if(label->textSize.y > label->pos.h)
  293. {
  294. // create slider and update widget
  295. label->pos.w = pos.w - 32;
  296. label->setText(text);
  297. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  298. slider = std::make_shared<CSlider>(Point(pos.w - 32, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  299. label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
  300. slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight());
  301. slider->setPanningStep(1);
  302. slider->setScrollBounds(pos - slider->pos.topLeft());
  303. }
  304. }
  305. void CGStatusBar::setEnteringMode(bool on)
  306. {
  307. consoleText.clear();
  308. if (on)
  309. {
  310. //assert(enteringText == false);
  311. alignment = ETextAlignment::TOPLEFT;
  312. GH.startTextInput(pos);
  313. setText(consoleText);
  314. }
  315. else
  316. {
  317. //assert(enteringText == true);
  318. alignment = ETextAlignment::CENTER;
  319. GH.stopTextInput();
  320. setText(hoverText);
  321. }
  322. enteringText = on;
  323. }
  324. void CGStatusBar::setEnteredText(const std::string & text)
  325. {
  326. assert(enteringText == true);
  327. consoleText = text;
  328. setText(text);
  329. }
  330. void CGStatusBar::write(const std::string & Text)
  331. {
  332. hoverText = Text;
  333. if (enteringText == false)
  334. setText(hoverText);
  335. }
  336. void CGStatusBar::clearIfMatching(const std::string & Text)
  337. {
  338. if (hoverText == Text)
  339. clear();
  340. }
  341. void CGStatusBar::clear()
  342. {
  343. write({});
  344. }
  345. CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const SDL_Color & Color)
  346. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "")
  347. , enteringText(false)
  348. {
  349. addUsedEvents(LCLICK);
  350. background = background_;
  351. addChild(background.get());
  352. pos = background->pos;
  353. getBorderSize();
  354. autoRedraw = false;
  355. }
  356. CGStatusBar::CGStatusBar(int x, int y, std::string name, int maxw)
  357. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  358. , enteringText(false)
  359. {
  360. addUsedEvents(LCLICK);
  361. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  362. auto backgroundImage = std::make_shared<CPicture>(name);
  363. background = backgroundImage;
  364. pos = background->pos;
  365. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  366. {
  367. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  368. vstd::amin(pos.w, maxw);
  369. backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
  370. }
  371. autoRedraw = false;
  372. }
  373. CGStatusBar::~CGStatusBar()
  374. {
  375. assert(GH.statusbar().get() != this);
  376. }
  377. void CGStatusBar::show(Canvas & to)
  378. {
  379. showAll(to);
  380. }
  381. void CGStatusBar::clickPressed(const Point & cursorPosition)
  382. {
  383. if(LOCPLINT && LOCPLINT->cingconsole->isActive())
  384. LOCPLINT->cingconsole->startEnteringText();
  385. }
  386. void CGStatusBar::activate()
  387. {
  388. GH.setStatusbar(shared_from_this());
  389. CIntObject::activate();
  390. }
  391. void CGStatusBar::deactivate()
  392. {
  393. assert(GH.statusbar().get() == this);
  394. GH.setStatusbar(nullptr);
  395. if (enteringText)
  396. LOCPLINT->cingconsole->endEnteringText(false);
  397. CIntObject::deactivate();
  398. }
  399. Point CGStatusBar::getBorderSize()
  400. {
  401. //Width of borders where text should not be printed
  402. static const Point borderSize(5, 1);
  403. switch(alignment)
  404. {
  405. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  406. case ETextAlignment::TOPCENTER: return Point(pos.w / 2, borderSize.y);
  407. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  408. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  409. }
  410. assert(0);
  411. return Point();
  412. }
  413. CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB)
  414. : CLabel(Pos.x, Pos.y, font, ETextAlignment::CENTER),
  415. cb(CB),
  416. CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  417. {
  418. setRedrawParent(true);
  419. pos.h = Pos.h;
  420. pos.w = Pos.w;
  421. background.reset();
  422. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  423. #if !defined(VCMI_MOBILE)
  424. giveFocus();
  425. #endif
  426. }
  427. CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const std::string & bgName, const CFunctionList<void(const std::string &)> & CB)
  428. :cb(CB), CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  429. {
  430. pos += Pos.topLeft();
  431. pos.h = Pos.h;
  432. pos.w = Pos.w;
  433. OBJ_CONSTRUCTION;
  434. background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
  435. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  436. #if !defined(VCMI_MOBILE)
  437. giveFocus();
  438. #endif
  439. }
  440. CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
  441. :CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  442. {
  443. pos += Pos.topLeft();
  444. OBJ_CONSTRUCTION;
  445. background = std::make_shared<CPicture>(srf, Pos);
  446. pos.w = background->pos.w;
  447. pos.h = background->pos.h;
  448. background->pos = pos;
  449. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  450. #if !defined(VCMI_MOBILE)
  451. giveFocus();
  452. #endif
  453. }
  454. std::atomic<int> CKeyboardFocusListener::usageIndex(0);
  455. CKeyboardFocusListener::CKeyboardFocusListener(CTextInput * textInput)
  456. :textInput(textInput)
  457. {
  458. }
  459. void CKeyboardFocusListener::focusGot()
  460. {
  461. GH.startTextInput(textInput->pos);
  462. usageIndex++;
  463. }
  464. void CKeyboardFocusListener::focusLost()
  465. {
  466. if(0 == --usageIndex)
  467. {
  468. GH.stopTextInput();
  469. }
  470. }
  471. std::string CTextInput::visibleText()
  472. {
  473. return focus ? text + newText + "_" : text;
  474. }
  475. void CTextInput::clickPressed(const Point & cursorPosition)
  476. {
  477. if(!focus)
  478. giveFocus();
  479. }
  480. void CTextInput::keyPressed(EShortcut key)
  481. {
  482. if(!focus)
  483. return;
  484. if(key == EShortcut::GLOBAL_MOVE_FOCUS)
  485. {
  486. moveFocus();
  487. return;
  488. }
  489. bool redrawNeeded = false;
  490. switch(key)
  491. {
  492. case EShortcut::GLOBAL_BACKSPACE:
  493. if(!newText.empty())
  494. {
  495. TextOperations::trimRightUnicode(newText);
  496. redrawNeeded = true;
  497. }
  498. else if(!text.empty())
  499. {
  500. TextOperations::trimRightUnicode(text);
  501. redrawNeeded = true;
  502. }
  503. break;
  504. default:
  505. break;
  506. }
  507. if(redrawNeeded)
  508. {
  509. redraw();
  510. cb(text);
  511. }
  512. }
  513. void CTextInput::setText(const std::string & nText)
  514. {
  515. setText(nText, false);
  516. }
  517. void CTextInput::setText(const std::string & nText, bool callCb)
  518. {
  519. CLabel::setText(nText);
  520. if(callCb)
  521. cb(text);
  522. }
  523. void CTextInput::textInputed(const std::string & enteredText)
  524. {
  525. if(!focus)
  526. return;
  527. std::string oldText = text;
  528. text += enteredText;
  529. filters(text, oldText);
  530. if(text != oldText)
  531. {
  532. redraw();
  533. cb(text);
  534. }
  535. newText.clear();
  536. }
  537. void CTextInput::textEdited(const std::string & enteredText)
  538. {
  539. if(!focus)
  540. return;
  541. newText = enteredText;
  542. redraw();
  543. cb(text + newText);
  544. }
  545. void CTextInput::filenameFilter(std::string & text, const std::string &)
  546. {
  547. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  548. size_t pos;
  549. while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  550. text.erase(pos, 1);
  551. }
  552. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  553. {
  554. assert(minValue < maxValue);
  555. if(text.empty())
  556. text = "0";
  557. size_t pos = 0;
  558. if(text[0] == '-') //allow '-' sign as first symbol only
  559. pos++;
  560. while(pos < text.size())
  561. {
  562. if(text[pos] < '0' || text[pos] > '9')
  563. {
  564. text = oldText;
  565. return; //new text is not number.
  566. }
  567. pos++;
  568. }
  569. try
  570. {
  571. int value = boost::lexical_cast<int>(text);
  572. if(value < minValue)
  573. text = std::to_string(minValue);
  574. else if(value > maxValue)
  575. text = std::to_string(maxValue);
  576. }
  577. catch(boost::bad_lexical_cast &)
  578. {
  579. //Should never happen. Unless I missed some cases
  580. logGlobal->warn("Warning: failed to convert %s to number!", text);
  581. text = oldText;
  582. }
  583. }
  584. CFocusable::CFocusable()
  585. :CFocusable(std::make_shared<IFocusListener>())
  586. {
  587. }
  588. CFocusable::CFocusable(std::shared_ptr<IFocusListener> focusListener)
  589. : focusListener(focusListener)
  590. {
  591. focus = false;
  592. focusables.push_back(this);
  593. }
  594. CFocusable::~CFocusable()
  595. {
  596. if(hasFocus())
  597. {
  598. inputWithFocus = nullptr;
  599. focusListener->focusLost();
  600. }
  601. focusables -= this;
  602. }
  603. bool CFocusable::hasFocus() const
  604. {
  605. return inputWithFocus == this;
  606. }
  607. void CFocusable::giveFocus()
  608. {
  609. focus = true;
  610. focusListener->focusGot();
  611. redraw();
  612. if(inputWithFocus)
  613. {
  614. inputWithFocus->focus = false;
  615. inputWithFocus->focusListener->focusLost();
  616. inputWithFocus->redraw();
  617. }
  618. inputWithFocus = this;
  619. }
  620. void CFocusable::moveFocus()
  621. {
  622. auto i = vstd::find(focusables, this),
  623. ourIt = i;
  624. for(i++; i != ourIt; i++)
  625. {
  626. if(i == focusables.end())
  627. i = focusables.begin();
  628. if((*i)->isActive())
  629. {
  630. (*i)->giveFocus();
  631. break;
  632. }
  633. }
  634. }