Skip to content

Commit

Permalink
Client|WindowSystem: Dispatch mouse position events at most once per …
Browse files Browse the repository at this point in the history
…frame

As far the regular widgets are concerned, there is no need to receive
mouse position events more frequently than once per frame. Now the
position is also checked so that the coordinates really have to be
different than in the previous event.
  • Loading branch information
skyjake committed Jul 16, 2013
1 parent 5556a97 commit 40407fb
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion doomsday/client/src/ui/windowsystem.cpp
Expand Up @@ -32,7 +32,11 @@ DENG2_PIMPL(WindowSystem)
Windows windows;
Style style;

Instance(Public *i) : Base(i)
// Mouse motion.
bool mouseMoved;
Vector2i latestMousePos;

Instance(Public *i) : Base(i), mouseMoved(false)
{
style.load(App::fileSystem().find("defaultstyle.pack").path());
}
Expand All @@ -41,6 +45,16 @@ DENG2_PIMPL(WindowSystem)
{
self.closeAll();
}

void dispatchLatestMousePosition()
{
if(mouseMoved)
{
mouseMoved = false;

self.main().root().processEvent(MouseEvent(MouseEvent::Absolute, latestMousePos));
}
}
};

WindowSystem::WindowSystem()
Expand Down Expand Up @@ -91,10 +105,33 @@ Style &WindowSystem::style()

bool WindowSystem::processEvent(Event const &event)
{
/*
* Mouse motion is filtered as it may be produced needlessly often with
* high-frequency mice. Note that this does not affect DirectInput mouse
* input at all (however, as DirectInput is polled once per frame, it is
* already filtered anyway).
*/

if(event.type() == Event::MousePosition)
{
MouseEvent const &mouse = event.as<MouseEvent>();

if(mouse.pos() != d->latestMousePos)
{
// This event will be emitted later, before widget tree update.
d->latestMousePos = mouse.pos();
d->mouseMoved = true;
}
return true;
}

// Dispatch the event to the main window's widget tree.
return main().root().processEvent(event);
}

void WindowSystem::timeChanged(Clock const &/*clock*/)
{
d->dispatchLatestMousePosition();

main().root().update();
}

0 comments on commit 40407fb

Please sign in to comment.