LineEdit.cpp 21 KB

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