Skip to content

Commit

Permalink
Add support touch having the same movement speed as pens
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrcubix committed Oct 25, 2024
1 parent 75f5999 commit f87b26a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
6 changes: 6 additions & 0 deletions OTD.EnhancedOutputMode.Lib/Tools/TouchSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public int PenResetTime

public TimeSpan PenResetTimeSpan => penResetTime;

[BooleanProperty("Match Pen Sensibility in Relative Mode", ""),
DefaultPropertyValue(false),
ToolTip("OTD.EnhancedOutputMode:\n\n" +
"When Enabled, the touch sensitivity will match to the sensitivity of the pen in Relative output mode.")]
public bool MatchPenSensibilityInRelativeMode { get; set; }

[Property("Max X"),
DefaultPropertyValue(4095),
ToolTip("OTD.EnhancedOutputMode:\n\n" +
Expand Down
37 changes: 35 additions & 2 deletions OTD.EnhancedOutputMode/Output/EnhancedRelativeOutputMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ namespace OTD.EnhancedOutputMode.Output
public class EnhancedRelativeOutputMode : RelativeOutputMode, IPointerProvider<IRelativePointer>
{
private readonly ITabletReport _convertedReport = new TouchConvertedReport();
private readonly HPETDeltaStopwatch _touchStopwatch = new(true);
private readonly HPETDeltaStopwatch _penStopwatch = new(true);
private TouchSettings _touchSettings = TouchSettings.Default;
private bool _initialized = false;
private bool skipReport = false;
private int _lastTouchID = -1;
private Vector2? _lastTransformedTouchPos;
private Vector2? _lastTransformedAbsolutePos;
private Vector2 _lastPos;

public Matrix3x2 TouchTransformationMatrix { get; protected set; }
Expand Down Expand Up @@ -58,9 +61,23 @@ public void Initialize()

TouchTransformationMatrix = TransformationMatrix;

if (TouchSettings.MatchPenSensibilityInRelativeMode)
UpdateTouchTransformMatrix();

_initialized = true;
}

private void UpdateTouchTransformMatrix()
{
// Pen & Touch digitizer suffer from a difference in resolution,
// resulting in different speeds for the same sensitivity.
var XMultiplier = Tablet.Properties.Specifications.Digitizer.MaxX / TouchSettings.MaxX;
var YMultiplier = Tablet.Properties.Specifications.Digitizer.MaxY / TouchSettings.MaxY;

// This should achieve about the same speed as the pen
TouchTransformationMatrix = TransformationMatrix * Matrix3x2.CreateScale(XMultiplier, YMultiplier);
}

#endregion

#region Report Handling
Expand Down Expand Up @@ -89,6 +106,7 @@ public override void Read(IDeviceReport deviceReport)
if (_lastTouchID != TouchConvertedReport.CurrentFirstTouchID && TouchConvertedReport.CurrentFirstTouchID != -1)
{
_lastPos = _convertedReport.Position;
_lastTransformedTouchPos = null;
skipReport = true;
}

Expand All @@ -99,8 +117,6 @@ public override void Read(IDeviceReport deviceReport)
base.Read(_convertedReport); // We send another report instead of overwriting the touch report since plugins might rely on it
else
skipReport = false;

_lastPos = _convertedReport.Position;
}
else if (deviceReport is IAbsolutePositionReport) // Restart the pen stopwatch when a pen report is received
if (_touchSettings.DisableWhenPenInRange)
Expand All @@ -109,6 +125,23 @@ public override void Read(IDeviceReport deviceReport)
base.Read(deviceReport);
}

protected override IAbsolutePositionReport Transform(IAbsolutePositionReport report)
{
if (report is not TouchConvertedReport)
return base.Transform(report);

Vector2 pos = report.Position;
Vector2? delta;

pos = Vector2.Transform(pos, TouchTransformationMatrix);
delta = pos - _lastTransformedTouchPos;
_lastTransformedTouchPos = pos;

report.Position = delta.GetValueOrDefault();

return report;
}

protected override void OnOutput(IDeviceReport report)
{
if (report is IAuxReport auxReport)
Expand Down

0 comments on commit f87b26a

Please sign in to comment.