LineEdit.cpp 21 KB

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