Advertisement
TizzyT

XPadMonitor -TizzyT

Oct 15th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 15.56 KB | None | 0 0
  1. Public Class XPadMonitor
  2.     Private Declare Function XInputGetBatteryInformation Lib "xinput1_3.dll" _
  3.         (ByVal uJoyID As Integer, ByRef GameState As XINPUT_BATTERY_INFORMATION) As Int16
  4.     Private Declare Function XInputGetState Lib "xinput1_3.dll" _
  5.         (ByVal uJoyID As Integer, ByRef GameState As XINPUT_STATE) As Boolean
  6.     Private Declare Function XInputSetState Lib "xinput1_3.dll" _
  7.         (ByVal uJoyID As Integer, ByRef GameState As XINPUT_VIBRATION) As Int16
  8.  
  9.     Private Shared ReadOnly StopVibrate As New XINPUT_VIBRATION(0, 0)
  10.  
  11.     Public Shared Property DeadZoneMode() As ZoneModes = ZoneModes.Accurate
  12.     Public Enum ZoneModes
  13.         Accurate
  14.         Block
  15.     End Enum
  16.  
  17.     Public Structure XINPUT_BATTERY_INFORMATION
  18.         Public BatteryType, BatteryLevel As Byte
  19.     End Structure
  20.     Public Structure XINPUT_GAMEPAD
  21.         Public wButtons As UShort
  22.         Public bLeftTrigger, bRightTrigger As Byte
  23.         Public sThumbLX, sThumbLY, sThumbRX, sThumbRY As Short
  24.     End Structure
  25.     Public Structure XINPUT_STATE
  26.         Public dwPacketNumber As Integer
  27.         Public Gamepad As XINPUT_GAMEPAD
  28.     End Structure
  29.     Public Structure XINPUT_VIBRATION
  30.         Public wLeftMotorSpeed, wRightMotorSpeed As UShort
  31.         Sub New(ByVal LMotor As UShort, ByVal RMotor As UShort)
  32.             wLeftMotorSpeed = LMotor
  33.             wRightMotorSpeed = RMotor
  34.         End Sub
  35.     End Structure
  36.  
  37.     Public Shared Property Pad0Enabled() As Boolean = False
  38.     Private Shared Pad0LDeadRad As Short = 6554
  39.     Private Shared Pad0RDeadRad As Short = 6554
  40.     Private Shared Pad0LDeadZone As Integer = 42954916
  41.     Private Shared Pad0RDeadZone As Integer = 42954916
  42.     Public Shared Property Pad0LeftDeadZone() As Short
  43.         Get
  44.             Return Pad0LDeadRad
  45.         End Get
  46.         Set(value As Short)
  47.             If value < 0 Then
  48.                 value += 1
  49.                 value *= -1
  50.             End If
  51.             Pad0LDeadRad = value
  52.             Pad0LDeadZone = value
  53.             Pad0LDeadZone *= Pad0LDeadZone
  54.         End Set
  55.     End Property
  56.     Public Shared Property Pad0RightDeadZone() As Short
  57.         Get
  58.             Return Pad0RDeadRad
  59.         End Get
  60.         Set(value As Short)
  61.             If value < 0 Then
  62.                 value += 1
  63.                 value *= -1
  64.             End If
  65.             Pad0RDeadRad = value
  66.             Pad0RDeadZone = value
  67.             Pad0RDeadZone *= Pad0RDeadZone
  68.         End Set
  69.     End Property
  70.  
  71.     Public Shared Property Pad1Enabled() As Boolean = False
  72.     Private Shared Pad1LDeadRad As Short = 6554
  73.     Private Shared Pad1RDeadRad As Short = 6554
  74.     Private Shared Pad1LDeadZone As Integer = 42954916
  75.     Private Shared Pad1RDeadZone As Integer = 42954916
  76.     Public Shared Property Pad1LeftDeadZone() As Short
  77.         Get
  78.             Return Pad1LDeadRad
  79.         End Get
  80.         Set(value As Short)
  81.             If value < 0 Then
  82.                 value += 1
  83.                 value *= -1
  84.             End If
  85.             Pad1LDeadRad = value
  86.             Pad1LDeadZone = value
  87.             Pad1LDeadZone *= Pad1LDeadZone
  88.         End Set
  89.     End Property
  90.     Public Shared Property Pad1RightDeadZone() As Short
  91.         Get
  92.             Return Pad1RDeadRad
  93.         End Get
  94.         Set(value As Short)
  95.             If value < 0 Then
  96.                 value += 1
  97.                 value *= -1
  98.             End If
  99.             Pad1RDeadRad = value
  100.             Pad1RDeadZone = value
  101.             Pad1RDeadZone *= Pad1RDeadZone
  102.         End Set
  103.     End Property
  104.  
  105.     Public Shared Property Pad2Enabled() As Boolean = False
  106.     Private Shared Pad2LDeadRad As Short = 6554
  107.     Private Shared Pad2RDeadRad As Short = 6554
  108.     Private Shared Pad2LDeadZone As Integer = 42954916
  109.     Private Shared Pad2RDeadZone As Integer = 42954916
  110.     Public Shared Property Pad2LeftDeadZone() As Short
  111.         Get
  112.             Return Pad2LDeadRad
  113.         End Get
  114.         Set(value As Short)
  115.             If value < 0 Then
  116.                 value += 1
  117.                 value *= -1
  118.             End If
  119.             Pad2LDeadRad = value
  120.             Pad2LDeadZone = value
  121.             Pad2LDeadZone *= Pad2LDeadZone
  122.         End Set
  123.     End Property
  124.     Public Shared Property Pad2RightDeadZone() As Short
  125.         Get
  126.             Return Pad2RDeadRad
  127.         End Get
  128.         Set(value As Short)
  129.             If value < 0 Then
  130.                 value += 1
  131.                 value *= -1
  132.             End If
  133.             Pad2RDeadRad = value
  134.             Pad2RDeadZone = value
  135.             Pad2RDeadZone *= Pad2RDeadZone
  136.         End Set
  137.     End Property
  138.  
  139.     Public Shared Property Pad3Enabled() As Boolean = False
  140.     Private Shared Pad3LDeadRad As Short = 6554
  141.     Private Shared Pad3RDeadRad As Short = 6554
  142.     Private Shared Pad3LDeadZone As Integer = 42954916
  143.     Private Shared Pad3RDeadZone As Integer = 42954916
  144.     Public Shared Property Pad3LeftDeadZone() As Short
  145.         Get
  146.             Return Pad3LDeadRad
  147.         End Get
  148.         Set(value As Short)
  149.             If value < 0 Then
  150.                 value += 1
  151.                 value *= -1
  152.             End If
  153.             Pad3LDeadRad = value
  154.             Pad3LDeadZone = value
  155.             Pad3LDeadZone *= Pad3LDeadZone
  156.         End Set
  157.     End Property
  158.     Public Shared Property Pad3RightDeadZone() As Short
  159.         Get
  160.             Return Pad3RDeadRad
  161.         End Get
  162.         Set(value As Short)
  163.             If value < 0 Then
  164.                 value += 1
  165.                 value *= -1
  166.             End If
  167.             Pad3RDeadRad = value
  168.             Pad3RDeadZone = value
  169.             Pad3RDeadZone *= Pad3RDeadZone
  170.         End Set
  171.     End Property
  172.  
  173.     Public Shared Sub SetStates(Optional ByVal Pad0 As Boolean = False, _
  174.                                 Optional ByVal Pad1 As Boolean = False, _
  175.                                 Optional ByVal Pad2 As Boolean = False, _
  176.                                 Optional ByVal Pad3 As Boolean = False)
  177.         If Pad0 Then Pad0Enabled = True
  178.         If Pad1 Then Pad1Enabled = True
  179.         If Pad2 Then Pad2Enabled = True
  180.         If Pad3 Then Pad3Enabled = True
  181.     End Sub
  182.  
  183.     Public Shared Function GetStates() As Boolean()
  184.         Return {Pad0Enabled, Pad1Enabled, Pad2Enabled, Pad3Enabled}
  185.     End Function
  186.  
  187.     Public Shared Sub EnablePad(ByVal PadID As SByte)
  188.         Select Case PadID
  189.             Case 0 : Pad0Enabled = True
  190.             Case 1 : Pad1Enabled = True
  191.             Case 2 : Pad2Enabled = True
  192.             Case 3 : Pad3Enabled = True
  193.         End Select
  194.     End Sub
  195.  
  196.     Public Shared Sub DisablePad(ByVal PadID As SByte)
  197.         Select Case PadID
  198.             Case 0 : Pad0Enabled = False
  199.             Case 1 : Pad1Enabled = False
  200.             Case 2 : Pad2Enabled = False
  201.             Case 3 : Pad3Enabled = False
  202.         End Select
  203.     End Sub
  204.  
  205.     Public Shared Function GetBatteryInfos() As XINPUT_BATTERY_INFORMATION()
  206.         Dim R(3) As XINPUT_BATTERY_INFORMATION
  207.         If Pad0Enabled Then XInputGetBatteryInformation(0, R(0))
  208.         If Pad1Enabled Then XInputGetBatteryInformation(1, R(1))
  209.         If Pad2Enabled Then XInputGetBatteryInformation(2, R(2))
  210.         If Pad3Enabled Then XInputGetBatteryInformation(3, R(3))
  211.         Return R
  212.     End Function
  213.  
  214.     Public Shared Function GetInputs() As XINPUT_STATE()
  215.         Dim States(3) As XINPUT_STATE
  216.         Dim X, Y As Integer
  217.         If Pad0Enabled Then
  218.             XInputGetState(0, States(0))
  219.             X = States(0).Gamepad.sThumbLX
  220.             Y = States(0).Gamepad.sThumbLY
  221.             If DeadZoneMode = ZoneModes.Block Then
  222.                 If X < 0 Then X *= -1
  223.                 If Y < 0 Then Y *= -1
  224.                 If X < Pad0LDeadRad AndAlso Y < Pad0LDeadRad Then
  225.                     States(0).Gamepad.sThumbLX = 0
  226.                     States(0).Gamepad.sThumbLY = 0
  227.                 End If
  228.                 X = States(0).Gamepad.sThumbRX
  229.                 Y = States(0).Gamepad.sThumbRY
  230.                 If X < 0 Then X *= -1
  231.                 If Y < 0 Then Y *= -1
  232.                 If X < Pad0RDeadRad AndAlso Y < Pad0RDeadRad Then
  233.                     States(0).Gamepad.sThumbRX = 0
  234.                     States(0).Gamepad.sThumbRY = 0
  235.                 End If
  236.             Else
  237.                 If X < 0 Then X *= -1
  238.                 If Y < 0 Then Y *= -1
  239.                 X *= X
  240.                 Y *= Y
  241.                 If X + Y < Pad0LDeadZone Then
  242.                     States(0).Gamepad.sThumbLX = 0
  243.                     States(0).Gamepad.sThumbLY = 0
  244.                 End If
  245.                 X = States(0).Gamepad.sThumbRX
  246.                 Y = States(0).Gamepad.sThumbRY
  247.                 If X < 0 Then X *= -1
  248.                 If Y < 0 Then Y *= -1
  249.                 X *= X
  250.                 Y *= Y
  251.                 If X + Y < Pad0RDeadZone Then
  252.                     States(0).Gamepad.sThumbRX = 0
  253.                     States(0).Gamepad.sThumbRY = 0
  254.                 End If
  255.             End If
  256.         End If
  257.         If Pad1Enabled Then
  258.             XInputGetState(1, States(1))
  259.             X = States(1).Gamepad.sThumbLX
  260.             Y = States(1).Gamepad.sThumbLY
  261.             If DeadZoneMode = ZoneModes.Block Then
  262.                 If X < 0 Then X *= -1
  263.                 If Y < 0 Then Y *= -1
  264.                 If X < Pad1LDeadRad AndAlso Y < Pad1LDeadRad Then
  265.                     States(1).Gamepad.sThumbLX = 0
  266.                     States(1).Gamepad.sThumbLY = 0
  267.                 End If
  268.                 X = States(1).Gamepad.sThumbRX
  269.                 Y = States(1).Gamepad.sThumbRY
  270.                 If X < 0 Then X *= -1
  271.                 If Y < 0 Then Y *= -1
  272.                 If X < Pad1RDeadRad AndAlso Y < Pad1RDeadRad Then
  273.                     States(1).Gamepad.sThumbRX = 0
  274.                     States(1).Gamepad.sThumbRY = 0
  275.                 End If
  276.             Else
  277.                 If X < 0 Then X *= -1
  278.                 If Y < 0 Then Y *= -1
  279.                 X *= X
  280.                 Y *= Y
  281.                 If X + Y < Pad1LDeadZone Then
  282.                     States(1).Gamepad.sThumbLX = 0
  283.                     States(1).Gamepad.sThumbLY = 0
  284.                 End If
  285.                 X = States(1).Gamepad.sThumbRX
  286.                 Y = States(1).Gamepad.sThumbRY
  287.                 If X < 0 Then X *= -1
  288.                 If Y < 0 Then Y *= -1
  289.                 X *= X
  290.                 Y *= Y
  291.                 If X + Y < Pad1RDeadZone Then
  292.                     States(1).Gamepad.sThumbRX = 0
  293.                     States(1).Gamepad.sThumbRY = 0
  294.                 End If
  295.             End If
  296.         End If
  297.         If Pad2Enabled Then
  298.             XInputGetState(2, States(2))
  299.             X = States(2).Gamepad.sThumbLX
  300.             Y = States(2).Gamepad.sThumbLY
  301.             If DeadZoneMode = ZoneModes.Block Then
  302.                 If X < 0 Then X *= -1
  303.                 If Y < 0 Then Y *= -1
  304.                 If X < Pad2LDeadRad AndAlso Y < Pad2LDeadRad Then
  305.                     States(2).Gamepad.sThumbLX = 0
  306.                     States(2).Gamepad.sThumbLY = 0
  307.                 End If
  308.                 X = States(2).Gamepad.sThumbRX
  309.                 Y = States(2).Gamepad.sThumbRY
  310.                 If X < 0 Then X *= -1
  311.                 If Y < 0 Then Y *= -1
  312.                 If X < Pad2RDeadRad AndAlso Y < Pad2RDeadRad Then
  313.                     States(2).Gamepad.sThumbRX = 0
  314.                     States(2).Gamepad.sThumbRY = 0
  315.                 End If
  316.             Else
  317.                 If X < 0 Then X *= -1
  318.                 If Y < 0 Then Y *= -1
  319.                 X *= X
  320.                 Y *= Y
  321.                 If X + Y < Pad2LDeadZone Then
  322.                     States(2).Gamepad.sThumbLX = 0
  323.                     States(2).Gamepad.sThumbLY = 0
  324.                 End If
  325.                 X = States(2).Gamepad.sThumbRX
  326.                 Y = States(2).Gamepad.sThumbRY
  327.                 If X < 0 Then X *= -1
  328.                 If Y < 0 Then Y *= -1
  329.                 X *= X
  330.                 Y *= Y
  331.                 If X + Y < Pad2RDeadZone Then
  332.                     States(2).Gamepad.sThumbRX = 0
  333.                     States(2).Gamepad.sThumbRY = 0
  334.                 End If
  335.             End If
  336.         End If
  337.         If Pad3Enabled Then
  338.             XInputGetState(3, States(3))
  339.             X = States(3).Gamepad.sThumbLX
  340.             Y = States(3).Gamepad.sThumbLY
  341.             If DeadZoneMode = ZoneModes.Block Then
  342.                 If X < 0 Then X *= -1
  343.                 If Y < 0 Then Y *= -1
  344.                 If X < Pad3LDeadRad AndAlso Y < Pad3LDeadRad Then
  345.                     States(3).Gamepad.sThumbLX = 0
  346.                     States(3).Gamepad.sThumbLY = 0
  347.                 End If
  348.                 X = States(3).Gamepad.sThumbRX
  349.                 Y = States(3).Gamepad.sThumbRY
  350.                 If X < 0 Then X *= -1
  351.                 If Y < 0 Then Y *= -1
  352.                 If X < Pad3RDeadRad AndAlso Y < Pad3RDeadRad Then
  353.                     States(3).Gamepad.sThumbRX = 0
  354.                     States(3).Gamepad.sThumbRY = 0
  355.                 End If
  356.             Else
  357.                 If X < 0 Then X *= -1
  358.                 If Y < 0 Then Y *= -1
  359.                 X *= X
  360.                 Y *= Y
  361.                 If X + Y < Pad3LDeadZone Then
  362.                     States(3).Gamepad.sThumbLX = 0
  363.                     States(3).Gamepad.sThumbLY = 0
  364.                 End If
  365.                 X = States(3).Gamepad.sThumbRX
  366.                 Y = States(3).Gamepad.sThumbRY
  367.                 If X < 0 Then X *= -1
  368.                 If Y < 0 Then Y *= -1
  369.                 X *= X
  370.                 Y *= Y
  371.                 If X + Y < Pad3RDeadZone Then
  372.                     States(3).Gamepad.sThumbRX = 0
  373.                     States(3).Gamepad.sThumbRY = 0
  374.                 End If
  375.             End If
  376.         End If
  377.         Return States
  378.     End Function
  379.  
  380.     Public Shared Sub Vibrate(Optional ByVal Pad0() As UShort = Nothing, _
  381.                               Optional ByVal Pad1() As UShort = Nothing, _
  382.                               Optional ByVal Pad2() As UShort = Nothing, _
  383.                               Optional ByVal Pad3() As UShort = Nothing)
  384.         If Pad0Enabled Then
  385.             If Pad0 IsNot Nothing Then
  386.                 If Pad0.Length = 2 _
  387.                     Then XInputSetState(0, New XINPUT_VIBRATION(Pad0(0), Pad0(1))) _
  388.                     Else XInputSetState(0, StopVibrate)
  389.             End If
  390.         End If
  391.         If Pad1Enabled Then
  392.             If Pad1 IsNot Nothing Then
  393.                 If Pad1.Length = 2 _
  394.                     Then XInputSetState(1, New XINPUT_VIBRATION(Pad1(0), Pad1(1))) _
  395.                     Else XInputSetState(1, StopVibrate)
  396.             End If
  397.         End If
  398.         If Pad2Enabled Then
  399.             If Pad2 IsNot Nothing Then
  400.                 If Pad2.Length = 2 _
  401.                     Then XInputSetState(2, New XINPUT_VIBRATION(Pad2(0), Pad2(1))) _
  402.                     Else XInputSetState(2, StopVibrate)
  403.             End If
  404.         End If
  405.         If Pad3Enabled Then
  406.             If Pad3 IsNot Nothing Then
  407.                 If Pad3.Length = 2 _
  408.                     Then XInputSetState(3, New XINPUT_VIBRATION(Pad3(0), Pad3(1))) _
  409.                     Else XInputSetState(3, StopVibrate)
  410.             End If
  411.         End If
  412.     End Sub
  413.  
  414.     Public Shared Sub VibratePad(ByVal PadID As Byte, ByVal LMotor As UShort, ByVal RMotor As UShort)
  415.         Select Case PadID
  416.             Case 0 : XInputSetState(0, New XINPUT_VIBRATION(LMotor, RMotor))
  417.             Case 1 : XInputSetState(1, New XINPUT_VIBRATION(LMotor, RMotor))
  418.             Case 2 : XInputSetState(2, New XINPUT_VIBRATION(LMotor, RMotor))
  419.             Case 3 : XInputSetState(3, New XINPUT_VIBRATION(LMotor, RMotor))
  420.         End Select
  421.     End Sub
  422. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement