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