1// Aseprite
2// Copyright (C) 2020-2022 Igara Studio S.A.
3//
4// This program is distributed under the terms of
5// the End-User License Agreement for Aseprite.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include "app/tools/velocity.h"
12
13#include <algorithm>
14
15namespace app {
16namespace tools {
17
18VelocitySensor::VelocitySensor()
19{
20 reset();
21}
22
23void VelocitySensor::reset()
24{
25 m_firstPoint = true;
26 m_lastUpdate = base::current_tick();
27 m_velocity = Vec2(0.0f, 0.0f);
28}
29
30void VelocitySensor::updateWithDisplayPoint(const gfx::Point& screenPoint)
31{
32 const base::tick_t t = base::current_tick();
33 const base::tick_t dt = t - m_lastUpdate;
34 m_lastUpdate = t;
35
36 // Calculate the velocity (new screen point - old screen point)
37 if (m_firstPoint)
38 m_firstPoint = false;
39 else {
40 gfx::PointF newVelocity(screenPoint - m_lastPoint);
41
42 const float a = std::clamp(float(dt) / kFullUpdateMSecs, 0.0f, 1.0f);
43 m_velocity.x = (1.0f-a)*m_velocity.x + a*newVelocity.x;
44 m_velocity.y = (1.0f-a)*m_velocity.y + a*newVelocity.y;
45 }
46
47 m_lastPoint.x = screenPoint.x;
48 m_lastPoint.y = screenPoint.y;
49}
50
51} // namespace tools
52} // namespace app
53