LineEdit.cpp 8.9 KB

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