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
58{
60
63 : value(value)
64 {
65 }
66
68 constexpr CursorParam(std::nullptr_t _ = nullptr)
69 : value(nullptr)
70 {
71 }
72
74 constexpr explicit operator bool() const { return !!value; }
75
77 constexpr auto operator<=>(const CursorParam& other) const = default;
78
80 constexpr operator CursorRaw() const { return value; }
81};
82
88using SystemCursor = SDL_SystemCursor;
89
91 SDL_SYSTEM_CURSOR_DEFAULT;
92
94 SDL_SYSTEM_CURSOR_TEXT;
95
97constexpr SystemCursor SYSTEM_CURSOR_WAIT = SDL_SYSTEM_CURSOR_WAIT;
98
100 SDL_SYSTEM_CURSOR_CROSSHAIR;
101
103constexpr SystemCursor SYSTEM_CURSOR_PROGRESS = SDL_SYSTEM_CURSOR_PROGRESS;
104
106 SDL_SYSTEM_CURSOR_NWSE_RESIZE;
108
110 SDL_SYSTEM_CURSOR_NESW_RESIZE;
112
114 SDL_SYSTEM_CURSOR_EW_RESIZE;
115
117 SDL_SYSTEM_CURSOR_NS_RESIZE;
118
120constexpr SystemCursor SYSTEM_CURSOR_MOVE = SDL_SYSTEM_CURSOR_MOVE;
121
124 SDL_SYSTEM_CURSOR_NOT_ALLOWED;
125
127constexpr SystemCursor SYSTEM_CURSOR_POINTER = SDL_SYSTEM_CURSOR_POINTER;
128
133constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE = SDL_SYSTEM_CURSOR_NW_RESIZE;
134
136 SDL_SYSTEM_CURSOR_N_RESIZE;
137
139 SDL_SYSTEM_CURSOR_NE_RESIZE;
140
142 SDL_SYSTEM_CURSOR_E_RESIZE;
143
145 SDL_SYSTEM_CURSOR_SE_RESIZE;
147
149 SDL_SYSTEM_CURSOR_S_RESIZE;
150
152 SDL_SYSTEM_CURSOR_SW_RESIZE;
154
156 SDL_SYSTEM_CURSOR_W_RESIZE;
157
158constexpr SystemCursor SYSTEM_CURSOR_COUNT = SDL_SYSTEM_CURSOR_COUNT;
159
170using MouseID = SDL_MouseID;
171
182{
183 CursorRaw m_resource = nullptr;
184
185public:
187 constexpr Cursor() = default;
188
196 constexpr explicit Cursor(const CursorRaw resource)
197 : m_resource(resource)
198 {
199 }
200
202 constexpr Cursor(const Cursor& other) = delete;
203
205 constexpr Cursor(Cursor&& other)
206 : Cursor(other.release())
207 {
208 }
209
210 constexpr Cursor(const CursorRef& other) = delete;
211
212 constexpr Cursor(CursorRef&& other) = delete;
213
255 Cursor(const Uint8* data,
256 const Uint8* mask,
257 const PointRaw& size,
258 const PointRaw& hot)
259 : m_resource(
260 CheckError(SDL_CreateCursor(data, mask, size.x, size.y, hot.x, hot.y)))
261 {
262 }
263
291 Cursor(SurfaceParam surface, const PointRaw& hot)
292 : m_resource(CheckError(SDL_CreateColorCursor(surface, hot.x, hot.y)))
293 {
294 }
295
310 : m_resource(CheckError(SDL_CreateSystemCursor(id)))
311 {
312 }
313
315 ~Cursor() { SDL_DestroyCursor(m_resource); }
316
319 {
320 std::swap(m_resource, other.m_resource);
321 return *this;
322 }
323
325 constexpr CursorRaw get() const { return m_resource; }
326
328 constexpr CursorRaw release()
329 {
330 auto r = m_resource;
331 m_resource = nullptr;
332 return r;
333 }
334
336 constexpr auto operator<=>(const Cursor& other) const = default;
337
339 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
340
342 constexpr explicit operator bool() const { return !!m_resource; }
343
345 constexpr operator CursorParam() const { return {m_resource}; }
346
362 void Destroy();
363
380 void Set();
381};
382
385{
394 : Cursor(resource.value)
395 {
396 }
397
399 CursorRef(const CursorRef& other)
400 : Cursor(other.get())
401 {
402 }
403
406};
407
413using MouseWheelDirection = SDL_MouseWheelDirection;
414
416 SDL_MOUSEWHEEL_NORMAL;
417
419 SDL_MOUSEWHEEL_FLIPPED;
420
426
427constexpr MouseButton BUTTON_LEFT = SDL_BUTTON_LEFT;
428
429constexpr MouseButton BUTTON_MIDDLE = SDL_BUTTON_MIDDLE;
430
431constexpr MouseButton BUTTON_RIGHT = SDL_BUTTON_RIGHT;
432
433constexpr MouseButton BUTTON_X1 = SDL_BUTTON_X1;
434
435constexpr MouseButton BUTTON_X2 = SDL_BUTTON_X2;
436
453
454constexpr MouseButtonFlags BUTTON_LMASK = SDL_BUTTON_LMASK;
455
456constexpr MouseButtonFlags BUTTON_MMASK = SDL_BUTTON_MMASK;
457
458constexpr MouseButtonFlags BUTTON_RMASK = SDL_BUTTON_RMASK;
459
460constexpr MouseButtonFlags BUTTON_X1MASK = SDL_BUTTON_X1MASK;
461
462constexpr MouseButtonFlags BUTTON_X2MASK = SDL_BUTTON_X2MASK;
463
466{
467 return SDL_BUTTON_MASK(button);
468}
469
481inline bool HasMouse() { return SDL_HasMouse(); }
482
502{
503 int count;
504 auto data = CheckError(SDL_GetMice(&count));
505 return OwnArray<MouseID>{data, size_t(count)};
506}
507
523inline const char* GetMouseNameForID(MouseID instance_id)
524{
525 return SDL_GetMouseNameForID(instance_id);
526}
527
537inline WindowRef GetMouseFocus() { return {SDL_GetMouseFocus()}; }
538
570inline MouseButtonFlags GetMouseState(float* x, float* y)
571{
572 return SDL_GetMouseState(x, y);
573}
574
610inline MouseButtonFlags GetGlobalMouseState(float* x, float* y)
611{
612 return SDL_GetGlobalMouseState(x, y);
613}
614
648inline MouseButtonFlags GetRelativeMouseState(float* x, float* y)
649{
650 return SDL_GetRelativeMouseState(x, y);
651}
652
653inline void Window::WarpMouse(const FPointRaw& p)
654{
655 SDL_WarpMouseInWindow(m_resource, p.x, p.y);
656}
657
678inline void WarpMouse(const FPointRaw& p)
679{
680 CheckError(SDL_WarpMouseGlobal(p.x, p.y));
681}
682
707inline void Window::SetRelativeMouseMode(bool enabled)
708{
709 CheckError(SDL_SetWindowRelativeMouseMode(m_resource, enabled));
710}
711
724{
725 return SDL_GetWindowRelativeMouseMode(m_resource);
726}
727
773inline void CaptureMouse(bool enabled)
774{
775 CheckError(SDL_CaptureMouse(enabled));
776}
777
819inline Cursor CreateCursor(const Uint8* data,
820 const Uint8* mask,
821 const PointRaw& size,
822 const PointRaw& hot)
823{
824 return Cursor(data, mask, size, hot);
825}
826
855{
856 return Cursor(surface, hot);
857}
858
873
891inline void SetCursor(CursorParam cursor) { CheckError(SDL_SetCursor(cursor)); }
892
893inline void Cursor::Set() { SDL::SetCursor(m_resource); }
894
909inline CursorRef GetCursor() { return {SDL_GetCursor()}; }
910
925{
926 return {CheckError(SDL_GetDefaultCursor())};
927}
928
945inline void DestroyCursor(CursorRaw cursor) { SDL_DestroyCursor(cursor); }
946
948
961inline void ShowCursor() { CheckError(SDL_ShowCursor()); }
962
975inline void HideCursor() { CheckError(SDL_HideCursor()); }
976
990inline bool CursorVisible() { return SDL_CursorVisible(); }
991
993
994} // namespace SDL
995
996#endif /* SDL3PP_MOUSE_H_ */
The structure used to identify an SDL cursor.
Definition: SDL3pp_mouse.h:182
constexpr Cursor(Cursor &&other)
Move constructor.
Definition: SDL3pp_mouse.h:205
constexpr Cursor()=default
Default ctor.
Cursor(SystemCursor id)
Create a system cursor.
Definition: SDL3pp_mouse.h:309
Cursor(SurfaceParam surface, const PointRaw &hot)
Create a color cursor.
Definition: SDL3pp_mouse.h:291
constexpr CursorRaw get() const
Retrieves underlying CursorRaw.
Definition: SDL3pp_mouse.h:325
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_mouse.h:339
constexpr Cursor(const CursorRaw resource)
Constructs from CursorParam.
Definition: SDL3pp_mouse.h:196
Cursor & operator=(Cursor other)
Assignment operator.
Definition: SDL3pp_mouse.h:318
constexpr CursorRaw release()
Retrieves underlying CursorRaw and clear this.
Definition: SDL3pp_mouse.h:328
constexpr Cursor(const Cursor &other)=delete
Copy constructor.
Cursor(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:255
~Cursor()
Destructor.
Definition: SDL3pp_mouse.h:315
constexpr auto operator<=>(const Cursor &other) const =default
Comparison.
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:198
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:819
constexpr SystemCursor SYSTEM_CURSOR_MOVE
Four pointed arrow pointing north, south, east, and west.
Definition: SDL3pp_mouse.h:120
bool GetRelativeMouseMode() const
Query whether relative mouse mode is enabled for a window.
Definition: SDL3pp_mouse.h:723
constexpr SystemCursor SYSTEM_CURSOR_POINTER
Pointer that indicates a link. Usually a pointing hand.
Definition: SDL3pp_mouse.h:127
constexpr MouseWheelDirection MOUSEWHEEL_NORMAL
The scroll direction is normal.
Definition: SDL3pp_mouse.h:415
SDL_SystemCursor SystemCursor
Cursor types for Cursor.Cursor().
Definition: SDL3pp_mouse.h:88
constexpr SystemCursor SYSTEM_CURSOR_SE_RESIZE
Window resize bottom-right.
Definition: SDL3pp_mouse.h:144
constexpr MouseButton BUTTON_RIGHT
Right button.
Definition: SDL3pp_mouse.h:431
constexpr SystemCursor SYSTEM_CURSOR_W_RESIZE
Window resize left. May be EW_RESIZE.
Definition: SDL3pp_mouse.h:155
constexpr MouseButton BUTTON_MIDDLE
Middle button.
Definition: SDL3pp_mouse.h:429
CursorRef GetCursor()
Get the active cursor.
Definition: SDL3pp_mouse.h:909
void Destroy()
Free a previously-created cursor.
Definition: SDL3pp_mouse.h:947
void DestroyCursor(CursorRaw cursor)
Free a previously-created cursor.
Definition: SDL3pp_mouse.h:945
constexpr SystemCursor SYSTEM_CURSOR_NESW_RESIZE
Double arrow pointing northeast and southwest.
Definition: SDL3pp_mouse.h:109
constexpr MouseWheelDirection MOUSEWHEEL_FLIPPED
The scroll direction is flipped / natural.
Definition: SDL3pp_mouse.h:418
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:648
constexpr MouseButtonFlags ButtonMask(MouseButton button)
Returns mask for button.
Definition: SDL3pp_mouse.h:465
void CaptureMouse(bool enabled)
Capture the mouse and to track input outside an SDL window.
Definition: SDL3pp_mouse.h:773
Cursor CreateSystemCursor(SystemCursor id)
Create a system cursor.
Definition: SDL3pp_mouse.h:872
Uint32 MouseButtonFlags
A bitmask of pressed mouse buttons, as reported by GetMouseState, etc.
Definition: SDL3pp_mouse.h:452
constexpr SystemCursor SYSTEM_CURSOR_NW_RESIZE
Window resize top-left.
Definition: SDL3pp_mouse.h:133
constexpr SystemCursor SYSTEM_CURSOR_CROSSHAIR
Crosshair.
Definition: SDL3pp_mouse.h:99
void WarpMouse(const FPointRaw &p)
Move the mouse to the given position in global screen space.
Definition: SDL3pp_mouse.h:678
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:610
constexpr SystemCursor SYSTEM_CURSOR_NWSE_RESIZE
Double arrow pointing northwest and southeast.
Definition: SDL3pp_mouse.h:105
constexpr SystemCursor SYSTEM_CURSOR_E_RESIZE
Window resize right. May be EW_RESIZE.
Definition: SDL3pp_mouse.h:141
constexpr MouseButton BUTTON_X1
X1 button.
Definition: SDL3pp_mouse.h:433
constexpr SystemCursor SYSTEM_CURSOR_NS_RESIZE
Double arrow pointing north and south.
Definition: SDL3pp_mouse.h:116
const char * GetMouseNameForID(MouseID instance_id)
Get the name of a mouse.
Definition: SDL3pp_mouse.h:523
constexpr SystemCursor SYSTEM_CURSOR_COUNT
COUNT.
Definition: SDL3pp_mouse.h:158
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:170
constexpr MouseButtonFlags BUTTON_RMASK
RMASK.
Definition: SDL3pp_mouse.h:458
constexpr SystemCursor SYSTEM_CURSOR_NOT_ALLOWED
Not permitted. Usually a slashed circle or crossbones.
Definition: SDL3pp_mouse.h:123
void Set()
Set the active cursor.
Definition: SDL3pp_mouse.h:893
constexpr MouseButton BUTTON_LEFT
Left button.
Definition: SDL3pp_mouse.h:427
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:570
constexpr MouseButtonFlags BUTTON_LMASK
LMASK.
Definition: SDL3pp_mouse.h:454
constexpr MouseButtonFlags BUTTON_X2MASK
X2MASK.
Definition: SDL3pp_mouse.h:462
void SetRelativeMouseMode(bool enabled)
Set relative mouse mode for a window.
Definition: SDL3pp_mouse.h:707
constexpr SystemCursor SYSTEM_CURSOR_WAIT
Wait. Usually an hourglass or watch or spinning ball.
Definition: SDL3pp_mouse.h:97
Uint8 MouseButton
Represents a button index.
Definition: SDL3pp_mouse.h:425
constexpr SystemCursor SYSTEM_CURSOR_SW_RESIZE
Window resize bottom-left.
Definition: SDL3pp_mouse.h:151
void WarpMouse(const FPointRaw &p)
Move the mouse cursor to the given position within the window.
Definition: SDL3pp_mouse.h:653
void SetCursor(CursorParam cursor)
Set the active cursor.
Definition: SDL3pp_mouse.h:891
constexpr SystemCursor SYSTEM_CURSOR_NE_RESIZE
Window resize top-right. May be NESW_RESIZE.
Definition: SDL3pp_mouse.h:138
constexpr SystemCursor SYSTEM_CURSOR_DEFAULT
Default cursor. Usually an arrow.
Definition: SDL3pp_mouse.h:90
constexpr MouseButtonFlags BUTTON_X1MASK
X1MASK.
Definition: SDL3pp_mouse.h:460
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:113
CursorRef GetDefaultCursor()
Get the default cursor.
Definition: SDL3pp_mouse.h:924
WindowRef GetMouseFocus()
Get the window which currently has mouse focus.
Definition: SDL3pp_mouse.h:537
bool CursorVisible()
Return whether the cursor is currently being shown.
Definition: SDL3pp_mouse.h:990
constexpr MouseButtonFlags BUTTON_MMASK
MMASK.
Definition: SDL3pp_mouse.h:456
constexpr SystemCursor SYSTEM_CURSOR_N_RESIZE
Window resize top. May be NS_RESIZE.
Definition: SDL3pp_mouse.h:135
Cursor CreateColorCursor(SurfaceParam surface, const PointRaw &hot)
Create a color cursor.
Definition: SDL3pp_mouse.h:854
constexpr SystemCursor SYSTEM_CURSOR_S_RESIZE
Window resize bottom. May be NS_RESIZE.
Definition: SDL3pp_mouse.h:148
SDL_MouseWheelDirection MouseWheelDirection
Scroll direction types for the Scroll event.
Definition: SDL3pp_mouse.h:413
OwnArray< MouseID > GetMice()
Get a list of currently connected mice.
Definition: SDL3pp_mouse.h:501
constexpr SystemCursor SYSTEM_CURSOR_TEXT
Text selection. Usually an I-beam.
Definition: SDL3pp_mouse.h:93
void HideCursor()
Hide the cursor.
Definition: SDL3pp_mouse.h:975
constexpr MouseButton BUTTON_X2
X2 button.
Definition: SDL3pp_mouse.h:435
void ShowCursor()
Show the cursor.
Definition: SDL3pp_mouse.h:961
constexpr SystemCursor SYSTEM_CURSOR_PROGRESS
Program is busy but still interactive. Usually it's WAIT with an arrow.
Definition: SDL3pp_mouse.h:103
bool HasMouse()
Return whether a mouse is currently connected.
Definition: SDL3pp_mouse.h:481
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
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:343
::Uint8 Uint8
An unsigned 8-bit integer type.
Definition: SDL3pp_stdinc.h:291
Main include header for the SDL3pp library.
Safely wrap Cursor for non owning parameters.
Definition: SDL3pp_mouse.h:58
CursorRaw value
parameter's CursorRaw
Definition: SDL3pp_mouse.h:59
constexpr CursorParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_mouse.h:68
constexpr auto operator<=>(const CursorParam &other) const =default
Comparison.
constexpr CursorParam(CursorRaw value)
Constructs from CursorRaw.
Definition: SDL3pp_mouse.h:62
Semi-safe reference for Cursor.
Definition: SDL3pp_mouse.h:385
CursorRef(CursorParam resource)
Constructs from CursorParam.
Definition: SDL3pp_mouse.h:393
~CursorRef()
Destructor.
Definition: SDL3pp_mouse.h:405
CursorRef(const CursorRef &other)
Copy constructor.
Definition: SDL3pp_mouse.h:399
Safely wrap Surface for non owning parameters.
Definition: SDL3pp_surface.h:46
Semi-safe reference for Window.
Definition: SDL3pp_video.h:2967