Skip to content

Commit

Permalink
Refactor|Canvas: Separate method for showing and hiding the cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed May 10, 2012
1 parent 40a1540 commit adf4464
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
5 changes: 5 additions & 0 deletions doomsday/engine/portable/include/canvas.h
Expand Up @@ -133,6 +133,11 @@ class Canvas : public QGLWidget

bool isMouseTrapped() const;

/**
* Determines if the mouse cursor is currently visible or not.
*/
bool isCursorVisible() const;

/**
* Redraws the Canvas contents immediately. Does not return until the frame
* has been swapped to the screen. This means if vsync is enabled, this
Expand Down
36 changes: 27 additions & 9 deletions doomsday/engine/portable/src/canvas.cpp
Expand Up @@ -71,6 +71,7 @@ struct Canvas::Instance
void (*drawCallback)(Canvas&);
void (*resizedCallback)(Canvas&);
void (*focusCallback)(Canvas&, bool);
bool cursorHidden;
bool mouseGrabbed;
#ifdef WIN32
bool altIsDown;
Expand All @@ -85,6 +86,7 @@ struct Canvas::Instance
drawCallback(0),
resizedCallback(0),
focusCallback(0),
cursorHidden(false),
mouseGrabbed(false)
{
wheelDir[0] = wheelDir[1] = 0;
Expand All @@ -93,6 +95,24 @@ struct Canvas::Instance
#endif
}

void showCursor(bool yes)
{
LOG_DEBUG("showing cursor %b (presently visible? %b)") << yes << !cursorHidden;

if(!yes && !cursorHidden)
{
cursorHidden = true;
self->setCursor(QCursor(Qt::BlankCursor));
qApp->setOverrideCursor(QCursor(Qt::BlankCursor));
}
else if(yes && cursorHidden)
{
cursorHidden = false;
qApp->restoreOverrideCursor();
self->setCursor(QCursor(Qt::ArrowCursor)); // Default cursor.
}
}

void grabMouse()
{
if(!self->isVisible()) return;
Expand All @@ -109,13 +129,7 @@ struct Canvas::Instance
// Start tracking the mouse now.
QCursor::setPos(self->mapToGlobal(self->rect().center()));
self->grabMouse();
self->setCursor(QCursor(Qt::BlankCursor));
qApp->setOverrideCursor(QCursor(Qt::BlankCursor));
/*
#ifndef LIBDENG_CANVAS_TRACK_WITH_MOUSE_MOVE_EVENTS
QTimer::singleShot(MOUSE_TRACK_INTERVAL, self, SLOT(trackMousePosition()));
#endif
*/
showCursor(false);
#endif

#ifdef MACOSX
Expand All @@ -133,8 +147,7 @@ struct Canvas::Instance

#ifndef WIN32
self->releaseMouse();
qApp->restoreOverrideCursor();
self->setCursor(QCursor(Qt::ArrowCursor)); // Default cursor.
showCursor(true);
#endif
#ifdef MACOSX
//CGAssociateMouseAndMouseCursorPosition(true);
Expand Down Expand Up @@ -251,6 +264,11 @@ bool Canvas::isMouseTrapped() const
return d->mouseGrabbed;
}

bool Canvas::isCursorVisible() const
{
return !d->cursorHidden;
}

void Canvas::forceImmediateRepaint()
{
QPaintEvent ev(rect());
Expand Down

0 comments on commit adf4464

Please sign in to comment.