Files
UnrealEngineUWP/Engine/Source/Runtime/Slate/Private/Framework/Layout/InertialScrollManager.cpp
ryan durand 0f0464a30e Updating copyright for Engine Runtime.
#rnx
#rb none


#ROBOMERGE-OWNER: ryan.durand
#ROBOMERGE-AUTHOR: ryan.durand
#ROBOMERGE-SOURCE: CL 10869210 via CL 10869511 via CL 10869900
#ROBOMERGE-BOT: (v613-10869866)

[CL 10870549 by ryan durand in Main branch]
2019-12-26 14:45:42 -05:00

70 lines
1.8 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "Framework/Layout/InertialScrollManager.h"
float FInertialScrollManager::FrictionCoefficient = 2.0f;
float FInertialScrollManager::StaticVelocityDrag = 100;
FInertialScrollManager::FInertialScrollManager(double InSampleTimeout)
: ScrollVelocity(0.f)
, SampleTimeout(InSampleTimeout)
{
}
void FInertialScrollManager::AddScrollSample(float Delta, double CurrentTime)
{
new( ScrollSamples ) FScrollSample(Delta, CurrentTime);
float Total = 0;
double OldestTime = 0;
for ( int32 VelIdx = ScrollSamples.Num() - 1; VelIdx >= 0; --VelIdx )
{
const double SampleTime = ScrollSamples[VelIdx].Time;
const float SampleDelta = ScrollSamples[VelIdx].Delta;
if ( CurrentTime - SampleTime > SampleTimeout )
{
ScrollSamples.RemoveAt(VelIdx);
}
else
{
if ( SampleTime < OldestTime || OldestTime == 0 )
{
OldestTime = SampleTime;
}
Total += SampleDelta;
}
}
// Set the current velocity to the average of the previous recent samples
const double Duration = (OldestTime > 0) ? (CurrentTime - OldestTime) : 0;
if ( Duration > 0 )
{
ScrollVelocity = Total / Duration;
}
else
{
ScrollVelocity = 0;
}
}
void FInertialScrollManager::UpdateScrollVelocity(const float InDeltaTime)
{
const float VelocityLostPerSecond = ScrollVelocity > 0 ? StaticVelocityDrag : -StaticVelocityDrag;
const float DeltaVelocity = FrictionCoefficient * ScrollVelocity * InDeltaTime + VelocityLostPerSecond * InDeltaTime;
if ( ScrollVelocity > 0 )
{
ScrollVelocity = FMath::Max<float>(0.f, ScrollVelocity - DeltaVelocity);
}
else if ( ScrollVelocity < 0 )
{
ScrollVelocity = FMath::Min<float>(0.f, ScrollVelocity - DeltaVelocity);
}
}
void FInertialScrollManager::ClearScrollVelocity()
{
ScrollVelocity = 0;
}