TextControls.cpp 17 KB

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