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 CursorBase;
49
50// Forward decl
51struct Cursor;
52
54using CursorRaw = SDL_Cursor*;
55
62
68using SystemCursor = SDL_SystemCursor;
69
71 SDL_SYSTEM_CURSOR_DEFAULT;
72
74 SDL_SYSTEM_CURSOR_TEXT;
75
77constexpr SystemCursor SYSTEM_CURSOR_WAIT = SDL_SYSTEM_CURSOR_WAIT;
78
80 SDL_SYSTEM_CURSOR_CROSSHAIR;
81
83constexpr SystemCursor SYSTEM_CURSOR_PROGRESS = SDL_SYSTEM_CURSOR_PROGRESS;
84
86 SDL_SYSTEM_CURSOR_NWSE_RESIZE;
88
90 SDL_SYSTEM_CURSOR_NESW_RESIZE;
92
94 SDL_SYSTEM_CURSOR_EW_RESIZE;
95
97 SDL_SYSTEM_CURSOR_NS_RESIZE;
98
100constexpr SystemCursor SYSTEM_CURSOR_MOVE = SDL_SYSTEM_CURSOR_MOVE;
101
104 SDL_SYSTEM_CURSOR_NOT_ALLOWED;
105
107constexpr SystemCursor SYSTEM_CURSOR_POINTER = SDL_SYSTEM_CURSOR_POINTER;
108
113constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE = SDL_SYSTEM_CURSOR_NW_RESIZE;
114
116 SDL_SYSTEM_CURSOR_N_RESIZE;
117
119 SDL_SYSTEM_CURSOR_NE_RESIZE;
120
122 SDL_SYSTEM_CURSOR_E_RESIZE;
123
125 SDL_SYSTEM_CURSOR_SE_RESIZE;
127
129 SDL_SYSTEM_CURSOR_S_RESIZE;
130
132 SDL_SYSTEM_CURSOR_SW_RESIZE;
134
136 SDL_SYSTEM_CURSOR_W_RESIZE;
137
138constexpr SystemCursor SYSTEM_CURSOR_COUNT = SDL_SYSTEM_CURSOR_COUNT;
139
150using MouseID = SDL_MouseID;
151
157struct CursorBase : ResourceBaseT<CursorRaw>
158{
160
176 void Destroy();
177
194 void Set();
195};
196
207{
208 using CursorBase::CursorBase;
209
217 constexpr explicit Cursor(CursorRaw resource) noexcept
218 : CursorBase(resource)
219 {
220 }
221
223 constexpr Cursor(Cursor&& other) noexcept
224 : Cursor(other.release())
225 {
226 }
227
270 Cursor(const Uint8* data,
271 const Uint8* mask,
272 const PointRaw& size,
273 const PointRaw& hot);
274
306 Cursor(SurfaceRef surface, const PointRaw& hot);
307
322
324 ~Cursor() { SDL_DestroyCursor(get()); }
325
327 constexpr Cursor& operator=(Cursor&& other) noexcept
328 {
329 swap(*this, other);
330 return *this;
331 }
332};
333
339using MouseWheelDirection = SDL_MouseWheelDirection;
340
342 SDL_MOUSEWHEEL_NORMAL;
343
345 SDL_MOUSEWHEEL_FLIPPED;
346
347#if SDL_VERSION_ATLEAST(3, 4, 0)
348
354using CursorFrameInfo = SDL_CursorFrameInfo;
355
356#endif // SDL_VERSION_ATLEAST(3, 4, 0)
357
363
364constexpr MouseButton BUTTON_LEFT = SDL_BUTTON_LEFT;
365
366constexpr MouseButton BUTTON_MIDDLE = SDL_BUTTON_MIDDLE;
367
368constexpr MouseButton BUTTON_RIGHT = SDL_BUTTON_RIGHT;
369
370constexpr MouseButton BUTTON_X1 = SDL_BUTTON_X1;
371
372constexpr MouseButton BUTTON_X2 = SDL_BUTTON_X2;
373
390
391constexpr MouseButtonFlags BUTTON_LMASK = SDL_BUTTON_LMASK;
392
393constexpr MouseButtonFlags BUTTON_MMASK = SDL_BUTTON_MMASK;
394
395constexpr MouseButtonFlags BUTTON_RMASK = SDL_BUTTON_RMASK;
396
397constexpr MouseButtonFlags BUTTON_X1MASK = SDL_BUTTON_X1MASK;
398
399constexpr MouseButtonFlags BUTTON_X2MASK = SDL_BUTTON_X2MASK;
400
403{
404 return SDL_BUTTON_MASK(button);
405}
406
407#if SDL_VERSION_ATLEAST(3, 4, 0)
408
439using MouseMotionTransformCallback = void(SDLCALL*)(void* userdata,
440 Uint64 timestamp,
441 WindowRaw window,
442 MouseID mouseID,
443 float* x,
444 float* y);
445
478 WindowRaw window,
479 MouseID mouseID,
480 float* x,
481 float* y)>;
482
483#endif // SDL_VERSION_ATLEAST(3, 4, 0)
484
496inline bool HasMouse() { return SDL_HasMouse(); }
497
516{
517 int count;
518 auto data = CheckError(SDL_GetMice(&count));
519 return OwnArray<MouseID>{data, size_t(count)};
520}
521
537inline const char* GetMouseNameForID(MouseID instance_id)
538{
539 return CheckError(SDL_GetMouseNameForID(instance_id));
540}
541
551inline WindowRef GetMouseFocus() { return {SDL_GetMouseFocus()}; }
552
584inline MouseButtonFlags GetMouseState(float* x, float* y)
585{
586 return SDL_GetMouseState(x, y);
587}
588
624inline MouseButtonFlags GetGlobalMouseState(float* x, float* y)
625{
626 return SDL_GetGlobalMouseState(x, y);
627}
628
662inline MouseButtonFlags GetRelativeMouseState(float* x, float* y)
663{
664 return SDL_GetRelativeMouseState(x, y);
665}
666
687inline void WarpMouseInWindow(WindowRef window, const FPointRaw& p)
688{
689 SDL_WarpMouseInWindow(window, p.x, p.y);
690}
691
692inline void WindowBase::WarpMouse(const FPointRaw& p)
693{
695}
696
717inline void WarpMouse(const FPointRaw& p)
718{
719 CheckError(SDL_WarpMouseGlobal(p.x, p.y));
720}
721
722#if SDL_VERSION_ATLEAST(3, 4, 0)
723
740 void* userdata)
741{
742 CheckError(SDL_SetRelativeMouseTransform(callback, userdata));
743}
744
760{
761 SetRelativeMouseTransform(callback.wrapper, callback.data);
762}
763
764#endif // SDL_VERSION_ATLEAST(3, 4, 0)
765
791inline void SetWindowRelativeMouseMode(WindowRef window, bool enabled)
792{
793 CheckError(SDL_SetWindowRelativeMouseMode(window, enabled));
794}
795
796inline void WindowBase::SetRelativeMouseMode(bool enabled)
797{
799}
800
814{
815 return SDL_GetWindowRelativeMouseMode(window);
816}
817
819{
821}
822
867inline void CaptureMouse(bool enabled)
868{
869 CheckError(SDL_CaptureMouse(enabled));
870}
871
914inline Cursor CreateCursor(const Uint8* data,
915 const Uint8* mask,
916 const PointRaw& size,
917 const PointRaw& hot)
918{
919 return Cursor(data, mask, size, hot);
920}
921
922inline Cursor::Cursor(const Uint8* data,
923 const Uint8* mask,
924 const PointRaw& size,
925 const PointRaw& hot)
926 : Cursor(
927 CheckError(SDL_CreateCursor(data, mask, size.x, size.y, hot.x, hot.y)))
928{
929}
930
931inline Cursor::Cursor(SurfaceRef surface, const PointRaw& hot)
932 : Cursor(CheckError(SDL_CreateColorCursor(surface, hot.x, hot.y)))
933{
934}
935
937 : Cursor(CheckError(SDL_CreateSystemCursor(id)))
938{
939}
940
971inline Cursor CreateColorCursor(SurfaceRef surface, const PointRaw& hot)
972{
973 return Cursor(surface, hot);
974}
975
976#if SDL_VERSION_ATLEAST(3, 4, 0)
977
1023 int frame_count,
1024 int hot_x,
1025 int hot_y)
1026{
1027 return CheckError(
1028 SDL_CreateAnimatedCursor(frames, frame_count, hot_x, hot_y));
1029}
1030
1031#endif // SDL_VERSION_ATLEAST(3, 4, 0)
1032
1047
1065inline void SetCursor(CursorRef cursor) { CheckError(SDL_SetCursor(cursor)); }
1066
1068
1083inline CursorRef GetCursor() { return {SDL_GetCursor()}; }
1084
1100{
1101 return {CheckError(SDL_GetDefaultCursor())};
1102}
1103
1121inline void DestroyCursor(CursorRaw cursor) { SDL_DestroyCursor(cursor); }
1122
1124
1137inline void ShowCursor() { CheckError(SDL_ShowCursor()); }
1138
1151inline void HideCursor() { CheckError(SDL_HideCursor()); }
1152
1166inline bool CursorVisible() { return SDL_CursorVisible(); }
1167
1169
1170} // namespace SDL
1171
1172#endif /* SDL3PP_MOUSE_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:44
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:57
friend constexpr void swap(ResourceBaseT &lhs, ResourceBaseT &rhs) noexcept
Definition SDL3pp_resource.h:65
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:54
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
void Set()
Set the active cursor.
Definition SDL3pp_mouse.h:1067
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:150
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:914
void WarpMouseInWindow(WindowRef window, const FPointRaw &p)
Move the mouse cursor to the given position within the window.
Definition SDL3pp_mouse.h:687
constexpr SystemCursor SYSTEM_CURSOR_MOVE
Four pointed arrow pointing north, south, east, and west.
Definition SDL3pp_mouse.h:100
void SetRelativeMouseTransform(MouseMotionTransformCallback callback, void *userdata)
Set a user-defined function by which to transform relative mouse inputs.
Definition SDL3pp_mouse.h:739
constexpr SystemCursor SYSTEM_CURSOR_POINTER
Pointer that indicates a link. Usually a pointing hand.
Definition SDL3pp_mouse.h:107
constexpr MouseWheelDirection MOUSEWHEEL_NORMAL
The scroll direction is normal.
Definition SDL3pp_mouse.h:341
constexpr SystemCursor SYSTEM_CURSOR_SE_RESIZE
Window resize bottom-right.
Definition SDL3pp_mouse.h:124
constexpr MouseButton BUTTON_RIGHT
Right button.
Definition SDL3pp_mouse.h:368
constexpr SystemCursor SYSTEM_CURSOR_W_RESIZE
Window resize left. May be EW_RESIZE.
Definition SDL3pp_mouse.h:135
constexpr MouseButton BUTTON_MIDDLE
Middle button.
Definition SDL3pp_mouse.h:366
CursorRef GetCursor()
Get the active cursor.
Definition SDL3pp_mouse.h:1083
void WarpMouse(const FPointRaw &p)
Move the mouse cursor to the given position within the window.
Definition SDL3pp_mouse.h:692
void DestroyCursor(CursorRaw cursor)
Free a previously-created cursor.
Definition SDL3pp_mouse.h:1121
constexpr SystemCursor SYSTEM_CURSOR_NESW_RESIZE
Double arrow pointing northeast and southwest.
Definition SDL3pp_mouse.h:89
constexpr MouseWheelDirection MOUSEWHEEL_FLIPPED
The scroll direction is flipped / natural.
Definition SDL3pp_mouse.h:344
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:662
constexpr MouseButtonFlags ButtonMask(MouseButton button)
Returns mask for button.
Definition SDL3pp_mouse.h:402
void CaptureMouse(bool enabled)
Capture the mouse and to track input outside an SDL window.
Definition SDL3pp_mouse.h:867
Cursor CreateSystemCursor(SystemCursor id)
Create a system cursor.
Definition SDL3pp_mouse.h:1046
constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE
Window resize top-left.
Definition SDL3pp_mouse.h:113
ResourceRefT< CursorBase > CursorRef
Reference for Cursor.
Definition SDL3pp_mouse.h:61
constexpr SystemCursor SYSTEM_CURSOR_CROSSHAIR
Crosshair.
Definition SDL3pp_mouse.h:79
Uint32 MouseButtonFlags
A bitmask of pressed mouse buttons, as reported by GetMouseState, etc.
Definition SDL3pp_mouse.h:389
void WarpMouse(const FPointRaw &p)
Move the mouse to the given position in global screen space.
Definition SDL3pp_mouse.h:717
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:624
void Destroy()
Free a previously-created cursor.
Definition SDL3pp_mouse.h:1123
constexpr SystemCursor SYSTEM_CURSOR_NWSE_RESIZE
Double arrow pointing northwest and southeast.
Definition SDL3pp_mouse.h:85
constexpr SystemCursor SYSTEM_CURSOR_E_RESIZE
Window resize right. May be EW_RESIZE.
Definition SDL3pp_mouse.h:121
constexpr MouseButton BUTTON_X1
X1 button.
Definition SDL3pp_mouse.h:370
constexpr SystemCursor SYSTEM_CURSOR_NS_RESIZE
Double arrow pointing north and south.
Definition SDL3pp_mouse.h:96
const char * GetMouseNameForID(MouseID instance_id)
Get the name of a mouse.
Definition SDL3pp_mouse.h:537
constexpr SystemCursor SYSTEM_CURSOR_COUNT
COUNT.
Definition SDL3pp_mouse.h:138
constexpr MouseButtonFlags BUTTON_RMASK
RMASK.
Definition SDL3pp_mouse.h:395
constexpr SystemCursor SYSTEM_CURSOR_NOT_ALLOWED
Not permitted. Usually a slashed circle or crossbones.
Definition SDL3pp_mouse.h:103
constexpr MouseButton BUTTON_LEFT
Left button.
Definition SDL3pp_mouse.h:364
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:584
constexpr MouseButtonFlags BUTTON_LMASK
LMASK.
Definition SDL3pp_mouse.h:391
SDL_Cursor * CursorRaw
Alias to raw representation for Cursor.
Definition SDL3pp_mouse.h:54
constexpr MouseButtonFlags BUTTON_X2MASK
X2MASK.
Definition SDL3pp_mouse.h:399
bool GetRelativeMouseMode() const
Query whether relative mouse mode is enabled for a window.
Definition SDL3pp_mouse.h:818
constexpr SystemCursor SYSTEM_CURSOR_WAIT
Wait. Usually an hourglass or watch or spinning ball.
Definition SDL3pp_mouse.h:77
void SetWindowRelativeMouseMode(WindowRef window, bool enabled)
Set relative mouse mode for a window.
Definition SDL3pp_mouse.h:791
constexpr SystemCursor SYSTEM_CURSOR_SW_RESIZE
Window resize bottom-left.
Definition SDL3pp_mouse.h:131
SDL_SystemCursor SystemCursor
Cursor types for CreateSystemCursor().
Definition SDL3pp_mouse.h:68
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:477
constexpr SystemCursor SYSTEM_CURSOR_NE_RESIZE
Window resize top-right. May be NESW_RESIZE.
Definition SDL3pp_mouse.h:118
Uint8 MouseButton
Represents a button index.
Definition SDL3pp_mouse.h:362
constexpr SystemCursor SYSTEM_CURSOR_DEFAULT
Default cursor. Usually an arrow.
Definition SDL3pp_mouse.h:70
constexpr MouseButtonFlags BUTTON_X1MASK
X1MASK.
Definition SDL3pp_mouse.h:397
constexpr SystemCursor SYSTEM_CURSOR_EW_RESIZE
Double arrow pointing west and east.
Definition SDL3pp_mouse.h:93
void SetRelativeMouseMode(bool enabled)
Set relative mouse mode for a window.
Definition SDL3pp_mouse.h:796
CursorRef GetDefaultCursor()
Get the default cursor.
Definition SDL3pp_mouse.h:1099
WindowRef GetMouseFocus()
Get the window which currently has mouse focus.
Definition SDL3pp_mouse.h:551
bool CursorVisible()
Return whether the cursor is currently being shown.
Definition SDL3pp_mouse.h:1166
Cursor CreateColorCursor(SurfaceRef surface, const PointRaw &hot)
Create a color cursor.
Definition SDL3pp_mouse.h:971
SDL_CursorFrameInfo CursorFrameInfo
Animated cursor frame info.
Definition SDL3pp_mouse.h:354
constexpr MouseButtonFlags BUTTON_MMASK
MMASK.
Definition SDL3pp_mouse.h:393
bool GetWindowRelativeMouseMode(WindowRef window)
Query whether relative mouse mode is enabled for a window.
Definition SDL3pp_mouse.h:813
constexpr SystemCursor SYSTEM_CURSOR_N_RESIZE
Window resize top. May be NS_RESIZE.
Definition SDL3pp_mouse.h:115
constexpr SystemCursor SYSTEM_CURSOR_S_RESIZE
Window resize bottom. May be NS_RESIZE.
Definition SDL3pp_mouse.h:128
void SetCursor(CursorRef cursor)
Set the active cursor.
Definition SDL3pp_mouse.h:1065
SDL_MouseWheelDirection MouseWheelDirection
Scroll direction types for the Scroll event.
Definition SDL3pp_mouse.h:339
OwnArray< MouseID > GetMice()
Get a list of currently connected mice.
Definition SDL3pp_mouse.h:515
constexpr SystemCursor SYSTEM_CURSOR_TEXT
Text selection. Usually an I-beam.
Definition SDL3pp_mouse.h:73
void HideCursor()
Hide the cursor.
Definition SDL3pp_mouse.h:1151
constexpr MouseButton BUTTON_X2
X2 button.
Definition SDL3pp_mouse.h:372
void ShowCursor()
Show the cursor.
Definition SDL3pp_mouse.h:1137
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:439
constexpr SystemCursor SYSTEM_CURSOR_PROGRESS
Program is busy but still interactive. Usually it's WAIT with an arrow.
Definition SDL3pp_mouse.h:83
bool HasMouse()
Return whether a mouse is currently connected.
Definition SDL3pp_mouse.h:496
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:3576
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:296
::Uint8 Uint8
An unsigned 8-bit integer type.
Definition SDL3pp_stdinc.h:244
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:326
ResourceRefT< SurfaceBase > SurfaceRef
Reference for Surface.
Definition SDL3pp_surface.h:57
SDL_Window * WindowRaw
Alias to raw representation for Window.
Definition SDL3pp_video.h:50
ResourceRefT< WindowBase > WindowRef
Reference for Window.
Definition SDL3pp_video.h:57
Main include header for the SDL3pp library.
Base class to Cursor.
Definition SDL3pp_mouse.h:158
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
The structure used to identify an SDL cursor.
Definition SDL3pp_mouse.h:207
constexpr Cursor & operator=(Cursor &&other) noexcept
Assignment operator.
Definition SDL3pp_mouse.h:327
constexpr Cursor(Cursor &&other) noexcept
Move constructor.
Definition SDL3pp_mouse.h:223
constexpr Cursor(CursorRaw resource) noexcept
Constructs from raw Cursor.
Definition SDL3pp_mouse.h:217
~Cursor()
Destructor.
Definition SDL3pp_mouse.h:324
Definition SDL3pp_callbackWrapper.h:169
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:93