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
46
47// Forward decl
48struct Cursor;
49
51using CursorRaw = SDL_Cursor*;
52
59
65using SystemCursor = SDL_SystemCursor;
66
68 SDL_SYSTEM_CURSOR_DEFAULT;
69
71 SDL_SYSTEM_CURSOR_TEXT;
72
74constexpr SystemCursor SYSTEM_CURSOR_WAIT = SDL_SYSTEM_CURSOR_WAIT;
75
77 SDL_SYSTEM_CURSOR_CROSSHAIR;
78
80constexpr SystemCursor SYSTEM_CURSOR_PROGRESS = SDL_SYSTEM_CURSOR_PROGRESS;
81
83 SDL_SYSTEM_CURSOR_NWSE_RESIZE;
85
87 SDL_SYSTEM_CURSOR_NESW_RESIZE;
89
91 SDL_SYSTEM_CURSOR_EW_RESIZE;
92
94 SDL_SYSTEM_CURSOR_NS_RESIZE;
95
97constexpr SystemCursor SYSTEM_CURSOR_MOVE = SDL_SYSTEM_CURSOR_MOVE;
98
101 SDL_SYSTEM_CURSOR_NOT_ALLOWED;
102
104constexpr SystemCursor SYSTEM_CURSOR_POINTER = SDL_SYSTEM_CURSOR_POINTER;
105
110constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE = SDL_SYSTEM_CURSOR_NW_RESIZE;
111
113 SDL_SYSTEM_CURSOR_N_RESIZE;
114
116 SDL_SYSTEM_CURSOR_NE_RESIZE;
117
119 SDL_SYSTEM_CURSOR_E_RESIZE;
120
122 SDL_SYSTEM_CURSOR_SE_RESIZE;
124
126 SDL_SYSTEM_CURSOR_S_RESIZE;
127
129 SDL_SYSTEM_CURSOR_SW_RESIZE;
131
133 SDL_SYSTEM_CURSOR_W_RESIZE;
134
135constexpr SystemCursor SYSTEM_CURSOR_COUNT = SDL_SYSTEM_CURSOR_COUNT;
136
147using MouseID = SDL_MouseID;
148
158struct Cursor : ResourceBase<CursorRaw>
159{
161
169 constexpr explicit Cursor(CursorRaw resource) noexcept
170 : ResourceBase(resource)
171 {
172 }
173
175 constexpr Cursor(const Cursor& other) = delete;
176
178 constexpr Cursor(Cursor&& other) noexcept
179 : Cursor(other.release())
180 {
181 }
182
183 constexpr Cursor(const CursorRef& other) = delete;
184
185 constexpr Cursor(CursorRef&& other) = delete;
186
229 Cursor(const Uint8* data,
230 const Uint8* mask,
231 const PointRaw& size,
232 const PointRaw& hot);
233
265 Cursor(SurfaceRef surface, const PointRaw& hot);
266
281
283 ~Cursor() { SDL_DestroyCursor(get()); }
284
286 constexpr Cursor& operator=(Cursor&& other) noexcept
287 {
288 swap(*this, other);
289 return *this;
290 }
291
293 Cursor& operator=(const Cursor& other) = delete;
294
310 void Destroy();
311
328 void Set();
329};
330
336using MouseWheelDirection = SDL_MouseWheelDirection;
337
339 SDL_MOUSEWHEEL_NORMAL;
340
342 SDL_MOUSEWHEEL_FLIPPED;
343
344#if SDL_VERSION_ATLEAST(3, 4, 0)
345
351using CursorFrameInfo = SDL_CursorFrameInfo;
352
353#endif // SDL_VERSION_ATLEAST(3, 4, 0)
354
360
361constexpr MouseButton BUTTON_LEFT = SDL_BUTTON_LEFT;
362
363constexpr MouseButton BUTTON_MIDDLE = SDL_BUTTON_MIDDLE;
364
365constexpr MouseButton BUTTON_RIGHT = SDL_BUTTON_RIGHT;
366
367constexpr MouseButton BUTTON_X1 = SDL_BUTTON_X1;
368
369constexpr MouseButton BUTTON_X2 = SDL_BUTTON_X2;
370
387
388constexpr MouseButtonFlags BUTTON_LMASK = SDL_BUTTON_LMASK;
389
390constexpr MouseButtonFlags BUTTON_MMASK = SDL_BUTTON_MMASK;
391
392constexpr MouseButtonFlags BUTTON_RMASK = SDL_BUTTON_RMASK;
393
394constexpr MouseButtonFlags BUTTON_X1MASK = SDL_BUTTON_X1MASK;
395
396constexpr MouseButtonFlags BUTTON_X2MASK = SDL_BUTTON_X2MASK;
397
400{
401 return SDL_BUTTON_MASK(button);
402}
403
404#if SDL_VERSION_ATLEAST(3, 4, 0)
405
436using MouseMotionTransformCallback = void(SDLCALL*)(void* userdata,
437 Uint64 timestamp,
438 WindowRaw window,
439 MouseID mouseID,
440 float* x,
441 float* y);
442
475 WindowRaw window,
476 MouseID mouseID,
477 float* x,
478 float* y)>;
479
480#endif // SDL_VERSION_ATLEAST(3, 4, 0)
481
493inline bool HasMouse() { return SDL_HasMouse(); }
494
513{
514 int count;
515 auto data = CheckError(SDL_GetMice(&count));
516 return OwnArray<MouseID>{data, size_t(count)};
517}
518
534inline const char* GetMouseNameForID(MouseID instance_id)
535{
536 return CheckError(SDL_GetMouseNameForID(instance_id));
537}
538
548inline WindowRef GetMouseFocus() { return {SDL_GetMouseFocus()}; }
549
581inline MouseButtonFlags GetMouseState(float* x, float* y)
582{
583 return SDL_GetMouseState(x, y);
584}
585
621inline MouseButtonFlags GetGlobalMouseState(float* x, float* y)
622{
623 return SDL_GetGlobalMouseState(x, y);
624}
625
659inline MouseButtonFlags GetRelativeMouseState(float* x, float* y)
660{
661 return SDL_GetRelativeMouseState(x, y);
662}
663
664inline void Window::WarpMouse(const FPointRaw& p)
665{
666 SDL_WarpMouseInWindow(get(), p.x, p.y);
667}
668
689inline void WarpMouse(const FPointRaw& p)
690{
691 CheckError(SDL_WarpMouseGlobal(p.x, p.y));
692}
693
694#if SDL_VERSION_ATLEAST(3, 4, 0)
695
712 void* userdata)
713{
714 CheckError(SDL_SetRelativeMouseTransform(callback, userdata));
715}
716
732{
733 SetRelativeMouseTransform(callback.wrapper, callback.data);
734}
735
736#endif // SDL_VERSION_ATLEAST(3, 4, 0)
737
738inline void Window::SetRelativeMouseMode(bool enabled)
739{
740 CheckError(SDL_SetWindowRelativeMouseMode(get(), enabled));
741}
742
744{
745 return SDL_GetWindowRelativeMouseMode(get());
746}
747
792inline void CaptureMouse(bool enabled)
793{
794 CheckError(SDL_CaptureMouse(enabled));
795}
796
839inline Cursor CreateCursor(const Uint8* data,
840 const Uint8* mask,
841 const PointRaw& size,
842 const PointRaw& hot)
843{
844 return Cursor(data, mask, size, hot);
845}
846
847inline Cursor::Cursor(const Uint8* data,
848 const Uint8* mask,
849 const PointRaw& size,
850 const PointRaw& hot)
851 : Cursor(
852 CheckError(SDL_CreateCursor(data, mask, size.x, size.y, hot.x, hot.y)))
853{
854}
855
856inline Cursor::Cursor(SurfaceRef surface, const PointRaw& hot)
857 : Cursor(CheckError(SDL_CreateColorCursor(surface, hot.x, hot.y)))
858{
859}
860
862 : Cursor(CheckError(SDL_CreateSystemCursor(id)))
863{
864}
865
896inline Cursor CreateColorCursor(SurfaceRef surface, const PointRaw& hot)
897{
898 return Cursor(surface, hot);
899}
900
901#if SDL_VERSION_ATLEAST(3, 4, 0)
902
948 int frame_count,
949 int hot_x,
950 int hot_y)
951{
952 return CheckError(
953 SDL_CreateAnimatedCursor(frames, frame_count, hot_x, hot_y));
954}
955
956#endif // SDL_VERSION_ATLEAST(3, 4, 0)
957
972
990inline void SetCursor(CursorRef cursor) { CheckError(SDL_SetCursor(cursor)); }
991
992inline void Cursor::Set() { SDL::SetCursor(get()); }
993
1008inline CursorRef GetCursor() { return {SDL_GetCursor()}; }
1009
1025{
1026 return {CheckError(SDL_GetDefaultCursor())};
1027}
1028
1046inline void DestroyCursor(CursorRaw cursor) { SDL_DestroyCursor(cursor); }
1047
1049
1062inline void ShowCursor() { CheckError(SDL_ShowCursor()); }
1063
1076inline void HideCursor() { CheckError(SDL_HideCursor()); }
1077
1091inline bool CursorVisible() { return SDL_CursorVisible(); }
1092
1094
1095} // namespace SDL
1096
1097#endif /* SDL3PP_MOUSE_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:44
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:53
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:56
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
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:147
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:839
constexpr SystemCursor SYSTEM_CURSOR_MOVE
Four pointed arrow pointing north, south, east, and west.
Definition SDL3pp_mouse.h:97
bool GetRelativeMouseMode() const
Query whether relative mouse mode is enabled for a window.
Definition SDL3pp_mouse.h:743
void SetRelativeMouseTransform(MouseMotionTransformCallback callback, void *userdata)
Set a user-defined function by which to transform relative mouse inputs.
Definition SDL3pp_mouse.h:711
constexpr SystemCursor SYSTEM_CURSOR_POINTER
Pointer that indicates a link. Usually a pointing hand.
Definition SDL3pp_mouse.h:104
constexpr MouseWheelDirection MOUSEWHEEL_NORMAL
The scroll direction is normal.
Definition SDL3pp_mouse.h:338
constexpr SystemCursor SYSTEM_CURSOR_SE_RESIZE
Window resize bottom-right.
Definition SDL3pp_mouse.h:121
constexpr MouseButton BUTTON_RIGHT
Right button.
Definition SDL3pp_mouse.h:365
constexpr SystemCursor SYSTEM_CURSOR_W_RESIZE
Window resize left. May be EW_RESIZE.
Definition SDL3pp_mouse.h:132
constexpr MouseButton BUTTON_MIDDLE
Middle button.
Definition SDL3pp_mouse.h:363
CursorRef GetCursor()
Get the active cursor.
Definition SDL3pp_mouse.h:1008
void Destroy()
Free a previously-created cursor.
Definition SDL3pp_mouse.h:1048
void DestroyCursor(CursorRaw cursor)
Free a previously-created cursor.
Definition SDL3pp_mouse.h:1046
constexpr SystemCursor SYSTEM_CURSOR_NESW_RESIZE
Double arrow pointing northeast and southwest.
Definition SDL3pp_mouse.h:86
constexpr MouseWheelDirection MOUSEWHEEL_FLIPPED
The scroll direction is flipped / natural.
Definition SDL3pp_mouse.h:341
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:659
constexpr MouseButtonFlags ButtonMask(MouseButton button)
Returns mask for button.
Definition SDL3pp_mouse.h:399
void CaptureMouse(bool enabled)
Capture the mouse and to track input outside an SDL window.
Definition SDL3pp_mouse.h:792
Cursor CreateSystemCursor(SystemCursor id)
Create a system cursor.
Definition SDL3pp_mouse.h:971
constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE
Window resize top-left.
Definition SDL3pp_mouse.h:110
constexpr SystemCursor SYSTEM_CURSOR_CROSSHAIR
Crosshair.
Definition SDL3pp_mouse.h:76
Uint32 MouseButtonFlags
A bitmask of pressed mouse buttons, as reported by GetMouseState, etc.
Definition SDL3pp_mouse.h:386
void WarpMouse(const FPointRaw &p)
Move the mouse to the given position in global screen space.
Definition SDL3pp_mouse.h:689
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:621
constexpr SystemCursor SYSTEM_CURSOR_NWSE_RESIZE
Double arrow pointing northwest and southeast.
Definition SDL3pp_mouse.h:82
constexpr SystemCursor SYSTEM_CURSOR_E_RESIZE
Window resize right. May be EW_RESIZE.
Definition SDL3pp_mouse.h:118
constexpr MouseButton BUTTON_X1
X1 button.
Definition SDL3pp_mouse.h:367
constexpr SystemCursor SYSTEM_CURSOR_NS_RESIZE
Double arrow pointing north and south.
Definition SDL3pp_mouse.h:93
const char * GetMouseNameForID(MouseID instance_id)
Get the name of a mouse.
Definition SDL3pp_mouse.h:534
constexpr SystemCursor SYSTEM_CURSOR_COUNT
COUNT.
Definition SDL3pp_mouse.h:135
constexpr MouseButtonFlags BUTTON_RMASK
RMASK.
Definition SDL3pp_mouse.h:392
constexpr SystemCursor SYSTEM_CURSOR_NOT_ALLOWED
Not permitted. Usually a slashed circle or crossbones.
Definition SDL3pp_mouse.h:100
void Set()
Set the active cursor.
Definition SDL3pp_mouse.h:992
constexpr MouseButton BUTTON_LEFT
Left button.
Definition SDL3pp_mouse.h:361
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:581
constexpr MouseButtonFlags BUTTON_LMASK
LMASK.
Definition SDL3pp_mouse.h:388
SDL_Cursor * CursorRaw
Alias to raw representation for Cursor.
Definition SDL3pp_mouse.h:51
ResourceRef< Cursor > CursorRef
Reference for Cursor.
Definition SDL3pp_mouse.h:58
constexpr MouseButtonFlags BUTTON_X2MASK
X2MASK.
Definition SDL3pp_mouse.h:396
void SetRelativeMouseMode(bool enabled)
Set relative mouse mode for a window.
Definition SDL3pp_mouse.h:738
constexpr SystemCursor SYSTEM_CURSOR_WAIT
Wait. Usually an hourglass or watch or spinning ball.
Definition SDL3pp_mouse.h:74
constexpr SystemCursor SYSTEM_CURSOR_SW_RESIZE
Window resize bottom-left.
Definition SDL3pp_mouse.h:128
void WarpMouse(const FPointRaw &p)
Move the mouse cursor to the given position within the window.
Definition SDL3pp_mouse.h:664
SDL_SystemCursor SystemCursor
Cursor types for CreateSystemCursor().
Definition SDL3pp_mouse.h:65
MakeFrontCallback< void(Uint64 timestamp, WindowRaw window, MouseID mouseID, float *x, float *y)> MouseMotionTransformCB
A callback used to transform mouse motion delta from raw values.
Definition SDL3pp_mouse.h:474
constexpr SystemCursor SYSTEM_CURSOR_NE_RESIZE
Window resize top-right. May be NESW_RESIZE.
Definition SDL3pp_mouse.h:115
Uint8 MouseButton
Represents a button index.
Definition SDL3pp_mouse.h:359
constexpr SystemCursor SYSTEM_CURSOR_DEFAULT
Default cursor. Usually an arrow.
Definition SDL3pp_mouse.h:67
constexpr MouseButtonFlags BUTTON_X1MASK
X1MASK.
Definition SDL3pp_mouse.h:394
constexpr SystemCursor SYSTEM_CURSOR_EW_RESIZE
Double arrow pointing west and east.
Definition SDL3pp_mouse.h:90
CursorRef GetDefaultCursor()
Get the default cursor.
Definition SDL3pp_mouse.h:1024
WindowRef GetMouseFocus()
Get the window which currently has mouse focus.
Definition SDL3pp_mouse.h:548
bool CursorVisible()
Return whether the cursor is currently being shown.
Definition SDL3pp_mouse.h:1091
Cursor CreateColorCursor(SurfaceRef surface, const PointRaw &hot)
Create a color cursor.
Definition SDL3pp_mouse.h:896
SDL_CursorFrameInfo CursorFrameInfo
Animated cursor frame info.
Definition SDL3pp_mouse.h:351
constexpr MouseButtonFlags BUTTON_MMASK
MMASK.
Definition SDL3pp_mouse.h:390
constexpr SystemCursor SYSTEM_CURSOR_N_RESIZE
Window resize top. May be NS_RESIZE.
Definition SDL3pp_mouse.h:112
constexpr SystemCursor SYSTEM_CURSOR_S_RESIZE
Window resize bottom. May be NS_RESIZE.
Definition SDL3pp_mouse.h:125
void SetCursor(CursorRef cursor)
Set the active cursor.
Definition SDL3pp_mouse.h:990
SDL_MouseWheelDirection MouseWheelDirection
Scroll direction types for the Scroll event.
Definition SDL3pp_mouse.h:336
OwnArray< MouseID > GetMice()
Get a list of currently connected mice.
Definition SDL3pp_mouse.h:512
constexpr SystemCursor SYSTEM_CURSOR_TEXT
Text selection. Usually an I-beam.
Definition SDL3pp_mouse.h:70
void HideCursor()
Hide the cursor.
Definition SDL3pp_mouse.h:1076
constexpr MouseButton BUTTON_X2
X2 button.
Definition SDL3pp_mouse.h:369
void ShowCursor()
Show the cursor.
Definition SDL3pp_mouse.h:1062
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:436
constexpr SystemCursor SYSTEM_CURSOR_PROGRESS
Program is busy but still interactive. Usually it's WAIT with an arrow.
Definition SDL3pp_mouse.h:80
bool HasMouse()
Return whether a mouse is currently connected.
Definition SDL3pp_mouse.h:493
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:3587
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:290
::Uint8 Uint8
An unsigned 8-bit integer type.
Definition SDL3pp_stdinc.h:238
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:320
ResourceRef< Surface > SurfaceRef
Reference for Surface.
Definition SDL3pp_surface.h:54
ResourceRef< Window > WindowRef
Reference for Window.
Definition SDL3pp_video.h:54
SDL_Window * WindowRaw
Alias to raw representation for Window.
Definition SDL3pp_video.h:47
Main include header for the SDL3pp library.
The structure used to identify an SDL cursor.
Definition SDL3pp_mouse.h:159
constexpr Cursor & operator=(Cursor &&other) noexcept
Assignment operator.
Definition SDL3pp_mouse.h:286
constexpr Cursor(Cursor &&other) noexcept
Move constructor.
Definition SDL3pp_mouse.h:178
constexpr Cursor(CursorRaw resource) noexcept
Constructs from raw Cursor.
Definition SDL3pp_mouse.h:169
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
constexpr Cursor(const Cursor &other)=delete
Copy constructor.
Cursor & operator=(const Cursor &other)=delete
Assignment operator.
~Cursor()
Destructor.
Definition SDL3pp_mouse.h:283
Definition SDL3pp_callbackWrapper.h:169
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:156