-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathRawInputDigitizerContact.cs
88 lines (77 loc) · 3.9 KB
/
RawInputDigitizerContact.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Collections.Generic;
using System.Linq;
namespace Linearstar.Windows.RawInput;
public class RawInputDigitizerContact
{
public static readonly HidUsageAndPage UsageX = new(0x01, 0x30);
public static readonly HidUsageAndPage UsageY = new(0x01, 0x31);
public static readonly HidUsageAndPage UsagePressure = new(0x0D, 0x30);
public static readonly HidUsageAndPage UsageInRange = new(0x0D, 0x32);
public static readonly HidUsageAndPage UsageInvert = new(0x0D, 0x3c);
public static readonly HidUsageAndPage UsageTipSwitch = new(0x0D, 0x42);
public static readonly HidUsageAndPage UsageBarrel = new(0x0D, 0x44);
public static readonly HidUsageAndPage UsageEraser = new(0x0D, 0x45);
public static readonly HidUsageAndPage UsageConfidence = new(0x0D, 0x47);
public static readonly HidUsageAndPage UsageWidth = new(0x0D, 0x48);
public static readonly HidUsageAndPage UsageHeight = new(0x0D, 0x49);
public static readonly HidUsageAndPage UsageIdentifier = new(0x0D, 0x51);
public RawInputDigitizerContactKind Kind { get; }
public int X { get; }
public int Y { get; }
public int MinX { get; }
public int MinY { get; }
public int MaxX { get; }
public int MaxY { get; }
public int? Pressure { get; }
public int? MaxPressure { get; }
public bool? IsInverted { get; }
public bool? IsButtonDown { get; }
public int? Width { get; }
public int? Height { get; }
public int? Identifier { get; }
public RawInputDigitizerContact(IEnumerable<HidButtonState> buttonStates, IEnumerable<HidValueState> valueStates)
{
var buttons = buttonStates.ToDictionary(x => x.Button.UsageAndPage, x => x.IsActive);
var values = valueStates.ToDictionary(x => x.Value.UsageAndPage);
X = values[UsageX].CurrentValue;
Y = values[UsageY].CurrentValue;
MinX = values[UsageX].Value.MinValue;
MinY = values[UsageY].Value.MinValue;
MaxX = values[UsageX].Value.MaxValue;
MaxY = values[UsageY].Value.MaxValue;
if (values.TryGetValue(UsagePressure, out var pressure))
{
Pressure = pressure.CurrentValue;
MaxPressure = pressure.Value.MaxValue;
}
IsInverted = buttons.TryGetValue(UsageInvert, out var invert) ? invert : (bool?)null;
IsButtonDown = buttons.TryGetValue(UsageBarrel, out var barrel) ? barrel : (bool?)null;
var isTipDown = buttons[UsageTipSwitch];
if (buttons.TryGetValue(UsageEraser, out var eraser) && eraser)
Kind = RawInputDigitizerContactKind.Eraser;
else
Kind = isTipDown
? buttons.TryGetValue(UsageConfidence, out var confidence) && confidence
? RawInputDigitizerContactKind.Finger
: RawInputDigitizerContactKind.Pen
: buttons.TryGetValue(UsageInRange, out var inRange) && inRange
? RawInputDigitizerContactKind.Hover
: RawInputDigitizerContactKind.None;
Width = values.TryGetValue(UsageWidth, out var width) ? width.CurrentValue : (int?)null;
Height = values.TryGetValue(UsageHeight, out var height) ? height.CurrentValue : (int?)null;
Identifier = values.TryGetValue(UsageIdentifier, out var identifier) ? identifier.CurrentValue : (int?)null;
}
public override string ToString() =>
"{" + string.Join(", ", new[]
{
$"X: {X}/{MaxX}",
$"Y: {Y}/{MaxY}",
$"Kind: {Kind}",
Pressure.HasValue ? $"Pressure: {Pressure}/{MaxPressure}" : null,
IsInverted.HasValue ? $"Inverted: {IsInverted}" : null,
IsButtonDown.HasValue ? $"Button: {IsButtonDown}" : null,
Width.HasValue ? $"Width: {Width}" : null,
Height.HasValue ? $"Height: {Height}" : null,
Identifier.HasValue ? $"Identifier: {Identifier}" : null,
}.Where(i => i != null)) + "}";
}