LineEdit.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "LineEdit.h"
  25. #include "Text.h"
  26. #include "UIEvents.h"
  27. #include "DebugNew.h"
  28. LineEdit::LineEdit(const std::string& name, const std::string& text) :
  29. BorderImage(name),
  30. mEchoCharacter(0),
  31. mCursorBlinkRate(1.0f),
  32. mCursorBlinkTimer(0.0f),
  33. mMaxLength(0),
  34. mDefocusable(true),
  35. mDefocus(false)
  36. {
  37. mClipChildren = true;
  38. mEnabled = true;
  39. mFocusable = true;
  40. mText = new Text();
  41. mCursor = new BorderImage();
  42. addChild(mText);
  43. addChild(mCursor);
  44. // Show cursor on top of text
  45. mCursor->setPriority(1);
  46. setText(text);
  47. }
  48. LineEdit::~LineEdit()
  49. {
  50. }
  51. void LineEdit::setStyle(const XMLElement& element, ResourceCache* cache)
  52. {
  53. BorderImage::setStyle(element, cache);
  54. if (element.hasChildElement("cursorblinkrate"))
  55. setCursorBlinkRate(element.getChildElement("cursorblinkrate").getFloat("value"));
  56. if (element.hasChildElement("echocharacter"))
  57. {
  58. std::string text = element.getChildElement("echocharacter").getString("value");
  59. if (text.length())
  60. setEchoCharacter(text[0]);
  61. }
  62. if (element.hasChildElement("maxlength"))
  63. setMaxLength(element.getChildElement("maxlength").getInt("value"));
  64. XMLElement textElem = element.getChildElement("text");
  65. if (textElem)
  66. {
  67. if (textElem.hasAttribute("value"))
  68. setText(textElem.getString("value"));
  69. mText->setStyle(textElem, cache);
  70. }
  71. XMLElement cursorElem = element.getChildElement("cursor");
  72. if (cursorElem)
  73. mCursor->setStyle(cursorElem, cache);
  74. }
  75. void LineEdit::update(float timeStep)
  76. {
  77. if (mCursorBlinkRate > 0.0f)
  78. mCursorBlinkTimer = fmodf(mCursorBlinkTimer + mCursorBlinkRate * timeStep, 1.0f);
  79. else
  80. mCursorBlinkTimer = 0.0f;
  81. bool cursorVisible = false;
  82. if (mFocus)
  83. {
  84. int textLength = 0;
  85. const std::vector<int>& rowWidths = mText->getRowWidths();
  86. if (rowWidths.size())
  87. textLength = rowWidths[0];
  88. // This assumes text alignment is top-left
  89. mCursor->setPosition(mText->getPosition() + IntVector2(textLength, 0));
  90. mCursor->setSize(mCursor->getWidth(), mText->getRowHeight());
  91. cursorVisible = mCursorBlinkTimer < 0.5f;
  92. // Scroll if text is longer than what can be visible at once
  93. int scrollThreshold = max(getWidth() - mClipBorder.mLeft - mClipBorder.mRight - mCursor->getWidth(), 0);
  94. if (textLength > scrollThreshold)
  95. setChildOffset(IntVector2(-(textLength - scrollThreshold), 0));
  96. else
  97. setChildOffset(IntVector2::sZero);
  98. }
  99. else
  100. setChildOffset(IntVector2::sZero);
  101. mCursor->setVisible(cursorVisible);
  102. if (mDefocus)
  103. {
  104. setFocus(false);
  105. mDefocus = false;
  106. }
  107. }
  108. void LineEdit::onChar(unsigned char c)
  109. {
  110. unsigned currentLength = mLine.length();
  111. bool changed = false;
  112. if (c == '\b')
  113. {
  114. if (mLine.length())
  115. {
  116. mLine = mLine.substr(0, currentLength - 1);
  117. changed = true;
  118. }
  119. }
  120. else if (c == '\r')
  121. {
  122. using namespace TextFinished;
  123. VariantMap eventData;
  124. eventData[P_ELEMENT] = (void*)this;
  125. sendEvent(EVENT_TEXTFINISHED, eventData);
  126. }
  127. else if (c == 27)
  128. {
  129. if (mDefocusable)
  130. mDefocus = true;
  131. }
  132. else if ((c >= 0x20) && ((!mMaxLength) || (currentLength < mMaxLength)))
  133. {
  134. mLine += (char)c;
  135. changed = true;
  136. }
  137. if (changed)
  138. {
  139. // Restart cursor blinking from the visible state
  140. mCursorBlinkTimer = 0.0f;
  141. updateText();
  142. using namespace TextChanged;
  143. VariantMap eventData;
  144. eventData[P_ELEMENT] = (void*)this;
  145. eventData[P_TEXT] = mLine;
  146. sendEvent(EVENT_TEXTCHANGED, eventData);
  147. }
  148. }
  149. void LineEdit::setText(const std::string& text)
  150. {
  151. mLine = text;
  152. mText->setText(text);
  153. updateText();
  154. }
  155. void LineEdit::setEchoCharacter(char c)
  156. {
  157. mEchoCharacter = c;
  158. updateText();
  159. }
  160. void LineEdit::setCursorBlinkRate(float rate)
  161. {
  162. mCursorBlinkRate = max(rate, 0.0f);
  163. }
  164. void LineEdit::setMaxLength(unsigned length)
  165. {
  166. mMaxLength = length;
  167. }
  168. void LineEdit::setDefocusable(bool enable)
  169. {
  170. mDefocusable = enable;
  171. }
  172. void LineEdit::updateText()
  173. {
  174. if (!mEchoCharacter)
  175. mText->setText(mLine);
  176. else
  177. {
  178. std::string echoText;
  179. echoText.resize(mLine.length());
  180. for (unsigned i = 0; i < mLine.length(); ++i)
  181. echoText[i] = mEchoCharacter;
  182. mText->setText(echoText);
  183. }
  184. }