LineEdit.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. mFocusable = true;
  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. changed = true;
  259. }
  260. break;
  261. }
  262. if (changed)
  263. {
  264. updateText();
  265. updateCursor();
  266. using namespace TextChanged;
  267. VariantMap eventData;
  268. eventData[P_ELEMENT] = (void*)this;
  269. eventData[P_TEXT] = mLine;
  270. sendEvent(EVENT_TEXTCHANGED, eventData);
  271. }
  272. else if (cursorMoved)
  273. updateCursor();
  274. }
  275. void LineEdit::onChar(unsigned char c, int buttons, int qualifiers)
  276. {
  277. bool changed = false;
  278. if (qualifiers & QUAL_CTRL)
  279. return;
  280. if (c == '\b')
  281. {
  282. if ((mLine.length()) && (mCursorPosition))
  283. {
  284. if (mCursorPosition < mLine.length())
  285. mLine = mLine.substr(0, mCursorPosition - 1) + mLine.substr(mCursorPosition);
  286. else
  287. mLine = mLine.substr(0, mCursorPosition - 1);
  288. --mCursorPosition;
  289. changed = true;
  290. }
  291. }
  292. else if (c == '\r')
  293. {
  294. using namespace TextFinished;
  295. VariantMap eventData;
  296. eventData[P_ELEMENT] = (void*)this;
  297. eventData[P_TEXT] = mLine;
  298. sendEvent(EVENT_TEXTFINISHED, eventData);
  299. mText->clearSelection();
  300. }
  301. else if ((c >= 0x20) && ((!mMaxLength) || (mLine.length() < mMaxLength)))
  302. {
  303. static std::string charStr;
  304. charStr.resize(1);
  305. charStr[0] = c;
  306. if (!mText->getSelectionLength())
  307. {
  308. if (mCursorPosition == mLine.length())
  309. mLine += charStr;
  310. else
  311. mLine = mLine.substr(0, mCursorPosition) + charStr + mLine.substr(mCursorPosition);
  312. ++mCursorPosition;
  313. }
  314. else
  315. {
  316. // If a selection exists, erase it first
  317. unsigned start = mText->getSelectionStart();
  318. unsigned length = mText->getSelectionLength();
  319. if (start + length < mLine.length())
  320. mLine = mLine.substr(0, start) + charStr + mLine.substr(start + length);
  321. else
  322. mLine = mLine.substr(0, start) + charStr;
  323. mCursorPosition = start + 1;
  324. }
  325. changed = true;
  326. }
  327. if (changed)
  328. {
  329. mText->clearSelection();
  330. updateText();
  331. updateCursor();
  332. using namespace TextChanged;
  333. VariantMap eventData;
  334. eventData[P_ELEMENT] = (void*)this;
  335. eventData[P_TEXT] = mLine;
  336. sendEvent(EVENT_TEXTCHANGED, eventData);
  337. }
  338. }
  339. void LineEdit::onFocus()
  340. {
  341. updateCursor();
  342. }
  343. void LineEdit::onDefocus()
  344. {
  345. mText->clearSelection();
  346. }
  347. void LineEdit::setText(const std::string& text)
  348. {
  349. mLine = text;
  350. mText->setText(text);
  351. // If cursor is not movable, make sure it's at the text end
  352. if (!mCursorMovable)
  353. setCursorPosition(mLine.length());
  354. updateText();
  355. }
  356. void LineEdit::setCursorPosition(unsigned position)
  357. {
  358. if ((position > mLine.length()) || (!mCursorMovable))
  359. position = mLine.length();
  360. if (position != mCursorPosition)
  361. {
  362. mCursorPosition = position;
  363. updateCursor();
  364. }
  365. }
  366. void LineEdit::setCursorBlinkRate(float rate)
  367. {
  368. mCursorBlinkRate = max(rate, 0.0f);
  369. }
  370. void LineEdit::setMaxLength(unsigned length)
  371. {
  372. mMaxLength = length;
  373. }
  374. void LineEdit::setEchoCharacter(char c)
  375. {
  376. mEchoCharacter = c;
  377. updateText();
  378. }
  379. void LineEdit::setCursorMovable(bool enable)
  380. {
  381. mCursorMovable = enable;
  382. }
  383. void LineEdit::setTextSelectable(bool enable)
  384. {
  385. mTextSelectable = enable;
  386. }
  387. void LineEdit::setTextCopyable(bool enable)
  388. {
  389. mTextCopyable = enable;
  390. }
  391. void LineEdit::updateText()
  392. {
  393. if (!mEchoCharacter)
  394. mText->setText(mLine);
  395. else
  396. {
  397. std::string echoText;
  398. echoText.resize(mLine.length());
  399. for (unsigned i = 0; i < mLine.length(); ++i)
  400. echoText[i] = mEchoCharacter;
  401. mText->setText(echoText);
  402. }
  403. if (mCursorPosition > mLine.length())
  404. {
  405. mCursorPosition = mLine.length();
  406. updateCursor();
  407. }
  408. }
  409. void LineEdit::updateCursor()
  410. {
  411. int x = 0;
  412. const std::vector<IntVector2>& charPositions = mText->getCharPositions();
  413. if (charPositions.size())
  414. x = mCursorPosition < charPositions.size() ? charPositions[mCursorPosition].mX : charPositions.back().mX;
  415. mCursor->setPosition(mText->getPosition() + IntVector2(x, 0));
  416. mCursor->setSize(mCursor->getWidth(), mText->getRowHeight());
  417. // Scroll if necessary
  418. int sx = -getChildOffset().mX;
  419. int left = mClipBorder.mLeft;
  420. int right = getWidth() - mClipBorder.mLeft - mClipBorder.mRight - mCursor->getWidth();
  421. if (x - sx > right)
  422. sx = x - right;
  423. if (x - sx < left)
  424. sx = x - left;
  425. setChildOffset(IntVector2(-sx, 0));
  426. // Restart blinking
  427. mCursorBlinkTimer = 0.0f;
  428. }
  429. unsigned LineEdit::getCharIndex(const IntVector2& position)
  430. {
  431. IntVector2 screenPosition = elementToScreen(position);
  432. IntVector2 textPosition = mText->screenToElement(screenPosition);
  433. const std::vector<IntVector2>& charPositions = mText->getCharPositions();
  434. for (unsigned i = charPositions.size() - 1; i < charPositions.size(); --i)
  435. {
  436. if (textPosition.mX >= charPositions[i].mX)
  437. return i;
  438. }
  439. return M_MAX_UNSIGNED;
  440. }