
'#require <api_mmsys.sbp>

Namespace ActiveBasic
Namespace Windows
Namespace MM


Enum JoyCapability
	HasZ = &H0001
	HasR = &H0002
	HasU = &H0004
	HasV = &H0008
	HasPointOfView = &H0010
	PointOfView4Direction = &H0020
	PointOfViewContinuous = &H0040
End Enum

Class JoyCaps
	caps As JOYCAPS

Protected
	Sub _Initialize ( id As DWord )
		joyGetDevCaps(id,caps,SizeOf(JOYCAPS))
	End Sub

Public
	Function ManufacturerID () As Word
		Return This.caps.wMid
	End Function
	Function ProductID () As Word
		Return This.caps.wPid
	End Function
	Function ProductName () As String
		Return New System.String(This.caps.szPname As LPTSTR)
	End Function
	Function MinimumX () As DWord
		Return This.caps.wXmin
	End Function
	Function MaximumX () As DWord
		Return This.caps.wXmax
	End Function
	Function MinimumY () As DWord
		Return This.caps.wYmin
	End Function
	Function MaximumY () As DWord
		Return This.caps.wYmax
	End Function
	Function MinimumZ () As DWord
		Return This.caps.wZmin
	End Function
	Function MaximumZ () As DWord
		Return This.caps.wZmax
	End Function
	Function NumberOfButtons () As DWord
		Return This.caps.wNumButtons
	End Function
	Function MinimumMessagePeriod () As DWord
		Return This.caps.wPeriodMin
	End Function
	Function MaximumMessagePeriod () As DWord
		Return This.caps.wPeriodMax
	End Function
	Function MinimumR () As DWord
		Return This.caps.wRmin
	End Function
	Function MaximumR () As DWord
		Return This.caps.wRmax
	End Function
	Function MinimumU () As DWord
		Return This.caps.wUmin
	End Function
	Function MaximumU () As DWord
		Return This.caps.wUmax
	End Function
	Function MinimumV () As DWord
		Return This.caps.wVmin
	End Function
	Function MaximumV () As DWord
		Return This.caps.wVmax
	End Function
	Function Capabilities () As JoyCapability
		Return This.caps.wCaps As JoyCapability
	End Function
	Function NumberOfAxesSupported () As DWord
		Return This.caps.wMaxAxes
	End Function
	Function NumberOfAxesUsed () As DWord
		Return This.caps.wNumAxes
	End Function
	Function NumberOfButtonsSupported () As DWord
		Return This.caps.wMaxButtons
	End Function
	Function RegistryKey () As String
		Return New System.String(This.caps.szRegKey As LPTSTR)
	End Function
	Function OEM_VxD () As String
		Return New System.String(This.caps.szOEMVxD As LPTSTR)
	End Function
End Class

Class JoyButtons
	dwButtons As DWord

Protected
	Sub _Initialize ( dw As DWord )
		This.dwButtons = dw
	End Sub

Public
	Function Button1 () As Boolean
		Return This.Button( 1 )
	End Function
	Function Button2 () As Boolean
		Return This.Button( 2 )
	End Function
	Function Button3 () As Boolean
		Return This.Button( 3 )
	End Function
	Function Button4 () As Boolean
		Return This.Button( 4 )
	End Function
	Function Button5 () As Boolean
		Return This.Button( 5 )
	End Function
	Function Button6 () As Boolean
		Return This.Button( 6 )
	End Function
	Function Button7 () As Boolean
		Return This.Button( 7 )
	End Function
	Function Button8 () As Boolean
		Return This.Button( 8 )
	End Function
	Function Button9 () As Boolean
		Return This.Button( 9 )
	End Function
	Function Button10 () As Boolean
		Return This.Button( 10 )
	End Function
	Function Button11 () As Boolean
		Return This.Button( 11 )
	End Function
	Function Button12 () As Boolean
		Return This.Button( 12 )
	End Function
	Function Button13 () As Boolean
		Return This.Button( 13 )
	End Function
	Function Button14 () As Boolean
		Return This.Button( 14 )
	End Function
	Function Button15 () As Boolean
		Return This.Button( 15 )
	End Function
	Function Button16 () As Boolean
		Return This.Button( 16 )
	End Function
	Function Button17 () As Boolean
		Return This.Button( 17 )
	End Function
	Function Button18 () As Boolean
		Return This.Button( 18 )
	End Function
	Function Button19 () As Boolean
		Return This.Button( 19 )
	End Function
	Function Button20 () As Boolean
		Return This.Button( 20 )
	End Function
	Function Button21 () As Boolean
		Return This.Button( 21 )
	End Function
	Function Button22 () As Boolean
		Return This.Button( 22 )
	End Function
	Function Button23 () As Boolean
		Return This.Button( 23 )
	End Function
	Function Button24 () As Boolean
		Return This.Button( 24 )
	End Function
	Function Button25 () As Boolean
		Return This.Button( 25 )
	End Function
	Function Button26 () As Boolean
		Return This.Button( 26 )
	End Function
	Function Button27 () As Boolean
		Return This.Button( 27 )
	End Function
	Function Button28 () As Boolean
		Return This.Button( 28 )
	End Function
	Function Button29 () As Boolean
		Return This.Button( 29 )
	End Function
	Function Button30 () As Boolean
		Return This.Button( 30 )
	End Function
	Function Button31 () As Boolean
		Return This.Button( 31 )
	End Function
	Function Button32 () As Boolean
		Return This.Button( 32 )
	End Function

Public
	Function Button ( n As DWord ) As Boolean
		Return (1<<(n-1) And This.dwButtons) <> 0
	End Function

Public
	Function Operator () As DWord
		Return This.dwButtons
	End Function

	Function Operator== (dw As DWord) As Boolean
		Return This.dwButtons = dw
	End Function
	Function Operator And (dw As DWord) As Boolean
		Return (This.dwButtons And dw) <> 0
	End Function

End Class

Class JoyPointOfView
	dwPOV As DWord

Protected
	Sub _Initialize ( dw As DWord )
		If LOWORD(dw) = &HFFFF Then dw = -1 
		This.dwPOV = dw
	End Sub

Public
	Static Const Centered = (-1) As DWord
	Static Const Forward = 0 As DWord
	Static Const ForwardAndRight = 4500 As DWord
	Static Const Right = 9000 As DWord
	Static Const BackwardAndRight = 13500 As DWord
	Static Const Backward = 18000 As DWord
	Static Const BackwardAndLeft = 22500 As DWord
	Static Const Left = 27000 As DWord
	Static Const ForwardAndLeft = 31500 As DWord

Public
	Function Operator () As DWord
		Return This.dwPOV
	End Function

	Function Operator== (value As DWord) As Boolean
		Return This.dwPOV = value
	End Function
	Function Operator<> ( value As DWord) As Boolean
		Return This.dwPOV <> value
	End Function
	Function Operator<= ( value As DWord) As Boolean
		Return This.dwPOV <= value
	End Function
	Function Operator>= ( value As DWord) As Boolean
		Return This.dwPOV >= value
	End Function
	Function Operator< ( value As DWord) As Boolean
		Return This.dwPOV < value
	End Function
	Function Operator> ( value As DWord) As Boolean
		Return This.dwPOV > value
	End Function

	Function Operator== ( value As JoyPointOfView ) As Boolean
		Return This = value As DWord
	End Function
	Function Operator<> ( value As JoyPointOfView ) As Boolean
		Return This <> value As DWord
	End Function
	Function Operator<= ( value As JoyPointOfView ) As Boolean
		Return This <= value As DWord
	End Function
	Function Operator>= ( value As JoyPointOfView ) As Boolean
		Return This >= value As DWord
	End Function
	Function Operator< ( value As JoyPointOfView ) As Boolean
		Return This < value As DWord
	End Function
	Function Operator> ( value As JoyPointOfView ) As Boolean
		Return This > value As DWord
	End Function
End Class

Class JoyInfo
	info As JOYINFOEX

Protected
	Sub _Initialize ( id As DWord )
		This.info.dwSize = SizeOf(JOYINFOEX)
		This.info.dwFlags = JOY_RETURNALL
		joyGetPosEx(id,info)
	End Sub

Public
	Function X () As DWord
		Return This.info.dwXpos
	End Function
	Function Y () As DWord
		Return This.info.dwYpos
	End Function
	Function Z () As DWord
		Return This.info.dwZpos
	End Function
	Function R () As DWord
		Return This.info.dwRpos
	End Function
	Function U () As DWord
		Return This.info.dwUpos
	End Function
	Function V () As DWord
		Return This.info.dwVpos
	End Function
	Function Buttons () As JoyButtons
		Return New Detail._System_JoyButtons(This.info.dwButtons)
	End Function
	Function ButtonNumber () As DWord
		Return This.info.dwButtonNumber
	End Function
	Function PointOfView () As JoyPointOfView
		Return New Detail._System_JoyPointOfView(This.info.dwPOV)
	End Function
End Class

Class Joystick
	id As DWord

Protected
	Sub _Initialize ( id As DWord )
		This.id = id
	End Sub

Public
	Sub Threshold ( value As DWord )
		joySetThreshold(id,value)
	End Sub
	Function Threshold () As DWord
		joyGetThreshold(id,VarPtr(Threshold))
	End Function

Public
	Function GetCapabilities () As JoyCaps
		Return New Detail._System_JoyCaps(This.id)
	End Function
	Function GetInfo() As JoyInfo
		Return New Detail._System_JoyInfo(This.id)
	End Function

Public
	Sub SetCupture ( hwnd As HWND, period As DWord, changed As Boolean)
		joySetCapture(hwnd,id,period,changed)
	End Sub
	Sub ReleaseCapture()
		joyReleaseCapture(id)
	End Sub
End Class

Class Joysticks

Public
	Static Function NumberOfDevices () As DWord
		Return joyGetNumDevs()
	End Function
	Static Function NumberOfConnectedDevices () As DWord
		Dim max = Joysticks.NumberOfDevices()
		Dim cnt=0 As DWord
		Dim info As JOYINFO
		While joyGetPos(cnt,info) = JOYERR_NOERROR
			cnt++
			If cnt >= max Then Exit While
		Wend 
		Return cnt
	End Function

	Static Function GetJoysticks() As System.Collections.Generic.List<Joystick>
		Dim list As System.Collections.Generic.List<Joystick>
		Dim cnt As DWord
		For cnt=0 To NumberOfConnectedDevices() - 1
			list.Add(Joysticks.GetJoystick(cnt))
		Next
		Return list
	End Function

	Static Function GetJoystick( id As DWord ) As Joystick
		Return New Detail._System_Joystick(id)
	End Function

End Class

Namespace Detail
Class _System_JoyCaps
	Inherits JoyCaps
Public
	Sub _System_JoyCaps ( id As DWord )
		This._Initialize(id)
	End Sub

	Override Function ToString () As String
		Return "JoyCaps"
	End Function
End Class

Class _System_JoyButtons
	Inherits JoyButtons
Public
	Sub _System_JoyButtons ( dw As DWord )
		This._Initialize(dw)
	End Sub

	Override Function ToString () As String
		Return "JoyButtons"
	End Function
End Class

Class _System_JoyPointOfView
	Inherits JoyPointOfView
Public
	Sub _System_JoyPointOfView ( dw As DWord )
		This._Initialize(dw)
	End Sub

	Override Function ToString () As String
		Return "JoyPointOfView"
	End Function
End Class

Class _System_JoyInfo
	Inherits JoyInfo
Public
	Sub _System_JoyInfo ( id As DWord )
		This._Initialize(id)
	End Sub

	Override Function ToString () As String
		Return "JoyInfo"
	End Function
End Class

Class _System_Joystick
	Inherits Joystick
Public
	Sub _System_Joystick ( id As DWord )
		This._Initialize(id)
	End Sub

	Override Function ToString () As String
		Return "Joystick"
	End Function
End Class

End Namespace

End Namespace
End Namespace
End Namespace
