LineEdit.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifndef UI_LINEEDIT_H
  24. #define UI_LINEEDIT_H
  25. #include "BorderImage.h"
  26. class Font;
  27. class Text;
  28. class LineEdit : public BorderImage
  29. {
  30. DEFINE_TYPE(LineEdit);
  31. public:
  32. //! Construct with name and initial text
  33. LineEdit(const std::string& name = std::string(), const std::string& text = std::string());
  34. //! Destruct
  35. virtual ~LineEdit();
  36. //! Set UI element style from XML data
  37. virtual void setStyle(const XMLElement& element, ResourceCache* cache);
  38. //! Perform UI element update
  39. virtual void update(float timeStep);
  40. //! React to mouse click
  41. virtual void onClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers);
  42. //! React to mouse drag start
  43. virtual void onDragStart(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers);
  44. //! React to mouse drag motion
  45. virtual void onDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers);
  46. //! React to a key press
  47. virtual void onKey(int key, int buttons, int qualifiers);
  48. //! React to a key press translated to a character
  49. virtual void onChar(unsigned char c, int buttons, int qualifiers);
  50. //! React to gaining focus
  51. virtual void onFocus();
  52. //! React to losing focus
  53. virtual void onDefocus();
  54. //! Set text
  55. void setText(const std::string& text);
  56. //! Set cursor position
  57. void setCursorPosition(unsigned position);
  58. //! Set cursor blink rate. 0 disables blinking
  59. void setCursorBlinkRate(float rate);
  60. //! Set maximum text length. 0 for unlimited
  61. void setMaxLength(unsigned length);
  62. //! Set echo character for password entry and such. 0 (default) shows the actual text
  63. void setEchoCharacter(char c);
  64. //! Set whether can move cursor with arrows or mouse, default true
  65. void setCursorMovable(bool enable);
  66. //! Set whether selections are allowed, default true
  67. void setTextSelectable(bool enable);
  68. //! Set whether copy-paste operations are allowed, default true
  69. void setTextCopyable(bool enable);
  70. //! Return text
  71. const std::string& getText() const { return mLine; }
  72. //! Return cursor position
  73. unsigned getCursorPosition() const { return mCursorPosition; }
  74. //! Return cursor blink rate
  75. float getCursorBlinkRate() const { return mCursorBlinkRate; }
  76. //! Return maximum text length
  77. unsigned getMaxLength() const { return mMaxLength; }
  78. //! Return echo character
  79. char getEchoCharacter() const { return mEchoCharacter; }
  80. //! Return whether can move cursor with arrows or mouse
  81. bool isCursorMovable() const { return mCursorMovable; }
  82. //! Return whether selections are allowed
  83. bool isTextSelectable() const { return mTextSelectable; }
  84. //! Return whether copy-paste operations are allowed
  85. bool isTextCopyable() const { return mTextCopyable; }
  86. //! Return text element
  87. Text* getTextElement() const { return mText; }
  88. //! Return cursor element
  89. BorderImage* getCursor() const { return mCursor; }
  90. protected:
  91. //! Update displayed text
  92. void updateText();
  93. //! Update cursor position and restart cursor blinking
  94. void updateCursor();
  95. //! Return char index corresponding to position within element, or M_MAX_UNSIGNED if not found
  96. unsigned getCharIndex(const IntVector2& position);
  97. //! Text element
  98. SharedPtr<Text> mText;
  99. //! Cursor element
  100. SharedPtr<BorderImage> mCursor;
  101. //! Text line
  102. std::string mLine;
  103. //! Last known text font
  104. Font* mLastFont;
  105. //! Last known text size
  106. int mLastFontSize;
  107. //! Cursor position
  108. unsigned mCursorPosition;
  109. //! Drag start cursor position
  110. unsigned mDragStartPosition;
  111. //! Cursor blink rate
  112. float mCursorBlinkRate;
  113. //! Cursor blink timer
  114. float mCursorBlinkTimer;
  115. //! Maximum text length
  116. unsigned mMaxLength;
  117. //! Echo character
  118. char mEchoCharacter;
  119. //! Cursor movable flag
  120. bool mCursorMovable;
  121. //! Text selectable flag
  122. bool mTextSelectable;
  123. //! Copy-paste enable flag
  124. bool mTextCopyable;
  125. };
  126. #endif // UI_LINEEDIT_H