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+
16+ public Point Filter ( Point point )
17+ {
18+ var timeDelta = DateTime . Now - _lastFilterTime ;
19+ // If a time difference hasn't been established or it has been 100 milliseconds since the last filter
20+ if ( timeDelta == null || timeDelta . Value . TotalMilliseconds > 100 )
21+ {
22+ SetPreviousState ( point ) ;
23+ return point ;
24+ }
25+ else
26+ {
27+ Point pos = new Point ( point . X , point . Y ) ;
28+ float deltaX = point . X - _lastPos . X ;
29+ float deltaY = point . Y - _lastPos . Y ;
30+
31+ float stepCount = Latency / TimerInterval ;
32+ float target = 1 - Threshold ;
33+ float weight = 1f - ( 1f / ( float ) Pow ( 1 / target , 1 / stepCount ) ) ;
34+
35+ pos . X += deltaX * weight ;
36+ pos . Y += deltaY * weight ;
37+ SetPreviousState ( point ) ;
38+ return pos ;
39+ }
40+ }
41+
42+ private void SetPreviousState ( Point lastPosition )
43+ {
44+ _lastPos = lastPosition ;
45+ _lastFilterTime = DateTime . Now ;
46+ }
47+
48+ public FilterStage FilterStage => FilterStage . PreTranspose ;
49+
50+ [ SliderProperty ( "Latency" , 0f , 5f , 2f ) ]
51+ public float Latency { set ; get ; }
52+
53+ [ SliderProperty ( "Weight" , 0f , 1f , 1f ) ]
54+ public float Weight { set ; get ; }
55+
56+ [ SliderProperty ( "Threshold" , 0f , 1f , 0.9f ) ]
57+ public float Threshold { set ; get ; }
58+
59+ [ UnitProperty ( "Timer Interval" , "hz" ) ]
60+ public float TimerInterval { set ; get ; }
61+ }
62+ }
0 commit comments