Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Throttle hover events to 10Hz (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluemarvin authored and MortimerGoro committed Sep 28, 2018
1 parent e6c2ba2 commit 164b1b5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
21 changes: 19 additions & 2 deletions app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ static const int GestureSwipeRight = 1;

static const float kScrollFactor = 20.0f; // Just picked what fell right.
static const float kWorldDPIRatio = 2.0f/720.0f;
static const double kHoverRate = 1.0 / 10.0;

#if SPACE_THEME == 1
static const std::string CubemapDay = "cubemap/space";
Expand Down Expand Up @@ -235,6 +236,20 @@ OutOfDeadZone(Controller& aController, const float aX, const float aY) {
return !aController.inDeadZone;
}

static bool
ThrottleHoverEvent(Controller& aController, const double aTimestamp, const bool aIsPressed, const bool aWasPressed) {
if (aIsPressed || aWasPressed) {
return false;
}

if ((aTimestamp - aController.lastHoverEvent) < kHoverRate) {
return true;
}

aController.lastHoverEvent = aTimestamp;
return false;
}

void
BrowserWorld::State::UpdateControllers(bool& aRelayoutWidgets) {
std::vector<Widget*> active;
Expand Down Expand Up @@ -310,12 +325,14 @@ BrowserWorld::State::UpdateControllers(bool& aRelayoutWidgets) {
}
const bool moved = pressed ? OutOfDeadZone(controller, theX, theY)
: (controller.pointerX != theX) || (controller.pointerY != theY);
const bool throttled = ThrottleHoverEvent(controller, context->GetTimestamp(), pressed, wasPressed);

if (moved || (controller.widget != handle) || (pressed != wasPressed)) {
if ((!throttled && moved) || (controller.widget != handle) || (pressed != wasPressed)) {
controller.widget = handle;
controller.pointerX = theX;
controller.pointerY = theY;
VRBrowser::HandleMotionEvent(handle, controller.index, jboolean(pressed), controller.pointerX, controller.pointerY);
VRBrowser::HandleMotionEvent(handle, controller.index, jboolean(pressed),
controller.pointerX, controller.pointerY);
}
if ((controller.scrollDeltaX != 0.0f) || controller.scrollDeltaY != 0.0f) {
VRBrowser::HandleScrollEvent(controller.widget, controller.index,
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/cpp/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Controller::operator=(const Controller& aController) {
numAxes = aController.numAxes;
leftHanded = aController.leftHanded;
inDeadZone = aController.inDeadZone;
lastHoverEvent = aController.lastHoverEvent;
return *this;
}

Expand Down Expand Up @@ -80,6 +81,7 @@ Controller::Reset() {
numAxes = 0;
leftHanded = false;
inDeadZone = true;
lastHoverEvent = 0.0;
}

} // namespace crow
1 change: 1 addition & 0 deletions app/src/main/cpp/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct Controller {
uint32_t numAxes;
bool leftHanded;
bool inDeadZone;
double lastHoverEvent;

Controller();
Controller(const Controller& aController);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/vrb

0 comments on commit 164b1b5

Please sign in to comment.