| 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 | /** Helper class for dealing with text selection for text input boxes and similar controls. */ |
| 16 | class BS_EXPORT GUIInputSelection : public GUIInputTool |
| 17 | { |
| 18 | public: |
| 19 | GUIInputSelection() = default; |
| 20 | ~GUIInputSelection(); |
| 21 | |
| 22 | /** Returns sprites representing the currently selected areas. */ |
| 23 | const Vector<ImageSprite*>& getSprites() const { return mSprites; } |
| 24 | |
| 25 | /** Returns how much to offset the sprite with the specified index, relative to the parent widget. */ |
| 26 | Vector2I getSelectionSpriteOffset(UINT32 spriteIdx) const; |
| 27 | |
| 28 | /** |
| 29 | * Returns clip rectangle relative to parent GUI element for the sprite with the specified index. |
| 30 | * |
| 31 | * @param[in] spriteIdx Index of the sprite to retrieve the clip rectangle for. |
| 32 | * @param[in] parentClipRect Clip rectangle of the parent GUI element. Selection clip rectangle will |
| 33 | * additionally be clipped by this area. Relative to parent element. |
| 34 | */ |
| 35 | Rect2I getSelectionSpriteClipRect(UINT32 spriteIdx, const Rect2I& parentClipRect) const; |
| 36 | |
| 37 | /** Recreates the selection clip sprites. */ |
| 38 | void updateSprite(); |
| 39 | |
| 40 | /** |
| 41 | * Shows the selection using the specified anchor. By default this will select 0 characters so you must manually |
| 42 | * move the selection using moveSelectionToCaret() before anything is considered selected. |
| 43 | * |
| 44 | * @param[in] anchorCaretPos Anchor position which to initially select. Anchor position determines selection |
| 45 | * area behavior when the input caret moves (determines whether left or right side of |
| 46 | * the selection will move with the caret). |
| 47 | */ |
| 48 | void showSelection(UINT32 anchorCaretPos); |
| 49 | |
| 50 | /** |
| 51 | * Clears the currently active selection. Note this does not clear the internal selection range, just the |
| 52 | * selection sprites. |
| 53 | */ |
| 54 | void clearSelectionVisuals(); |
| 55 | |
| 56 | /** |
| 57 | * Moves the selection to caret. Selected area will be from the anchor provided in showSelection() to the caret |
| 58 | * position provided here. |
| 59 | */ |
| 60 | void moveSelectionToCaret(UINT32 caretPos); |
| 61 | |
| 62 | /** Checks is anything selected. */ |
| 63 | bool isSelectionEmpty() const; |
| 64 | |
| 65 | /** Selects all available text. */ |
| 66 | void selectAll(); |
| 67 | |
| 68 | /** |
| 69 | * Starts selection drag at the specified caret position. Call selectionDragUpdate() and selectionDragEnd() as the |
| 70 | * drag operation progresses. |
| 71 | */ |
| 72 | void selectionDragStart(UINT32 caretPos); |
| 73 | |
| 74 | /** Updates selection drag at the specified caret position. */ |
| 75 | void selectionDragUpdate(UINT32 caretPos); |
| 76 | |
| 77 | /** Stops selection drag. */ |
| 78 | void selectionDragEnd(); |
| 79 | |
| 80 | /** Gets caret index of selection start. */ |
| 81 | UINT32 getSelectionStart() const { return mSelectionStart; } |
| 82 | |
| 83 | /** Gets caret index of selection end. */ |
| 84 | UINT32 getSelectionEnd() const { return mSelectionEnd; } |
| 85 | |
| 86 | private: |
| 87 | /** Returns rectangles describing the currently selected areas. Rectangles are relative to parent GUI element. */ |
| 88 | Vector<Rect2I> getSelectionRects() const; |
| 89 | |
| 90 | private: |
| 91 | UINT32 mSelectionStart = 0; |
| 92 | UINT32 mSelectionEnd = 0; |
| 93 | UINT32 mSelectionAnchor = 0; |
| 94 | UINT32 mSelectionDragAnchor = 0; |
| 95 | |
| 96 | Vector<Rect2I> mSelectionRects; |
| 97 | Vector<ImageSprite*> mSprites; |
| 98 | }; |
| 99 | |
| 100 | /** @} */ |
| 101 | } |