LineEdit.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. mCursorBlinkRate(1.0f),
  35. mCursorBlinkTimer(0.0f),
  36. mMaxLength(0),
  37. mEchoCharacter(0),
  38. mDefocusable(true),
  39. mDefocus(false)
  40. {
  41. mClipChildren = true;
  42. mEnabled = true;
  43. mFocusable = true;
  44. mText = new Text();
  45. mCursor = new BorderImage();
  46. addChild(mText);
  47. addChild(mCursor);
  48. // Show cursor on top of text
  49. mCursor->setPriority(1);
  50. setText(text);
  51. }
  52. LineEdit::~LineEdit()
  53. {
  54. }
  55. void LineEdit::setStyle(const XMLElement& element, ResourceCache* cache)
  56. {
  57. BorderImage::setStyle(element, cache);
  58. if (element.hasChildElement("cursorblinkrate"))
  59. setCursorBlinkRate(element.getChildElement("cursorblinkrate").getFloat("value"));
  60. if (element.hasChildElement("echocharacter"))
  61. {
  62. std::string text = element.getChildElement("echocharacter").getString("value");
  63. if (text.length())
  64. setEchoCharacter(text[0]);
  65. }
  66. if (element.hasChildElement("maxlength"))
  67. setMaxLength(element.getChildElement("maxlength").getInt("value"));
  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. }
  79. void LineEdit::update(float timeStep)
  80. {
  81. if (mCursorBlinkRate > 0.0f)
  82. mCursorBlinkTimer = fmodf(mCursorBlinkTimer + mCursorBlinkRate * timeStep, 1.0f);
  83. else
  84. mCursorBlinkTimer = 0.0f;
  85. // Update cursor position if font has changed
  86. if ((mText->getFont() != mLastFont) || (mText->getFontSize() != mLastFontSize))
  87. {
  88. mLastFont = mText->getFont();
  89. mLastFontSize = mText->getFontSize();
  90. updateCursor();
  91. }
  92. bool cursorVisible = false;
  93. if (mFocus)
  94. cursorVisible = mCursorBlinkTimer < 0.5f;
  95. mCursor->setVisible(cursorVisible);
  96. if (mDefocus)
  97. {
  98. setFocus(false);
  99. mDefocus = false;
  100. }
  101. }
  102. void LineEdit::onClick(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  103. {
  104. if (buttons & MOUSEB_LEFT)
  105. {
  106. IntVector2 textPosition = mText->screenToElement(screenPosition);
  107. const std::vector<IntVector2>& charPositions = mText->getCharPositions();
  108. for (unsigned i = charPositions.size() - 1; i < charPositions.size(); --i)
  109. {
  110. if (textPosition.mX >= charPositions[i].mX)
  111. {
  112. setCursorPosition(i);
  113. break;
  114. }
  115. }
  116. }
  117. }
  118. void LineEdit::onKey(int key)
  119. {
  120. bool changed = false;
  121. bool cursorMoved = false;
  122. switch (key)
  123. {
  124. case KEY_LEFT:
  125. if (mCursorPosition > 0)
  126. {
  127. --mCursorPosition;
  128. cursorMoved = true;
  129. }
  130. break;
  131. case KEY_RIGHT:
  132. if (mCursorPosition < mLine.length())
  133. {
  134. ++mCursorPosition;
  135. cursorMoved = true;
  136. }
  137. break;
  138. case KEY_DELETE:
  139. if (mCursorPosition < mLine.length())
  140. {
  141. if (mCursorPosition)
  142. mLine = mLine.substr(0, mCursorPosition) + mLine.substr(mCursorPosition + 1);
  143. else
  144. mLine = mLine.substr(mCursorPosition + 1);
  145. changed = true;
  146. }
  147. break;
  148. }
  149. if (changed)
  150. {
  151. updateText();
  152. updateCursor();
  153. using namespace TextChanged;
  154. VariantMap eventData;
  155. eventData[P_ELEMENT] = (void*)this;
  156. eventData[P_TEXT] = mLine;
  157. sendEvent(EVENT_TEXTCHANGED, eventData);
  158. }
  159. else if (cursorMoved)
  160. updateCursor();
  161. }
  162. void LineEdit::onChar(unsigned char c)
  163. {
  164. bool changed = false;
  165. if (c == '\b')
  166. {
  167. if ((mLine.length()) && (mCursorPosition))
  168. {
  169. if (mCursorPosition < mLine.length())
  170. mLine = mLine.substr(0, mCursorPosition - 1) + mLine.substr(mCursorPosition);
  171. else
  172. mLine = mLine.substr(0, mCursorPosition - 1);
  173. --mCursorPosition;
  174. changed = true;
  175. }
  176. }
  177. else if (c == '\r')
  178. {
  179. using namespace TextFinished;
  180. VariantMap eventData;
  181. eventData[P_ELEMENT] = (void*)this;
  182. sendEvent(EVENT_TEXTFINISHED, eventData);
  183. }
  184. else if (c == 27)
  185. {
  186. if (mDefocusable)
  187. mDefocus = true;
  188. }
  189. else if ((c >= 0x20) && ((!mMaxLength) || (mLine.length() < mMaxLength)))
  190. {
  191. static std::string charStr;
  192. charStr.resize(1);
  193. charStr[0] = c;
  194. if (mCursorPosition == mLine.length())
  195. mLine += charStr;
  196. else
  197. mLine = mLine.substr(0, mCursorPosition) + charStr + mLine.substr(mCursorPosition);
  198. ++mCursorPosition;
  199. changed = true;
  200. }
  201. if (changed)
  202. {
  203. updateText();
  204. updateCursor();
  205. using namespace TextChanged;
  206. VariantMap eventData;
  207. eventData[P_ELEMENT] = (void*)this;
  208. eventData[P_TEXT] = mLine;
  209. sendEvent(EVENT_TEXTCHANGED, eventData);
  210. }
  211. }
  212. void LineEdit::setText(const std::string& text)
  213. {
  214. mLine = text;
  215. mText->setText(text);
  216. updateText();
  217. }
  218. void LineEdit::setCursorPosition(unsigned position)
  219. {
  220. if (position > mLine.length())
  221. position = mLine.length();
  222. if (position != mCursorPosition)
  223. {
  224. mCursorPosition = position;
  225. updateCursor();
  226. }
  227. }
  228. void LineEdit::setCursorBlinkRate(float rate)
  229. {
  230. mCursorBlinkRate = max(rate, 0.0f);
  231. }
  232. void LineEdit::setMaxLength(unsigned length)
  233. {
  234. mMaxLength = length;
  235. }
  236. void LineEdit::setEchoCharacter(char c)
  237. {
  238. mEchoCharacter = c;
  239. updateText();
  240. }
  241. void LineEdit::setDefocusable(bool enable)
  242. {
  243. mDefocusable = enable;
  244. }
  245. void LineEdit::updateText()
  246. {
  247. if (!mEchoCharacter)
  248. mText->setText(mLine);
  249. else
  250. {
  251. std::string echoText;
  252. echoText.resize(mLine.length());
  253. for (unsigned i = 0; i < mLine.length(); ++i)
  254. echoText[i] = mEchoCharacter;
  255. mText->setText(echoText);
  256. }
  257. if (mCursorPosition > mLine.length())
  258. {
  259. mCursorPosition = mLine.length();
  260. updateCursor();
  261. }
  262. }
  263. void LineEdit::updateCursor()
  264. {
  265. int x = 0;
  266. const std::vector<IntVector2>& charPositions = mText->getCharPositions();
  267. if (charPositions.size())
  268. x = mCursorPosition < charPositions.size() ? charPositions[mCursorPosition].mX : charPositions.back().mX;
  269. mCursor->setPosition(mText->getPosition() + IntVector2(x, 0));
  270. mCursor->setSize(mCursor->getWidth(), mText->getRowHeight());
  271. // Scroll if necessary
  272. int sx = -getChildOffset().mX;
  273. int left = mClipBorder.mLeft;
  274. int right = getWidth() - mClipBorder.mLeft - mClipBorder.mRight - mCursor->getWidth();
  275. if (x - sx > right)
  276. sx = x - right;
  277. if (x - sx < left)
  278. sx = x - left;
  279. setChildOffset(IntVector2(-sx, 0));
  280. // Restart blinking
  281. mCursorBlinkTimer = 0.0f;
  282. }