LineEdit.cpp 21 KB

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