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
48// Forward decl
49struct CursorBase;
50
51// Forward decl
52struct CursorRef;
53
54// Forward decl
55struct Cursor;
56
62using SystemCursor = SDL_SystemCursor;
63
65 SDL_SYSTEM_CURSOR_DEFAULT;
66
68 SDL_SYSTEM_CURSOR_TEXT;
69
73constexpr SystemCursor SYSTEM_CURSOR_WAIT = SDL_SYSTEM_CURSOR_WAIT;
74
76 SDL_SYSTEM_CURSOR_CROSSHAIR;
77
81constexpr SystemCursor SYSTEM_CURSOR_PROGRESS = SDL_SYSTEM_CURSOR_PROGRESS;
82
84 SDL_SYSTEM_CURSOR_NWSE_RESIZE;
86
88 SDL_SYSTEM_CURSOR_NESW_RESIZE;
90
92 SDL_SYSTEM_CURSOR_EW_RESIZE;
93
95 SDL_SYSTEM_CURSOR_NS_RESIZE;
96
100constexpr SystemCursor SYSTEM_CURSOR_MOVE = SDL_SYSTEM_CURSOR_MOVE;
101
106 SDL_SYSTEM_CURSOR_NOT_ALLOWED;
107
111constexpr SystemCursor SYSTEM_CURSOR_POINTER = SDL_SYSTEM_CURSOR_POINTER;
112
117constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE = SDL_SYSTEM_CURSOR_NW_RESIZE;
118
120 SDL_SYSTEM_CURSOR_N_RESIZE;
121
123 SDL_SYSTEM_CURSOR_NE_RESIZE;
125
127 SDL_SYSTEM_CURSOR_E_RESIZE;
128
130 SDL_SYSTEM_CURSOR_SE_RESIZE;
132
134 SDL_SYSTEM_CURSOR_S_RESIZE;
135
137 SDL_SYSTEM_CURSOR_SW_RESIZE;
139
141 SDL_SYSTEM_CURSOR_W_RESIZE;
142
143constexpr SystemCursor SYSTEM_CURSOR_COUNT = SDL_SYSTEM_CURSOR_COUNT;
144
155using MouseID = SDL_MouseID;
156
169struct CursorBase : Resource<SDL_Cursor*>
170{
171 using Resource::Resource;
172
217 CursorBase(const Uint8* data,
218 const Uint8* mask,
219 int w,
220 int h,
221 int hot_x,
222 int hot_y)
223 : Resource(CheckError(SDL_CreateCursor(data, mask, w, h, hot_x, hot_y)))
224 {
225 }
226
255 CursorBase(SurfaceBase& surface, int hot_x, int hot_y)
256 : Resource(CheckError(SDL_CreateColorCursor(surface.get(), hot_x, hot_y)))
257 {
258 }
259
274 : Resource(CheckError(SDL_CreateSystemCursor(id)))
275 {
276 }
277};
278
288{
290
294 constexpr CursorRef(const CursorRef& other)
295 : CursorBase(other.get())
296 {
297 }
298
302 constexpr CursorRef(CursorRef&& other)
303 : CursorBase(other.release())
304 {
305 }
306
310 constexpr ~CursorRef() = default;
311
316 {
317 release(other.release());
318 return *this;
319 }
320
334 void reset(SDL_Cursor* newResource = {})
335 {
336 SDL_DestroyCursor(release(newResource));
337 }
338};
339
349{
351
355 constexpr explicit Cursor(SDL_Cursor* resource = {})
356 : CursorRef(resource)
357 {
358 }
359
360 constexpr Cursor(const Cursor& other) = delete;
361
365 constexpr Cursor(Cursor&& other) = default;
366
370 ~Cursor() { reset(); }
371
376 {
377 reset(other.release());
378 return *this;
379 }
380};
381
386using MouseButton = Uint8;
387
388constexpr MouseButton BUTTON_LEFT = SDL_BUTTON_LEFT;
389
390constexpr MouseButton BUTTON_MIDDLE = SDL_BUTTON_MIDDLE;
391
392constexpr MouseButton BUTTON_RIGHT = SDL_BUTTON_RIGHT;
393
394constexpr MouseButton BUTTON_X1 = SDL_BUTTON_X1;
395
396constexpr MouseButton BUTTON_X2 = SDL_BUTTON_X2;
397
403using MouseWheelDirection = SDL_MouseWheelDirection;
404
406 SDL_MOUSEWHEEL_NORMAL;
407
409 SDL_MOUSEWHEEL_FLIPPED;
410
426using MouseButtonFlags = Uint32;
427
435{
436 return SDL_BUTTON_MASK(button);
437}
438
440 SDL_BUTTON_LMASK;
441
443 SDL_BUTTON_MMASK;
444
446 SDL_BUTTON_RMASK;
447
449 SDL_BUTTON_X1MASK;
450
452 SDL_BUTTON_X2MASK;
453
465inline bool HasMouse() { return SDL_HasMouse(); }
466
486{
487 int count;
488 auto data = CheckError(SDL_GetMice(&count));
489 return OwnArray<MouseID>{data, size_t(count)};
490}
491
507inline const char* GetMouseNameForID(MouseID instance_id)
508{
509 return CheckError(SDL_GetMouseNameForID(instance_id));
510}
511
521inline WindowRef GetMouseFocus() { return SDL_GetMouseFocus(); }
522
554inline MouseButtonFlags GetMouseState(float* x, float* y)
555{
556 return SDL_GetMouseState(x, y);
557}
558
594inline MouseButtonFlags GetGlobalMouseState(float* x, float* y)
595{
596 return SDL_GetGlobalMouseState(x, y);
597}
598
632inline MouseButtonFlags GetRelativeMouseState(float* x, float* y)
633{
634 return SDL_GetRelativeMouseState(x, y);
635}
636
657inline void WindowBase::WarpMouse(float x, float y)
658{
659 SDL_WarpMouseInWindow(get(), x, y);
660}
661
683inline void WarpMouse(float x, float y)
684{
685 CheckError(SDL_WarpMouseGlobal(x, y));
686}
687
712inline void WindowBase::SetRelativeMouseMode(bool enabled)
713{
714 CheckError(SDL_SetWindowRelativeMouseMode(get(), enabled));
715}
716
729{
730 return SDL_GetWindowRelativeMouseMode(get());
731}
732
778inline void CaptureMouse(bool enabled)
779{
780 CheckError(SDL_CaptureMouse(enabled));
781}
782
800inline void SetCursor(CursorBase& cursor)
801{
802 CheckError(SDL_SetCursor(cursor.get()));
803}
804
819inline CursorRef GetCursor() { return SDL_GetCursor(); }
820
835{
836 return CheckError(SDL_GetDefaultCursor());
837}
838
851inline void ShowCursor() { CheckError(SDL_ShowCursor()); }
852
865inline void HideCursor() { CheckError(SDL_HideCursor()); }
866
880inline bool CursorVisible() { return SDL_CursorVisible(); }
881
883} // namespace SDL
884
885#endif /* SDL3PP_MOUSE_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
A SDL managed resource.
Definition SDL3pp_resource.h:17
constexpr SDL_Cursor * release(SDL_Cursor * newResource={})
Return contained resource and empties or replace value.
Definition SDL3pp_resource.h:60
constexpr Resource(T resource={})
Constructs the underlying resource.
Definition SDL3pp_resource.h:22
constexpr SDL_Cursor * get() const
Return contained resource;.
Definition SDL3pp_resource.h:57
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
constexpr SystemCursor SYSTEM_CURSOR_MOVE
Four pointed arrow pointing north, south, east, and west.
Definition SDL3pp_mouse.h:100
constexpr SystemCursor SYSTEM_CURSOR_POINTER
Pointer that indicates a link.
Definition SDL3pp_mouse.h:111
constexpr MouseWheelDirection MOUSEWHEEL_NORMAL
The scroll direction is normal.
Definition SDL3pp_mouse.h:405
SDL_SystemCursor SystemCursor
Cursor types for CursorBase.CursorBase().
Definition SDL3pp_mouse.h:62
constexpr SystemCursor SYSTEM_CURSOR_SE_RESIZE
Window resize bottom-right.
Definition SDL3pp_mouse.h:129
constexpr MouseButton BUTTON_RIGHT
Right button.
Definition SDL3pp_mouse.h:392
constexpr SystemCursor SYSTEM_CURSOR_W_RESIZE
Window resize left. May be EW_RESIZE.
Definition SDL3pp_mouse.h:140
constexpr MouseButton BUTTON_MIDDLE
Middle button.
Definition SDL3pp_mouse.h:390
CursorRef GetCursor()
Get the active cursor.
Definition SDL3pp_mouse.h:819
constexpr SystemCursor SYSTEM_CURSOR_NESW_RESIZE
Double arrow pointing northeast and southwest.
Definition SDL3pp_mouse.h:87
constexpr MouseWheelDirection MOUSEWHEEL_FLIPPED
The scroll direction is flipped / natural.
Definition SDL3pp_mouse.h:408
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:632
constexpr MouseButtonFlags ButtonMask(MouseButton button)
Convert a button index to a button mask.
Definition SDL3pp_mouse.h:434
void CaptureMouse(bool enabled)
Capture the mouse and to track input outside an SDL window.
Definition SDL3pp_mouse.h:778
Uint32 MouseButtonFlags
A bitmask of pressed mouse buttons, as reported by GetMouseState, etc.
Definition SDL3pp_mouse.h:426
void WarpMouse(float x, float y)
Move the mouse cursor to the given position within the window.
Definition SDL3pp_mouse.h:657
constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE
Window resize top-left.
Definition SDL3pp_mouse.h:117
void WarpMouse(float x, float y)
Move the mouse to the given position in global screen space.
Definition SDL3pp_mouse.h:683
constexpr SystemCursor SYSTEM_CURSOR_CROSSHAIR
Crosshair.
Definition SDL3pp_mouse.h:75
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:594
constexpr SystemCursor SYSTEM_CURSOR_NWSE_RESIZE
Double arrow pointing northwest and southeast.
Definition SDL3pp_mouse.h:83
constexpr SystemCursor SYSTEM_CURSOR_E_RESIZE
Window resize right. May be EW_RESIZE.
Definition SDL3pp_mouse.h:126
constexpr MouseButton BUTTON_X1
X1 button.
Definition SDL3pp_mouse.h:394
constexpr SystemCursor SYSTEM_CURSOR_NS_RESIZE
Double arrow pointing north and south.
Definition SDL3pp_mouse.h:94
const char * GetMouseNameForID(MouseID instance_id)
Get the name of a mouse.
Definition SDL3pp_mouse.h:507
constexpr SystemCursor SYSTEM_CURSOR_COUNT
COUNT.
Definition SDL3pp_mouse.h:143
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:155
constexpr MouseButtonFlags BUTTON_RMASK
Right button mask.
Definition SDL3pp_mouse.h:445
constexpr SystemCursor SYSTEM_CURSOR_NOT_ALLOWED
Not permitted.
Definition SDL3pp_mouse.h:105
constexpr MouseButton BUTTON_LEFT
Left button.
Definition SDL3pp_mouse.h:388
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:554
constexpr MouseButtonFlags BUTTON_LMASK
Left button mask.
Definition SDL3pp_mouse.h:439
constexpr MouseButtonFlags BUTTON_X2MASK
X2 button mask.
Definition SDL3pp_mouse.h:451
bool GetRelativeMouseMode() const
Query whether relative mouse mode is enabled for a window.
Definition SDL3pp_mouse.h:728
constexpr SystemCursor SYSTEM_CURSOR_WAIT
Wait.
Definition SDL3pp_mouse.h:73
Uint8 MouseButton
Represents a button index.
Definition SDL3pp_mouse.h:386
constexpr SystemCursor SYSTEM_CURSOR_SW_RESIZE
Window resize bottom-left.
Definition SDL3pp_mouse.h:136
constexpr SystemCursor SYSTEM_CURSOR_NE_RESIZE
Window resize top-right.
Definition SDL3pp_mouse.h:122
constexpr SystemCursor SYSTEM_CURSOR_DEFAULT
Default cursor. Usually an arrow.
Definition SDL3pp_mouse.h:64
constexpr MouseButtonFlags BUTTON_X1MASK
X1 button mask.
Definition SDL3pp_mouse.h:448
constexpr SystemCursor SYSTEM_CURSOR_EW_RESIZE
Double arrow pointing west and east.
Definition SDL3pp_mouse.h:91
void SetRelativeMouseMode(bool enabled)
Set relative mouse mode for a window.
Definition SDL3pp_mouse.h:712
CursorRef GetDefaultCursor()
Get the default cursor.
Definition SDL3pp_mouse.h:834
WindowRef GetMouseFocus()
Get the window which currently has mouse focus.
Definition SDL3pp_mouse.h:521
bool CursorVisible()
Return whether the cursor is currently being shown.
Definition SDL3pp_mouse.h:880
constexpr MouseButtonFlags BUTTON_MMASK
Middle button mask.
Definition SDL3pp_mouse.h:442
constexpr SystemCursor SYSTEM_CURSOR_N_RESIZE
Window resize top. May be NS_RESIZE.
Definition SDL3pp_mouse.h:119
constexpr SystemCursor SYSTEM_CURSOR_S_RESIZE
Window resize bottom. May be NS_RESIZE.
Definition SDL3pp_mouse.h:133
SDL_MouseWheelDirection MouseWheelDirection
Scroll direction types for the Scroll event.
Definition SDL3pp_mouse.h:403
void SetCursor(CursorBase &cursor)
Set the active cursor.
Definition SDL3pp_mouse.h:800
OwnArray< MouseID > GetMice()
Get a list of currently connected mice.
Definition SDL3pp_mouse.h:485
constexpr SystemCursor SYSTEM_CURSOR_TEXT
Text selection. Usually an I-beam.
Definition SDL3pp_mouse.h:67
void HideCursor()
Hide the cursor.
Definition SDL3pp_mouse.h:865
constexpr MouseButton BUTTON_X2
X2 button.
Definition SDL3pp_mouse.h:396
void ShowCursor()
Show the cursor.
Definition SDL3pp_mouse.h:851
constexpr SystemCursor SYSTEM_CURSOR_PROGRESS
Program is busy but still interactive.
Definition SDL3pp_mouse.h:81
bool HasMouse()
Return whether a mouse is currently connected.
Definition SDL3pp_mouse.h:465
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
The structure used to identify an SDL cursor.
Definition SDL3pp_mouse.h:170
CursorBase(SystemCursor id)
Create a system cursor.
Definition SDL3pp_mouse.h:273
CursorBase(const Uint8 *data, const Uint8 *mask, int w, int h, int hot_x, int hot_y)
Create a cursor using the specified bitmap data and mask (in MSB format).
Definition SDL3pp_mouse.h:217
CursorBase(SurfaceBase &surface, int hot_x, int hot_y)
Create a color cursor.
Definition SDL3pp_mouse.h:255
Handle to a non owned cursor.
Definition SDL3pp_mouse.h:288
constexpr CursorRef(CursorRef &&other)
Move constructor.
Definition SDL3pp_mouse.h:302
void reset(SDL_Cursor *newResource={})
Free a previously-created cursor.
Definition SDL3pp_mouse.h:334
CursorRef & operator=(CursorRef other)
Assignment operator.
Definition SDL3pp_mouse.h:315
constexpr ~CursorRef()=default
Default constructor.
constexpr CursorRef(const CursorRef &other)
Copy constructor.
Definition SDL3pp_mouse.h:294
Handle to an owned cursor.
Definition SDL3pp_mouse.h:349
constexpr Cursor(Cursor &&other)=default
Move constructor.
constexpr Cursor(SDL_Cursor *resource={})
Constructs from the underlying resource.
Definition SDL3pp_mouse.h:355
Cursor & operator=(Cursor other)
Assignment operator.
Definition SDL3pp_mouse.h:375
~Cursor()
Frees up resource when object goes out of scope.
Definition SDL3pp_mouse.h:370
A collection of pixels used in software blitting.
Definition SDL3pp_surface.h:138
Handle to a non owned window.
Definition SDL3pp_video.h:2778