LineEdit.cpp 8.5 KB

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