LineEdit.cpp 21 KB

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