Skip to content

Commit

Permalink
Refactor: Mouse trapping is the window's responsibility
Browse files Browse the repository at this point in the history
Canvas will tell the mouse driver when to trap or untrap the mouse.
Other code will tell the window if they wish the mouse be trapped
or untrapped.
  • Loading branch information
skyjake committed Mar 22, 2012
1 parent ef89ded commit cf27077
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
6 changes: 6 additions & 0 deletions doomsday/engine/portable/include/window.h
Expand Up @@ -291,12 +291,18 @@ void Window_RestoreState(Window* wnd);
/**
* Activates or deactivates the window mouse trap. When trapped, the mouse cursor is
* not visible and all mouse motions are interpreted as deltas.
*
* @param wnd Window instance.
* @param enable @c true, if the mouse is to be trapped in the window.
*
* @note This is a C wrapper for Canvas::trapMouse().
*/
void Window_TrapMouse(const Window* wnd, boolean enable);

void* Window_NativeHandle(const Window* wnd);

#ifdef _DEBUG
/// @todo Get rid of this; libdeng2 should do debug output.
void debugPrint(const char* msg);
#endif

Expand Down
16 changes: 12 additions & 4 deletions doomsday/engine/portable/src/canvas.cpp
Expand Up @@ -70,25 +70,33 @@ struct Canvas::Instance
{
if(mouseGrabbed) return;

// Tell the mouse driver that the mouse is supposed to be trapped now.
mouseGrabbed = true;
Mouse_Trap(true);

// Start grabbing the mouse now.
#ifndef WIN32
// Start tracking the mouse now.
QCursor::setPos(self->mapToGlobal(self->rect().center()));
self->grabMouse();
self->setCursor(QCursor(Qt::BlankCursor));
qApp->setOverrideCursor(QCursor(Qt::BlankCursor));

QTimer::singleShot(1, self, SLOT(trackMousePosition()));
#endif
}

void ungrabMouse()
{
if(!mouseGrabbed) return;

mouseGrabbed = false;
#ifndef WIN32
self->releaseMouse();
qApp->restoreOverrideCursor();
self->setCursor(QCursor(Qt::ArrowCursor)); // Default cursor.
#endif

// Tell the mouse driver that the mouse is untrapepd.
mouseGrabbed = false;
Mouse_Trap(false);
}
};

Expand Down Expand Up @@ -339,7 +347,7 @@ void Canvas::mouseReleaseEvent(QMouseEvent* ev)
if(!d->mouseGrabbed)
{
// Start grabbing after a click.
Mouse_Trap(true);
trapMouse();
return;
}

Expand Down
4 changes: 3 additions & 1 deletion doomsday/engine/portable/src/mouse_qt.c
Expand Up @@ -21,6 +21,7 @@
* 02110-1301 USA</small>
*/

#include "dd_share.h"
#include "sys_input.h"
#include "window.h"
#include <string.h>
Expand Down Expand Up @@ -75,7 +76,8 @@ static void Mouse_Qt_GetState(mousestate_t *state)

static void Mouse_Qt_Trap(boolean enabled)
{
Window_TrapMouse(Window_Main(), enabled);
// nothing to do -- the window will submit input to us when trapped
DENG_UNUSED(enabled);
}

void Mouse_Qt_SubmitButton(int button, boolean isDown)
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/window.cpp
Expand Up @@ -184,7 +184,7 @@ struct ddwindow_s
{
if(DisplayMode_Change(mode, true /* fullscreen: capture */))
{
Mouse_Trap(true);
Window_TrapMouse(this, true);

geometry.size.width = DisplayMode_Current()->width;
geometry.size.height = DisplayMode_Current()->height;
Expand Down Expand Up @@ -899,7 +899,7 @@ static void windowFocusChanged(Canvas& canvas, bool focus)
{
DD_ClearEvents();
I_ResetAllDevices();
Mouse_Trap(false);
Window_TrapMouse(wnd, false);
}

#if 0
Expand Down

0 comments on commit cf27077

Please sign in to comment.