FullscreenTouchSecheuristic.cpp [plain text]
#include "config.h"
#include "FullscreenTouchSecheuristic.h"
#if ENABLE(FULLSCREEN_API) && PLATFORM(IOS_FAMILY)
#include <wtf/MonotonicTime.h>
namespace WebKit {
double FullscreenTouchSecheuristic::scoreOfNextTouch(CGPoint location)
{
Seconds now = MonotonicTime::now().secondsSinceEpoch();
if (!m_lastTouchTime) {
m_lastTouchTime = WTFMove(now);
m_lastTouchLocation = WTFMove(location);
return 0;
}
Seconds deltaTime = now - m_lastTouchTime;
double coefficient = attenuationFactor(deltaTime);
m_lastScore = coefficient * distanceScore(location, m_lastTouchLocation, deltaTime) + (1 - coefficient) * m_lastScore;
m_lastTouchTime = now;
m_lastTouchLocation = location;
return m_lastScore;
}
void FullscreenTouchSecheuristic::reset()
{
m_lastTouchTime = 0_s;
m_lastTouchLocation = { };
m_lastScore = 0;
}
double FullscreenTouchSecheuristic::distanceScore(const CGPoint& nextLocation, const CGPoint& lastLocation, const Seconds& deltaTime)
{
double distance = sqrt(
m_xWeight * pow(nextLocation.x - lastLocation.x, 2) +
m_yWeight * pow(nextLocation.y - lastLocation.y, 2));
double sizeFactor = sqrt(
m_xWeight * pow(m_size.width, 2) +
m_yWeight * pow(m_size.height, 2));
double scaledDistance = distance / sizeFactor;
if (scaledDistance <= m_cutoff)
return scaledDistance * (m_rampUpSpeed / deltaTime);
double exponentialDistance = m_cutoff + pow((scaledDistance - m_cutoff) / (1 - m_cutoff), m_gamma);
return exponentialDistance * (m_rampUpSpeed / deltaTime);
}
double FullscreenTouchSecheuristic::attenuationFactor(Seconds delta)
{
double normalizedTimeDelta = delta / m_rampDownSpeed;
return std::max(std::min(normalizedTimeDelta * m_weight, 1.0), 0.0);
}
}
#endif // ENABLE(FULLSCREEN_API) && PLATFORM(IOS_FAMILY)