| 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 "Utility/BsModule.h" |
| 7 | #include "Image/BsPixelData.h" |
| 8 | #include "Math/BsVector2I.h" |
| 9 | |
| 10 | namespace bs |
| 11 | { |
| 12 | /** @addtogroup Platform-Engine |
| 13 | * @{ |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * Allows you to manipulate the platform cursor in various ways. |
| 18 | * |
| 19 | * @note Thread safe. |
| 20 | */ |
| 21 | class BS_EXPORT Cursor : public Module<Cursor> |
| 22 | { |
| 23 | /** Internal container for data about a single cursor icon. */ |
| 24 | struct CustomIcon |
| 25 | { |
| 26 | CustomIcon() = default; |
| 27 | CustomIcon(const PixelData& pixelData, const Vector2I& hotSpot) |
| 28 | :hotSpot(hotSpot), pixelData(pixelData) |
| 29 | { } |
| 30 | |
| 31 | Vector2I hotSpot; |
| 32 | PixelData pixelData; |
| 33 | }; |
| 34 | |
| 35 | public: |
| 36 | Cursor(); |
| 37 | |
| 38 | /** Moves the cursor to the specified screen position. */ |
| 39 | void setScreenPosition(const Vector2I& screenPos); |
| 40 | |
| 41 | /** Retrieves the cursor position in screen coordinates. */ |
| 42 | Vector2I getScreenPosition(); |
| 43 | |
| 44 | /** Hides the cursor. */ |
| 45 | void hide(); |
| 46 | |
| 47 | /** Shows the cursor. */ |
| 48 | void show(); |
| 49 | |
| 50 | /** Limit cursor movement to the specified window. */ |
| 51 | void clipToWindow(const RenderWindow& window); |
| 52 | |
| 53 | /** Limit cursor movement to specific area on the screen. */ |
| 54 | void clipToRect(const Rect2I& screenRect); |
| 55 | |
| 56 | /** Disables cursor clipping that was set using any of the clipTo* methods. */ |
| 57 | void clipDisable(); |
| 58 | |
| 59 | /** Sets a cursor icon. Uses one of the built-in cursor types. */ |
| 60 | void setCursor(CursorType type); |
| 61 | |
| 62 | /** |
| 63 | * Sets a cursor icon. Uses one of the manually registered icons. |
| 64 | * |
| 65 | * @param[in] name The name to identify the cursor, one set previously by calling setCursorIcon(). |
| 66 | */ |
| 67 | void setCursor(const String& name); |
| 68 | |
| 69 | /** |
| 70 | * Registers a new custom cursor icon you can then set by calling "setCursor". |
| 71 | * |
| 72 | * @param[in] name The name to identify the cursor. |
| 73 | * @param[in] pixelData Cursor image data. |
| 74 | * @param[in] hotSpot Offset on the cursor image to where the actual input happens (for example tip of the |
| 75 | * Arrow cursor). |
| 76 | * |
| 77 | * @note |
| 78 | * Stores an internal copy of the pixel data. Clear it by calling removeCursorIcon(). If a custom icon with the |
| 79 | * same name already exists it will be replaced. |
| 80 | */ |
| 81 | void setCursorIcon(const String& name, const PixelData& pixelData, const Vector2I& hotSpot); |
| 82 | |
| 83 | /** |
| 84 | * Registers a new custom cursor icon you can then set by calling setCursor(). |
| 85 | * |
| 86 | * @param[in] type One of the built-in cursor types. |
| 87 | * @param[in] pixelData Cursor image data. |
| 88 | * @param[in] hotSpot Offset on the cursor image to where the actual input happens (for example tip of the |
| 89 | * Arrow cursor). |
| 90 | * |
| 91 | * @note |
| 92 | * Stores an internal copy of the pixel data. Clear it by calling removeCursorIcon(). If a custom icon with the |
| 93 | * same type already exists it will be replaced. |
| 94 | */ |
| 95 | void setCursorIcon(CursorType type, const PixelData& pixelData, const Vector2I& hotSpot); |
| 96 | |
| 97 | /** Removes a custom cursor icon and releases any data associated with it. */ |
| 98 | void clearCursorIcon(const String& name); |
| 99 | |
| 100 | /** |
| 101 | * Removes a custom cursor icon and releases any data associated with it. Restores original icon associated with |
| 102 | * this cursor type. |
| 103 | */ |
| 104 | void clearCursorIcon(CursorType type); |
| 105 | |
| 106 | private: |
| 107 | /** Restores the default cursor icon for the specified cursor type. */ |
| 108 | void restoreCursorIcon(CursorType type); |
| 109 | |
| 110 | /** Sends the cursor image to the OS, making it active. */ |
| 111 | void updateCursorImage(); |
| 112 | |
| 113 | UnorderedMap<String, UINT32> mCustomIconNameToId; |
| 114 | UnorderedMap<UINT32, CustomIcon> mCustomIcons; |
| 115 | UINT32 mNextUniqueId = (UINT32)CursorType::Count; |
| 116 | INT32 mActiveCursorId = -1; |
| 117 | }; |
| 118 | |
| 119 | /** Easy way to access Cursor. */ |
| 120 | BS_EXPORT Cursor& gCursor(); |
| 121 | |
| 122 | /** @} */ |
| 123 | } |