SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_mouse.h
1#ifndef SDL3PP_MOUSE_H_
2#define SDL3PP_MOUSE_H_
3
4#include <SDL3/SDL_mouse.h>
5#include "SDL3pp_stdinc.h"
6#include "SDL3pp_video.h"
7
8namespace SDL {
9
47// Forward decl
48struct Cursor;
49
51using CursorRaw = SDL_Cursor*;
52
53// Forward decl
54struct CursorRef;
55
58{
60
63 : value(value)
64 {
65 }
66
68 constexpr CursorParam(std::nullptr_t _ = nullptr)
69 : value(nullptr)
70 {
71 }
72
74 constexpr explicit operator bool() const { return !!value; }
75
77 constexpr auto operator<=>(const CursorParam& other) const = default;
78
80 constexpr operator CursorRaw() const { return value; }
81};
82
88using SystemCursor = SDL_SystemCursor;
89
91 SDL_SYSTEM_CURSOR_DEFAULT;
92
94 SDL_SYSTEM_CURSOR_TEXT;
95
97constexpr SystemCursor SYSTEM_CURSOR_WAIT = SDL_SYSTEM_CURSOR_WAIT;
98
100 SDL_SYSTEM_CURSOR_CROSSHAIR;
101
103constexpr SystemCursor SYSTEM_CURSOR_PROGRESS = SDL_SYSTEM_CURSOR_PROGRESS;
104
106 SDL_SYSTEM_CURSOR_NWSE_RESIZE;
108
110 SDL_SYSTEM_CURSOR_NESW_RESIZE;
112
114 SDL_SYSTEM_CURSOR_EW_RESIZE;
115
117 SDL_SYSTEM_CURSOR_NS_RESIZE;
118
120constexpr SystemCursor SYSTEM_CURSOR_MOVE = SDL_SYSTEM_CURSOR_MOVE;
121
124 SDL_SYSTEM_CURSOR_NOT_ALLOWED;
125
127constexpr SystemCursor SYSTEM_CURSOR_POINTER = SDL_SYSTEM_CURSOR_POINTER;
128
133constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE = SDL_SYSTEM_CURSOR_NW_RESIZE;
134
136 SDL_SYSTEM_CURSOR_N_RESIZE;
137
139 SDL_SYSTEM_CURSOR_NE_RESIZE;
140
142 SDL_SYSTEM_CURSOR_E_RESIZE;
143
145 SDL_SYSTEM_CURSOR_SE_RESIZE;
147
149 SDL_SYSTEM_CURSOR_S_RESIZE;
150
152 SDL_SYSTEM_CURSOR_SW_RESIZE;
154
156 SDL_SYSTEM_CURSOR_W_RESIZE;
157
158constexpr SystemCursor SYSTEM_CURSOR_COUNT = SDL_SYSTEM_CURSOR_COUNT;
159
170using MouseID = SDL_MouseID;
171
182{
183 CursorRaw m_resource = nullptr;
184
185public:
187 constexpr Cursor(std::nullptr_t = nullptr) noexcept
188 : m_resource(0)
189 {
190 }
191
199 constexpr explicit Cursor(const CursorRaw resource) noexcept
200 : m_resource(resource)
201 {
202 }
203
205 constexpr Cursor(const Cursor& other) = delete;
206
208 constexpr Cursor(Cursor&& other) noexcept
209 : Cursor(other.release())
210 {
211 }
212
213 constexpr Cursor(const CursorRef& other) = delete;
214
215 constexpr Cursor(CursorRef&& other) = delete;
216
259 Cursor(const Uint8* data,
260 const Uint8* mask,
261 const PointRaw& size,
262 const PointRaw& hot)
263 : m_resource(
264 CheckError(SDL_CreateCursor(data, mask, size.x, size.y, hot.x, hot.y)))
265 {
266 }
267
299 Cursor(SurfaceParam surface, const PointRaw& hot)
300 : m_resource(CheckError(SDL_CreateColorCursor(surface, hot.x, hot.y)))
301 {
302 }
303
318 : m_resource(CheckError(SDL_CreateSystemCursor(id)))
319 {
320 }
321
323 ~Cursor() { SDL_DestroyCursor(m_resource); }
324
326 constexpr Cursor& operator=(Cursor&& other) noexcept
327 {
328 std::swap(m_resource, other.m_resource);
329 return *this;
330 }
331
332protected:
334 constexpr Cursor& operator=(const Cursor& other) noexcept = default;
335
336public:
338 constexpr CursorRaw get() const noexcept { return m_resource; }
339
341 constexpr CursorRaw release() noexcept
342 {
343 auto r = m_resource;
344 m_resource = nullptr;
345 return r;
346 }
347
349 constexpr auto operator<=>(const Cursor& other) const noexcept = default;
350
352 constexpr explicit operator bool() const noexcept { return !!m_resource; }
353
355 constexpr operator CursorParam() const noexcept { return {m_resource}; }
356
372 void Destroy();
373
390 void Set();
391};
392
395{
396 using Cursor::Cursor;
397
405 CursorRef(CursorParam resource) noexcept
406 : Cursor(resource.value)
407 {
408 }
409
417 CursorRef(CursorRaw resource) noexcept
418 : Cursor(resource)
419 {
420 }
421
423 CursorRef(const CursorRef& other) noexcept
424 : Cursor(other.get())
425 {
426 }
427
430};
431
437using MouseWheelDirection = SDL_MouseWheelDirection;
438
440 SDL_MOUSEWHEEL_NORMAL;
441
443 SDL_MOUSEWHEEL_FLIPPED;
444
445#if SDL_VERSION_ATLEAST(3, 4, 0)
446
452using CursorFrameInfo = SDL_CursorFrameInfo;
453
454#endif // SDL_VERSION_ATLEAST(3, 4, 0)
455
461
462constexpr MouseButton BUTTON_LEFT = SDL_BUTTON_LEFT;
463
464constexpr MouseButton BUTTON_MIDDLE = SDL_BUTTON_MIDDLE;
465
466constexpr MouseButton BUTTON_RIGHT = SDL_BUTTON_RIGHT;
467
468constexpr MouseButton BUTTON_X1 = SDL_BUTTON_X1;
469
470constexpr MouseButton BUTTON_X2 = SDL_BUTTON_X2;
471
488
489constexpr MouseButtonFlags BUTTON_LMASK = SDL_BUTTON_LMASK;
490
491constexpr MouseButtonFlags BUTTON_MMASK = SDL_BUTTON_MMASK;
492
493constexpr MouseButtonFlags BUTTON_RMASK = SDL_BUTTON_RMASK;
494
495constexpr MouseButtonFlags BUTTON_X1MASK = SDL_BUTTON_X1MASK;
496
497constexpr MouseButtonFlags BUTTON_X2MASK = SDL_BUTTON_X2MASK;
498
501{
502 return SDL_BUTTON_MASK(button);
503}
504
505#if SDL_VERSION_ATLEAST(3, 4, 0)
506
537using MouseMotionTransformCallback = void(SDLCALL*)(void* userdata,
538 Uint64 timestamp,
539 SDL_Window* window,
540 MouseID mouseID,
541 float* x,
542 float* y);
543
576 SDL_Window* window,
577 MouseID mouseID,
578 float* x,
579 float* y)>;
580
581#endif // SDL_VERSION_ATLEAST(3, 4, 0)
582
594inline bool HasMouse() { return SDL_HasMouse(); }
595
614{
615 int count;
616 auto data = CheckError(SDL_GetMice(&count));
617 return OwnArray<MouseID>{data, size_t(count)};
618}
619
635inline const char* GetMouseNameForID(MouseID instance_id)
636{
637 return SDL_GetMouseNameForID(instance_id);
638}
639
649inline WindowRef GetMouseFocus() { return {SDL_GetMouseFocus()}; }
650
682inline MouseButtonFlags GetMouseState(float* x, float* y)
683{
684 return SDL_GetMouseState(x, y);
685}
686
722inline MouseButtonFlags GetGlobalMouseState(float* x, float* y)
723{
724 return SDL_GetGlobalMouseState(x, y);
725}
726
760inline MouseButtonFlags GetRelativeMouseState(float* x, float* y)
761{
762 return SDL_GetRelativeMouseState(x, y);
763}
764
765inline void Window::WarpMouse(const FPointRaw& p)
766{
767 SDL_WarpMouseInWindow(m_resource, p.x, p.y);
768}
769
790inline void WarpMouse(const FPointRaw& p)
791{
792 CheckError(SDL_WarpMouseGlobal(p.x, p.y));
793}
794
795#if SDL_VERSION_ATLEAST(3, 4, 0)
796
813 void* userdata)
814{
815 CheckError(SDL_SetRelativeMouseTransform(callback, userdata));
816}
817
833{
834 SetRelativeMouseTransform(callback.wrapper, callback.data);
835}
836
837#endif // SDL_VERSION_ATLEAST(3, 4, 0)
838
839inline void Window::SetRelativeMouseMode(bool enabled)
840{
841 CheckError(SDL_SetWindowRelativeMouseMode(m_resource, enabled));
842}
843
845{
846 return SDL_GetWindowRelativeMouseMode(m_resource);
847}
848
893inline void CaptureMouse(bool enabled)
894{
895 CheckError(SDL_CaptureMouse(enabled));
896}
897
940inline Cursor CreateCursor(const Uint8* data,
941 const Uint8* mask,
942 const PointRaw& size,
943 const PointRaw& hot)
944{
945 return Cursor(data, mask, size, hot);
946}
947
979{
980 return Cursor(surface, hot);
981}
982
983#if SDL_VERSION_ATLEAST(3, 4, 0)
984
1030 int frame_count,
1031 int hot_x,
1032 int hot_y)
1033{
1034 return CheckError(
1035 SDL_CreateAnimatedCursor(frames, frame_count, hot_x, hot_y));
1036}
1037
1038#endif // SDL_VERSION_ATLEAST(3, 4, 0)
1039
1054
1072inline void SetCursor(CursorParam cursor) { CheckError(SDL_SetCursor(cursor)); }
1073
1074inline void Cursor::Set() { SDL::SetCursor(m_resource); }
1075
1090inline CursorRef GetCursor() { return {SDL_GetCursor()}; }
1091
1107{
1108 return {CheckError(SDL_GetDefaultCursor())};
1109}
1110
1128inline void DestroyCursor(CursorRaw cursor) { SDL_DestroyCursor(cursor); }
1129
1131
1144inline void ShowCursor() { CheckError(SDL_ShowCursor()); }
1145
1158inline void HideCursor() { CheckError(SDL_HideCursor()); }
1159
1173inline bool CursorVisible() { return SDL_CursorVisible(); }
1174
1176
1177} // namespace SDL
1178
1179#endif /* SDL3PP_MOUSE_H_ */
The structure used to identify an SDL cursor.
Definition: SDL3pp_mouse.h:182
constexpr Cursor & operator=(Cursor &&other) noexcept
Assignment operator.
Definition: SDL3pp_mouse.h:326
constexpr CursorRaw get() const noexcept
Retrieves underlying CursorRaw.
Definition: SDL3pp_mouse.h:338
Cursor(SystemCursor id)
Create a system cursor.
Definition: SDL3pp_mouse.h:317
constexpr Cursor(Cursor &&other) noexcept
Move constructor.
Definition: SDL3pp_mouse.h:208
constexpr CursorRaw release() noexcept
Retrieves underlying CursorRaw and clear this.
Definition: SDL3pp_mouse.h:341
constexpr auto operator<=>(const Cursor &other) const noexcept=default
Comparison.
Cursor(SurfaceParam surface, const PointRaw &hot)
Create a color cursor.
Definition: SDL3pp_mouse.h:299
constexpr Cursor & operator=(const Cursor &other) noexcept=default
Assignment operator.
constexpr Cursor(const CursorRaw resource) noexcept
Constructs from CursorParam.
Definition: SDL3pp_mouse.h:199
constexpr Cursor(const Cursor &other)=delete
Copy constructor.
Cursor(const Uint8 *data, const Uint8 *mask, const PointRaw &size, const PointRaw &hot)
Create a cursor using the specified bitmap data and mask (in MSB format).
Definition: SDL3pp_mouse.h:259
constexpr Cursor(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_mouse.h:187
~Cursor()
Destructor.
Definition: SDL3pp_mouse.h:323
Base class for SDL memory allocated array wrap.
Definition: SDL3pp_ownPtr.h:44
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
Cursor CreateCursor(const Uint8 *data, const Uint8 *mask, const PointRaw &size, const PointRaw &hot)
Create a cursor using the specified bitmap data and mask (in MSB format).
Definition: SDL3pp_mouse.h:940
constexpr SystemCursor SYSTEM_CURSOR_MOVE
Four pointed arrow pointing north, south, east, and west.
Definition: SDL3pp_mouse.h:120
bool GetRelativeMouseMode() const
Query whether relative mouse mode is enabled for a window.
Definition: SDL3pp_mouse.h:844
void SetRelativeMouseTransform(MouseMotionTransformCallback callback, void *userdata)
Set a user-defined function by which to transform relative mouse inputs.
Definition: SDL3pp_mouse.h:812
constexpr SystemCursor SYSTEM_CURSOR_POINTER
Pointer that indicates a link. Usually a pointing hand.
Definition: SDL3pp_mouse.h:127
constexpr MouseWheelDirection MOUSEWHEEL_NORMAL
The scroll direction is normal.
Definition: SDL3pp_mouse.h:439
CursorRef CreateAnimatedCursor(CursorFrameInfo *frames, int frame_count, int hot_x, int hot_y)
Create an animated color cursor.
Definition: SDL3pp_mouse.h:1029
SDL_SystemCursor SystemCursor
Cursor types for Cursor.Cursor().
Definition: SDL3pp_mouse.h:88
constexpr SystemCursor SYSTEM_CURSOR_SE_RESIZE
Window resize bottom-right.
Definition: SDL3pp_mouse.h:144
constexpr MouseButton BUTTON_RIGHT
Right button.
Definition: SDL3pp_mouse.h:466
constexpr SystemCursor SYSTEM_CURSOR_W_RESIZE
Window resize left. May be EW_RESIZE.
Definition: SDL3pp_mouse.h:155
constexpr MouseButton BUTTON_MIDDLE
Middle button.
Definition: SDL3pp_mouse.h:464
CursorRef GetCursor()
Get the active cursor.
Definition: SDL3pp_mouse.h:1090
void Destroy()
Free a previously-created cursor.
Definition: SDL3pp_mouse.h:1130
void DestroyCursor(CursorRaw cursor)
Free a previously-created cursor.
Definition: SDL3pp_mouse.h:1128
constexpr SystemCursor SYSTEM_CURSOR_NESW_RESIZE
Double arrow pointing northeast and southwest.
Definition: SDL3pp_mouse.h:109
constexpr MouseWheelDirection MOUSEWHEEL_FLIPPED
The scroll direction is flipped / natural.
Definition: SDL3pp_mouse.h:442
MouseButtonFlags GetRelativeMouseState(float *x, float *y)
Query SDL's cache for the synchronous mouse button state and accumulated mouse delta since last call.
Definition: SDL3pp_mouse.h:760
constexpr MouseButtonFlags ButtonMask(MouseButton button)
Returns mask for button.
Definition: SDL3pp_mouse.h:500
void CaptureMouse(bool enabled)
Capture the mouse and to track input outside an SDL window.
Definition: SDL3pp_mouse.h:893
Cursor CreateSystemCursor(SystemCursor id)
Create a system cursor.
Definition: SDL3pp_mouse.h:1053
Uint32 MouseButtonFlags
A bitmask of pressed mouse buttons, as reported by GetMouseState, etc.
Definition: SDL3pp_mouse.h:487
constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE
Window resize top-left.
Definition: SDL3pp_mouse.h:133
constexpr SystemCursor SYSTEM_CURSOR_CROSSHAIR
Crosshair.
Definition: SDL3pp_mouse.h:99
void WarpMouse(const FPointRaw &p)
Move the mouse to the given position in global screen space.
Definition: SDL3pp_mouse.h:790
MouseButtonFlags GetGlobalMouseState(float *x, float *y)
Query the platform for the asynchronous mouse button state and the desktop-relative platform-cursor p...
Definition: SDL3pp_mouse.h:722
constexpr SystemCursor SYSTEM_CURSOR_NWSE_RESIZE
Double arrow pointing northwest and southeast.
Definition: SDL3pp_mouse.h:105
constexpr SystemCursor SYSTEM_CURSOR_E_RESIZE
Window resize right. May be EW_RESIZE.
Definition: SDL3pp_mouse.h:141
constexpr MouseButton BUTTON_X1
X1 button.
Definition: SDL3pp_mouse.h:468
constexpr SystemCursor SYSTEM_CURSOR_NS_RESIZE
Double arrow pointing north and south.
Definition: SDL3pp_mouse.h:116
const char * GetMouseNameForID(MouseID instance_id)
Get the name of a mouse.
Definition: SDL3pp_mouse.h:635
constexpr SystemCursor SYSTEM_CURSOR_COUNT
COUNT.
Definition: SDL3pp_mouse.h:158
SDL_MouseID MouseID
This is a unique ID for a mouse for the time it is connected to the system, and is never reused for t...
Definition: SDL3pp_mouse.h:170
constexpr MouseButtonFlags BUTTON_RMASK
RMASK.
Definition: SDL3pp_mouse.h:493
constexpr SystemCursor SYSTEM_CURSOR_NOT_ALLOWED
Not permitted. Usually a slashed circle or crossbones.
Definition: SDL3pp_mouse.h:123
void Set()
Set the active cursor.
Definition: SDL3pp_mouse.h:1074
constexpr MouseButton BUTTON_LEFT
Left button.
Definition: SDL3pp_mouse.h:462
MouseButtonFlags GetMouseState(float *x, float *y)
Query SDL's cache for the synchronous mouse button state and the window-relative SDL-cursor position.
Definition: SDL3pp_mouse.h:682
constexpr MouseButtonFlags BUTTON_LMASK
LMASK.
Definition: SDL3pp_mouse.h:489
constexpr MouseButtonFlags BUTTON_X2MASK
X2MASK.
Definition: SDL3pp_mouse.h:497
void SetRelativeMouseMode(bool enabled)
Set relative mouse mode for a window.
Definition: SDL3pp_mouse.h:839
constexpr SystemCursor SYSTEM_CURSOR_WAIT
Wait. Usually an hourglass or watch or spinning ball.
Definition: SDL3pp_mouse.h:97
Uint8 MouseButton
Represents a button index.
Definition: SDL3pp_mouse.h:460
constexpr SystemCursor SYSTEM_CURSOR_SW_RESIZE
Window resize bottom-left.
Definition: SDL3pp_mouse.h:151
void WarpMouse(const FPointRaw &p)
Move the mouse cursor to the given position within the window.
Definition: SDL3pp_mouse.h:765
void SetCursor(CursorParam cursor)
Set the active cursor.
Definition: SDL3pp_mouse.h:1072
constexpr SystemCursor SYSTEM_CURSOR_NE_RESIZE
Window resize top-right. May be NESW_RESIZE.
Definition: SDL3pp_mouse.h:138
constexpr SystemCursor SYSTEM_CURSOR_DEFAULT
Default cursor. Usually an arrow.
Definition: SDL3pp_mouse.h:90
constexpr MouseButtonFlags BUTTON_X1MASK
X1MASK.
Definition: SDL3pp_mouse.h:495
SDL_Cursor * CursorRaw
Alias to raw representation for Cursor.
Definition: SDL3pp_mouse.h:51
constexpr SystemCursor SYSTEM_CURSOR_EW_RESIZE
Double arrow pointing west and east.
Definition: SDL3pp_mouse.h:113
SDL_CursorFrameInfo CursorFrameInfo
Animated cursor frame info.
Definition: SDL3pp_mouse.h:452
CursorRef GetDefaultCursor()
Get the default cursor.
Definition: SDL3pp_mouse.h:1106
WindowRef GetMouseFocus()
Get the window which currently has mouse focus.
Definition: SDL3pp_mouse.h:649
bool CursorVisible()
Return whether the cursor is currently being shown.
Definition: SDL3pp_mouse.h:1173
constexpr MouseButtonFlags BUTTON_MMASK
MMASK.
Definition: SDL3pp_mouse.h:491
constexpr SystemCursor SYSTEM_CURSOR_N_RESIZE
Window resize top. May be NS_RESIZE.
Definition: SDL3pp_mouse.h:135
Cursor CreateColorCursor(SurfaceParam surface, const PointRaw &hot)
Create a color cursor.
Definition: SDL3pp_mouse.h:978
constexpr SystemCursor SYSTEM_CURSOR_S_RESIZE
Window resize bottom. May be NS_RESIZE.
Definition: SDL3pp_mouse.h:148
SDL_MouseWheelDirection MouseWheelDirection
Scroll direction types for the Scroll event.
Definition: SDL3pp_mouse.h:437
OwnArray< MouseID > GetMice()
Get a list of currently connected mice.
Definition: SDL3pp_mouse.h:613
constexpr SystemCursor SYSTEM_CURSOR_TEXT
Text selection. Usually an I-beam.
Definition: SDL3pp_mouse.h:93
void HideCursor()
Hide the cursor.
Definition: SDL3pp_mouse.h:1158
void(SDLCALL *)(void *userdata, Uint64 timestamp, SDL_Window *window, MouseID mouseID, float *x, float *y) MouseMotionTransformCallback
A callback used to transform mouse motion delta from raw values.
Definition: SDL3pp_mouse.h:542
constexpr MouseButton BUTTON_X2
X2 button.
Definition: SDL3pp_mouse.h:470
void ShowCursor()
Show the cursor.
Definition: SDL3pp_mouse.h:1144
constexpr SystemCursor SYSTEM_CURSOR_PROGRESS
Program is busy but still interactive. Usually it's WAIT with an arrow.
Definition: SDL3pp_mouse.h:103
bool HasMouse()
Return whether a mouse is currently connected.
Definition: SDL3pp_mouse.h:594
SDL_FPoint FPointRaw
Alias to raw representation for FPoint.
Definition: SDL3pp_rect.h:28
SDL_Point PointRaw
Alias to raw representation for Point.
Definition: SDL3pp_rect.h:22
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:341
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:371
::Uint8 Uint8
An unsigned 8-bit integer type.
Definition: SDL3pp_stdinc.h:289
Main include header for the SDL3pp library.
Safely wrap Cursor for non owning parameters.
Definition: SDL3pp_mouse.h:58
CursorRaw value
parameter's CursorRaw
Definition: SDL3pp_mouse.h:59
constexpr CursorParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_mouse.h:68
constexpr auto operator<=>(const CursorParam &other) const =default
Comparison.
constexpr CursorParam(CursorRaw value)
Constructs from CursorRaw.
Definition: SDL3pp_mouse.h:62
Semi-safe reference for Cursor.
Definition: SDL3pp_mouse.h:395
CursorRef(CursorRaw resource) noexcept
Constructs from CursorParam.
Definition: SDL3pp_mouse.h:417
~CursorRef()
Destructor.
Definition: SDL3pp_mouse.h:429
CursorRef(const CursorRef &other) noexcept
Copy constructor.
Definition: SDL3pp_mouse.h:423
CursorRef(CursorParam resource) noexcept
Constructs from CursorParam.
Definition: SDL3pp_mouse.h:405
Definition: SDL3pp_callbackWrapper.h:169
Safely wrap Surface for non owning parameters.
Definition: SDL3pp_surface.h:50
Semi-safe reference for Window.
Definition: SDL3pp_video.h:3168