BsGUIInputTool.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. #include "BsPrerequisites.h"
  3. #include "BsTextSprite.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Represents a single line of text used by the input tools.
  8. */
  9. class BS_EXPORT GUIInputLineDesc
  10. {
  11. public:
  12. /**
  13. * @brief Constructs a new input line description.
  14. *
  15. * @param startChar Index of the first character on the line.
  16. * @param endChar Index of the last character on the line.
  17. * @param lineHeight Height of the line in pixels.
  18. * @param lineYStart Vertical offset from the top of the text to the start of this line (0 for first line usually)
  19. * @param includesNewLine True if the lines end character is a newline character.
  20. */
  21. GUIInputLineDesc(UINT32 startChar, UINT32 endChar, UINT32 lineHeight, INT32 lineYStart, bool includesNewline);
  22. /**
  23. * @brief Returns index of the last character on the line. If lines contains a newline character it will be returned
  24. * unless you set "includeNewLine" to false, in which case the next end-most character is returned.
  25. * (If newline is the only character on the line, it's index will still be returned).
  26. */
  27. UINT32 getEndChar(bool includeNewline = true) const;
  28. /**
  29. * @brief Returns index of the first character on the line.
  30. */
  31. UINT32 getStartChar() const { return mStartChar; }
  32. /**
  33. * @brief Returns line height in pixels.
  34. */
  35. UINT32 getLineHeight() const { return mLineHeight; }
  36. /**
  37. * @brief Returns vertical offset from the top of the text to the start of this line (0 for first line usually).
  38. */
  39. INT32 getLineYStart() const { return mLineYStart; }
  40. /**
  41. * @brief Checks is the specified character index a newline. Character index is a global character index,
  42. * not relative to the start character index of this line. If the index is out of range of this line
  43. * character indices, it will always return false.
  44. */
  45. bool isNewline(UINT32 charIdx) const;
  46. /**
  47. * @brief Returns true if the last character on this line is a newline.
  48. */
  49. bool hasNewlineChar() const { return mIncludesNewline; }
  50. private:
  51. UINT32 mStartChar;
  52. UINT32 mEndChar;
  53. UINT32 mLineHeight;
  54. INT32 mLineYStart;
  55. bool mIncludesNewline;
  56. };
  57. class BS_EXPORT GUIInputTool
  58. {
  59. public:
  60. GUIInputTool();
  61. ~GUIInputTool();
  62. /**
  63. * @brief Updates the input tool with new text descriptor and parent GUI element. These
  64. * values will be used for all further calculations.
  65. */
  66. void updateText(const GUIElement* element, const TEXT_SPRITE_DESC& textDesc);
  67. protected:
  68. /**
  69. * @brief Returns text offset relative to parent widget.
  70. */
  71. Vector2I getTextOffset() const;
  72. /**
  73. * @brief Returns number of lines in the current text string.
  74. */
  75. UINT32 getNumLines() const { return (UINT32)mLineDescs.size(); }
  76. /**
  77. * @brief Returns descriptor for a line with the specified index.
  78. */
  79. const GUIInputLineDesc& getLineDesc(UINT32 lineIdx) const { return mLineDescs.at(lineIdx); }
  80. /**
  81. * @brief Returns index of a line containing the specified character.
  82. *
  83. * @param charIdx Index of the character to look for.
  84. * @param newlineCountsOnNextLine If true, newline characters will return the next line
  85. * and not the line they're actually on.
  86. */
  87. UINT32 getLineForChar(UINT32 charIdx, bool newlineCountsOnNextLine = false) const;
  88. /**
  89. * @brief Returns a rectangle containing position and size of the character with the provided
  90. * index, relative to parent widget.
  91. */
  92. Rect2I getCharRect(UINT32 charIdx) const;
  93. /**
  94. * @brief Returns a rectangle containing position and size of the character with the provided
  95. * index, relative to parent element.
  96. */
  97. Rect2I getLocalCharRect(UINT32 charIdx) const;
  98. /**
  99. * @brief Returns character index nearest to the specified position. Position should be relative
  100. * to parent widget.
  101. */
  102. INT32 getCharIdxAtPos(const Vector2I& pos) const;
  103. /**
  104. * @brief Returns true if the currently set text desctiptor is valid (has any characters).
  105. */
  106. bool isDescValid() const;
  107. /**
  108. * @brief Gets a character index after the input index. Input index represents the empty areas between the characters.
  109. * Newline counts as a character. (e.g. 0 is before the first character, 1 is after the first character but before
  110. * the second, etc.)
  111. *
  112. * @note This can return an out of range character index, in case the input index is specified after the last character.
  113. */
  114. UINT32 getCharIdxAtInputIdx(UINT32 inputIdx) const;
  115. /**
  116. * @brief Checks is the specified character index a newline.
  117. */
  118. bool isNewlineChar(UINT32 charIdx) const;
  119. /**
  120. * @brief Checks is the character after the specified input index a newline.
  121. *
  122. * @see getCharIdxAtInputIdx
  123. */
  124. bool isNewline(UINT32 inputIdx) const;
  125. protected:
  126. const GUIElement* mElement;
  127. Vector2* mQuads;
  128. UINT32 mNumQuads;
  129. TEXT_SPRITE_DESC mTextDesc;
  130. Vector<GUIInputLineDesc> mLineDescs;
  131. };
  132. }