Here's my complete joypad code. Everything you need to get working joypads in DBP using standard commands.
`Joypad Related ============================================================================================================================================================================
Type tDPad
XAxis#
YAxis#
EndType
Global DPad As tDPad
Type tLStick
XAxis#
YAxis#
XNeutral
YNeutral
MinX#
MaxX#
MinY#
MaxY#
DeadZone#
EndType
Global LStick As tLStick
Type tRStick
XAxis#
YAxis#
XNeutral
YNeutral
MinX#
MaxX#
MinY#
MaxY#
DeadZone#
EndType
Global RStick As tRStick
Global Joypad = 0
Global GamePad$ = ""
Function InitialiseJoypad()
Empty Checklist : Perform Checklist For Control Devices
If Checklist Quantity()
GamePad$ = Checklist String$( 1 )
Joypad = 1
LoadJoypadDetails()
Else
Joypad = 0
EndIf
EndFunction
`==============================================================================================================================
`==============================================================================================================================
`==============================================================================================================================
Function CalibrateJoypad( Stage )
`This needs to be integrated into your game's menus.
`You need to call CalibrateJoypad( Stage ) once a loop with the required stage setting.
`Stage 2 first, then stage 1, then stage 2 again.
If Not Joypad Then ExitFunction
If Stage = 1: `This stage sets the neutral point of the stick(s). It completes automatically in 1.25 seconds.
ResetJoypad()
Samples = 0 : LX = 0 : LY = 0 : RX = 0 : RY = 0
TimeOut = Timer() + 1250
Backdrop Off
Repeat
Samples = Samples + 1
LX = LX + Joystick X() : LY = LY + Joystick Y()
RX = RX + Joystick Z() : RY = RY + Joystick Twist Z()
LStick.XNeutral = LX / Samples : LStick.YNeutral = LY / Samples
RStick.XNeutral = RX / Samples : RStick.YNeutral = RY / Samples
Sync
Until Timer() > Timeout
Backdrop On
EndIf
If Stage = 2: `This stage sets the upper, and lower thresholds to the stick(s)
If Joystick X() < LStick.MinX# Then LStick.MinX# = Joystick X()
If Joystick X() > LStick.MaxX# Then LStick.MaxX# = Joystick X()
If Joystick Y() < LStick.MinY# Then LStick.MinY# = Joystick Y()
If Joystick Y() > LStick.MaxY# Then LStick.MaxY# = Joystick Y()
If Joystick Z() < RStick.MinX# Then RStick.MinX# = Joystick Z()
If Joystick Z() > RStick.MaxX# Then RStick.MaxX# = Joystick Z()
If Joystick Twist Z() < RStick.MinY# Then RStick.MinY# = Joystick Twist Z()
If Joystick Twist Z() > RStick.MaxY# Then RStick.MaxY# = Joystick Twist Z()
UpdateJoypad()
EndIf
EndFunction
`==============================================================================================================================
`==============================================================================================================================
`==============================================================================================================================
Function UpdateJoypad()
If Not Joypad Then ExitFunction
`Get The DPad / Hat Inputs. ===================================================================================================
HatX = 0 : HatY = 0 : Ang = Joystick Hat Angle( 0 )
If Ang = 0 Then HatY = 1
If Ang = 4500 Then HatX = 1: HatY = 1
If Ang = 9000 Then HatX = 1
If Ang = 13500 Then HatX = 1: HatY = -1
If Ang = 18000 Then HatY = -1
If Ang = 22500 Then HatX = -1: HatY = -1
If Ang = 27000 Then HatX = -1
If Ang = 31500 Then HatX =- 1: HatY = 1
DPad.XAxis# = HatX + 0.0
DPad.YAxis# = HatY + 0.0
`Get The left thumbstick Inputs. ==============================================================================================
Left# = 0.0 : Right# = 0.0 : Up# = 0.0 : Down# = 0.0
If Joystick X() < LStick.XNeutral Then Left# = 0.0 - Abs( Joystick X() - LStick.XNeutral ) / Abs( LStick.MinX# - LStick.XNeutral )
If Joystick X() > LStick.XNeutral Then Right# = Abs( Joystick X() - LStick.XNeutral ) / Abs( LStick.MaxX# - LStick.XNeutral )
If Joystick Y() < LStick.YNeutral Then Down# = 0.0 - Abs( Joystick Y() - LStick.YNeutral ) / Abs( LStick.MinY# - LStick.YNeutral )
If Joystick Y() > LStick.YNeutral Then Up# = Abs( Joystick Y() - LStick.YNeutral ) / Abs( LStick.MaxY# - LStick.YNeutral )
LStick.XAxis# = Left# + Right#
LStick.YAxis# = Up# + Down#
If Abs( LStick.XAxis# ) < LStick.DeadZone# Then LStick.XAxis# = 0.0
If Abs( LStick.YAxis# ) < LStick.DeadZone# Then LStick.YAxis# = 0.0
`Get The right thumbstick Inputs. =============================================================================================
Left# = 0.0 : Right# = 0.0 : Up# = 0.0 : Down# = 0.0
If Joystick Z() < RStick.XNeutral Then Left# = 0.0 - ( Abs( Joystick Z() - RStick.XNeutral ) / Abs( RStick.MinX# - RStick.XNeutral ) )
If Joystick Z() > RStick.XNeutral Then Right# = Abs( Joystick Z() - RStick.XNeutral ) / Abs( RStick.MaxX# - RStick.XNeutral )
If Joystick Twist Z() < RStick.YNeutral Then Up# = 0.0 - Abs( Joystick Twist Z() - RStick.YNeutral ) / Abs( RStick.MinY# - RStick.YNeutral )
If Joystick Twist Z() > RStick.YNeutral Then Down# = Abs( Joystick Twist Z() - RStick.YNeutral) / Abs( RStick.MaxY# - RStick.YNeutral)
RStick.XAxis# = Left# + Right#
RStick.YAxis# = Up# + Down#
If Abs( RStick.XAxis# ) < RStick.DeadZone# Then RStick.XAxis# = 0.0
If Abs( RStick.YAxis# ) < RStick.DeadZone# Then RStick.YAxis# = 0.0
EndFunction
`==============================================================================================================================
`==============================================================================================================================
`==============================================================================================================================
Function ResetJoypad()
DPad.XAxis# = 0.0 : DPad.YAxis# = 0.0
LStick.XAxis# = 0.0 : LStick.YAxis# = 0.0
LStick.XNeutral = 0 : LStick.YNeutral = 0
LStick.MinX# = 0.0 : LStick.MaxX# = 0.0
LStick.MinY# = 0.0 : LStick.MaxY# = 0.0
LStick.DeadZone# = 0.1
RStick.XAxis# = 0.0 : RStick.YAxis# = 0.0
RStick.XNeutral = 0 : RStick.YNeutral = 0
RStick.MinX# = 0.0 : RStick.MaxX# = 0.0
RStick.MinY# = 0.0 : RStick.MaxY# = 0.0
RStick.DeadZone# = 0.1
EndFunction
`==============================================================================================================================
`==============================================================================================================================
`==============================================================================================================================
Function SaveJoypadDetails()
If Not Joypad Then ExitFunction
If File Exist( ".\Resources\Profiles\" + GamePad$ + ".Txt" ) Then Delete File ".\Resources\Profiles\" + GamePad$ + ".Txt"
Open To Write 1, ".\Resources\Profiles\" + GamePad$ + ".Txt"
Write String 1, Str$( DPad.XAxis# )
Write String 1, Str$( DPad.YAxis# )
Write String 1, Str$( LStick.XAxis# )
Write String 1, Str$( LStick.YAxis# )
Write String 1, Str$( LStick.XNeutral )
Write String 1, Str$( LStick.YNeutral )
Write String 1, Str$( LStick.MinX# )
Write String 1, Str$( LStick.MaxX# )
Write String 1, Str$( LStick.MinY# )
Write String 1, Str$( LStick.MaxY# )
Write String 1, Str$( LStick.DeadZone# )
Write String 1, Str$( RStick.XAxis# )
Write String 1, Str$( RStick.YAxis# )
Write String 1, Str$( RStick.XNeutral )
Write String 1, Str$( RStick.YNeutral )
Write String 1, Str$( RStick.MinX# )
Write String 1, Str$( RStick.MaxX# )
Write String 1, Str$( RStick.MinY# )
Write String 1, Str$( RStick.MaxY# )
Write String 1, Str$( RStick.DeadZone# )
Close File 1
EndFunction
`==============================================================================================================================
`==============================================================================================================================
`==============================================================================================================================
Function LoadJoypadDetails()
If Not Joypad Then ExitFunction
If Not File Exist( ".\Resources\Profiles\" + GamePad$ + ".Txt" )
Joypad = 0
GamePad$ = ""
ExitFunction
EndIf
ResetJoypad()
Open To Read 1, ".\Resources\Profiles\" + GamePad$ + ".Txt"
Read String 1, Temp$: DPad.XAxis# = Val( Temp$ )
Read String 1, Temp$: DPad.YAxis# = Val( Temp$ )
Read String 1, Temp$: LStick.XAxis# = Val( Temp$ )
Read String 1, Temp$: LStick.YAxis# = Val( Temp$ )
Read String 1, Temp$: LStick.XNeutral = Val( Temp$ )
Read String 1, Temp$: LStick.YNeutral = Val( Temp$ )
Read String 1, Temp$: LStick.MinX# = Val( Temp$ )
Read String 1, Temp$: LStick.MaxX# = Val( Temp$ )
Read String 1, Temp$: LStick.MinY# = Val( Temp$ )
Read String 1, Temp$: LStick.MaxY# = Val( Temp$ )
Read String 1, Temp$: LStick.DeadZone# = Val( Temp$ )
Read String 1, Temp$: RStick.XAxis# = Val( Temp$ )
Read String 1, Temp$: RStick.YAxis# = Val( Temp$ )
Read String 1, Temp$: RStick.XNeutral = Val( Temp$ )
Read String 1, Temp$: RStick.YNeutral = Val( Temp$ )
Read String 1, Temp$: RStick.MinX# = Val( Temp$ )
Read String 1, Temp$: RStick.MaxX# = Val( Temp$ )
Read String 1, Temp$: RStick.MinY# = Val( Temp$ )
Read String 1, Temp$: RStick.MaxY# = Val( Temp$ )
Read String 1, Temp$: RStick.DeadZone# = Val( Temp$ )
Close File 1
EndFunction
`==============================================================================================================================
`==============================================================================================================================
`==============================================================================================================================
Function GetJoypadValue#( Name$ )
If Not Joypad Then ExitFunction
Name$ = Lower$( Name$ )
If Name$ = "hatx" Then ExitFunction DPad.XAxis#
If Name$ = "haty" Then ExitFunction DPad.YAxis#
If Name$ = "leftthumbx" Then ExitFunction LStick.XAxis#
If Name$ = "leftthumby" Then ExitFunction LStick.YAxis#
If Name$ = "rightthumbx" Then ExitFunction RStick.XAxis#
If Name$ = "rightthumby" Then ExitFunction RStick.YAxis#
EndFunction 0.0
`============================================================================================================================
`============================================================================================================================
`============================================================================================================================
Function GetJoypadName$()
If Not Joypad Then ExitFunction
If Abs( DPad.XAxis# ) > 0.5 Then ExitFunction "HatX"
If Abs( DPad.YAxis# ) > 0.5 Then ExitFunction "HatY"
If Abs( LStick.XAxis# ) > 0.5 Then ExitFunction "LeftThumbX"
If Abs( LStick.YAxis# ) > 0.5 Then ExitFunction "LeftThumbY"
If Abs( RStick.XAxis# ) > 0.5 Then ExitFunction "RightThumbX"
If Abs( RStick.YAxis# ) > 0.5 Then ExitFunction "RightThumbY"
EndFunction ""
Just call InitialiseJoypad() first. This will check to see if a saved joypad profile exists. If it does, it will load it and your joypad is ready to go. If the profile does not exist, then you'll need to call CalibrateJoypad( Stage ) a number of times.
Call it with stage = 2 first. This is when you wiggle all the axis to set the upper and lower boundaries of movement. Then call it with stage 1 which sets the neutral point, then call stage 2 again which confirms everything is working.
Call UpdateJoypad() to get the current stick/hat angles.
Call GetJoypadValue#( Name$ ) to get the value of a stick/hat.
For example, GetJoypadValue#( "hatx" ) will return the hat X axis in a range of -1.0, to 1.0, GetJoypadValue#( "rightthumby" ) will return the Right Thumbsticks Y Axis in a range of -1.0, to 1.0.
Axis$ = GetJoypadName$() will get the name of the stick/hat your using. (This is only really useful when your setting up which axis does what in your game.) Call this, move a stick/hat and it'll tell you which one you moved.
All axes return values from -1.0 to 1.0.
Any questions then just ask.
My signature is NOT a moderator plaything! Stop changing it!
