载入中...
搜索中...
未找到
Mouse.cpp
浏览该文件的文档.
1#include "mouse/sdl/Mouse.h"
2#include "window/Window.h"
3#include "window/sdl/Window.h"
4
5#include <SDL2/SDL_mouse.h>
6#if defined(_WIN32)
7#include <windows.h>
8#endif
9
10namespace eve::mouse::sdl {
11
12// SDL reports mouse coordinates in the window coordinate system in OS X, but
13// we want them in pixel coordinates (may be different with high-DPI enabled.)
14static void windowToDPICoords(double *x, double *y)
15{
16 // if (window)
17 // window->windowToDPICoords(x, y);
18}
19
20// And vice versa for setting mouse coordinates.
21static void DPIToWindowCoords(double *x, double *y)
22{
23 // if (window)
24 // window->DPIToWindowCoords(x, y);
25}
26
27
29 : curCursor(nullptr)
30{
31 // SDL may need the video subsystem in order to clean up the cursor when
32 // quitting. Subsystems are reference-counted.
33 SDL_InitSubSystem(SDL_INIT_VIDEO);
34}
35
37{
38 if (curCursor)
39 setCursor();
40
41 for (auto &c : systemCursors)
42 delete c.second;
43
44 SDL_QuitSubSystem(SDL_INIT_VIDEO);
45}
46
48{
49 return new Cursor(data, hotx, hoty);
50}
51
53{
54 Cursor *cursor = nullptr;
55 auto it = systemCursors.find(cursortype);
56
57 if (it != systemCursors.end())
58 cursor = it->second;
59 else
60 {
61 cursor = new Cursor(cursortype);
62 systemCursors[cursortype] = cursor;
63 }
64
65 return cursor;
66}
67
69{
70 curCursor = cursor;
71 SDL_SetCursor((SDL_Cursor *) cursor->getHandle());
72}
73
75{
76 curCursor = nullptr;
77 SDL_SetCursor(SDL_GetDefaultCursor());
78}
79
81{
82 return curCursor;
83}
84
85
87{
88 return SDL_GetDefaultCursor() != nullptr;
89}
90
91double Mouse::getX() const {
92 double x, y;
93 getPosition(x, y);
94 return x;
95}
96
97double Mouse::getY() const {
98 double x, y;
99 getPosition(x, y);
100 return y;
101}
102
103void Mouse::getPosition(double &x, double &y) const {
104 int mx, my;
105 SDL_GetMouseState(&mx, &my);
106#if defined(_WIN32)
107 // A warp can move the OS cursor without delivering WM_MOUSEMOVE. With no
108 // mouse focus SDL retains its old event coordinates; query the OS relative
109 // to the keyboard-focused window instead. Relative mode stays event-based.
110 if (!SDL_GetMouseFocus() && !SDL_GetRelativeMouseMode()) {
111 if (auto *focusedWindow = SDL_GetKeyboardFocus()) {
112 POINT cursor;
113 // SDL_GetGlobalMouseState has no query-failure status. A service
114 // desktop can deny GetCursorPos; preserve SDL's cache in that case.
115 if (GetCursorPos(&cursor)) {
116 int windowX, windowY;
117 SDL_GetWindowPosition(focusedWindow, &windowX, &windowY);
118 mx = cursor.x - windowX;
119 my = cursor.y - windowY;
120 }
121 }
122 }
123#endif
124
125 x = (double)mx;
126 y = (double)my;
127 windowToDPICoords(&x, &y);
128}
129
130void Mouse::setPosition(double x, double y)
131{
132 // Avoid `auto window = getModInst(window, ...)` — local name shadows namespace `window`.
133 auto *winMod = eve::ModuleManager::getInstance<eve::window::Window>("Window");
134
135 SDL_Window *handle = nullptr;
136 if (winMod)
137 handle = (SDL_Window *) winMod->getHandle();
138
139 DPIToWindowCoords(&x, &y);
140 if (handle) {
141 SDL_WarpMouseInWindow(handle, (int) x, (int) y);
142 // SDL_WarpMouse doesn't directly update SDL's internal mouse state on
143 // Linux/Windows; pump so the next getPosition sees the new coords.
144 SDL_PumpEvents();
145 }
146}
147
148void Mouse::setX(double x)
149{
150 setPosition(x, getY());
151}
152
153void Mouse::setY(double y)
154{
155 setPosition(getX(), y);
156}
157
159{
160 SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
161}
162
163bool Mouse::isDown(const std::vector<int> &buttons) const
164{
165 Uint32 buttonstate = SDL_GetMouseState(nullptr, nullptr);
166
167 for (int button : buttons)
168 {
169 if (button <= 0)
170 continue;
171
172 // We use button index 2 to represent the right mouse button, but SDL
173 // uses 2 to represent the middle mouse button.
174 switch (button)
175 {
176 case 2:
177 button = SDL_BUTTON_RIGHT;
178 break;
179 case 3:
180 button = SDL_BUTTON_MIDDLE;
181 break;
182 }
183
184 if (buttonstate & SDL_BUTTON(button))
185 return true;
186 }
187
188 return false;
189}
190
192{
193 return SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE;
194}
195
196void Mouse::setGrabbed(bool grab)
197{
198 // if (window)
199 // window->setMouseGrab(grab);
200}
201
203{
204 // if (window)
205 // return window->isMouseGrabbed();
206 // else
207 return false;
208}
209
211{
212 return SDL_SetRelativeMouseMode(relative ? SDL_TRUE : SDL_FALSE) == 0;
213}
214
216{
217 return SDL_GetRelativeMouseMode() != SDL_FALSE;
218}
219
221{
222 int x = 0, y = 0;
223 SDL_GetRelativeMouseState(&x, &y);
224 lastRelY = y;
225 return double(x);
226}
227
229{
230 return double(lastRelY);
231}
232
233} // eve::mouse::sdl
double y
double x
Vec3 relative
Definition AnimSmr.cpp:164
std::size_t cursor
std::int32_t c
PrimitiveHandle handle
bool visible
Represents raw pixel data.
Definition ImageData.h:38
Opaque mouse cursor (system-defined or custom image).
Definition Cursor.h:12
SDL mouse cursor (custom image or named system cursor).
Definition Cursor.h:14
void setVisible(bool visible) override
Shows or hides the OS cursor.
Definition Mouse.cpp:158
bool isCursorSupported() const override
True when the platform supports software/hardware cursors.
Definition Mouse.cpp:86
bool isGrabbed() const override
True when cursor grab is active.
Definition Mouse.cpp:202
virtual ~Mouse()
Releases cached system cursors.
Definition Mouse.cpp:36
double getY() const override
Cursor Y in window logical coordinates.
Definition Mouse.cpp:97
eve::mouse::Cursor * newCursor(eve::image::ImageData *data, int hotx, int hoty) override
Builds a custom cursor from image pixels with hotspot (hotx, hoty).
Definition Mouse.cpp:47
void setPosition(double x, double y) override
Warps the cursor to a logical window position.
Definition Mouse.cpp:130
void setCursor() override
Installs a cursor as the active OS cursor.
Definition Mouse.cpp:74
eve::mouse::Cursor * getSystemCursor(std::string cursortype) override
Returns a named system cursor (e.g. "arrow", "ibeam"); may cache.
Definition Mouse.cpp:52
void setGrabbed(bool grab) override
Confines the cursor to the window when grab is true.
Definition Mouse.cpp:196
void getPosition(double &x, double &y) const override
Writes cursor position in window logical coordinates.
Definition Mouse.cpp:103
bool getRelativeMode() const override
True when relative mouse mode is active.
Definition Mouse.cpp:215
void setX(double x) override
Warps the cursor X while keeping Y.
Definition Mouse.cpp:148
bool isVisible() const override
True when the OS cursor is visible.
Definition Mouse.cpp:191
double getX() const override
Cursor X in window logical coordinates.
Definition Mouse.cpp:91
bool setRelativeMode(bool relative) override
Enables relative mouse mode (hidden cursor, motion deltas).
Definition Mouse.cpp:210
eve::mouse::Cursor * getCursor() const override
Currently installed cursor, or null for the default.
Definition Mouse.cpp:80
double getMovementX() override
Pixel delta since the last getMovementX() (also latches Y for getMovementY).
Definition Mouse.cpp:220
double getMovementY() const override
Pixel delta latched by the most recent getMovementX(); call X then Y each frame.
Definition Mouse.cpp:228
void setY(double y) override
Warps the cursor Y while keeping X.
Definition Mouse.cpp:153
Mouse()
Constructs the SDL mouse module with default cursor state.
Definition Mouse.cpp:28
bool isDown(const std::vector< int > &buttons) const override
True if any listed mouse button index is currently down.
Definition Mouse.cpp:163