LineEdit.cpp 15 KB

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