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
61using SystemCursor = SDL_SystemCursor;
62
64 SDL_SYSTEM_CURSOR_DEFAULT;
65
67 SDL_SYSTEM_CURSOR_TEXT;
68
70constexpr SystemCursor SYSTEM_CURSOR_WAIT = SDL_SYSTEM_CURSOR_WAIT;
71
73 SDL_SYSTEM_CURSOR_CROSSHAIR;
74
76constexpr SystemCursor SYSTEM_CURSOR_PROGRESS = SDL_SYSTEM_CURSOR_PROGRESS;
77
79 SDL_SYSTEM_CURSOR_NWSE_RESIZE;
81
83 SDL_SYSTEM_CURSOR_NESW_RESIZE;
85
87 SDL_SYSTEM_CURSOR_EW_RESIZE;
88
90 SDL_SYSTEM_CURSOR_NS_RESIZE;
91
93constexpr SystemCursor SYSTEM_CURSOR_MOVE = SDL_SYSTEM_CURSOR_MOVE;
94
97 SDL_SYSTEM_CURSOR_NOT_ALLOWED;
98
100constexpr SystemCursor SYSTEM_CURSOR_POINTER = SDL_SYSTEM_CURSOR_POINTER;
101
106constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE = SDL_SYSTEM_CURSOR_NW_RESIZE;
107
109 SDL_SYSTEM_CURSOR_N_RESIZE;
110
112 SDL_SYSTEM_CURSOR_NE_RESIZE;
113
115 SDL_SYSTEM_CURSOR_E_RESIZE;
116
118 SDL_SYSTEM_CURSOR_SE_RESIZE;
120
122 SDL_SYSTEM_CURSOR_S_RESIZE;
123
125 SDL_SYSTEM_CURSOR_SW_RESIZE;
127
129 SDL_SYSTEM_CURSOR_W_RESIZE;
130
131constexpr SystemCursor SYSTEM_CURSOR_COUNT = SDL_SYSTEM_CURSOR_COUNT;
132
143using MouseID = SDL_MouseID;
144
155{
156 CursorRaw m_resource = nullptr;
157
158public:
160 constexpr Cursor(std::nullptr_t = nullptr) noexcept
161 : m_resource(0)
162 {
163 }
164
172 constexpr explicit Cursor(const CursorRaw resource) noexcept
173 : m_resource(resource)
174 {
175 }
176
177protected:
179 constexpr Cursor(const Cursor& other) noexcept = default;
180
181public:
183 constexpr Cursor(Cursor&& other) noexcept
184 : Cursor(other.release())
185 {
186 }
187
188 constexpr Cursor(const CursorRef& other) = delete;
189
190 constexpr Cursor(CursorRef&& other) = delete;
191
234 Cursor(const Uint8* data,
235 const Uint8* mask,
236 const PointRaw& size,
237 const PointRaw& hot);
238
270 Cursor(SurfaceRef surface, const PointRaw& hot);
271
286
288 ~Cursor() { SDL_DestroyCursor(m_resource); }
289
291 constexpr Cursor& operator=(Cursor&& other) noexcept
292 {
293 std::swap(m_resource, other.m_resource);
294 return *this;
295 }
296
297protected:
299 constexpr Cursor& operator=(const Cursor& other) noexcept = default;
300
301public:
303 constexpr CursorRaw get() const noexcept { return m_resource; }
304
306 constexpr CursorRaw release() noexcept
307 {
308 auto r = m_resource;
309 m_resource = nullptr;
310 return r;
311 }
312
314 constexpr auto operator<=>(const Cursor& other) const noexcept = default;
315
317 constexpr explicit operator bool() const noexcept { return !!m_resource; }
318
334 void Destroy();
335
352 void Set();
353};
354
361{
362 using Cursor::Cursor;
363
371 CursorRef(CursorRaw resource) noexcept
372 : Cursor(resource)
373 {
374 }
375
383 constexpr CursorRef(const Cursor& resource) noexcept
384 : Cursor(resource.get())
385 {
386 }
387
389 constexpr CursorRef(const CursorRef& other) noexcept
390 : Cursor(other.get())
391 {
392 }
393
395 constexpr CursorRef(CursorRef&& other) noexcept
396 : Cursor(other.release())
397 {
398 }
399
402
404 constexpr CursorRef& operator=(CursorRef other) noexcept
405 {
406 std::swap(*this, other);
407 return *this;
408 }
409
411 constexpr operator CursorRaw() const noexcept { return get(); }
412};
413
419using MouseWheelDirection = SDL_MouseWheelDirection;
420
422 SDL_MOUSEWHEEL_NORMAL;
423
425 SDL_MOUSEWHEEL_FLIPPED;
426
427#if SDL_VERSION_ATLEAST(3, 4, 0)
428
434using CursorFrameInfo = SDL_CursorFrameInfo;
435
436#endif // SDL_VERSION_ATLEAST(3, 4, 0)
437
443
444constexpr MouseButton BUTTON_LEFT = SDL_BUTTON_LEFT;
445
446constexpr MouseButton BUTTON_MIDDLE = SDL_BUTTON_MIDDLE;
447
448constexpr MouseButton BUTTON_RIGHT = SDL_BUTTON_RIGHT;
449
450constexpr MouseButton BUTTON_X1 = SDL_BUTTON_X1;
451
452constexpr MouseButton BUTTON_X2 = SDL_BUTTON_X2;
453
470
471constexpr MouseButtonFlags BUTTON_LMASK = SDL_BUTTON_LMASK;
472
473constexpr MouseButtonFlags BUTTON_MMASK = SDL_BUTTON_MMASK;
474
475constexpr MouseButtonFlags BUTTON_RMASK = SDL_BUTTON_RMASK;
476
477constexpr MouseButtonFlags BUTTON_X1MASK = SDL_BUTTON_X1MASK;
478
479constexpr MouseButtonFlags BUTTON_X2MASK = SDL_BUTTON_X2MASK;
480
483{
484 return SDL_BUTTON_MASK(button);
485}
486
487#if SDL_VERSION_ATLEAST(3, 4, 0)
488
519using MouseMotionTransformCallback = void(SDLCALL*)(void* userdata,
520 Uint64 timestamp,
521 WindowRaw window,
522 MouseID mouseID,
523 float* x,
524 float* y);
525
558 WindowRaw window,
559 MouseID mouseID,
560 float* x,
561 float* y)>;
562
563#endif // SDL_VERSION_ATLEAST(3, 4, 0)
564
576inline bool HasMouse() { return SDL_HasMouse(); }
577
596{
597 int count;
598 auto data = CheckError(SDL_GetMice(&count));
599 return OwnArray<MouseID>{data, size_t(count)};
600}
601
617inline const char* GetMouseNameForID(MouseID instance_id)
618{
619 return SDL_GetMouseNameForID(instance_id);
620}
621
631inline WindowRef GetMouseFocus() { return {SDL_GetMouseFocus()}; }
632
664inline MouseButtonFlags GetMouseState(float* x, float* y)
665{
666 return SDL_GetMouseState(x, y);
667}
668
704inline MouseButtonFlags GetGlobalMouseState(float* x, float* y)
705{
706 return SDL_GetGlobalMouseState(x, y);
707}
708
742inline MouseButtonFlags GetRelativeMouseState(float* x, float* y)
743{
744 return SDL_GetRelativeMouseState(x, y);
745}
746
747inline void Window::WarpMouse(const FPointRaw& p)
748{
749 SDL_WarpMouseInWindow(m_resource, p.x, p.y);
750}
751
772inline void WarpMouse(const FPointRaw& p)
773{
774 CheckError(SDL_WarpMouseGlobal(p.x, p.y));
775}
776
777#if SDL_VERSION_ATLEAST(3, 4, 0)
778
795 void* userdata)
796{
797 CheckError(SDL_SetRelativeMouseTransform(callback, userdata));
798}
799
815{
816 SetRelativeMouseTransform(callback.wrapper, callback.data);
817}
818
819#endif // SDL_VERSION_ATLEAST(3, 4, 0)
820
821inline void Window::SetRelativeMouseMode(bool enabled)
822{
823 CheckError(SDL_SetWindowRelativeMouseMode(m_resource, enabled));
824}
825
827{
828 return SDL_GetWindowRelativeMouseMode(m_resource);
829}
830
875inline void CaptureMouse(bool enabled)
876{
877 CheckError(SDL_CaptureMouse(enabled));
878}
879
922inline Cursor CreateCursor(const Uint8* data,
923 const Uint8* mask,
924 const PointRaw& size,
925 const PointRaw& hot)
926{
927 return Cursor(data, mask, size, hot);
928}
929
930inline Cursor::Cursor(const Uint8* data,
931 const Uint8* mask,
932 const PointRaw& size,
933 const PointRaw& hot)
934 : m_resource(
935 CheckError(SDL_CreateCursor(data, mask, size.x, size.y, hot.x, hot.y)))
936{
937}
938
939inline Cursor::Cursor(SurfaceRef surface, const PointRaw& hot)
940 : m_resource(CheckError(SDL_CreateColorCursor(surface, hot.x, hot.y)))
941{
942}
943
945 : m_resource(CheckError(SDL_CreateSystemCursor(id)))
946{
947}
948
979inline Cursor CreateColorCursor(SurfaceRef surface, const PointRaw& hot)
980{
981 return Cursor(surface, hot);
982}
983
984#if SDL_VERSION_ATLEAST(3, 4, 0)
985
1031 int frame_count,
1032 int hot_x,
1033 int hot_y)
1034{
1035 return CheckError(
1036 SDL_CreateAnimatedCursor(frames, frame_count, hot_x, hot_y));
1037}
1038
1039#endif // SDL_VERSION_ATLEAST(3, 4, 0)
1040
1055
1073inline void SetCursor(CursorRef cursor) { CheckError(SDL_SetCursor(cursor)); }
1074
1075inline void Cursor::Set() { SDL::SetCursor(m_resource); }
1076
1091inline CursorRef GetCursor() { return {SDL_GetCursor()}; }
1092
1108{
1109 return {CheckError(SDL_GetDefaultCursor())};
1110}
1111
1129inline void DestroyCursor(CursorRaw cursor) { SDL_DestroyCursor(cursor); }
1130
1132
1145inline void ShowCursor() { CheckError(SDL_ShowCursor()); }
1146
1159inline void HideCursor() { CheckError(SDL_HideCursor()); }
1160
1174inline bool CursorVisible() { return SDL_CursorVisible(); }
1175
1177
1178} // namespace SDL
1179
1180#endif /* SDL3PP_MOUSE_H_ */
The structure used to identify an SDL cursor.
Definition: SDL3pp_mouse.h:155
constexpr Cursor & operator=(Cursor &&other) noexcept
Assignment operator.
Definition: SDL3pp_mouse.h:291
constexpr CursorRaw get() const noexcept
Retrieves underlying CursorRaw.
Definition: SDL3pp_mouse.h:303
constexpr Cursor(Cursor &&other) noexcept
Move constructor.
Definition: SDL3pp_mouse.h:183
constexpr CursorRaw release() noexcept
Retrieves underlying CursorRaw and clear this.
Definition: SDL3pp_mouse.h:306
constexpr auto operator<=>(const Cursor &other) const noexcept=default
Comparison.
constexpr Cursor(const Cursor &other) noexcept=default
Copy constructor.
constexpr Cursor & operator=(const Cursor &other) noexcept=default
Assignment operator.
constexpr Cursor(const CursorRaw resource) noexcept
Constructs from CursorRef.
Definition: SDL3pp_mouse.h:172
constexpr Cursor(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_mouse.h:160
~Cursor()
Destructor.
Definition: SDL3pp_mouse.h:288
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:922
constexpr SystemCursor SYSTEM_CURSOR_MOVE
Four pointed arrow pointing north, south, east, and west.
Definition: SDL3pp_mouse.h:93
bool GetRelativeMouseMode() const
Query whether relative mouse mode is enabled for a window.
Definition: SDL3pp_mouse.h:826
void SetRelativeMouseTransform(MouseMotionTransformCallback callback, void *userdata)
Set a user-defined function by which to transform relative mouse inputs.
Definition: SDL3pp_mouse.h:794
constexpr SystemCursor SYSTEM_CURSOR_POINTER
Pointer that indicates a link. Usually a pointing hand.
Definition: SDL3pp_mouse.h:100
constexpr MouseWheelDirection MOUSEWHEEL_NORMAL
The scroll direction is normal.
Definition: SDL3pp_mouse.h:421
SDL_SystemCursor SystemCursor
Cursor types for Cursor.Cursor().
Definition: SDL3pp_mouse.h:61
constexpr SystemCursor SYSTEM_CURSOR_SE_RESIZE
Window resize bottom-right.
Definition: SDL3pp_mouse.h:117
constexpr MouseButton BUTTON_RIGHT
Right button.
Definition: SDL3pp_mouse.h:448
constexpr SystemCursor SYSTEM_CURSOR_W_RESIZE
Window resize left. May be EW_RESIZE.
Definition: SDL3pp_mouse.h:128
constexpr MouseButton BUTTON_MIDDLE
Middle button.
Definition: SDL3pp_mouse.h:446
CursorRef GetCursor()
Get the active cursor.
Definition: SDL3pp_mouse.h:1091
void Destroy()
Free a previously-created cursor.
Definition: SDL3pp_mouse.h:1131
void DestroyCursor(CursorRaw cursor)
Free a previously-created cursor.
Definition: SDL3pp_mouse.h:1129
constexpr SystemCursor SYSTEM_CURSOR_NESW_RESIZE
Double arrow pointing northeast and southwest.
Definition: SDL3pp_mouse.h:82
constexpr MouseWheelDirection MOUSEWHEEL_FLIPPED
The scroll direction is flipped / natural.
Definition: SDL3pp_mouse.h:424
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:742
constexpr MouseButtonFlags ButtonMask(MouseButton button)
Returns mask for button.
Definition: SDL3pp_mouse.h:482
void CaptureMouse(bool enabled)
Capture the mouse and to track input outside an SDL window.
Definition: SDL3pp_mouse.h:875
Cursor CreateSystemCursor(SystemCursor id)
Create a system cursor.
Definition: SDL3pp_mouse.h:1054
Uint32 MouseButtonFlags
A bitmask of pressed mouse buttons, as reported by GetMouseState, etc.
Definition: SDL3pp_mouse.h:469
constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE
Window resize top-left.
Definition: SDL3pp_mouse.h:106
constexpr SystemCursor SYSTEM_CURSOR_CROSSHAIR
Crosshair.
Definition: SDL3pp_mouse.h:72
void WarpMouse(const FPointRaw &p)
Move the mouse to the given position in global screen space.
Definition: SDL3pp_mouse.h:772
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:704
constexpr SystemCursor SYSTEM_CURSOR_NWSE_RESIZE
Double arrow pointing northwest and southeast.
Definition: SDL3pp_mouse.h:78
constexpr SystemCursor SYSTEM_CURSOR_E_RESIZE
Window resize right. May be EW_RESIZE.
Definition: SDL3pp_mouse.h:114
constexpr MouseButton BUTTON_X1
X1 button.
Definition: SDL3pp_mouse.h:450
constexpr SystemCursor SYSTEM_CURSOR_NS_RESIZE
Double arrow pointing north and south.
Definition: SDL3pp_mouse.h:89
const char * GetMouseNameForID(MouseID instance_id)
Get the name of a mouse.
Definition: SDL3pp_mouse.h:617
constexpr SystemCursor SYSTEM_CURSOR_COUNT
COUNT.
Definition: SDL3pp_mouse.h:131
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:143
constexpr MouseButtonFlags BUTTON_RMASK
RMASK.
Definition: SDL3pp_mouse.h:475
constexpr SystemCursor SYSTEM_CURSOR_NOT_ALLOWED
Not permitted. Usually a slashed circle or crossbones.
Definition: SDL3pp_mouse.h:96
void Set()
Set the active cursor.
Definition: SDL3pp_mouse.h:1075
constexpr MouseButton BUTTON_LEFT
Left button.
Definition: SDL3pp_mouse.h:444
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:664
constexpr MouseButtonFlags BUTTON_LMASK
LMASK.
Definition: SDL3pp_mouse.h:471
constexpr MouseButtonFlags BUTTON_X2MASK
X2MASK.
Definition: SDL3pp_mouse.h:479
void SetRelativeMouseMode(bool enabled)
Set relative mouse mode for a window.
Definition: SDL3pp_mouse.h:821
constexpr SystemCursor SYSTEM_CURSOR_WAIT
Wait. Usually an hourglass or watch or spinning ball.
Definition: SDL3pp_mouse.h:70
Uint8 MouseButton
Represents a button index.
Definition: SDL3pp_mouse.h:442
constexpr SystemCursor SYSTEM_CURSOR_SW_RESIZE
Window resize bottom-left.
Definition: SDL3pp_mouse.h:124
void WarpMouse(const FPointRaw &p)
Move the mouse cursor to the given position within the window.
Definition: SDL3pp_mouse.h:747
constexpr SystemCursor SYSTEM_CURSOR_NE_RESIZE
Window resize top-right. May be NESW_RESIZE.
Definition: SDL3pp_mouse.h:111
void(SDLCALL *)(void *userdata, Uint64 timestamp, WindowRaw window, MouseID mouseID, float *x, float *y) MouseMotionTransformCallback
A callback used to transform mouse motion delta from raw values.
Definition: SDL3pp_mouse.h:524
constexpr SystemCursor SYSTEM_CURSOR_DEFAULT
Default cursor. Usually an arrow.
Definition: SDL3pp_mouse.h:63
constexpr MouseButtonFlags BUTTON_X1MASK
X1MASK.
Definition: SDL3pp_mouse.h:477
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:86
SDL_CursorFrameInfo CursorFrameInfo
Animated cursor frame info.
Definition: SDL3pp_mouse.h:434
CursorRef GetDefaultCursor()
Get the default cursor.
Definition: SDL3pp_mouse.h:1107
WindowRef GetMouseFocus()
Get the window which currently has mouse focus.
Definition: SDL3pp_mouse.h:631
bool CursorVisible()
Return whether the cursor is currently being shown.
Definition: SDL3pp_mouse.h:1174
Cursor CreateColorCursor(SurfaceRef surface, const PointRaw &hot)
Create a color cursor.
Definition: SDL3pp_mouse.h:979
constexpr MouseButtonFlags BUTTON_MMASK
MMASK.
Definition: SDL3pp_mouse.h:473
constexpr SystemCursor SYSTEM_CURSOR_N_RESIZE
Window resize top. May be NS_RESIZE.
Definition: SDL3pp_mouse.h:108
constexpr SystemCursor SYSTEM_CURSOR_S_RESIZE
Window resize bottom. May be NS_RESIZE.
Definition: SDL3pp_mouse.h:121
SDL_MouseWheelDirection MouseWheelDirection
Scroll direction types for the Scroll event.
Definition: SDL3pp_mouse.h:419
void SetCursor(CursorRef cursor)
Set the active cursor.
Definition: SDL3pp_mouse.h:1073
OwnArray< MouseID > GetMice()
Get a list of currently connected mice.
Definition: SDL3pp_mouse.h:595
constexpr SystemCursor SYSTEM_CURSOR_TEXT
Text selection. Usually an I-beam.
Definition: SDL3pp_mouse.h:66
void HideCursor()
Hide the cursor.
Definition: SDL3pp_mouse.h:1159
constexpr MouseButton BUTTON_X2
X2 button.
Definition: SDL3pp_mouse.h:452
void ShowCursor()
Show the cursor.
Definition: SDL3pp_mouse.h:1145
constexpr SystemCursor SYSTEM_CURSOR_PROGRESS
Program is busy but still interactive. Usually it's WAIT with an arrow.
Definition: SDL3pp_mouse.h:76
bool HasMouse()
Return whether a mouse is currently connected.
Definition: SDL3pp_mouse.h:576
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
Cursor CreateAnimatedCursor(AnimationRef anim, const PointRaw &hotspot)
Create an animated cursor from an animation.
Definition: SDL3pp_image.h:3621
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:280
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:310
::Uint8 Uint8
An unsigned 8-bit integer type.
Definition: SDL3pp_stdinc.h:228
SDL_Window * WindowRaw
Alias to raw representation for Window.
Definition: SDL3pp_video.h:47
Main include header for the SDL3pp library.
Reference for Cursor.
Definition: SDL3pp_mouse.h:361
constexpr CursorRef(CursorRef &&other) noexcept
Move constructor.
Definition: SDL3pp_mouse.h:395
CursorRef(CursorRaw resource) noexcept
Constructs from raw Cursor.
Definition: SDL3pp_mouse.h:371
constexpr CursorRef(const CursorRef &other) noexcept
Copy constructor.
Definition: SDL3pp_mouse.h:389
constexpr CursorRef & operator=(CursorRef other) noexcept
Assignment operator.
Definition: SDL3pp_mouse.h:404
~CursorRef()
Destructor.
Definition: SDL3pp_mouse.h:401
constexpr CursorRef(const Cursor &resource) noexcept
Constructs from Cursor.
Definition: SDL3pp_mouse.h:383
Definition: SDL3pp_callbackWrapper.h:169
Reference for Surface.
Definition: SDL3pp_surface.h:1963
Reference for Window.
Definition: SDL3pp_video.h:3105