LineEdit.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Input.h"
  26. #include "LineEdit.h"
  27. #include "Text.h"
  28. #include "UI.h"
  29. #include "UIEvents.h"
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. OBJECTTYPESTATIC(LineEdit);
  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. active_ = true;
  50. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  51. text_ = CreateChild<Text>();
  52. text_->SetInternal(true);
  53. cursor_ = CreateChild<BorderImage>();
  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. }
  59. LineEdit::~LineEdit()
  60. {
  61. }
  62. void LineEdit::RegisterObject(Context* context)
  63. {
  64. context->RegisterFactory<LineEdit>();
  65. ACCESSOR_ATTRIBUTE(LineEdit, VAR_INT, "Max Length", GetMaxLength, SetMaxLength, unsigned, 0, AM_FILE);
  66. ACCESSOR_ATTRIBUTE(LineEdit, VAR_BOOL, "Is Cursor Movable", IsCursorMovable, SetCursorMovable, bool, true, AM_FILE);
  67. ACCESSOR_ATTRIBUTE(LineEdit, VAR_BOOL, "Is Text Selectable", IsTextSelectable, SetTextSelectable, bool, true, AM_FILE);
  68. ACCESSOR_ATTRIBUTE(LineEdit, VAR_BOOL, "Is Text Copyable", IsTextCopyable, SetTextCopyable, bool, true, AM_FILE);
  69. ACCESSOR_ATTRIBUTE(LineEdit, VAR_FLOAT, "Cursor Blink Rate", GetCursorBlinkRate, SetCursorBlinkRate, float, 1.0f, AM_FILE);
  70. ATTRIBUTE(LineEdit, VAR_INT, "Echo Character", echoCharacter_, 0, AM_FILE);
  71. COPY_BASE_ATTRIBUTES(LineEdit, BorderImage);
  72. }
  73. void LineEdit::ApplyAttributes()
  74. {
  75. UIElement::ApplyAttributes();
  76. // Set the text's position to match clipping, so that text left edge is not left partially hidden
  77. text_->SetPosition(clipBorder_.left_, clipBorder_.top_);
  78. }
  79. void LineEdit::Update(float timeStep)
  80. {
  81. if (cursorBlinkRate_ > 0.0f)
  82. cursorBlinkTimer_ = fmodf(cursorBlinkTimer_ + cursorBlinkRate_ * timeStep, 1.0f);
  83. else
  84. cursorBlinkTimer_ = 0.0f;
  85. // Update cursor position if font has changed
  86. if (text_->GetFont() != lastFont_ || text_->GetFontSize() != lastFontSize_)
  87. {
  88. lastFont_ = text_->GetFont();
  89. lastFontSize_ = text_->GetFontSize();
  90. UpdateCursor();
  91. }
  92. bool cursorVisible = false;
  93. if (HasFocus())
  94. cursorVisible = cursorBlinkTimer_ < 0.5f;
  95. cursor_->SetVisible(cursorVisible);
  96. }
  97. void LineEdit::OnClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  98. {
  99. if (buttons & MOUSEB_LEFT && cursorMovable_)
  100. {
  101. unsigned pos = GetCharIndex(position);
  102. if (pos != M_MAX_UNSIGNED)
  103. {
  104. SetCursorPosition(pos);
  105. text_->ClearSelection();
  106. }
  107. }
  108. }
  109. void LineEdit::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  110. {
  111. dragBeginCursor_ = GetCharIndex(position);
  112. }
  113. void LineEdit::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  114. {
  115. if (cursorMovable_ && textSelectable_)
  116. {
  117. unsigned start = dragBeginCursor_;
  118. unsigned current = GetCharIndex(position);
  119. if (start != M_MAX_UNSIGNED && current != M_MAX_UNSIGNED)
  120. {
  121. if (start < current)
  122. text_->SetSelection(start, current - start);
  123. else
  124. text_->SetSelection(current, start - current);
  125. SetCursorPosition(current);
  126. }
  127. }
  128. }
  129. bool LineEdit::OnDragDropTest(UIElement* source)
  130. {
  131. return (dynamic_cast<LineEdit*>(source) != 0);
  132. }
  133. bool LineEdit::OnDragDropFinish(UIElement* source)
  134. {
  135. LineEdit* sourceLineEdit = dynamic_cast<LineEdit*>(source);
  136. if (sourceLineEdit)
  137. {
  138. SetText(sourceLineEdit->GetText());
  139. return true;
  140. }
  141. else
  142. return false;
  143. }
  144. void LineEdit::OnKey(int key, int buttons, int qualifiers)
  145. {
  146. bool changed = false;
  147. bool cursorMoved = false;
  148. switch (key)
  149. {
  150. case 'X':
  151. case 'C':
  152. if (textCopyable_ && qualifiers & QUAL_CTRL)
  153. {
  154. unsigned start = text_->GetSelectionStart();
  155. unsigned length = text_->GetSelectionLength();
  156. if (text_->GetSelectionLength())
  157. GetSubsystem<UI>()->SetClipBoardText(line_.SubstringUTF8(start, length));
  158. if (key == 'X')
  159. {
  160. if (start + length < line_.LengthUTF8())
  161. line_ = line_.SubstringUTF8(0, start) + line_.SubstringUTF8(start + length);
  162. else
  163. line_ = line_.SubstringUTF8(0, start);
  164. text_->ClearSelection();
  165. cursorPosition_ = start;
  166. changed = true;
  167. }
  168. }
  169. break;
  170. case 'V':
  171. if (textCopyable_ && qualifiers & QUAL_CTRL)
  172. {
  173. const String& clipBoard = GetSubsystem<UI>()->GetClipBoardText();
  174. if (!clipBoard.Empty())
  175. {
  176. if (cursorPosition_ < line_.LengthUTF8())
  177. line_ = line_.SubstringUTF8(0, cursorPosition_) + clipBoard + line_.SubstringUTF8(cursorPosition_);
  178. else
  179. line_ += clipBoard;
  180. cursorPosition_ += clipBoard.LengthUTF8();
  181. changed = true;
  182. }
  183. }
  184. break;
  185. case KEY_LEFT:
  186. if (!(qualifiers & QUAL_SHIFT))
  187. text_->ClearSelection();
  188. if (cursorMovable_ && cursorPosition_ > 0)
  189. {
  190. if (textSelectable_ && qualifiers & QUAL_SHIFT && !text_->GetSelectionLength())
  191. dragBeginCursor_ = cursorPosition_;
  192. if (qualifiers & QUAL_CTRL)
  193. cursorPosition_ = 0;
  194. else
  195. --cursorPosition_;
  196. cursorMoved = true;
  197. if (textSelectable_ && qualifiers & QUAL_SHIFT)
  198. {
  199. unsigned start = dragBeginCursor_;
  200. unsigned current = cursorPosition_;
  201. if (start < current)
  202. text_->SetSelection(start, current - start);
  203. else
  204. text_->SetSelection(current, start - current);
  205. }
  206. }
  207. break;
  208. case KEY_RIGHT:
  209. if (!(qualifiers & QUAL_SHIFT))
  210. text_->ClearSelection();
  211. if (cursorMovable_ && cursorPosition_ < line_.LengthUTF8())
  212. {
  213. if (textSelectable_ && qualifiers & QUAL_SHIFT && !text_->GetSelectionLength())
  214. dragBeginCursor_ = cursorPosition_;
  215. if (qualifiers & QUAL_CTRL)
  216. cursorPosition_ = line_.LengthUTF8();
  217. else
  218. ++cursorPosition_;
  219. cursorMoved = true;
  220. if (textSelectable_ && qualifiers & QUAL_SHIFT)
  221. {
  222. unsigned start = dragBeginCursor_;
  223. unsigned current = cursorPosition_;
  224. if (start < current)
  225. text_->SetSelection(start, current - start);
  226. else
  227. text_->SetSelection(current, start - current);
  228. }
  229. }
  230. break;
  231. case KEY_HOME:
  232. if (cursorMovable_ && cursorPosition_ > 0)
  233. {
  234. cursorPosition_ = 0;
  235. cursorMoved = true;
  236. }
  237. break;
  238. case KEY_END:
  239. if (cursorMovable_ && cursorPosition_ < line_.Length())
  240. {
  241. cursorPosition_ = line_.LengthUTF8();
  242. cursorMoved = true;
  243. }
  244. break;
  245. case KEY_DELETE:
  246. if (!text_->GetSelectionLength())
  247. {
  248. if (cursorPosition_ < line_.LengthUTF8())
  249. {
  250. line_ = line_.SubstringUTF8(0, cursorPosition_) + line_.SubstringUTF8(cursorPosition_ + 1);
  251. changed = true;
  252. }
  253. }
  254. else
  255. {
  256. // If a selection exists, erase it
  257. unsigned start = text_->GetSelectionStart();
  258. unsigned length = text_->GetSelectionLength();
  259. if (start + length < line_.LengthUTF8())
  260. line_ = line_.SubstringUTF8(0, start) + line_.SubstringUTF8(start + length);
  261. else
  262. line_ = line_.SubstringUTF8(0, start);
  263. text_->ClearSelection();
  264. cursorPosition_ = start;
  265. changed = true;
  266. }
  267. break;
  268. case KEY_UP:
  269. case KEY_DOWN:
  270. case KEY_PAGEUP:
  271. case KEY_PAGEDOWN:
  272. {
  273. using namespace UnhandledKey;
  274. VariantMap eventData;
  275. eventData[P_ELEMENT] = (void*)this;
  276. eventData[P_KEY] = key;
  277. eventData[P_BUTTONS] = buttons;
  278. eventData[P_QUALIFIERS] = qualifiers;
  279. SendEvent(E_UNHANDLEDKEY, eventData);
  280. }
  281. return;
  282. case KEY_BACKSPACE:
  283. if (!text_->GetSelectionLength())
  284. {
  285. if (line_.LengthUTF8() && cursorPosition_)
  286. {
  287. if (cursorPosition_ < line_.LengthUTF8())
  288. line_ = line_.SubstringUTF8(0, cursorPosition_ - 1) + line_.SubstringUTF8(cursorPosition_);
  289. else
  290. line_ = line_.SubstringUTF8(0, cursorPosition_ - 1);
  291. --cursorPosition_;
  292. changed = true;
  293. }
  294. }
  295. else
  296. {
  297. // If a selection exists, erase it
  298. unsigned start = text_->GetSelectionStart();
  299. unsigned length = text_->GetSelectionLength();
  300. if (start + length < line_.LengthUTF8())
  301. line_ = line_.SubstringUTF8(0, start) + line_.SubstringUTF8(start + length);
  302. else
  303. line_ = line_.SubstringUTF8(0, start);
  304. text_->ClearSelection();
  305. cursorPosition_ = start;
  306. changed = true;
  307. }
  308. break;
  309. case KEY_RETURN:
  310. {
  311. using namespace TextFinished;
  312. VariantMap eventData;
  313. eventData[P_ELEMENT] = (void*)this;
  314. eventData[P_TEXT] = line_;
  315. SendEvent(E_TEXTFINISHED, eventData);
  316. return;
  317. }
  318. break;
  319. }
  320. if (changed)
  321. {
  322. UpdateText();
  323. UpdateCursor();
  324. }
  325. else if (cursorMoved)
  326. UpdateCursor();
  327. }
  328. void LineEdit::OnChar(unsigned c, int buttons, int qualifiers)
  329. {
  330. bool changed = false;
  331. // If only CTRL is held down, do not edit
  332. if ((qualifiers & (QUAL_CTRL | QUAL_ALT)) == QUAL_CTRL)
  333. return;
  334. if (c >= 0x20 && (!maxLength_ || line_.LengthUTF8() < maxLength_))
  335. {
  336. String charStr;
  337. charStr.AppendUTF8(c);
  338. if (!text_->GetSelectionLength())
  339. {
  340. if (cursorPosition_ == line_.LengthUTF8())
  341. line_ += charStr;
  342. else
  343. line_ = line_.SubstringUTF8(0, cursorPosition_) + charStr + line_.SubstringUTF8(cursorPosition_);
  344. ++cursorPosition_;
  345. }
  346. else
  347. {
  348. // If a selection exists, erase it first
  349. unsigned start = text_->GetSelectionStart();
  350. unsigned length = text_->GetSelectionLength();
  351. if (start + length < line_.LengthUTF8())
  352. line_ = line_.SubstringUTF8(0, start) + charStr + line_.SubstringUTF8(start + length);
  353. else
  354. line_ = line_.SubstringUTF8(0, start) + charStr;
  355. cursorPosition_ = start + 1;
  356. }
  357. changed = true;
  358. }
  359. if (changed)
  360. {
  361. text_->ClearSelection();
  362. UpdateText();
  363. UpdateCursor();
  364. }
  365. }
  366. void LineEdit::SetText(const String& text)
  367. {
  368. if (text != line_)
  369. {
  370. line_ = text;
  371. cursorPosition_ = line_.LengthUTF8();
  372. UpdateText();
  373. UpdateCursor();
  374. }
  375. }
  376. void LineEdit::SetCursorPosition(unsigned position)
  377. {
  378. if (position > line_.LengthUTF8() || !cursorMovable_)
  379. position = line_.LengthUTF8();
  380. if (position != cursorPosition_)
  381. {
  382. cursorPosition_ = position;
  383. UpdateCursor();
  384. }
  385. }
  386. void LineEdit::SetCursorBlinkRate(float rate)
  387. {
  388. cursorBlinkRate_ = Max(rate, 0.0f);
  389. }
  390. void LineEdit::SetMaxLength(unsigned length)
  391. {
  392. maxLength_ = length;
  393. }
  394. void LineEdit::SetEchoCharacter(unsigned c)
  395. {
  396. echoCharacter_ = c;
  397. UpdateText();
  398. }
  399. void LineEdit::SetCursorMovable(bool enable)
  400. {
  401. cursorMovable_ = enable;
  402. }
  403. void LineEdit::SetTextSelectable(bool enable)
  404. {
  405. textSelectable_ = enable;
  406. }
  407. void LineEdit::SetTextCopyable(bool enable)
  408. {
  409. textCopyable_ = enable;
  410. }
  411. void LineEdit::UpdateText()
  412. {
  413. unsigned utf8Length = line_.LengthUTF8();
  414. if (!echoCharacter_)
  415. text_->SetText(line_);
  416. else
  417. {
  418. String echoText;
  419. for (unsigned i = 0; i < utf8Length; ++i)
  420. echoText.AppendUTF8(echoCharacter_);
  421. text_->SetText(echoText);
  422. }
  423. if (cursorPosition_ > utf8Length)
  424. {
  425. cursorPosition_ = utf8Length;
  426. UpdateCursor();
  427. }
  428. using namespace TextChanged;
  429. VariantMap eventData;
  430. eventData[P_ELEMENT] = (void*)this;
  431. eventData[P_TEXT] = line_;
  432. SendEvent(E_TEXTCHANGED, eventData);
  433. }
  434. void LineEdit::UpdateCursor()
  435. {
  436. int x = 0;
  437. const PODVector<IntVector2>& charPositions = text_->GetCharPositions();
  438. if (charPositions.Size())
  439. x = cursorPosition_ < charPositions.Size() ? charPositions[cursorPosition_].x_ : charPositions.Back().x_;
  440. text_->SetPosition(clipBorder_.left_, clipBorder_.top_);
  441. cursor_->SetPosition(text_->GetPosition() + IntVector2(x, 0));
  442. cursor_->SetSize(cursor_->GetWidth(), text_->GetRowHeight());
  443. // Scroll if necessary
  444. int sx = -GetChildOffset().x_;
  445. int left = clipBorder_.left_;
  446. int right = GetWidth() - clipBorder_.left_ - clipBorder_.right_ - cursor_->GetWidth();
  447. if (x - sx > right)
  448. sx = x - right;
  449. if (x - sx < left)
  450. sx = x - left;
  451. if (sx < 0)
  452. sx = 0;
  453. SetChildOffset(IntVector2(-sx, 0));
  454. // Restart blinking
  455. cursorBlinkTimer_ = 0.0f;
  456. }
  457. unsigned LineEdit::GetCharIndex(const IntVector2& position)
  458. {
  459. IntVector2 screenPosition = ElementToScreen(position);
  460. IntVector2 textPosition = text_->ScreenToElement(screenPosition);
  461. const PODVector<IntVector2>& charPositions = text_->GetCharPositions();
  462. if (textPosition.x_ < 0)
  463. return 0;
  464. for (unsigned i = charPositions.Size() - 1; i < charPositions.Size(); --i)
  465. {
  466. if (textPosition.x_ >= charPositions[i].x_)
  467. return i;
  468. }
  469. return M_MAX_UNSIGNED;
  470. }
  471. void LineEdit::HandleFocused(StringHash eventType, VariantMap& eventData)
  472. {
  473. UpdateCursor();
  474. }
  475. void LineEdit::HandleDefocused(StringHash eventType, VariantMap& eventData)
  476. {
  477. text_->ClearSelection();
  478. }
  479. }