Skip to content

Commit 191fd23

Browse files
Merge pull request #1 from InfinityGhost/smoothingfilter
Add hawku/TabletDriver TabletFilterSmoothing
2 parents fe0ef42 + 967c725 commit 191fd23

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using TabletDriverPlugin;
3+
using TabletDriverPlugin.Attributes;
4+
using TabletDriverPlugin.Tablet;
5+
6+
namespace TabletDriverFilters
7+
{
8+
using static Math;
9+
10+
[PluginName("TabletDriver Smoothing Filter")]
11+
public class TabletFilterSmoothing : IFilter
12+
{
13+
private DateTime? _lastFilterTime;
14+
private Point _lastPos;
15+
private float _timerInterval;
16+
private const float _threshold = 0.63f;
17+
18+
public Point Filter(Point point)
19+
{
20+
var timeDelta = DateTime.Now - _lastFilterTime;
21+
// If a time difference hasn't been established or it has been 100 milliseconds since the last filter
22+
if (timeDelta == null || timeDelta.Value.TotalMilliseconds > 100 || _lastPos == null)
23+
{
24+
SetPreviousState(point);
25+
return point;
26+
}
27+
else
28+
{
29+
Point pos = new Point(_lastPos.X, _lastPos.Y);
30+
float deltaX = point.X - _lastPos.X;
31+
float deltaY = point.Y - _lastPos.Y;
32+
33+
double stepCount = Latency / TimerInterval;
34+
double target = 1 - _threshold;
35+
double weight = 1.0 - (1.0 / Pow(1.0 / target, 1.0 / stepCount));
36+
37+
pos.X += (float)(deltaX * weight);
38+
pos.Y += (float)(deltaY * weight);
39+
SetPreviousState(pos);
40+
return pos;
41+
}
42+
}
43+
44+
private void SetPreviousState(Point lastPosition)
45+
{
46+
_lastPos = lastPosition;
47+
_lastFilterTime = DateTime.Now;
48+
}
49+
50+
public FilterStage FilterStage => FilterStage.PostTranspose;
51+
52+
[SliderProperty("Latency", 0f, 5f, 2f)]
53+
public float Latency { set; get; }
54+
55+
[UnitProperty("Timer Interval", "hz")]
56+
public float TimerInterval
57+
{
58+
set => _timerInterval = 1000f / value;
59+
get => _timerInterval;
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)