BsGUIInputTool.h 5.0 KB

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