
'#require <api_mmsys.sbp>

Namespace ActiveBasic
Namespace Windows
Namespace MM

'ジョイスティックの構成を表す列挙体
Enum JoyCapability
	'ジョイスティックはZ軸情報を所有
	HasZ = &H0001
	'ジョイスティックはR軸情報を所有
	HasR = &H0002
	'ジョイスティックはU軸情報を所有
	HasU = &H0004
	'ジョイスティックはV軸情報を所有
	HasV = &H0008
	'ジョイスティックはPoint of View情報を所有
	HasPointOfView = &H0010
	'ジョイスティックは4方向Point of Viewサポート
	PointOfView4Direction = &H0020
	'ジョイスティックは連続した角度のPoint of Viewをサポート
	PointOfViewContinuous = &H0040
End Enum


'ジョイスティックの性能を取得するためのJOYCAPS構造体のラップクラス
Class JoyCaps
	caps As JOYCAPS

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

Public 'Property
	'製造業者IDの取得
	Function ManufacturerID () As Word
		Return This.caps.wMid
	End Function

	'製品IDの取得
	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

	'X軸の最小値の取得
	Function MinimumX () As DWord
		Return This.caps.wXmin
	End Function

	'X軸の最大値の取得
	Function MaximumX () As DWord
		Return This.caps.wXmax
	End Function

	'Y軸の最小値の取得
	Function MinimumY () As DWord
		Return This.caps.wYmin
	End Function

	'Y軸の最大値の取得
	Function MaximumY () As DWord
		Return This.caps.wYmax
	End Function

	'Z軸の最小値の取得
	Function MinimumZ () As DWord
		Return This.caps.wZmin
	End Function

	'Z軸の最大値の取得
	Function MaximumZ () As DWord
		Return This.caps.wZmax
	End Function

	'
	Function NumberOfButtons () As DWord
		Return This.caps.wNumButtons
	End Function

	'キャプチャ時のメッセージ送信間隔の最小値の取得
	Function MinimumPeriod () As DWord
		Return This.caps.wPeriodMin
	End Function

	'キャプチャ時のメッセージ送信間隔の最大値の取得
	Function MaximumPeriod () As DWord
		Return This.caps.wPeriodMax
	End Function

	'4軸目であるラダー(R軸)の最小値の取得
	Function MinimumR () As DWord
		Return This.caps.wRmin
	End Function

	'4軸目であるラダー(R軸)の最大値の取得
	Function MaximumR () As DWord
		Return This.caps.wRmax
	End Function

	'5軸目であるU軸の最小値の取得
	Function MinimumU () As DWord
		Return This.caps.wUmin
	End Function

	'5軸目であるU軸の最大値の取得
	Function MaximumU () As DWord
		Return This.caps.wUmax
	End Function

	'6軸目であるV軸の最小値の取得
	Function MinimumV () As DWord
		Return This.caps.wVmin
	End Function

	'6軸目であるV軸の最大値の取得
	Function MaximumV () As DWord
		Return This.caps.wVmax
	End Function

	'ジョイスティックの構成を取得
	Function Capabilities () As JoyCapability
		Return This.caps.wCaps As JoyCapability
	End Function

	'サポートしている動きの軸の数を取得
	Function NumberOfSupportedAxes () As DWord
		Return This.caps.wMaxAxes
	End Function

	'実際に使用している動きの軸の数を取得
	Function NumberOfUsedAxes () As DWord
		Return This.caps.wNumAxes
	End Function

	'サポートしているボタン数を取得
	Function NumberOfSupportedButtons () As DWord
		Return This.caps.wMaxButtons
	End Function

	'登録キーを文字列の取得
	Function RegistryKey () As String
		Return New System.String(This.caps.szRegKey As LPTSTR)
	End Function

	'OEMジョイスティックドライバを特定する文字列の取得
	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 'Property
	'各ボタンの状態を取得する
	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 'Method
	'指定した番号のボタンの状態を取得
	Function Button ( n As DWord ) As Boolean
		Return (1<<(n-1) And This.dwButtons) <> 0
	End Function

Public 'Operator
	'DWord値へ変換
	Function Operator () As DWord
		Return This.dwButtons
	End Function

	'DWord値と比較
	Function Operator== (dw As DWord) As Boolean
		Return This.dwButtons = dw
	End Function
	Function Operator<> (dw As DWord) As Boolean
		Return This.dwButtons <> dw
	End Function

	'DWord値とAnd演算
	Function Operator And (dw As DWord) As Boolean
		Return (This.dwButtons And dw) <> 0
	End Function

End Class


'PointOfView値を格納するクラス
Class JoyPointOfView
	pov As DWord

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

Public 'Static Constant
	'中心
	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 'Proparty
	Function IsCentered() As Boolean
		Return This.pov <> Centered
	End Function
	Function IsForward() As Boolean
		Return This.pov <> Forward
	End Function
	Function IsRight() As Boolean
		Return This.pov <> Right
	End Function
	Function IsBackward() As Boolean
		Return This.pov <> Backward
	End Function
	Function IsLeft() As Boolean
		Return This.pov <> Left
	End Function

Public 'Method
	'角度に変換
	Function ToDegrees() As Double
		Return (This.pov / 100)
	End Function


Public 'Operator
	'DWord値へ変換
	Function Operator () As DWord
		Return This.pov
	End Function

	'DWord値と比較
	Function Operator== (value As DWord ) As Boolean
		Return This.pov = value
	End Function
	Function Operator<> ( value As DWord ) As Boolean
		Return This.pov <> value
	End Function
	Function Operator<= ( value As DWord ) As Boolean
		Return This.pov <= value
	End Function
	Function Operator>= ( value As DWord ) As Boolean
		Return This.pov >= value
	End Function
	Function Operator< ( value As DWord ) As Boolean
		Return This.pov < value
	End Function
	Function Operator> ( value As DWord ) As Boolean
		Return This.pov > value
	End Function

	'DWord値と演算
	Function Operator+ ( value As DWord ) As DWord
		Return This.pov + value
	End Function
	Function Operator- ( value As DWord ) As DWord
		Return This.pov - value
	End Function
	Function Operator* ( value As DWord ) As DWord
		Return This.pov * value
	End Function
	Function Operator Mod ( value As DWord ) As DWord
		Return This.pov Mod value
	End Function
	Function Operator / ( value As DWord ) As Double
		Return This.pov / value
	End Function
End Class


'ジョイスティックの情報を格納するJOYINFOEX構造体のラップクラス
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 'Property
	'X軸の値を取得
	Function X () As DWord
		Return This.info.dwXpos
	End Function

	'Y軸の値を取得
	Function Y () As DWord
		Return This.info.dwYpos
	End Function

	'Z軸の値を取得
	Function Z () As DWord
		Return This.info.dwZpos
	End Function

	'ラダー(R軸)の値を取得
	Function R () As DWord
		Return This.info.dwRpos
	End Function

	'U軸の値を取得
	Function U () As DWord
		Return This.info.dwUpos
	End Function

	'V軸の値を取得
	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 JoystickDevice
	id As DWord

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

Public 'Property
	'スティックの動きの閾値を設定
	Sub Threshold ( value As DWord )
		joySetThreshold(id,value)
	End Sub

	'スティックの動きの閾値を取得
	Function Threshold () As DWord
		joyGetThreshold(id,Threshold)
	End Function

Public 'Method
	'ジョイステックの性能を取得
	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

	'キャプチャを開始
	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 Joystick
Public 'Static Constant
	Static Const Joystick1 = 0
	Static Const Joystick2 = 1

Public 'Static Property
	'ドライバによってサポートされるジョイスティック数を取得
	Static Function NumberOfDevices () As DWord
		Return joyGetNumDevs()
	End Function

	'物理的に接続されたジョイスティック数を取得
	Static Function NumberOfConnectedDevices () As DWord
		Dim max = 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

Public 'Static Method
	'使用可能なジョイスティックIDを列挙したIListを取得
	Static Function GetValidJoystickIDs() As System.Collections.Generic.IList<DWord>
		Dim max = NumberOfDevices()
		GetJoystickIDs = New System.Collections.Generic.List<DWord>
		Dim cnt As DWord
		Dim info As JOYINFO
		For cnt=0 To max-1
			If joyGetPos(cnt,info) = JOYERR_NOERROR Then
				GetJoystickIDs.Add(cnt)
			End If
		Next
	End Function

	'使用可能なジョイスティックデバイスを列挙したIListを取得
	Static Function GetDevices() As System.Collections.Generic.IList<JoystickDevice>
		Dim max = NumberOfDevices()
		GetDevices = New System.Collections.Generic.List<JoystickDevice>
		Dim cnt As DWord
		Dim info As JOYINFO
		For cnt=0 To max-1
			If joyGetPos(cnt,info) = JOYERR_NOERROR Then
				GetDevices.Add(GetDeviceFromID(cnt))
			End If
		Next
	End Function

	'ジョイスティックデバイスを取得
	Static Function GetDeviceFromID( id As DWord ) As JoystickDevice
		Return New Detail._System_JoystickDevice(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_JoystickDevice
	Inherits JoystickDevice
Public
	Sub _System_JoystickDevice ( 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
