BsGUIInputCaret.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #pragma once
  2. #include "BsPrerequisites.h"
  3. #include "BsGUIInputTool.h"
  4. #include "BsTextSprite.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief When paired with a character index determines should the caret be placed
  9. * before or after it.
  10. */
  11. enum CaretPos
  12. {
  13. CARET_BEFORE,
  14. CARET_AFTER
  15. };
  16. /**
  17. * @brief Helper class for dealing with caret for text input boxes and similar
  18. * controls.
  19. */
  20. class BS_EXPORT GUIInputCaret : public GUIInputTool
  21. {
  22. public:
  23. GUIInputCaret();
  24. ~GUIInputCaret();
  25. /**
  26. * @brief Returns sprite used for rendering the caret.
  27. */
  28. ImageSprite* getSprite() const { return mCaretSprite; }
  29. /**
  30. * @brief Returns offset relative to parent widget that determines placement of the
  31. * caret sprite.
  32. */
  33. Vector2I getSpriteOffset() const;
  34. /**
  35. * @brief Returns clip rectangle relative to parent GUI element that determines
  36. * how is caret sprite clipped.
  37. *
  38. * @param parentClipRect Clip rectangle of the parent GUI element. Caret clip rectangle will additionally be
  39. * clipped by this area. Relative to parent element.
  40. */
  41. Rect2I getSpriteClipRect(const Rect2I& parentClipRect) const;
  42. /**
  43. * @brief Rebuilts internal caret sprite using current properties.
  44. */
  45. void updateSprite();
  46. /**
  47. * @brief Moves caret to the start of text.
  48. */
  49. void moveCaretToStart();
  50. /**
  51. * @brief Moves caret to the end of text.
  52. */
  53. void moveCaretToEnd();
  54. /**
  55. * @brief Moves caret one character to the left, if not at start already.
  56. */
  57. void moveCaretLeft();
  58. /**
  59. * @brief Moves caret one character to the right, if not at end already.
  60. */
  61. void moveCaretRight();
  62. /**
  63. * @brief Moves caret one line up if possible.
  64. */
  65. void moveCaretUp();
  66. /**
  67. * @brief Moves caret one line down if possible.
  68. */
  69. void moveCaretDown();
  70. /**
  71. * @brief Moves caret to the character nearest to the specified position.
  72. * Position is relative to parent widget.
  73. */
  74. void moveCaretToPos(const Vector2I& pos);
  75. /**
  76. * @brief Moves the caret to a specific character index.
  77. *
  78. * @param charIdx Index of the character to move the caret to.
  79. * @param caretPos Whether to place the caret before or after the character.
  80. */
  81. void moveCaretToChar(UINT32 charIdx, CaretPos caretPos);
  82. /**
  83. * @brief Returns character index after the current caret position.
  84. */
  85. UINT32 getCharIdxAtCaretPos() const;
  86. /**
  87. * @brief Returns current caret position, relative to parent widget. Requires
  88. * you to provide offset to text the caret is used for (also relative to parent widget).
  89. */
  90. Vector2I getCaretPosition(const Vector2I& offset) const;
  91. /**
  92. * @brief Returns height of the caret, in pixels.
  93. */
  94. UINT32 getCaretHeight() const;
  95. /**
  96. * @brief Returns true if the character after the caret is newline.
  97. */
  98. bool isCaretAtNewline() const;
  99. /**
  100. * @brief Returns maximum valid caret index.
  101. */
  102. UINT32 getMaxCaretPos() const;
  103. /**
  104. * @brief Returns current caret index (not equal to character index).
  105. */
  106. UINT32 getCaretPos() const { return mCaretPos; }
  107. private:
  108. UINT32 mCaretPos;
  109. ImageSprite* mCaretSprite;
  110. };
  111. }