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 CursorRef;
50
51// Forward decl
52struct Cursor;
53
63
73
79using SystemCursor = SDL_SystemCursor;
80
82 SDL_SYSTEM_CURSOR_DEFAULT;
83
85 SDL_SYSTEM_CURSOR_TEXT;
86
90constexpr SystemCursor SYSTEM_CURSOR_WAIT = SDL_SYSTEM_CURSOR_WAIT;
91
93 SDL_SYSTEM_CURSOR_CROSSHAIR;
94
98constexpr SystemCursor SYSTEM_CURSOR_PROGRESS = SDL_SYSTEM_CURSOR_PROGRESS;
99
101 SDL_SYSTEM_CURSOR_NWSE_RESIZE;
103
105 SDL_SYSTEM_CURSOR_NESW_RESIZE;
107
109 SDL_SYSTEM_CURSOR_EW_RESIZE;
110
112 SDL_SYSTEM_CURSOR_NS_RESIZE;
113
117constexpr SystemCursor SYSTEM_CURSOR_MOVE = SDL_SYSTEM_CURSOR_MOVE;
118
123 SDL_SYSTEM_CURSOR_NOT_ALLOWED;
124
128constexpr SystemCursor SYSTEM_CURSOR_POINTER = SDL_SYSTEM_CURSOR_POINTER;
129
134constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE = SDL_SYSTEM_CURSOR_NW_RESIZE;
135
137 SDL_SYSTEM_CURSOR_N_RESIZE;
138
140 SDL_SYSTEM_CURSOR_NE_RESIZE;
142
144 SDL_SYSTEM_CURSOR_E_RESIZE;
145
147 SDL_SYSTEM_CURSOR_SE_RESIZE;
149
151 SDL_SYSTEM_CURSOR_S_RESIZE;
152
154 SDL_SYSTEM_CURSOR_SW_RESIZE;
156
158 SDL_SYSTEM_CURSOR_W_RESIZE;
159
160constexpr SystemCursor SYSTEM_CURSOR_COUNT = SDL_SYSTEM_CURSOR_COUNT;
161
172using MouseID = SDL_MouseID;
173
185struct CursorRef : Resource<SDL_Cursor*>
186{
187 using Resource::Resource;
204 static void reset(SDL_Cursor* resource) { SDL_DestroyCursor(resource); }
205};
206
214struct Cursor : ResourceUnique<CursorRef>
215{
217
257 static Cursor Create(const Uint8* data,
258 const Uint8* mask,
259 const SDL_Point& size,
260 const SDL_Point& hot)
261 {
262 return Cursor(
263 CheckError(SDL_CreateCursor(data, mask, size.x, size.y, hot.x, hot.y)));
264 }
265
293 static Cursor CreateColor(SurfaceRef surface, const SDL_Point& hot)
294 {
295 return Cursor(
296 CheckError(SDL_CreateColorCursor(surface.get(), hot.x, hot.y)));
297 }
298
313 {
314 return Cursor(CheckError(SDL_CreateSystemCursor(id)));
315 }
316
329 void Destroy() { reset(); }
334
335};
336
337
339{
340 return CursorShared(std::move(*this));
341}
342
352struct CursorUnsafe : ResourceUnsafe<CursorRef>
353{
355
359 constexpr explicit CursorUnsafe(Cursor&& other)
360 : CursorUnsafe(other.release())
361 {
362 }
363};
364
369using MouseButton = Uint8;
370
371constexpr MouseButton BUTTON_LEFT = SDL_BUTTON_LEFT;
372
373constexpr MouseButton BUTTON_MIDDLE = SDL_BUTTON_MIDDLE;
374
375constexpr MouseButton BUTTON_RIGHT = SDL_BUTTON_RIGHT;
376
377constexpr MouseButton BUTTON_X1 = SDL_BUTTON_X1;
378
379constexpr MouseButton BUTTON_X2 = SDL_BUTTON_X2;
380
386using MouseWheelDirection = SDL_MouseWheelDirection;
387
389 SDL_MOUSEWHEEL_NORMAL;
390
392 SDL_MOUSEWHEEL_FLIPPED;
393
409using MouseButtonFlags = Uint32;
410
418{
419 return SDL_BUTTON_MASK(button);
420}
421
423 SDL_BUTTON_LMASK;
424
426 SDL_BUTTON_MMASK;
427
429 SDL_BUTTON_RMASK;
430
432 SDL_BUTTON_X1MASK;
433
435 SDL_BUTTON_X2MASK;
436
448inline bool HasMouse() { return SDL_HasMouse(); }
449
469{
470 int count;
471 auto data = CheckError(SDL_GetMice(&count));
472 return OwnArray<MouseID>{data, size_t(count)};
473}
474
490inline const char* GetMouseNameForID(MouseID instance_id)
491{
492 return CheckError(SDL_GetMouseNameForID(instance_id));
493}
494
504inline WindowRef GetMouseFocus() { return SDL_GetMouseFocus(); }
505
537inline MouseButtonFlags GetMouseState(float* x, float* y)
538{
539 return SDL_GetMouseState(x, y);
540}
541
577inline MouseButtonFlags GetGlobalMouseState(float* x, float* y)
578{
579 return SDL_GetGlobalMouseState(x, y);
580}
581
615inline MouseButtonFlags GetRelativeMouseState(float* x, float* y)
616{
617 return SDL_GetRelativeMouseState(x, y);
618}
619
640inline void WindowRef::WarpMouse(float x, float y)
641{
642 SDL_WarpMouseInWindow(get(), x, y);
643}
644
666inline void WarpMouse(float x, float y)
667{
668 CheckError(SDL_WarpMouseGlobal(x, y));
669}
670
695inline void WindowRef::SetRelativeMouseMode(bool enabled)
696{
697 CheckError(SDL_SetWindowRelativeMouseMode(get(), enabled));
698}
699
712{
713 return SDL_GetWindowRelativeMouseMode(get());
714}
715
761inline void CaptureMouse(bool enabled)
762{
763 CheckError(SDL_CaptureMouse(enabled));
764}
765
783inline void SetCursor(CursorRef cursor)
784{
785 CheckError(SDL_SetCursor(cursor.get()));
786}
787
802inline CursorRef GetCursor() { return SDL_GetCursor(); }
803
818{
819 return CheckError(SDL_GetDefaultCursor());
820}
821
834inline void ShowCursor() { CheckError(SDL_ShowCursor()); }
835
848inline void HideCursor() { CheckError(SDL_HideCursor()); }
849
863inline bool CursorVisible() { return SDL_CursorVisible(); }
864
866} // namespace SDL
867
868#endif /* SDL3PP_MOUSE_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
RESOURCE release()
Returns reference and reset this.
Definition SDL3pp_resource.h:178
Implement shared ownership for a resource.
Definition SDL3pp_resource.h:283
Implement unique ownership for a resource.
Definition SDL3pp_resource.h:226
constexpr ResourceUnique(std::nullptr_t=nullptr)
Default constructor.
Definition SDL3pp_resource.h:231
void reset()
Resets the value, destroying the resource if not nullptr.
Definition SDL3pp_resource.h:265
A dumb pointer to resource.
Definition SDL3pp_resource.h:197
constexpr ResourceUnsafe()=default
Default constructor.
Implement weak ownership for a resource.
Definition SDL3pp_resource.h:328
A SDL managed resource.
Definition SDL3pp_resource.h:29
constexpr Resource(T resource={})
Constructs from the underlying resource.
Definition SDL3pp_resource.h:37
constexpr T get() const
Return contained resource;.
Definition SDL3pp_resource.h:76
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
void WarpMouse(float x, float y)
Move the mouse cursor to the given position within the window.
Definition SDL3pp_mouse.h:640
constexpr SystemCursor SYSTEM_CURSOR_MOVE
Four pointed arrow pointing north, south, east, and west.
Definition SDL3pp_mouse.h:117
constexpr SystemCursor SYSTEM_CURSOR_POINTER
Pointer that indicates a link.
Definition SDL3pp_mouse.h:128
constexpr MouseWheelDirection MOUSEWHEEL_NORMAL
The scroll direction is normal.
Definition SDL3pp_mouse.h:388
SDL_SystemCursor SystemCursor
Cursor types for Cursor.CreateSystem().
Definition SDL3pp_mouse.h:79
constexpr SystemCursor SYSTEM_CURSOR_SE_RESIZE
Window resize bottom-right.
Definition SDL3pp_mouse.h:146
constexpr MouseButton BUTTON_RIGHT
Right button.
Definition SDL3pp_mouse.h:375
CursorShared share()
Move this cursor into a CursorShared.
Definition SDL3pp_mouse.h:338
constexpr SystemCursor SYSTEM_CURSOR_W_RESIZE
Window resize left. May be EW_RESIZE.
Definition SDL3pp_mouse.h:157
constexpr MouseButton BUTTON_MIDDLE
Middle button.
Definition SDL3pp_mouse.h:373
CursorRef GetCursor()
Get the active cursor.
Definition SDL3pp_mouse.h:802
constexpr SystemCursor SYSTEM_CURSOR_NESW_RESIZE
Double arrow pointing northeast and southwest.
Definition SDL3pp_mouse.h:104
constexpr MouseWheelDirection MOUSEWHEEL_FLIPPED
The scroll direction is flipped / natural.
Definition SDL3pp_mouse.h:391
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:615
constexpr MouseButtonFlags ButtonMask(MouseButton button)
Convert a button index to a button mask.
Definition SDL3pp_mouse.h:417
void CaptureMouse(bool enabled)
Capture the mouse and to track input outside an SDL window.
Definition SDL3pp_mouse.h:761
Uint32 MouseButtonFlags
A bitmask of pressed mouse buttons, as reported by GetMouseState, etc.
Definition SDL3pp_mouse.h:409
constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE
Window resize top-left.
Definition SDL3pp_mouse.h:134
void WarpMouse(float x, float y)
Move the mouse to the given position in global screen space.
Definition SDL3pp_mouse.h:666
constexpr SystemCursor SYSTEM_CURSOR_CROSSHAIR
Crosshair.
Definition SDL3pp_mouse.h:92
bool GetRelativeMouseMode() const
Query whether relative mouse mode is enabled for a window.
Definition SDL3pp_mouse.h:711
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:577
constexpr SystemCursor SYSTEM_CURSOR_NWSE_RESIZE
Double arrow pointing northwest and southeast.
Definition SDL3pp_mouse.h:100
constexpr SystemCursor SYSTEM_CURSOR_E_RESIZE
Window resize right. May be EW_RESIZE.
Definition SDL3pp_mouse.h:143
constexpr MouseButton BUTTON_X1
X1 button.
Definition SDL3pp_mouse.h:377
constexpr SystemCursor SYSTEM_CURSOR_NS_RESIZE
Double arrow pointing north and south.
Definition SDL3pp_mouse.h:111
const char * GetMouseNameForID(MouseID instance_id)
Get the name of a mouse.
Definition SDL3pp_mouse.h:490
constexpr SystemCursor SYSTEM_CURSOR_COUNT
COUNT.
Definition SDL3pp_mouse.h:160
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:172
void SetRelativeMouseMode(bool enabled)
Set relative mouse mode for a window.
Definition SDL3pp_mouse.h:695
constexpr MouseButtonFlags BUTTON_RMASK
Right button mask.
Definition SDL3pp_mouse.h:428
constexpr SystemCursor SYSTEM_CURSOR_NOT_ALLOWED
Not permitted.
Definition SDL3pp_mouse.h:122
constexpr MouseButton BUTTON_LEFT
Left button.
Definition SDL3pp_mouse.h:371
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:537
constexpr MouseButtonFlags BUTTON_LMASK
Left button mask.
Definition SDL3pp_mouse.h:422
constexpr MouseButtonFlags BUTTON_X2MASK
X2 button mask.
Definition SDL3pp_mouse.h:434
constexpr SystemCursor SYSTEM_CURSOR_WAIT
Wait.
Definition SDL3pp_mouse.h:90
Uint8 MouseButton
Represents a button index.
Definition SDL3pp_mouse.h:369
constexpr SystemCursor SYSTEM_CURSOR_SW_RESIZE
Window resize bottom-left.
Definition SDL3pp_mouse.h:153
constexpr SystemCursor SYSTEM_CURSOR_NE_RESIZE
Window resize top-right.
Definition SDL3pp_mouse.h:139
constexpr SystemCursor SYSTEM_CURSOR_DEFAULT
Default cursor. Usually an arrow.
Definition SDL3pp_mouse.h:81
constexpr MouseButtonFlags BUTTON_X1MASK
X1 button mask.
Definition SDL3pp_mouse.h:431
constexpr SystemCursor SYSTEM_CURSOR_EW_RESIZE
Double arrow pointing west and east.
Definition SDL3pp_mouse.h:108
CursorRef GetDefaultCursor()
Get the default cursor.
Definition SDL3pp_mouse.h:817
WindowRef GetMouseFocus()
Get the window which currently has mouse focus.
Definition SDL3pp_mouse.h:504
bool CursorVisible()
Return whether the cursor is currently being shown.
Definition SDL3pp_mouse.h:863
constexpr MouseButtonFlags BUTTON_MMASK
Middle button mask.
Definition SDL3pp_mouse.h:425
constexpr SystemCursor SYSTEM_CURSOR_N_RESIZE
Window resize top. May be NS_RESIZE.
Definition SDL3pp_mouse.h:136
constexpr SystemCursor SYSTEM_CURSOR_S_RESIZE
Window resize bottom. May be NS_RESIZE.
Definition SDL3pp_mouse.h:150
SDL_MouseWheelDirection MouseWheelDirection
Scroll direction types for the Scroll event.
Definition SDL3pp_mouse.h:386
void SetCursor(CursorRef cursor)
Set the active cursor.
Definition SDL3pp_mouse.h:783
ResourceShared< Cursor > CursorShared
Handle to a shared cursor.
Definition SDL3pp_mouse.h:62
OwnArray< MouseID > GetMice()
Get a list of currently connected mice.
Definition SDL3pp_mouse.h:468
constexpr SystemCursor SYSTEM_CURSOR_TEXT
Text selection. Usually an I-beam.
Definition SDL3pp_mouse.h:84
void HideCursor()
Hide the cursor.
Definition SDL3pp_mouse.h:848
constexpr MouseButton BUTTON_X2
X2 button.
Definition SDL3pp_mouse.h:379
void ShowCursor()
Show the cursor.
Definition SDL3pp_mouse.h:834
constexpr SystemCursor SYSTEM_CURSOR_PROGRESS
Program is busy but still interactive.
Definition SDL3pp_mouse.h:98
bool HasMouse()
Return whether a mouse is currently connected.
Definition SDL3pp_mouse.h:448
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:186
static void reset(SDL_Cursor *resource)
Free a previously-created cursor.
Definition SDL3pp_mouse.h:204
Unsafe Handle to cursor.
Definition SDL3pp_mouse.h:353
constexpr CursorUnsafe(Cursor &&other)
Constructs CursorUnsafe from Cursor.
Definition SDL3pp_mouse.h:359
Handle to an owned cursor.
Definition SDL3pp_mouse.h:215
void Destroy()
Free a previously-created cursor.
Definition SDL3pp_mouse.h:329
static Cursor Create(const Uint8 *data, const Uint8 *mask, const SDL_Point &size, const SDL_Point &hot)
Create a cursor using the specified bitmap data and mask (in MSB format).
Definition SDL3pp_mouse.h:257
static Cursor CreateColor(SurfaceRef surface, const SDL_Point &hot)
Create a color cursor.
Definition SDL3pp_mouse.h:293
static Cursor CreateSystem(SystemCursor id)
Create a system cursor.
Definition SDL3pp_mouse.h:312
A collection of pixels used in software blitting.
Definition SDL3pp_surface.h:153
Represents a handle to a window.
Definition SDL3pp_video.h:860