LineEdit.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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)
  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)
  120. {
  121. mDragStartPosition = getCharIndex(position);
  122. }
  123. void LineEdit::onDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers)
  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. void LineEdit::onKey(int key, int buttons, int qualifiers)
  140. {
  141. bool changed = false;
  142. bool cursorMoved = false;
  143. switch (key)
  144. {
  145. case 'X':
  146. case 'C':
  147. if ((mTextCopyable) && (qualifiers & QUAL_CTRL))
  148. {
  149. unsigned start = mText->getSelectionStart();
  150. unsigned length = mText->getSelectionLength();
  151. if (mText->getSelectionLength())
  152. sClipBoard = mLine.substr(start, length);
  153. if (key == 'X')
  154. {
  155. if (start + length < mLine.length())
  156. mLine = mLine.substr(0, start) + mLine.substr(start + length);
  157. else
  158. mLine = mLine.substr(0, start);
  159. mText->clearSelection();
  160. mCursorPosition = start;
  161. changed = true;
  162. }
  163. }
  164. break;
  165. case 'V':
  166. if ((mTextCopyable) && (qualifiers & QUAL_CTRL))
  167. {
  168. if (sClipBoard.length())
  169. {
  170. if (mCursorPosition < mLine.length())
  171. mLine = mLine.substr(0, mCursorPosition) + sClipBoard + mLine.substr(mCursorPosition);
  172. else
  173. mLine += sClipBoard;
  174. mCursorPosition += sClipBoard.length();
  175. changed = true;
  176. }
  177. }
  178. break;
  179. case KEY_LEFT:
  180. if (!(qualifiers & QUAL_SHIFT))
  181. mText->clearSelection();
  182. if ((mCursorMovable) && (mCursorPosition > 0))
  183. {
  184. if ((mTextSelectable) && (qualifiers & QUAL_SHIFT) && (!mText->getSelectionLength()))
  185. mDragStartPosition = mCursorPosition;
  186. if (qualifiers & QUAL_CTRL)
  187. mCursorPosition = 0;
  188. else
  189. --mCursorPosition;
  190. cursorMoved = true;
  191. if ((mTextSelectable) && (qualifiers & QUAL_SHIFT))
  192. {
  193. unsigned start = mDragStartPosition;
  194. unsigned current = mCursorPosition;
  195. if (start < current)
  196. mText->setSelection(start, current - start);
  197. else
  198. mText->setSelection(current, start - current);
  199. }
  200. }
  201. break;
  202. case KEY_RIGHT:
  203. if (!(qualifiers & QUAL_SHIFT))
  204. mText->clearSelection();
  205. if ((mCursorMovable) && (mCursorPosition < mLine.length()))
  206. {
  207. if ((mTextSelectable) && (qualifiers & QUAL_SHIFT) && (!mText->getSelectionLength()))
  208. mDragStartPosition = mCursorPosition;
  209. if (qualifiers & QUAL_CTRL)
  210. mCursorPosition = mLine.length();
  211. else
  212. ++mCursorPosition;
  213. cursorMoved = true;
  214. if ((mTextSelectable) && (qualifiers & QUAL_SHIFT))
  215. {
  216. unsigned start = mDragStartPosition;
  217. unsigned current = mCursorPosition;
  218. if (start < current)
  219. mText->setSelection(start, current - start);
  220. else
  221. mText->setSelection(current, start - current);
  222. }
  223. }
  224. break;
  225. case KEY_HOME:
  226. if ((mCursorMovable) && (mCursorPosition > 0))
  227. {
  228. mCursorPosition = 0;
  229. cursorMoved = true;
  230. }
  231. break;
  232. case KEY_END:
  233. if ((mCursorMovable) && (mCursorPosition < mLine.length()))
  234. {
  235. mCursorPosition = mLine.length();
  236. cursorMoved = true;
  237. }
  238. break;
  239. case KEY_DELETE:
  240. if (!mText->getSelectionLength())
  241. {
  242. if (mCursorPosition < mLine.length())
  243. {
  244. mLine = mLine.substr(0, mCursorPosition) + mLine.substr(mCursorPosition + 1);
  245. changed = true;
  246. }
  247. }
  248. else
  249. {
  250. // If a selection exists, erase it
  251. unsigned start = mText->getSelectionStart();
  252. unsigned length = mText->getSelectionLength();
  253. if (start + length < mLine.length())
  254. mLine = mLine.substr(0, start) + mLine.substr(start + length);
  255. else
  256. mLine = mLine.substr(0, start);
  257. mText->clearSelection();
  258. mCursorPosition = start;
  259. changed = true;
  260. }
  261. break;
  262. }
  263. if (changed)
  264. {
  265. updateText();
  266. updateCursor();
  267. }
  268. else if (cursorMoved)
  269. updateCursor();
  270. }
  271. void LineEdit::onChar(unsigned char c, int buttons, int qualifiers)
  272. {
  273. bool changed = false;
  274. if (qualifiers & QUAL_CTRL)
  275. return;
  276. if (c == '\b')
  277. {
  278. if (!mText->getSelectionLength())
  279. {
  280. if ((mLine.length()) && (mCursorPosition))
  281. {
  282. if (mCursorPosition < mLine.length())
  283. mLine = mLine.substr(0, mCursorPosition - 1) + mLine.substr(mCursorPosition);
  284. else
  285. mLine = mLine.substr(0, mCursorPosition - 1);
  286. --mCursorPosition;
  287. changed = true;
  288. }
  289. }
  290. else
  291. {
  292. // If a selection exists, erase it
  293. unsigned start = mText->getSelectionStart();
  294. unsigned length = mText->getSelectionLength();
  295. if (start + length < mLine.length())
  296. mLine = mLine.substr(0, start) + mLine.substr(start + length);
  297. else
  298. mLine = mLine.substr(0, start);
  299. mText->clearSelection();
  300. mCursorPosition = start;
  301. changed = true;
  302. }
  303. }
  304. else if (c == '\r')
  305. {
  306. using namespace TextFinished;
  307. VariantMap eventData;
  308. eventData[P_ELEMENT] = (void*)this;
  309. eventData[P_TEXT] = mLine;
  310. sendEvent(EVENT_TEXTFINISHED, eventData);
  311. mText->clearSelection();
  312. }
  313. else if ((c >= 0x20) && ((!mMaxLength) || (mLine.length() < mMaxLength)))
  314. {
  315. static std::string charStr;
  316. charStr.resize(1);
  317. charStr[0] = c;
  318. if (!mText->getSelectionLength())
  319. {
  320. if (mCursorPosition == mLine.length())
  321. mLine += charStr;
  322. else
  323. mLine = mLine.substr(0, mCursorPosition) + charStr + mLine.substr(mCursorPosition);
  324. ++mCursorPosition;
  325. }
  326. else
  327. {
  328. // If a selection exists, erase it first
  329. unsigned start = mText->getSelectionStart();
  330. unsigned length = mText->getSelectionLength();
  331. if (start + length < mLine.length())
  332. mLine = mLine.substr(0, start) + charStr + mLine.substr(start + length);
  333. else
  334. mLine = mLine.substr(0, start) + charStr;
  335. mCursorPosition = start + 1;
  336. }
  337. changed = true;
  338. }
  339. if (changed)
  340. {
  341. mText->clearSelection();
  342. updateText();
  343. updateCursor();
  344. }
  345. }
  346. void LineEdit::onFocus()
  347. {
  348. updateCursor();
  349. }
  350. void LineEdit::onDefocus()
  351. {
  352. mText->clearSelection();
  353. }
  354. void LineEdit::setText(const std::string& text)
  355. {
  356. if (text != mLine)
  357. {
  358. mLine = text;
  359. // If cursor is not movable, make sure it's at the text end
  360. if (!mCursorMovable)
  361. setCursorPosition(mLine.length());
  362. updateText();
  363. }
  364. }
  365. void LineEdit::setCursorPosition(unsigned position)
  366. {
  367. if ((position > mLine.length()) || (!mCursorMovable))
  368. position = mLine.length();
  369. if (position != mCursorPosition)
  370. {
  371. mCursorPosition = position;
  372. updateCursor();
  373. }
  374. }
  375. void LineEdit::setCursorBlinkRate(float rate)
  376. {
  377. mCursorBlinkRate = max(rate, 0.0f);
  378. }
  379. void LineEdit::setMaxLength(unsigned length)
  380. {
  381. mMaxLength = length;
  382. }
  383. void LineEdit::setEchoCharacter(char c)
  384. {
  385. mEchoCharacter = c;
  386. updateText();
  387. }
  388. void LineEdit::setCursorMovable(bool enable)
  389. {
  390. mCursorMovable = enable;
  391. }
  392. void LineEdit::setTextSelectable(bool enable)
  393. {
  394. mTextSelectable = enable;
  395. }
  396. void LineEdit::setTextCopyable(bool enable)
  397. {
  398. mTextCopyable = enable;
  399. }
  400. void LineEdit::updateText()
  401. {
  402. if (!mEchoCharacter)
  403. mText->setText(mLine);
  404. else
  405. {
  406. std::string echoText;
  407. echoText.resize(mLine.length());
  408. for (unsigned i = 0; i < mLine.length(); ++i)
  409. echoText[i] = mEchoCharacter;
  410. mText->setText(echoText);
  411. }
  412. if (mCursorPosition > mLine.length())
  413. {
  414. mCursorPosition = mLine.length();
  415. updateCursor();
  416. }
  417. using namespace TextChanged;
  418. VariantMap eventData;
  419. eventData[P_ELEMENT] = (void*)this;
  420. eventData[P_TEXT] = mLine;
  421. sendEvent(EVENT_TEXTCHANGED, eventData);
  422. }
  423. void LineEdit::updateCursor()
  424. {
  425. int x = 0;
  426. const std::vector<IntVector2>& charPositions = mText->getCharPositions();
  427. if (charPositions.size())
  428. x = mCursorPosition < charPositions.size() ? charPositions[mCursorPosition].mX : charPositions.back().mX;
  429. mCursor->setPosition(mText->getPosition() + IntVector2(x, 0));
  430. mCursor->setSize(mCursor->getWidth(), mText->getRowHeight());
  431. // Scroll if necessary
  432. int sx = -getChildOffset().mX;
  433. int left = mClipBorder.mLeft;
  434. int right = getWidth() - mClipBorder.mLeft - mClipBorder.mRight - mCursor->getWidth();
  435. if (x - sx > right)
  436. sx = x - right;
  437. if (x - sx < left)
  438. sx = x - left;
  439. setChildOffset(IntVector2(-sx, 0));
  440. // Restart blinking
  441. mCursorBlinkTimer = 0.0f;
  442. }
  443. unsigned LineEdit::getCharIndex(const IntVector2& position)
  444. {
  445. IntVector2 screenPosition = elementToScreen(position);
  446. IntVector2 textPosition = mText->screenToElement(screenPosition);
  447. const std::vector<IntVector2>& charPositions = mText->getCharPositions();
  448. for (unsigned i = charPositions.size() - 1; i < charPositions.size(); --i)
  449. {
  450. if (textPosition.mX >= charPositions[i].mX)
  451. return i;
  452. }
  453. return M_MAX_UNSIGNED;
  454. }