11using System ;
22using System . Numerics ;
3- using OpenTabletDriver . Plugin ;
43using OpenTabletDriver . Plugin . Attributes ;
5- using OpenTabletDriver . Plugin . Tablet . Interpolator ;
6- using OpenTabletDriver . Plugin . Timers ;
4+ using OpenTabletDriver . Plugin . Output ;
5+ using OpenTabletDriver . Plugin . Tablet ;
76
87namespace TabletDriverFilters . Devocub
98{
10- using static MathF ;
11-
129 [ PluginName ( "Devocub Antichatter" ) ]
13- public class Antichatter : Interpolator
10+ public class Antichatter : MillimeterAsyncPositionedPipelineElement
1411 {
1512 private const string LATENCY_TOOLTIP =
1613 "Smoothing latency\n "
@@ -43,7 +40,7 @@ public class Antichatter : Interpolator
4340 + "\n "
4441 + " - Smooth: Latency ~10 ms, Strength 3, Multiplier 100, OffsetX 1.5, OffsetY 1.\n "
4542 + " Change OffsetX between 0-2 to switch between stickiness and smooth.\n "
46- + " Increase Strength to 4-10 to get sharper changes in smoothing . Decrease Strength to 1-2 to get more smoothing.\n "
43+ + " Increase Strength to 4-10 to get harper . Decrease Strength to 1-2 to get more smoothing.\n "
4744 + "\n "
4845 + " - Low latency: Set Offset Y to 0 (and potentially set Latency to 1-10 ms. However, with some settings this can break smoothing, usually OffsetY 0 is enough to being able to go to lowest latency)." ;
4946 private const string PREDICTION_TOOLTIP =
@@ -73,10 +70,7 @@ public class Antichatter : Interpolator
7370 + " Smoothing: Latency 40ms, Strength 3, Multiplier 10, OffsetX 1, OffsetY 1\n "
7471 + " Prediction: Strength 4, Sharpness 0.75, Offset 2.5, OffsetY 1" ;
7572
76- public Antichatter ( ITimer scheduler ) : base ( scheduler )
77- {
78- GetMMScale ( ) ;
79- }
73+ public override PipelinePosition Position => PipelinePosition . PreTransform ;
8074
8175 [ SliderProperty ( "Latency" , 0f , 1000f , 2f ) , DefaultPropertyValue ( 2f ) , ToolTip ( LATENCY_TOOLTIP ) ]
8276 public float Latency
@@ -98,8 +92,8 @@ public float Latency
9892 public float AntichatterOffsetY { set ; get ; }
9993
10094 [ BooleanProperty ( "Prediction" , "" ) , ToolTip ( PREDICTION_TOOLTIP ) ]
101-
10295 public bool PredictionEnabled { set ; get ; }
96+
10397 [ Property ( "Prediction Strength" ) , DefaultPropertyValue ( 1.1f ) , ToolTip ( PREDICTION_TOOLTIP ) ]
10498 public float PredictionStrength { set ; get ; }
10599
@@ -117,45 +111,52 @@ public float Latency
117111 private float timerInterval => 1000 / Frequency ;
118112 private float latency = 2.0f ;
119113 private float weight ;
120- private Vector2 mmScale ;
121114 private Vector2 position ;
122115 private Vector2 prevTargetPos , targetPos , calcTarget ;
123- private SyntheticTabletReport report ;
124116
125- public override void UpdateState ( SyntheticTabletReport report )
117+ protected override void ConsumeState ( )
126118 {
127- this . targetPos = report . Position * mmScale ;
128-
129- if ( PredictionEnabled )
119+ if ( State is ITabletReport report )
130120 {
131- // Calculate predicted position onNewPacket
132- if ( this . prevTargetPos . X != this . targetPos . X || this . prevTargetPos . Y != this . targetPos . Y )
133- {
134- // Calculate distance between last 2 packets and prediction
135- var delta = this . targetPos - this . prevTargetPos ;
136- var distance = Vector2 . Distance ( this . prevTargetPos , this . targetPos ) ;
137- var predictionModifier = 1 / Cosh ( ( distance - PredictionOffsetX ) * PredictionSharpness ) * PredictionStrength + PredictionOffsetY ;
138-
139- // Apply prediction
140- delta *= predictionModifier ;
141-
142- // Update predicted position
143- this . calcTarget = this . targetPos + delta ;
121+ this . targetPos = report . Position * MillimeterScale ;
144122
145- // Update old position for further prediction
146- this . prevTargetPos = this . targetPos ;
123+ if ( PredictionEnabled )
124+ {
125+ // Calculate predicted position onNewPacket
126+ if ( this . prevTargetPos . X != this . targetPos . X || this . prevTargetPos . Y != this . targetPos . Y )
127+ {
128+ // Calculate distance between last 2 packets and prediction
129+ var delta = this . targetPos - this . prevTargetPos ;
130+ var distance = Vector2 . Distance ( this . prevTargetPos , this . targetPos ) ;
131+ var predictionModifier = 1 / MathF . Cosh ( ( distance - PredictionOffsetX ) * PredictionSharpness ) * PredictionStrength + PredictionOffsetY ;
132+
133+ // Apply prediction
134+ delta *= predictionModifier ;
135+
136+ // Update predicted position
137+ this . calcTarget = this . targetPos + delta ;
138+
139+ // Update old position for further prediction
140+ this . prevTargetPos = this . targetPos ;
141+ }
147142 }
143+ else
144+ calcTarget = targetPos ;
148145 }
149- else
150- calcTarget = targetPos ;
151-
152- this . report = report ;
153146 }
154147
155- public override SyntheticTabletReport Interpolate ( )
148+ protected override void UpdateState ( )
156149 {
157- this . report . Position = Filter ( this . calcTarget ) / mmScale ;
158- return this . report ;
150+ if ( State is ITabletReport report )
151+ {
152+ report . Position = Filter ( calcTarget ) / MillimeterScale ;
153+ State = report ;
154+ }
155+
156+ if ( PenIsInRange ( ) || State is not ITabletReport )
157+ {
158+ OnEmit ( ) ;
159+ }
159160 }
160161
161162 public Vector2 Filter ( Vector2 calcTarget )
@@ -173,7 +174,7 @@ public Vector2 Filter(Vector2 calcTarget)
173174
174175 // Devocub smoothing
175176 // Increase weight of filter in {formula} times
176- var weightModifier = ( float ) ( Pow ( distance + AntichatterOffsetX , AntichatterStrength * - 1 ) * AntichatterMultiplier ) ;
177+ var weightModifier = ( float ) ( MathF . Pow ( distance + AntichatterOffsetX , AntichatterStrength * - 1 ) * AntichatterMultiplier ) ;
177178
178179 // Limit minimum
179180 if ( weightModifier + AntichatterOffsetY < 0 )
@@ -194,15 +195,5 @@ private void SetWeight(float latency)
194195 float target = 1 - THRESHOLD ;
195196 this . weight = 1f - ( 1f / MathF . Pow ( 1f / target , 1f / stepCount ) ) ;
196197 }
197-
198- private void GetMMScale ( )
199- {
200- var digitizer = Info . Driver . Tablet . Digitizer ;
201- this . mmScale = new Vector2
202- {
203- X = digitizer . Width / digitizer . MaxX ,
204- Y = digitizer . Height / digitizer . MaxY
205- } ;
206- }
207198 }
208199}
0 commit comments