| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #pragma once |
| 4 | |
| 5 | #include "BsPrerequisites.h" |
| 6 | #include "GUI/BsGUIInputTool.h" |
| 7 | #include "2D/BsTextSprite.h" |
| 8 | |
| 9 | namespace bs |
| 10 | { |
| 11 | /** @addtogroup GUI-Internal |
| 12 | * @{ |
| 13 | */ |
| 14 | |
| 15 | /** When paired with a character index determines should the caret be placed before or after it. */ |
| 16 | enum CaretPos |
| 17 | { |
| 18 | CARET_BEFORE, |
| 19 | CARET_AFTER |
| 20 | }; |
| 21 | |
| 22 | /** Helper class for dealing with caret for text input boxes and similar controls. */ |
| 23 | class BS_EXPORT GUIInputCaret : public GUIInputTool |
| 24 | { |
| 25 | public: |
| 26 | GUIInputCaret(); |
| 27 | ~GUIInputCaret(); |
| 28 | |
| 29 | /** Returns sprite used for rendering the caret. */ |
| 30 | ImageSprite* getSprite() const { return mCaretSprite; } |
| 31 | |
| 32 | /** Returns offset relative to parent widget that determines placement of the caret sprite. */ |
| 33 | Vector2I getSpriteOffset() const; |
| 34 | |
| 35 | /** |
| 36 | * Returns clip rectangle relative to parent GUI element that determines how is caret sprite clipped. |
| 37 | * |
| 38 | * @param[in] 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 | /** Rebuilts internal caret sprite using current properties. */ |
| 44 | void updateSprite(); |
| 45 | |
| 46 | /** Moves caret to the start of text. */ |
| 47 | void moveCaretToStart(); |
| 48 | |
| 49 | /** Moves caret to the end of text. */ |
| 50 | void moveCaretToEnd(); |
| 51 | |
| 52 | /** Moves caret one character to the left, if not at start already. */ |
| 53 | void moveCaretLeft(); |
| 54 | |
| 55 | /** Moves caret one character to the right, if not at end already. */ |
| 56 | void moveCaretRight(); |
| 57 | |
| 58 | /** Moves caret one line up if possible. */ |
| 59 | void moveCaretUp(); |
| 60 | |
| 61 | /** Moves caret one line down if possible. */ |
| 62 | void moveCaretDown(); |
| 63 | |
| 64 | /** Moves caret to the character nearest to the specified position. Position is relative to parent widget. */ |
| 65 | void moveCaretToPos(const Vector2I& pos); |
| 66 | |
| 67 | /** |
| 68 | * Moves the caret to a specific character index. |
| 69 | * |
| 70 | * @param[in] charIdx Index of the character to move the caret to. |
| 71 | * @param[in] caretPos Whether to place the caret before or after the character. |
| 72 | */ |
| 73 | void moveCaretToChar(UINT32 charIdx, CaretPos caretPos); |
| 74 | |
| 75 | /** Returns character index after the current caret position. */ |
| 76 | UINT32 getCharIdxAtCaretPos() const; |
| 77 | |
| 78 | /** |
| 79 | * Returns current caret position, relative to parent widget. Requires you to provide offset to text the caret is |
| 80 | * used for (also relative to parent widget). |
| 81 | */ |
| 82 | Vector2I getCaretPosition(const Vector2I& offset) const; |
| 83 | |
| 84 | /** Returns height of the caret, in pixels. */ |
| 85 | UINT32 getCaretHeight() const; |
| 86 | |
| 87 | /** Returns true if the character after the caret is newline. */ |
| 88 | bool isCaretAtNewline() const; |
| 89 | |
| 90 | /** Returns maximum valid caret index. */ |
| 91 | UINT32 getMaxCaretPos() const; |
| 92 | |
| 93 | /** Returns current caret index (not equal to character index). */ |
| 94 | UINT32 getCaretPos() const { return mCaretPos; } |
| 95 | private: |
| 96 | UINT32 mCaretPos = 0; |
| 97 | ImageSprite* mCaretSprite; |
| 98 | }; |
| 99 | |
| 100 | /** @} */ |
| 101 | } |