LineEdit.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../UI/BorderImage.h"
  5. namespace Urho3D
  6. {
  7. class Font;
  8. class Text;
  9. /// Single-line text editor %UI element.
  10. class URHO3D_API LineEdit : public BorderImage
  11. {
  12. URHO3D_OBJECT(LineEdit, BorderImage);
  13. public:
  14. /// Construct.
  15. explicit LineEdit(Context* context);
  16. /// Destruct.
  17. ~LineEdit() override;
  18. /// Register object factory.
  19. /// @nobind
  20. static void RegisterObject(Context* context);
  21. /// Apply attribute changes that can not be applied immediately.
  22. void ApplyAttributes() override;
  23. /// Perform UI element update.
  24. void Update(float timeStep) override;
  25. /// React to mouse click begin.
  26. void OnClickBegin
  27. (const IntVector2& position, const IntVector2& screenPosition, MouseButton button, MouseButtonFlags buttons, QualifierFlags qualifiers, Cursor* cursor) override;
  28. /// React to mouse doubleclick.
  29. void OnDoubleClick
  30. (const IntVector2& position, const IntVector2& screenPosition, MouseButton button, MouseButtonFlags buttons, QualifierFlags qualifiers, Cursor* cursor) override;
  31. /// React to mouse drag begin.
  32. void
  33. OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, MouseButtonFlags buttons, QualifierFlags qualifiers, Cursor* cursor) override;
  34. /// React to mouse drag motion.
  35. void OnDragMove
  36. (const IntVector2& position, const IntVector2& screenPosition, const IntVector2& deltaPos, MouseButtonFlags buttons, QualifierFlags qualifiers,
  37. Cursor* cursor) override;
  38. /// React to drag and drop test. Return true to signal that the drop is acceptable.
  39. bool OnDragDropTest(UIElement* source) override;
  40. /// React to drag and drop finish. Return true to signal that the drop was accepted.
  41. bool OnDragDropFinish(UIElement* source) override;
  42. /// React to a key press.
  43. void OnKey(Key key, MouseButtonFlags buttons, QualifierFlags qualifiers) override;
  44. /// React to text input event.
  45. void OnTextInput(const String& text) override;
  46. /// Set text.
  47. /// @property
  48. void SetText(const String& text);
  49. /// Set cursor position.
  50. /// @property
  51. void SetCursorPosition(i32 position);
  52. /// Set cursor blink rate. 0 disables blinking.
  53. /// @property
  54. void SetCursorBlinkRate(float rate);
  55. /// Set maximum text length. 0 for unlimited.
  56. /// @property
  57. void SetMaxLength(i32 length);
  58. /// Set echo character for password entry and such. 0 (default) shows the actual text.
  59. /// @property
  60. void SetEchoCharacter(c32 c);
  61. /// Set whether can move cursor with arrows or mouse, default true.
  62. /// @property
  63. void SetCursorMovable(bool enable);
  64. /// Set whether selections are allowed, default true.
  65. /// @property
  66. void SetTextSelectable(bool enable);
  67. /// Set whether copy-paste operations are allowed, default true.
  68. /// @property
  69. void SetTextCopyable(bool enable);
  70. /// Return text.
  71. /// @property
  72. const String& GetText() const { return line_; }
  73. /// Return cursor position.
  74. /// @property
  75. i32 GetCursorPosition() const { return cursorPosition_; }
  76. /// Return cursor blink rate.
  77. /// @property
  78. float GetCursorBlinkRate() const { return cursorBlinkRate_; }
  79. /// Return maximum text length.
  80. /// @property
  81. i32 GetMaxLength() const { return maxLength_; }
  82. /// Return echo character.
  83. /// @property
  84. c32 GetEchoCharacter() const { return echoCharacter_; }
  85. /// Return whether can move cursor with arrows or mouse.
  86. /// @property
  87. bool IsCursorMovable() const { return cursorMovable_; }
  88. /// Return whether selections are allowed.
  89. /// @property
  90. bool IsTextSelectable() const { return textSelectable_; }
  91. /// Return whether copy-paste operations are allowed.
  92. /// @property
  93. bool IsTextCopyable() const { return textCopyable_; }
  94. /// Return text element.
  95. /// @property
  96. Text* GetTextElement() const { return text_; }
  97. /// Return cursor element.
  98. /// @property
  99. BorderImage* GetCursor() const { return cursor_; }
  100. protected:
  101. /// Filter implicit attributes in serialization process.
  102. bool FilterImplicitAttributes(XMLElement& dest) const override;
  103. /// Update displayed text.
  104. void UpdateText();
  105. /// Update cursor position and restart cursor blinking.
  106. void UpdateCursor();
  107. /// Return char index corresponding to position within element, or NINDEX if not found.
  108. i32 GetCharIndex(const IntVector2& position);
  109. /// Text element.
  110. SharedPtr<Text> text_;
  111. /// Cursor element.
  112. SharedPtr<BorderImage> cursor_;
  113. /// Text line.
  114. String line_;
  115. /// Last used text font.
  116. Font* lastFont_;
  117. /// Last used text size.
  118. int lastFontSize_;
  119. /// Text edit cursor position.
  120. i32 cursorPosition_;
  121. /// Drag begin cursor position.
  122. i32 dragBeginCursor_;
  123. /// Cursor blink rate.
  124. float cursorBlinkRate_;
  125. /// Cursor blink timer.
  126. float cursorBlinkTimer_;
  127. /// Maximum text length.
  128. i32 maxLength_;
  129. /// Echo character.
  130. c32 echoCharacter_;
  131. /// Cursor movable flag.
  132. bool cursorMovable_;
  133. /// Text selectable flag.
  134. bool textSelectable_;
  135. /// Copy-paste enable flag.
  136. bool textCopyable_;
  137. private:
  138. /// Handle being focused.
  139. void HandleFocused(StringHash eventType, VariantMap& eventData);
  140. /// Handle being defocused.
  141. void HandleDefocused(StringHash eventType, VariantMap& eventData);
  142. /// Handle the element layout having been updated.
  143. void HandleLayoutUpdated(StringHash eventType, VariantMap& eventData);
  144. };
  145. }