1212using OTD . EnhancedOutputMode . Lib . Interface ;
1313using OTD . EnhancedOutputMode . Lib . Tools ;
1414using OTD . EnhancedOutputMode . Lib . Tablet ;
15+ using OpenTabletDriver . Plugin ;
1516
1617namespace OTD . EnhancedOutputMode . Output
1718{
1819 [ PluginName ( "Enhanced Relative Mode" ) ]
1920 public class EnhancedRelativeOutputMode : RelativeOutputMode , IPointerOutputMode < IRelativePointer >
2021 {
22+ private IList < IFilter > _filters , preFilters , postFilters ;
2123 private ITabletReport _convertedReport = new TouchConvertedReport ( ) ;
24+ private HPETDeltaStopwatch _Touchstopwatch = new ( true ) ;
2225 private HPETDeltaStopwatch _penStopwatch = new ( true ) ;
2326 private bool _firstReport = true ;
2427 private int _lastTouchID = - 1 ;
28+ private Vector2 _lastTransformedPos ;
2529 private Vector2 _lastPos ;
30+ private bool _skipReport = false ;
2631
32+ public Matrix3x2 TransformationMatrix { get ; private set ; }
2733 public Matrix3x2 TouchTransformationMatrix { get ; private set ; }
2834
2935 public override IRelativePointer Pointer => SystemInterop . RelativePointer ;
@@ -44,22 +50,56 @@ public void Initialize()
4450 GateFilters = Filters . OfType < IGateFilter > ( ) . ToList ( ) ;
4551 AuxFilters = Filters . OfType < IAuxFilter > ( ) . ToList ( ) ;
4652
53+ FetchFilters ( ) ;
54+
4755 // Initialize filters that require initialization
4856 foreach ( var filter in Filters . OfType < IInitialize > ( ) )
4957 filter . Initialize ( ) ;
5058
59+ this . _skipReport = true ;
60+
61+ UpdateTransformMatrix ( ) ;
62+
63+ // Someone asked for a feature to match the pen's speed in relative mode
64+ if ( TouchSettings . MatchPenSensibilityInRelativeMode )
65+ UpdateTouchTransformMatrix ( ) ;
66+
5167 // we don't want to initialize again
5268 _firstReport = false ;
5369 }
5470
71+ private void FetchFilters ( )
72+ {
73+ if ( Info . Driver . InterpolatorActive )
74+ this . preFilters = Filters . Where ( t => t . FilterStage == FilterStage . PreTranspose ) . ToList ( ) ;
75+ else
76+ this . preFilters = Filters . Where ( t => t . FilterStage == FilterStage . PreTranspose || t . FilterStage == FilterStage . PreInterpolate ) . ToList ( ) ;
77+
78+ this . postFilters = Filters . Where ( t => t . FilterStage == FilterStage . PostTranspose ) . ToList ( ) ;
79+ }
80+
5581 private void UpdateTransformMatrix ( )
5682 {
57- this . TouchTransformationMatrix = Matrix3x2 . CreateRotation (
83+ TransformationMatrix = Matrix3x2 . CreateRotation (
5884 ( float ) ( - Rotation * System . Math . PI / 180 ) ) ;
5985
60- this . TouchTransformationMatrix *= Matrix3x2 . CreateScale (
86+ TransformationMatrix *= Matrix3x2 . CreateScale (
6187 Sensitivity . X * ( ( Tablet ? . Digitizer ? . Width / Tablet ? . Digitizer ? . MaxX ) ?? 0.01f ) ,
6288 Sensitivity . Y * ( ( Tablet ? . Digitizer ? . Height / Tablet ? . Digitizer ? . MaxY ) ?? 0.01f ) ) ;
89+
90+ TouchTransformationMatrix = TransformationMatrix ;
91+ }
92+
93+ // This is only used when
94+ private void UpdateTouchTransformMatrix ( )
95+ {
96+ // Pen & Touch digitizer suffer from a difference in resolution,
97+ // resulting in different speeds for the same sensitivity.
98+ var XMultiplier = Tablet . Digitizer . MaxX / TouchSettings . MaxX ;
99+ var YMultiplier = Tablet . Digitizer . MaxY / TouchSettings . MaxY ;
100+
101+ // This should achieve about the same speed as the pen
102+ TouchTransformationMatrix = TransformationMatrix * Matrix3x2 . CreateScale ( XMultiplier , YMultiplier ) ;
63103 }
64104
65105 public override void Read ( IDeviceReport report )
@@ -111,7 +151,7 @@ protected virtual bool HandleTouch(IDeviceReport report, ITouchReport touchRepor
111151 {
112152 _lastPos = _convertedReport . Position ;
113153
114- if ( Transpose ( _convertedReport ) is Vector2 pos && _lastTouchID == TouchConvertedReport . CurrentFirstTouchID )
154+ if ( TransposeTouch ( _convertedReport ) is Vector2 pos && _lastTouchID == TouchConvertedReport . CurrentFirstTouchID )
115155 Pointer . Translate ( pos ) ;
116156
117157 _lastTouchID = TouchConvertedReport . CurrentFirstTouchID ;
@@ -128,5 +168,32 @@ protected bool ShouldReport(IDeviceReport report, ref ITabletReport tabletreport
128168
129169 return true ;
130170 }
171+
172+ public Vector2 ? TransposeTouch ( ITabletReport report )
173+ {
174+ var deltaTime = _Touchstopwatch . Restart ( ) ;
175+
176+ var pos = report . Position ;
177+
178+ // Pre Filter
179+ foreach ( IFilter filter in this . preFilters ??= Array . Empty < IFilter > ( ) )
180+ pos = filter . Filter ( pos ) ;
181+
182+ pos = Vector2 . Transform ( pos , this . TouchTransformationMatrix ) ;
183+
184+ // Post Filter
185+ foreach ( IFilter filter in this . postFilters ??= Array . Empty < IFilter > ( ) )
186+ pos = filter . Filter ( pos ) ;
187+
188+ var delta = pos - this . _lastTransformedPos ;
189+ this . _lastTransformedPos = pos ;
190+
191+ if ( _skipReport )
192+ {
193+ _skipReport = false ;
194+ return null ;
195+ }
196+ return ( deltaTime > ResetTime ) ? null : delta ;
197+ }
131198 }
132199}
0 commit comments