22using System . Numerics ;
33using OpenTabletDriver . Plugin . Attributes ;
44using OpenTabletDriver . Plugin . Tablet ;
5+ using OpenTabletDriver . Plugin . Tablet . Interpolator ;
6+ using OpenTabletDriver . Plugin . Timers ;
57
68namespace TabletDriverFilters . Devocub
79{
810 using static MathF ;
911
1012 [ PluginName ( "TabletDriver AntiChatter Filter" ) ]
11- public class AntiChatter : IFilter
13+ public class AntiChatter : Interpolator
1214 {
13- private Vector2 _lastPos ;
14- private float _timerInterval ;
15- private const float _threshold = 0.9f ;
16- private float _latency = 2.0f ;
15+ public AntiChatter ( ITimer scheduler ) : base ( scheduler ) { }
16+
17+ private Vector2 lastPos ;
18+ private Vector2 targetPos ;
19+ private SyntheticTabletReport report ;
20+ private const float threshold = 0.9f ;
21+ private float latency = 2.0f ;
22+
23+ public override void UpdateState ( SyntheticTabletReport report )
24+ {
25+ this . targetPos = report . Position ;
26+ this . report = report ;
27+ }
28+
29+ public override SyntheticTabletReport Interpolate ( )
30+ {
31+ this . report . Position = Filter ( this . targetPos ) ;
32+ return this . report ;
33+ }
1734
1835 public Vector2 Filter ( Vector2 point )
1936 {
2037 Vector2 calcTarget = new Vector2 ( ) ;
21- float deltaX , deltaY , distance , weightModifier , predictionModifier ;
22-
23- if ( _lastPos == null )
24- {
25- _lastPos = point ;
26- return point ;
27- }
38+ Vector2 delta ;
39+ float distance , weightModifier , predictionModifier ;
2840
2941 if ( PredictionEnabled )
3042 {
3143 // Calculate predicted position onNewPacket
32- if ( _lastPos . X != point . X || _lastPos . Y != point . Y )
44+ if ( this . lastPos . X != point . X || this . lastPos . Y != point . Y )
3345 {
3446 // Calculate distance between last 2 packets and prediction
35- deltaX = point . X - _lastPos . X ;
36- deltaY = point . Y - _lastPos . Y ;
37- distance = Sqrt ( deltaX * deltaX + deltaY * deltaY ) ;
47+ delta = point - lastPos ;
48+ distance = Vector2 . Distance ( lastPos , point ) ;
3849 predictionModifier = 1 / Cosh ( ( distance - PredictionOffsetX ) * PredictionSharpness ) * PredictionStrength + PredictionOffsetY ;
3950
4051 // Apply prediction
41- deltaX *= predictionModifier ;
42- deltaY *= predictionModifier ;
52+ delta *= predictionModifier ;
4353
4454 // Update predicted position
45- calcTarget . X = ( float ) ( point . X + deltaX ) ;
46- calcTarget . Y = ( float ) ( point . Y + deltaY ) ;
55+ calcTarget = point + delta ;
4756
4857 // Update old position for further prediction
49- _lastPos . X = point . X ;
50- _lastPos . Y = point . Y ;
58+ this . lastPos = point ;
5159 }
5260 }
5361 else
54- {
55- calcTarget . X = point . X ;
56- calcTarget . Y = point . Y ;
57- }
62+ calcTarget = point ;
5863
59- deltaX = calcTarget . X - _lastPos . X ;
60- deltaY = calcTarget . Y - _lastPos . Y ;
61- distance = Sqrt ( deltaX * deltaX + deltaY * deltaY ) ;
64+ delta = calcTarget - lastPos ;
65+ distance = Vector2 . Distance ( lastPos , calcTarget ) ;
6266
6367 float stepCount = Latency / TimerInterval ;
64- float target = 1 - _threshold ;
68+ float target = 1 - threshold ;
6569 float weight = ( float ) ( 1.0 - ( 1.0 / Pow ( ( float ) ( 1.0 / target ) , ( float ) ( 1.0 / stepCount ) ) ) ) ;
6670
6771 // Devocub smoothing
@@ -76,26 +80,23 @@ public Vector2 Filter(Vector2 point)
7680
7781 weightModifier = weight / weightModifier ;
7882 weightModifier = Math . Clamp ( weightModifier , 0 , 1 ) ;
79- _lastPos . X += ( float ) ( deltaX * weightModifier ) ;
80- _lastPos . Y += ( float ) ( deltaY * weightModifier ) ;
83+ this . lastPos += delta * weightModifier ;
8184
82- return _lastPos ;
85+ return lastPos ;
8386 }
8487
85- public FilterStage FilterStage => FilterStage . PostTranspose ;
88+ public static FilterStage FilterStage => FilterStage . PostTranspose ;
8689
8790 [ SliderProperty ( "Latency" , 0f , 5f , 2f ) ]
8891 public float Latency
8992 {
90- set => _latency = Math . Clamp ( value , 0 , 1000 ) ;
91- get => _latency ;
93+ set => this . latency = Math . Clamp ( value , 0 , 1000 ) ;
94+ get => this . latency ;
9295 }
9396
94- [ Property ( "Timer Interval" ) , Unit ( "hz" ) ]
9597 public float TimerInterval
9698 {
97- set => _timerInterval = 1000f / value ;
98- get => _timerInterval ;
99+ get => 1000 / Hertz ;
99100 }
100101
101102 [ Property ( "Antichatter Strength" ) ]
0 commit comments