Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Platform: implement mouse capturing for SDL and GLFW #419

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Magnum/Platform/GlfwApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,10 @@ class GlfwApplication {
glfwSetCursorPos(_window, Double(position.x()), Double(position.y()));
}

/** @todo GLFW_CURSOR_CAPTURED, currently only in a branch:
https://github.com/glfw/glfw/issues/58
https://github.com/glfw/glfw/compare/captured-cursor-mode-r2 */

private:
/** @copydoc Sdl2Application::mousePressEvent() */
virtual void mousePressEvent(MouseEvent& event);
Expand Down
20 changes: 20 additions & 0 deletions src/Magnum/Platform/Sdl2Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,26 @@ class Sdl2Application {
void warpCursor(const Vector2i& position) {
SDL_WarpMouseInWindow(_window, position.x(), position.y());
}

/**
* @brief Whether the mouse is captured
* @m_since_latest
*
* @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten".
*/
bool isMouseCaptured() {
return SDL_GetWindowFlags(_window) & SDL_WINDOW_MOUSE_CAPTURE;
}

/**
* @brief Capture mouse
* @m_since_latest
*
* @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten".
*/
void setMouseCaptured(bool enabled) {
SDL_CaptureMouse(enabled ? SDL_TRUE : SDL_FALSE);
}
#endif

#ifdef MAGNUM_BUILD_DEPRECATED
Expand Down
5 changes: 4 additions & 1 deletion src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ struct Sdl2ApplicationTest: Platform::Application {
setWindowTitle("This is a UTF-8 Window Title™!");
}
#ifndef CORRADE_TARGET_EMSCRIPTEN
else if(event.key() == KeyEvent::Key::S) {
else if(event.key() == KeyEvent::Key::C) {
Debug{} << "toggling mouse capture to" << !isMouseCaptured();
setMouseCaptured(!isMouseCaptured());
} else if(event.key() == KeyEvent::Key::S) {
Debug{} << "setting window size, which should trigger a viewport event";
setWindowSize(Vector2i{300, 200});
} else if(event.key() == KeyEvent::Key::W) {
Expand Down