Skip to content

Commit

Permalink
Adjust gesture timing constants / respect DPI for inertial scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
peak3d committed Mar 23, 2020
1 parent 900d00d commit c7daa3e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
26 changes: 18 additions & 8 deletions xbmc/input/InertialScrollingHandler.cpp
Expand Up @@ -14,18 +14,21 @@
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindowManager.h"
#include "input/Key.h"
#include "input/touch/generic/GenericTouchInputHandler.h"
#include "utils/TimeUtils.h"
#include "utils/log.h"
#include "windowing/WinSystem.h"

#include <cmath>
#include <numeric>

//time for reaching velocity 0 in secs
// time for reaching velocity 0 in secs
#define TIME_TO_ZERO_SPEED 1.0f
//minimum speed for doing inertial scroll is 100 pixels / s
#define MINIMUM_SPEED_FOR_INERTIA 100
//maximum time between last movement and gesture end in ms to consider as moving
// minimum speed for doing inertial scroll is 100 pixels / s
#define MINIMUM_SPEED_FOR_INERTIA 200
// maximum speed for reducing time to zero
#define MAXIMUM_SPEED_FOR_REDUCTION 750
// maximum time between last movement and gesture end in ms to consider as moving
#define MAXIMUM_DELAY_FOR_INERTIA 200

CInertialScrollingHandler::CInertialScrollingHandler()
Expand Down Expand Up @@ -95,8 +98,15 @@ bool CInertialScrollingHandler::CheckForInertialScrolling(const CAction* action)
auto velocityX = velocitySum.x / m_panPoints.size();
auto velocityY = velocitySum.y / m_panPoints.size();

if (std::abs(velocityX) > MINIMUM_SPEED_FOR_INERTIA || std::abs(velocityY) > MINIMUM_SPEED_FOR_INERTIA)
m_timeToZero = TIME_TO_ZERO_SPEED;
auto velocityMax = std::max(std::abs(velocityX), std::abs(velocityY));
float dpiScale = CGenericTouchInputHandler::GetInstance().GetScreenDPI() / 160.0f;

if (velocityMax > MINIMUM_SPEED_FOR_INERTIA * dpiScale)
{
if (velocityMax < MAXIMUM_SPEED_FOR_REDUCTION * dpiScale)
m_timeToZero = (m_timeToZero * velocityMax) / (MAXIMUM_SPEED_FOR_REDUCTION * dpiScale);

bool inertialRequested = false;
CGUIMessage message(GUI_MSG_GESTURE_NOTIFY, 0, 0, static_cast<int> (velocityX), static_cast<int> (velocityY));

Expand Down Expand Up @@ -128,8 +138,8 @@ bool CInertialScrollingHandler::CheckForInertialScrolling(const CAction* action)
//calc deacceleration for fullstop in TIME_TO_ZERO_SPEED secs
//v = a*t + v0 -> set v = 0 because we want to stop scrolling
//a = -v0 / t
m_inertialDeacceleration.x = -1*m_iFlickVelocity.x/TIME_TO_ZERO_SPEED;
m_inertialDeacceleration.y = -1*m_iFlickVelocity.y/TIME_TO_ZERO_SPEED;
m_inertialDeacceleration.x = -1 * m_iFlickVelocity.x / m_timeToZero;
m_inertialDeacceleration.y = -1 * m_iFlickVelocity.y / m_timeToZero;

m_inertialStartTime = CTimeUtils::GetFrameTime();//start time of inertial scrolling
ret = true;
Expand Down Expand Up @@ -159,7 +169,7 @@ bool CInertialScrollingHandler::ProcessInertialScroll(float frameTime)
float absoluteInertialTime = (CTimeUtils::GetFrameTime() - m_inertialStartTime)/(float)1000;

//as long as we aren't over the overall inertial scroll time - do the deacceleration
if ( absoluteInertialTime < TIME_TO_ZERO_SPEED )
if (absoluteInertialTime < m_timeToZero)
{
//v = s/t -> s = t * v
xMovement = frameTime * m_iFlickVelocity.x;
Expand Down
1 change: 1 addition & 0 deletions xbmc/input/InertialScrollingHandler.h
Expand Up @@ -45,4 +45,5 @@ class CInertialScrollingHandler
CPoint m_iLastGesturePoint;
CVector m_inertialDeacceleration;
unsigned int m_inertialStartTime = 0;
float m_timeToZero = 0.0f;
};
1 change: 1 addition & 0 deletions xbmc/input/touch/ITouchInputHandler.h
Expand Up @@ -86,6 +86,7 @@ class ITouchInputHandler : public ITouchInputHandling
virtual bool UpdateTouchPointer(int32_t pointer, float x, float y, int64_t time, float size = 0.0f) { return false; }

void SetScreenDPI(float dpi) { if (dpi > 0.0f) m_dpi = dpi; }
float GetScreenDPI() { return m_dpi; }

protected:
/*!
Expand Down
2 changes: 1 addition & 1 deletion xbmc/input/touch/generic/GenericTouchInputHandler.cpp
Expand Up @@ -19,7 +19,7 @@

namespace
{
constexpr int TOUCH_HOLD_TIMEOUT = 1000;
constexpr int TOUCH_HOLD_TIMEOUT = 500;
}

CGenericTouchInputHandler::CGenericTouchInputHandler()
Expand Down

0 comments on commit c7daa3e

Please sign in to comment.