LineEdit.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Core/Context.h"
  23. #include "../Input/Input.h"
  24. #include "../UI/LineEdit.h"
  25. #include "../UI/Text.h"
  26. #include "../UI/UI.h"
  27. #include "../UI/UIEvents.h"
  28. #include "../DebugNew.h"
  29. namespace Urho3D
  30. {
  31. StringHash VAR_DRAGDROPCONTENT("DragDropContent");
  32. extern const char* UI_CATEGORY;
  33. LineEdit::LineEdit(Context* context) :
  34. BorderImage(context),
  35. lastFont_(0),
  36. lastFontSize_(0),
  37. cursorPosition_(0),
  38. dragBeginCursor_(M_MAX_UNSIGNED),
  39. cursorBlinkRate_(1.0f),
  40. cursorBlinkTimer_(0.0f),
  41. maxLength_(0),
  42. echoCharacter_(0),
  43. cursorMovable_(true),
  44. textSelectable_(true),
  45. textCopyable_(true)
  46. {
  47. clipChildren_ = true;
  48. SetEnabled(true);
  49. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  50. text_ = CreateChild<Text>("LE_Text");
  51. text_->SetInternal(true);
  52. cursor_ = CreateChild<BorderImage>("LE_Cursor");
  53. cursor_->SetInternal(true);
  54. cursor_->SetPriority(1); // Show over text
  55. SubscribeToEvent(this, E_FOCUSED, HANDLER(LineEdit, HandleFocused));
  56. SubscribeToEvent(this, E_DEFOCUSED, HANDLER(LineEdit, HandleDefocused));
  57. SubscribeToEvent(this, E_LAYOUTUPDATED, HANDLER(LineEdit, HandleLayoutUpdated));
  58. }
  59. LineEdit::~LineEdit()
  60. {
  61. }
  62. void LineEdit::RegisterObject(Context* context)
  63. {
  64. context->RegisterFactory<LineEdit>(UI_CATEGORY);
  65. COPY_BASE_ATTRIBUTES(BorderImage);
  66. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Clip Children", true);
  67. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
  68. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Focus Mode", FM_FOCUSABLE_DEFOCUSABLE);
  69. ACCESSOR_ATTRIBUTE("Max Length", GetMaxLength, SetMaxLength, unsigned, 0, AM_FILE);
  70. ACCESSOR_ATTRIBUTE("Is Cursor Movable", IsCursorMovable, SetCursorMovable, bool, true, AM_FILE);
  71. ACCESSOR_ATTRIBUTE("Is Text Selectable", IsTextSelectable, SetTextSelectable, bool, true, AM_FILE);
  72. ACCESSOR_ATTRIBUTE("Is Text Copyable", IsTextCopyable, SetTextCopyable, bool, true, AM_FILE);
  73. ACCESSOR_ATTRIBUTE("Cursor Blink Rate", GetCursorBlinkRate, SetCursorBlinkRate, float, 1.0f, AM_FILE);
  74. ATTRIBUTE("Echo Character", int, echoCharacter_, 0, AM_FILE);
  75. }
  76. void LineEdit::ApplyAttributes()
  77. {
  78. BorderImage::ApplyAttributes();
  79. // Set the text's position to match clipping and indent width, so that text left edge is not left partially hidden
  80. text_->SetPosition(GetIndentWidth() + clipBorder_.left_, clipBorder_.top_);
  81. // Sync the text line
  82. line_ = text_->GetText();
  83. }
  84. void LineEdit::Update(float timeStep)
  85. {
  86. if (cursorBlinkRate_ > 0.0f)
  87. cursorBlinkTimer_ = fmodf(cursorBlinkTimer_ + cursorBlinkRate_ * timeStep, 1.0f);
  88. // Update cursor position if font has changed
  89. if (text_->GetFont() != lastFont_ || text_->GetFontSize() != lastFontSize_)
  90. {
  91. lastFont_ = text_->GetFont();
  92. lastFontSize_ = text_->GetFontSize();
  93. UpdateCursor();
  94. }
  95. bool cursorVisible = HasFocus() ? cursorBlinkTimer_ < 0.5f : false;
  96. cursor_->SetVisible(cursorVisible);
  97. }
  98. void LineEdit::OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor)
  99. {
  100. if (button == MOUSEB_LEFT && cursorMovable_)
  101. {
  102. unsigned pos = GetCharIndex(position);
  103. if (pos != M_MAX_UNSIGNED)
  104. {
  105. SetCursorPosition(pos);
  106. text_->ClearSelection();
  107. }
  108. }
  109. }
  110. void LineEdit::OnDoubleClick(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor)
  111. {
  112. if (button == MOUSEB_LEFT)
  113. text_->SetSelection(0);
  114. }
  115. void LineEdit::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  116. {
  117. UIElement::OnDragBegin(position, screenPosition, buttons, qualifiers, cursor);
  118. dragBeginCursor_ = GetCharIndex(position);
  119. }
  120. void LineEdit::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, const IntVector2& deltaPos, int buttons, int qualifiers, Cursor* cursor)
  121. {
  122. if (cursorMovable_ && textSelectable_)
  123. {
  124. unsigned start = dragBeginCursor_;
  125. unsigned current = GetCharIndex(position);
  126. if (start != M_MAX_UNSIGNED && current != M_MAX_UNSIGNED)
  127. {
  128. if (start < current)
  129. text_->SetSelection(start, current - start);
  130. else
  131. text_->SetSelection(current, start - current);
  132. SetCursorPosition(current);
  133. }
  134. }
  135. }
  136. bool LineEdit::OnDragDropTest(UIElement* source)
  137. {
  138. if (source && editable_)
  139. {
  140. if (source->GetVars().Contains(VAR_DRAGDROPCONTENT))
  141. return true;
  142. StringHash sourceType = source->GetType();
  143. return sourceType == LineEdit::GetTypeStatic() || sourceType == Text::GetTypeStatic();
  144. }
  145. return false;
  146. }
  147. bool LineEdit::OnDragDropFinish(UIElement* source)
  148. {
  149. if (source && editable_)
  150. {
  151. // If the UI element in question has a drag-and-drop content string defined, use it instead of element text
  152. if (source->GetVars().Contains(VAR_DRAGDROPCONTENT))
  153. {
  154. SetText(source->GetVar(VAR_DRAGDROPCONTENT).GetString());
  155. return true;
  156. }
  157. StringHash sourceType = source->GetType();
  158. if (sourceType == LineEdit::GetTypeStatic())
  159. {
  160. LineEdit* sourceLineEdit = static_cast<LineEdit*>(source);
  161. SetText(sourceLineEdit->GetText());
  162. return true;
  163. }
  164. else if (sourceType == Text::GetTypeStatic())
  165. {
  166. Text* sourceText = static_cast<Text*>(source);
  167. SetText(sourceText->GetText());
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. void LineEdit::OnKey(int key, int buttons, int qualifiers)
  174. {
  175. bool changed = false;
  176. bool cursorMoved = false;
  177. switch (key)
  178. {
  179. case 'X':
  180. case 'C':
  181. if (textCopyable_ && qualifiers & QUAL_CTRL)
  182. {
  183. unsigned start = text_->GetSelectionStart();
  184. unsigned length = text_->GetSelectionLength();
  185. if (text_->GetSelectionLength())
  186. GetSubsystem<UI>()->SetClipboardText(line_.SubstringUTF8(start, length));
  187. if (key == 'X' && editable_)
  188. {
  189. if (start + length < line_.LengthUTF8())
  190. line_ = line_.SubstringUTF8(0, start) + line_.SubstringUTF8(start + length);
  191. else
  192. line_ = line_.SubstringUTF8(0, start);
  193. text_->ClearSelection();
  194. cursorPosition_ = start;
  195. changed = true;
  196. }
  197. }
  198. break;
  199. case 'V':
  200. if (editable_ && textCopyable_ && qualifiers & QUAL_CTRL)
  201. {
  202. const String& clipBoard = GetSubsystem<UI>()->GetClipboardText();
  203. if (!clipBoard.Empty())
  204. {
  205. // Remove selected text first
  206. if(text_->GetSelectionLength() > 0)
  207. {
  208. unsigned start = text_->GetSelectionStart();
  209. unsigned length = text_->GetSelectionLength();
  210. if (start + length < line_.LengthUTF8())
  211. line_ = line_.SubstringUTF8(0, start) + line_.SubstringUTF8(start + length);
  212. else
  213. line_ = line_.SubstringUTF8(0, start);
  214. text_->ClearSelection();
  215. cursorPosition_ = start;
  216. }
  217. if (cursorPosition_ < line_.LengthUTF8())
  218. line_ = line_.SubstringUTF8(0, cursorPosition_) + clipBoard + line_.SubstringUTF8(cursorPosition_);
  219. else
  220. line_ += clipBoard;
  221. cursorPosition_ += clipBoard.LengthUTF8();
  222. changed = true;
  223. }
  224. }
  225. break;
  226. case KEY_HOME:
  227. qualifiers |= QUAL_CTRL;
  228. // Fallthru
  229. case KEY_LEFT:
  230. if (cursorMovable_ && cursorPosition_ > 0)
  231. {
  232. if (textSelectable_ && qualifiers & QUAL_SHIFT && !text_->GetSelectionLength())
  233. dragBeginCursor_ = cursorPosition_;
  234. if (qualifiers & QUAL_CTRL)
  235. cursorPosition_ = 0;
  236. else if (text_->GetSelectionLength() && !(qualifiers & QUAL_SHIFT))
  237. cursorPosition_ = text_->GetSelectionStart();
  238. else
  239. --cursorPosition_;
  240. cursorMoved = true;
  241. if (textSelectable_ && qualifiers & QUAL_SHIFT)
  242. {
  243. unsigned start = dragBeginCursor_;
  244. unsigned current = cursorPosition_;
  245. if (start < current)
  246. text_->SetSelection(start, current - start);
  247. else
  248. text_->SetSelection(current, start - current);
  249. }
  250. }
  251. if (!(qualifiers & QUAL_SHIFT))
  252. text_->ClearSelection();
  253. break;
  254. case KEY_END:
  255. qualifiers |= QUAL_CTRL;
  256. // Fallthru
  257. case KEY_RIGHT:
  258. if (cursorMovable_ && cursorPosition_ < line_.LengthUTF8())
  259. {
  260. if (textSelectable_ && qualifiers & QUAL_SHIFT && !text_->GetSelectionLength())
  261. dragBeginCursor_ = cursorPosition_;
  262. if (qualifiers & QUAL_CTRL)
  263. cursorPosition_ = line_.LengthUTF8();
  264. else if (text_->GetSelectionLength() && !(qualifiers & QUAL_SHIFT))
  265. cursorPosition_ = text_->GetSelectionStart() + text_->GetSelectionLength();
  266. else
  267. ++cursorPosition_;
  268. cursorMoved = true;
  269. if (textSelectable_ && qualifiers & QUAL_SHIFT)
  270. {
  271. unsigned start = dragBeginCursor_;
  272. unsigned current = cursorPosition_;
  273. if (start < current)
  274. text_->SetSelection(start, current - start);
  275. else
  276. text_->SetSelection(current, start - current);
  277. }
  278. }
  279. if (!(qualifiers & QUAL_SHIFT))
  280. text_->ClearSelection();
  281. break;
  282. case KEY_DELETE:
  283. if (editable_)
  284. {
  285. if (!text_->GetSelectionLength())
  286. {
  287. if (cursorPosition_ < line_.LengthUTF8())
  288. {
  289. line_ = line_.SubstringUTF8(0, cursorPosition_) + line_.SubstringUTF8(cursorPosition_ + 1);
  290. changed = true;
  291. }
  292. }
  293. else
  294. {
  295. // If a selection exists, erase it
  296. unsigned start = text_->GetSelectionStart();
  297. unsigned length = text_->GetSelectionLength();
  298. if (start + length < line_.LengthUTF8())
  299. line_ = line_.SubstringUTF8(0, start) + line_.SubstringUTF8(start + length);
  300. else
  301. line_ = line_.SubstringUTF8(0, start);
  302. text_->ClearSelection();
  303. cursorPosition_ = start;
  304. changed = true;
  305. }
  306. }
  307. break;
  308. case KEY_UP:
  309. case KEY_DOWN:
  310. case KEY_PAGEUP:
  311. case KEY_PAGEDOWN:
  312. {
  313. using namespace UnhandledKey;
  314. VariantMap& eventData = GetEventDataMap();
  315. eventData[P_ELEMENT] = this;
  316. eventData[P_KEY] = key;
  317. eventData[P_BUTTONS] = buttons;
  318. eventData[P_QUALIFIERS] = qualifiers;
  319. SendEvent(E_UNHANDLEDKEY, eventData);
  320. }
  321. return;
  322. case KEY_BACKSPACE:
  323. if (editable_)
  324. {
  325. if (!text_->GetSelectionLength())
  326. {
  327. if (line_.LengthUTF8() && cursorPosition_)
  328. {
  329. if (cursorPosition_ < line_.LengthUTF8())
  330. line_ = line_.SubstringUTF8(0, cursorPosition_ - 1) + line_.SubstringUTF8(cursorPosition_);
  331. else
  332. line_ = line_.SubstringUTF8(0, cursorPosition_ - 1);
  333. --cursorPosition_;
  334. changed = true;
  335. }
  336. }
  337. else
  338. {
  339. // If a selection exists, erase it
  340. unsigned start = text_->GetSelectionStart();
  341. unsigned length = text_->GetSelectionLength();
  342. if (start + length < line_.LengthUTF8())
  343. line_ = line_.SubstringUTF8(0, start) + line_.SubstringUTF8(start + length);
  344. else
  345. line_ = line_.SubstringUTF8(0, start);
  346. text_->ClearSelection();
  347. cursorPosition_ = start;
  348. changed = true;
  349. }
  350. }
  351. break;
  352. case KEY_RETURN:
  353. case KEY_RETURN2:
  354. case KEY_KP_ENTER:
  355. {
  356. // If using the on-screen keyboard, defocus this element to hide it now
  357. if (GetSubsystem<UI>()->GetUseScreenKeyboard() && HasFocus())
  358. SetFocus(false);
  359. using namespace TextFinished;
  360. VariantMap& eventData = GetEventDataMap();
  361. eventData[P_ELEMENT] = this;
  362. eventData[P_TEXT] = line_;
  363. SendEvent(E_TEXTFINISHED, eventData);
  364. return;
  365. }
  366. break;
  367. }
  368. if (changed)
  369. {
  370. UpdateText();
  371. UpdateCursor();
  372. }
  373. else if (cursorMoved)
  374. UpdateCursor();
  375. }
  376. void LineEdit::OnTextInput(const String& text, int buttons, int qualifiers)
  377. {
  378. if (!editable_)
  379. return;
  380. bool changed = false;
  381. // If only CTRL is held down, do not edit
  382. if ((qualifiers & (QUAL_CTRL | QUAL_ALT)) == QUAL_CTRL)
  383. return;
  384. // Send char as an event to allow changing it
  385. using namespace CharEntry;
  386. VariantMap& eventData = GetEventDataMap();
  387. eventData[P_ELEMENT] = this;
  388. eventData[P_TEXT] = text;
  389. eventData[P_BUTTONS] = buttons;
  390. eventData[P_QUALIFIERS] = qualifiers;
  391. SendEvent(E_TEXTENTRY, eventData);
  392. const String newText = eventData[P_TEXT].GetString().SubstringUTF8(0);
  393. if (!newText.Empty() && (!maxLength_ || line_.LengthUTF8() + newText.LengthUTF8() <= maxLength_))
  394. {
  395. if (!text_->GetSelectionLength())
  396. {
  397. if (cursorPosition_ == line_.LengthUTF8())
  398. line_ += newText;
  399. else
  400. line_ = line_.SubstringUTF8(0, cursorPosition_) + newText + line_.SubstringUTF8(cursorPosition_);
  401. cursorPosition_ += newText.LengthUTF8();
  402. }
  403. else
  404. {
  405. // If a selection exists, erase it first
  406. unsigned start = text_->GetSelectionStart();
  407. unsigned length = text_->GetSelectionLength();
  408. if (start + length < line_.LengthUTF8())
  409. line_ = line_.SubstringUTF8(0, start) + newText + line_.SubstringUTF8(start + length);
  410. else
  411. line_ = line_.SubstringUTF8(0, start) + newText;
  412. cursorPosition_ = start + newText.LengthUTF8();
  413. }
  414. changed = true;
  415. }
  416. if (changed)
  417. {
  418. text_->ClearSelection();
  419. UpdateText();
  420. UpdateCursor();
  421. }
  422. }
  423. void LineEdit::SetText(const String& text)
  424. {
  425. if (text != line_)
  426. {
  427. line_ = text;
  428. cursorPosition_ = line_.LengthUTF8();
  429. UpdateText();
  430. UpdateCursor();
  431. }
  432. }
  433. void LineEdit::SetCursorPosition(unsigned position)
  434. {
  435. if (position > line_.LengthUTF8() || !cursorMovable_)
  436. position = line_.LengthUTF8();
  437. if (position != cursorPosition_)
  438. {
  439. cursorPosition_ = position;
  440. UpdateCursor();
  441. }
  442. }
  443. void LineEdit::SetCursorBlinkRate(float rate)
  444. {
  445. cursorBlinkRate_ = Max(rate, 0.0f);
  446. if (cursorBlinkRate_ == 0.0f)
  447. cursorBlinkTimer_ = 0.0f; // Cursor does not blink, i.e. always visible
  448. }
  449. void LineEdit::SetMaxLength(unsigned length)
  450. {
  451. maxLength_ = length;
  452. }
  453. void LineEdit::SetEchoCharacter(unsigned c)
  454. {
  455. echoCharacter_ = c;
  456. UpdateText();
  457. }
  458. void LineEdit::SetCursorMovable(bool enable)
  459. {
  460. cursorMovable_ = enable;
  461. }
  462. void LineEdit::SetTextSelectable(bool enable)
  463. {
  464. textSelectable_ = enable;
  465. }
  466. void LineEdit::SetTextCopyable(bool enable)
  467. {
  468. textCopyable_ = enable;
  469. }
  470. bool LineEdit::FilterImplicitAttributes(XMLElement& dest) const
  471. {
  472. if (!BorderImage::FilterImplicitAttributes(dest))
  473. return false;
  474. XMLElement childElem = dest.GetChild("element");
  475. if (!childElem)
  476. return false;
  477. if (!RemoveChildXML(childElem, "Name", "LE_Text"))
  478. return false;
  479. if (!RemoveChildXML(childElem, "Position"))
  480. return false;
  481. childElem = childElem.GetNext("element");
  482. if (!childElem)
  483. return false;
  484. if (!RemoveChildXML(childElem, "Name", "LE_Cursor"))
  485. return false;
  486. if (!RemoveChildXML(childElem, "Priority", "1"))
  487. return false;
  488. if (!RemoveChildXML(childElem, "Position"))
  489. return false;
  490. if (!RemoveChildXML(childElem, "Is Visible"))
  491. return false;
  492. return true;
  493. }
  494. void LineEdit::UpdateText()
  495. {
  496. unsigned utf8Length = line_.LengthUTF8();
  497. if (!echoCharacter_)
  498. text_->SetText(line_);
  499. else
  500. {
  501. String echoText;
  502. for (unsigned i = 0; i < utf8Length; ++i)
  503. echoText.AppendUTF8(echoCharacter_);
  504. text_->SetText(echoText);
  505. }
  506. if (cursorPosition_ > utf8Length)
  507. {
  508. cursorPosition_ = utf8Length;
  509. UpdateCursor();
  510. }
  511. using namespace TextChanged;
  512. VariantMap& eventData = GetEventDataMap();
  513. eventData[P_ELEMENT] = this;
  514. eventData[P_TEXT] = line_;
  515. SendEvent(E_TEXTCHANGED, eventData);
  516. }
  517. void LineEdit::UpdateCursor()
  518. {
  519. int x = text_->GetCharPosition(cursorPosition_).x_;
  520. text_->SetPosition(GetIndentWidth() + clipBorder_.left_, clipBorder_.top_);
  521. cursor_->SetPosition(text_->GetPosition() + IntVector2(x, 0));
  522. cursor_->SetSize(cursor_->GetWidth(), text_->GetRowHeight());
  523. // Scroll if necessary
  524. int sx = -GetChildOffset().x_;
  525. int left = clipBorder_.left_;
  526. int right = GetWidth() - clipBorder_.left_ - clipBorder_.right_ - cursor_->GetWidth();
  527. if (x - sx > right)
  528. sx = x - right;
  529. if (x - sx < left)
  530. sx = x - left;
  531. if (sx < 0)
  532. sx = 0;
  533. SetChildOffset(IntVector2(-sx, 0));
  534. // Restart blinking
  535. cursorBlinkTimer_ = 0.0f;
  536. }
  537. unsigned LineEdit::GetCharIndex(const IntVector2& position)
  538. {
  539. IntVector2 screenPosition = ElementToScreen(position);
  540. IntVector2 textPosition = text_->ScreenToElement(screenPosition);
  541. if (textPosition.x_ < 0)
  542. return 0;
  543. int numChars = text_->GetNumChars();
  544. for (int i = numChars; i >= 0; --i)
  545. {
  546. if (textPosition.x_ >= text_->GetCharPosition(i).x_)
  547. return i;
  548. }
  549. return M_MAX_UNSIGNED;
  550. }
  551. void LineEdit::HandleFocused(StringHash eventType, VariantMap& eventData)
  552. {
  553. if (eventData[Focused::P_BYKEY].GetBool())
  554. {
  555. cursorPosition_ = line_.LengthUTF8();
  556. text_->SetSelection(0);
  557. }
  558. UpdateCursor();
  559. if (GetSubsystem<UI>()->GetUseScreenKeyboard())
  560. GetSubsystem<Input>()->SetScreenKeyboardVisible(true);
  561. }
  562. void LineEdit::HandleDefocused(StringHash eventType, VariantMap& eventData)
  563. {
  564. text_->ClearSelection();
  565. if (GetSubsystem<UI>()->GetUseScreenKeyboard())
  566. GetSubsystem<Input>()->SetScreenKeyboardVisible(false);
  567. }
  568. void LineEdit::HandleLayoutUpdated(StringHash eventType, VariantMap& eventData)
  569. {
  570. UpdateCursor();
  571. }
  572. }