LineEdit.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "Input.h"
  25. #include "LineEdit.h"
  26. #include "Text.h"
  27. #include "UIEvents.h"
  28. #include "DebugNew.h"
  29. LineEdit::LineEdit(const std::string& name, const std::string& text) :
  30. BorderImage(name),
  31. mLastFont(0),
  32. mLastFontSize(0),
  33. mCursorPosition(0),
  34. mDragStartPosition(M_MAX_UNSIGNED),
  35. mCursorBlinkRate(1.0f),
  36. mCursorBlinkTimer(0.0f),
  37. mMaxLength(0),
  38. mEchoCharacter(0),
  39. mCursorMovable(true),
  40. mTextSelectable(true),
  41. mTextCopyable(true)
  42. {
  43. mClipChildren = true;
  44. mEnabled = true;
  45. mFocusMode = FM_FOCUSABLE_DEFOCUSABLE;
  46. mText = new Text();
  47. mCursor = new BorderImage();
  48. addChild(mText);
  49. addChild(mCursor);
  50. // Show cursor on top of text
  51. mCursor->setPriority(1);
  52. setText(text);
  53. }
  54. LineEdit::~LineEdit()
  55. {
  56. }
  57. void LineEdit::setStyle(const XMLElement& element, ResourceCache* cache)
  58. {
  59. BorderImage::setStyle(element, cache);
  60. if (element.hasChildElement("maxlength"))
  61. setMaxLength(element.getChildElement("maxlength").getInt("value"));
  62. if (element.hasChildElement("cursormovable"))
  63. setCursorMovable(element.getChildElement("cursormovable").getBool("enable"));
  64. if (element.hasChildElement("textselectable"))
  65. setTextSelectable(element.getChildElement("textselectable").getBool("enable"));
  66. if (element.hasChildElement("textcopyable"))
  67. setTextCopyable(element.getChildElement("textcopyable").getBool("enable"));
  68. XMLElement textElem = element.getChildElement("text");
  69. if (textElem)
  70. {
  71. if (textElem.hasAttribute("value"))
  72. setText(textElem.getString("value"));
  73. mText->setStyle(textElem, cache);
  74. }
  75. XMLElement cursorElem = element.getChildElement("cursor");
  76. if (cursorElem)
  77. mCursor->setStyle(cursorElem, cache);
  78. if (element.hasChildElement("cursorposition"))
  79. setCursorPosition(element.getChildElement("cursorposition").getInt("value"));
  80. if (element.hasChildElement("cursorblinkrate"))
  81. setCursorBlinkRate(element.getChildElement("cursorblinkrate").getFloat("value"));
  82. if (element.hasChildElement("echocharacter"))
  83. {
  84. std::string text = element.getChildElement("echocharacter").getString("value");
  85. if (text.length())
  86. setEchoCharacter(text[0]);
  87. }
  88. }
  89. void LineEdit::update(float timeStep)
  90. {
  91. if (mCursorBlinkRate > 0.0f)
  92. mCursorBlinkTimer = fmodf(mCursorBlinkTimer + mCursorBlinkRate * timeStep, 1.0f);
  93. else
  94. mCursorBlinkTimer = 0.0f;
  95. // Update cursor position if font has changed
  96. if ((mText->getFont() != mLastFont) || (mText->getFontSize() != mLastFontSize))
  97. {
  98. mLastFont = mText->getFont();
  99. mLastFontSize = mText->getFontSize();
  100. updateCursor();
  101. }
  102. bool cursorVisible = false;
  103. if (mFocus)
  104. cursorVisible = mCursorBlinkTimer < 0.5f;
  105. mCursor->setVisible(cursorVisible);
  106. }
  107. void LineEdit::onClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  108. {
  109. if ((buttons & MOUSEB_LEFT) && (mCursorMovable))
  110. {
  111. unsigned pos = getCharIndex(position);
  112. if (pos != M_MAX_UNSIGNED)
  113. {
  114. setCursorPosition(pos);
  115. mText->clearSelection();
  116. }
  117. }
  118. }
  119. void LineEdit::onDragStart(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  120. {
  121. mDragStartPosition = getCharIndex(position);
  122. }
  123. void LineEdit::onDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  124. {
  125. if ((mCursorMovable) && (mTextSelectable))
  126. {
  127. unsigned start = mDragStartPosition;
  128. unsigned current = getCharIndex(position);
  129. if ((start != M_MAX_UNSIGNED) && (current != M_MAX_UNSIGNED))
  130. {
  131. if (start < current)
  132. mText->setSelection(start, current - start);
  133. else
  134. mText->setSelection(current, start - current);
  135. setCursorPosition(current);
  136. }
  137. }
  138. }
  139. bool LineEdit::onDragDropTest(UIElement* source)
  140. {
  141. return (dynamic_cast<LineEdit*>(source) != 0);
  142. }
  143. bool LineEdit::onDragDropFinish(UIElement* source)
  144. {
  145. LineEdit* sourceLineEdit = dynamic_cast<LineEdit*>(source);
  146. if (sourceLineEdit)
  147. {
  148. setText(sourceLineEdit->getText());
  149. return true;
  150. }
  151. else
  152. return false;
  153. }
  154. void LineEdit::onKey(int key, int buttons, int qualifiers)
  155. {
  156. bool changed = false;
  157. bool cursorMoved = false;
  158. switch (key)
  159. {
  160. case 'X':
  161. case 'C':
  162. if ((mTextCopyable) && (qualifiers & QUAL_CTRL))
  163. {
  164. unsigned start = mText->getSelectionStart();
  165. unsigned length = mText->getSelectionLength();
  166. if (mText->getSelectionLength())
  167. sClipBoard = mLine.substr(start, length);
  168. if (key == 'X')
  169. {
  170. if (start + length < mLine.length())
  171. mLine = mLine.substr(0, start) + mLine.substr(start + length);
  172. else
  173. mLine = mLine.substr(0, start);
  174. mText->clearSelection();
  175. mCursorPosition = start;
  176. changed = true;
  177. }
  178. }
  179. break;
  180. case 'V':
  181. if ((mTextCopyable) && (qualifiers & QUAL_CTRL))
  182. {
  183. if (sClipBoard.length())
  184. {
  185. if (mCursorPosition < mLine.length())
  186. mLine = mLine.substr(0, mCursorPosition) + sClipBoard + mLine.substr(mCursorPosition);
  187. else
  188. mLine += sClipBoard;
  189. mCursorPosition += sClipBoard.length();
  190. changed = true;
  191. }
  192. }
  193. break;
  194. case KEY_LEFT:
  195. if (!(qualifiers & QUAL_SHIFT))
  196. mText->clearSelection();
  197. if ((mCursorMovable) && (mCursorPosition > 0))
  198. {
  199. if ((mTextSelectable) && (qualifiers & QUAL_SHIFT) && (!mText->getSelectionLength()))
  200. mDragStartPosition = mCursorPosition;
  201. if (qualifiers & QUAL_CTRL)
  202. mCursorPosition = 0;
  203. else
  204. --mCursorPosition;
  205. cursorMoved = true;
  206. if ((mTextSelectable) && (qualifiers & QUAL_SHIFT))
  207. {
  208. unsigned start = mDragStartPosition;
  209. unsigned current = mCursorPosition;
  210. if (start < current)
  211. mText->setSelection(start, current - start);
  212. else
  213. mText->setSelection(current, start - current);
  214. }
  215. }
  216. break;
  217. case KEY_RIGHT:
  218. if (!(qualifiers & QUAL_SHIFT))
  219. mText->clearSelection();
  220. if ((mCursorMovable) && (mCursorPosition < mLine.length()))
  221. {
  222. if ((mTextSelectable) && (qualifiers & QUAL_SHIFT) && (!mText->getSelectionLength()))
  223. mDragStartPosition = mCursorPosition;
  224. if (qualifiers & QUAL_CTRL)
  225. mCursorPosition = mLine.length();
  226. else
  227. ++mCursorPosition;
  228. cursorMoved = true;
  229. if ((mTextSelectable) && (qualifiers & QUAL_SHIFT))
  230. {
  231. unsigned start = mDragStartPosition;
  232. unsigned current = mCursorPosition;
  233. if (start < current)
  234. mText->setSelection(start, current - start);
  235. else
  236. mText->setSelection(current, start - current);
  237. }
  238. }
  239. break;
  240. case KEY_HOME:
  241. if ((mCursorMovable) && (mCursorPosition > 0))
  242. {
  243. mCursorPosition = 0;
  244. cursorMoved = true;
  245. }
  246. break;
  247. case KEY_END:
  248. if ((mCursorMovable) && (mCursorPosition < mLine.length()))
  249. {
  250. mCursorPosition = mLine.length();
  251. cursorMoved = true;
  252. }
  253. break;
  254. case KEY_DELETE:
  255. if (!mText->getSelectionLength())
  256. {
  257. if (mCursorPosition < mLine.length())
  258. {
  259. mLine = mLine.substr(0, mCursorPosition) + mLine.substr(mCursorPosition + 1);
  260. changed = true;
  261. }
  262. }
  263. else
  264. {
  265. // If a selection exists, erase it
  266. unsigned start = mText->getSelectionStart();
  267. unsigned length = mText->getSelectionLength();
  268. if (start + length < mLine.length())
  269. mLine = mLine.substr(0, start) + mLine.substr(start + length);
  270. else
  271. mLine = mLine.substr(0, start);
  272. mText->clearSelection();
  273. mCursorPosition = start;
  274. changed = true;
  275. }
  276. break;
  277. case KEY_UP:
  278. case KEY_DOWN:
  279. case KEY_PAGEUP:
  280. case KEY_PAGEDOWN:
  281. {
  282. using namespace UnhandledKey;
  283. VariantMap eventData;
  284. eventData[P_ELEMENT] = (void*)this;
  285. eventData[P_KEY] = key;
  286. eventData[P_BUTTONS] = buttons;
  287. eventData[P_QUALIFIERS] = qualifiers;
  288. sendEvent(EVENT_UNHANDLEDKEY, eventData);
  289. }
  290. return;
  291. }
  292. if (changed)
  293. {
  294. updateText();
  295. updateCursor();
  296. }
  297. else if (cursorMoved)
  298. updateCursor();
  299. }
  300. void LineEdit::onChar(unsigned char c, int buttons, int qualifiers)
  301. {
  302. bool changed = false;
  303. if (qualifiers & QUAL_CTRL)
  304. return;
  305. if (c == '\b')
  306. {
  307. if (!mText->getSelectionLength())
  308. {
  309. if ((mLine.length()) && (mCursorPosition))
  310. {
  311. if (mCursorPosition < mLine.length())
  312. mLine = mLine.substr(0, mCursorPosition - 1) + mLine.substr(mCursorPosition);
  313. else
  314. mLine = mLine.substr(0, mCursorPosition - 1);
  315. --mCursorPosition;
  316. changed = true;
  317. }
  318. }
  319. else
  320. {
  321. // If a selection exists, erase it
  322. unsigned start = mText->getSelectionStart();
  323. unsigned length = mText->getSelectionLength();
  324. if (start + length < mLine.length())
  325. mLine = mLine.substr(0, start) + mLine.substr(start + length);
  326. else
  327. mLine = mLine.substr(0, start);
  328. mText->clearSelection();
  329. mCursorPosition = start;
  330. changed = true;
  331. }
  332. }
  333. else if (c == '\r')
  334. {
  335. using namespace TextFinished;
  336. VariantMap eventData;
  337. eventData[P_ELEMENT] = (void*)this;
  338. eventData[P_TEXT] = mLine;
  339. sendEvent(EVENT_TEXTFINISHED, eventData);
  340. return;
  341. }
  342. else if ((c >= 0x20) && ((!mMaxLength) || (mLine.length() < mMaxLength)))
  343. {
  344. static std::string charStr;
  345. charStr.resize(1);
  346. charStr[0] = c;
  347. if (!mText->getSelectionLength())
  348. {
  349. if (mCursorPosition == mLine.length())
  350. mLine += charStr;
  351. else
  352. mLine = mLine.substr(0, mCursorPosition) + charStr + mLine.substr(mCursorPosition);
  353. ++mCursorPosition;
  354. }
  355. else
  356. {
  357. // If a selection exists, erase it first
  358. unsigned start = mText->getSelectionStart();
  359. unsigned length = mText->getSelectionLength();
  360. if (start + length < mLine.length())
  361. mLine = mLine.substr(0, start) + charStr + mLine.substr(start + length);
  362. else
  363. mLine = mLine.substr(0, start) + charStr;
  364. mCursorPosition = start + 1;
  365. }
  366. changed = true;
  367. }
  368. if (changed)
  369. {
  370. mText->clearSelection();
  371. updateText();
  372. updateCursor();
  373. }
  374. }
  375. void LineEdit::onFocus()
  376. {
  377. updateCursor();
  378. }
  379. void LineEdit::onDefocus()
  380. {
  381. mText->clearSelection();
  382. }
  383. void LineEdit::setText(const std::string& text)
  384. {
  385. if (text != mLine)
  386. {
  387. mLine = text;
  388. mCursorPosition = mLine.length();
  389. updateText();
  390. updateCursor();
  391. }
  392. }
  393. void LineEdit::setCursorPosition(unsigned position)
  394. {
  395. if ((position > mLine.length()) || (!mCursorMovable))
  396. position = mLine.length();
  397. if (position != mCursorPosition)
  398. {
  399. mCursorPosition = position;
  400. updateCursor();
  401. }
  402. }
  403. void LineEdit::setCursorBlinkRate(float rate)
  404. {
  405. mCursorBlinkRate = max(rate, 0.0f);
  406. }
  407. void LineEdit::setMaxLength(unsigned length)
  408. {
  409. mMaxLength = length;
  410. }
  411. void LineEdit::setEchoCharacter(char c)
  412. {
  413. mEchoCharacter = c;
  414. updateText();
  415. }
  416. void LineEdit::setCursorMovable(bool enable)
  417. {
  418. mCursorMovable = enable;
  419. }
  420. void LineEdit::setTextSelectable(bool enable)
  421. {
  422. mTextSelectable = enable;
  423. }
  424. void LineEdit::setTextCopyable(bool enable)
  425. {
  426. mTextCopyable = enable;
  427. }
  428. void LineEdit::updateText()
  429. {
  430. if (!mEchoCharacter)
  431. mText->setText(mLine);
  432. else
  433. {
  434. std::string echoText;
  435. echoText.resize(mLine.length());
  436. for (unsigned i = 0; i < mLine.length(); ++i)
  437. echoText[i] = mEchoCharacter;
  438. mText->setText(echoText);
  439. }
  440. if (mCursorPosition > mLine.length())
  441. {
  442. mCursorPosition = mLine.length();
  443. updateCursor();
  444. }
  445. using namespace TextChanged;
  446. VariantMap eventData;
  447. eventData[P_ELEMENT] = (void*)this;
  448. eventData[P_TEXT] = mLine;
  449. sendEvent(EVENT_TEXTCHANGED, eventData);
  450. }
  451. void LineEdit::updateCursor()
  452. {
  453. int x = 0;
  454. const std::vector<IntVector2>& charPositions = mText->getCharPositions();
  455. if (charPositions.size())
  456. x = mCursorPosition < charPositions.size() ? charPositions[mCursorPosition].mX : charPositions.back().mX;
  457. mCursor->setPosition(mText->getPosition() + IntVector2(x, 0));
  458. mCursor->setSize(mCursor->getWidth(), mText->getRowHeight());
  459. // Scroll if necessary
  460. int sx = -getChildOffset().mX;
  461. int left = mClipBorder.mLeft;
  462. int right = getWidth() - mClipBorder.mLeft - mClipBorder.mRight - mCursor->getWidth();
  463. if (x - sx > right)
  464. sx = x - right;
  465. if (x - sx < left)
  466. sx = x - left;
  467. setChildOffset(IntVector2(-sx, 0));
  468. // Restart blinking
  469. mCursorBlinkTimer = 0.0f;
  470. }
  471. unsigned LineEdit::getCharIndex(const IntVector2& position)
  472. {
  473. IntVector2 screenPosition = elementToScreen(position);
  474. IntVector2 textPosition = mText->screenToElement(screenPosition);
  475. const std::vector<IntVector2>& charPositions = mText->getCharPositions();
  476. if (textPosition.mX < 0)
  477. return 0;
  478. for (unsigned i = charPositions.size() - 1; i < charPositions.size(); --i)
  479. {
  480. if (textPosition.mX >= charPositions[i].mX)
  481. return i;
  482. }
  483. return M_MAX_UNSIGNED;
  484. }