'
'                           ___________________________________________
'         _______ _ _ _     \___           ___________________________/
'    /\  (_______) (_) |       /  //  //  /  
'   /  \  _  __  | |_| | _    /  //  //  / << version 0.86 alpha test >>
'  / /\ \| |(__| | | | || \  /  //  //  /  For FreeBASIC (v0.20b or later).
' | |__| | |     | | | |_) )/  //  //  /     
' |_|  |_|_|     |_|_|____//  //  //  /   Original Development by
'  _______________________/  //  //  /___ Adigun Azikiwe Polack for Aura Flow.
' /_____________________________________/
'
'  A  u  r  a      F  l  o  w      L  i  b  r  a  r  y      3
'_________________________________________________________________________
'
' (c)2005-2016 Adigun Azikiwe Polack/Aura Flow.  All Rights Reserved.
'
'
'
'   ______                                     __
'  / ____/___ _____ ___  ___  ____  ____ _____/ /
' / / __/ __ `/ __ `__ \/ _ \/ __ \/ __ `/ __  /
'/ /_/ / /_/ / / / / / /  __/ /_/ / /_/ / /_/ /
'\____/\__,_/_/ /_/ /_/\___/ .___/\__,_/\__,_/
'                         /_/        __  ___          __      __   
'                                   /  |/  /___  ____/ /_  __/ /__ 
'                                  / /|_/ / __ \/ __  / / / / / _ \
'                                 / /  / / /_/ / /_/ / /_/ / /  __/
'                                /_/  /_/\____/\__,_/\__,_/_/\___/                                              
'                                                                                    
'
'_________________________________________________________________________
'                        ----- N O T I C E -----
'_________________________________________________________________________
' The following is this piece of source code to this version of AFlib III.
' You are free to look at it to give you some inspiring ideas in your own
' programming in FreeBASIC, but you are *never* to even change or adjust
' any of this source code whatsoever in any way, shape, or form at all.
' EVER.  ;)
'
' The reason for this is to ensure that all of the routines in this source
' code are working as properly as they should in FreeBASIC v0.20b or
' newer.  That is all.
'
' With that said, do enjoy the library and be inspired even more
' greatly than ever!!!  (^-^)//
'_________________________________________________________________________



'$profile
'$stop_scan

#include once "fbgfx.bi"
ScreenControl(FB.SET_DRIVER_NAME, "GDI")

Const AF3_GP_Active = 1
Const AF3_GP_Inactive = 0

Static Shared as Single AF3_JoyLeft_X,  AF3_JoyLeft_Y, _
                        AF3_JoyRight_X, AF3_JoyRight_Y, _
                        AF3_JoyThrottle
                        
Static Shared as Integer AF3_Joystick_id, AF3_JoyButtonPress




Declare Sub AFlibIII_GrabJoystick ( Byval Joy_id as Integer )

Declare Function AFlibIII_Joy_Active as Integer

Declare Function AFlibIII_Joy_Left_X_axis as Single
Declare Function AFlibIII_Joy_Left_Y_axis as Single
Declare Function AFlibIII_Joy_Right_X_axis as Single
Declare Function AFlibIII_Joy_Right_Y_axis as Single
Declare Function AFlibIII_Joy_Throttle_axis as Single

Declare Function AFlibIII_Joy_Trigger1 as Integer
Declare Function AFlibIII_Joy_Trigger2 as Integer
Declare Function AFlibIII_Joy_Trigger3 as Integer
Declare Function AFlibIII_Joy_Trigger4 as Integer
Declare Function AFlibIII_Joy_Trigger5 as Integer
Declare Function AFlibIII_Joy_Trigger6 as Integer
Declare Function AFlibIII_Joy_Trigger7 as Integer
Declare Function AFlibIII_Joy_Trigger8 as Integer
Declare Function AFlibIII_Joy_Trigger9 as Integer
Declare Function AFlibIII_Joy_Trigger10 as Integer
Declare Function AFlibIII_Joy_Trigger11 as Integer
Declare Function AFlibIII_Joy_Trigger12 as Integer
Declare Function AFlibIII_Joy_Trigger13 as Integer
Declare Function AFlibIII_Joy_Trigger14 as Integer
Declare Function AFlibIII_Joy_Trigger15 as Integer
Declare Function AFlibIII_Joy_Trigger16 as Integer





'_________________________________________________________________________ 
Sub AFlibIII_GrabJoystick ( Byval Joy_id as Integer )
                            
  Dim Joy_Buttons as Integer = 0, _
      X_Axis_Left_Analogue_or_Digital as Single = 0, _
      Y_Axis_Left_Analogue_or_Digital as Single = 0, _
      X_Axis_Right_Analogue as Single = 0, _
      Y_Axis_Right_Analogue as Single = 0, _
      Joy_Throttle as Single = 0
  
  AF3_Joystick_id = 0
  AF3_JoyButtonPress = 0
  AF3_JoyLeft_X = 0
  AF3_JoyLeft_Y = 0
  AF3_JoyRight_X = 0
  AF3_JoyRight_Y = 0
  AF3_JoyThrottle = 0

  GetJoystick Joy_id, Joy_Buttons, _
              X_Axis_Left_Analogue_or_Digital, Y_Axis_Left_Analogue_or_Digital, _
              Joy_Throttle, Y_Axis_Right_Analogue, X_Axis_Right_Analogue

  AF3_Joystick_id = Joy_id
  AF3_JoyButtonPress = Joy_Buttons
  AF3_JoyLeft_X = X_Axis_Left_Analogue_or_Digital
  AF3_JoyLeft_Y = Y_Axis_Left_Analogue_or_Digital
  AF3_JoyRight_X = X_Axis_Right_Analogue
  AF3_JoyRight_Y = Y_Axis_Right_Analogue
  AF3_JoyThrottle = Joy_Throttle
  
End Sub



Function AFlibIII_Joy_Active as Integer
  If AF3_JoyLeft_X < -1.0 then
    Return 0: Exit Function  ' <--- Joystick/Gamepad IS detected!
  else
    Return -1: Exit Function  ' <--- Joystick/Gamepad not detected.
  End if
End Function

Function AFlibIII_Joy_Left_X_axis as Single
  Return AF3_JoyLeft_X
End Function

Function AFlibIII_Joy_Left_Y_axis as Single
  Return AF3_JoyLeft_Y
End Function

Function AFlibIII_Joy_Right_X_axis as Single
  Return AF3_JoyRight_X
End Function

Function AFlibIII_Joy_Right_Y_axis as Single
  Return AF3_JoyRight_Y
End Function

Function AFlibIII_Joy_Throttle_axis as Single
  Return AF3_JoyThrottle
End Function




'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger1 as Integer
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B1, AF3_GP_Active, 0) 
End Function 

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger2 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B10, AF3_GP_Active, 0) 
End Function 

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger3 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B100, AF3_GP_Active, 0) 
End Function 

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger4 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B1000, AF3_GP_Active, 0) 
End Function

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger5 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B10000, AF3_GP_Active, 0) 
End Function 

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger6 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B100000, AF3_GP_Active, 0) 
End Function 

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger7 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B1000000, AF3_GP_Active, 0) 
End Function 

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger8 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B10000000, AF3_GP_Active, 0) 
End Function

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger9 as Integer
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B100000000, AF3_GP_Active, 0) 
End Function 

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger10 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B1000000000, AF3_GP_Active, 0) 
End Function

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger11 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B10000000000, AF3_GP_Active, 0) 
End Function

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger12 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B100000000000, AF3_GP_Active, 0) 
End Function

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger13 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B1000000000000, AF3_GP_Active, 0) 
End Function

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger14 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B10000000000000, AF3_GP_Active, 0) 
End Function

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger15 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B100000000000000, AF3_GP_Active, 0) 
End Function

'_________________________________________________________________________ 
Function AFlibIII_Joy_Trigger16 as Integer 
  Dim CheckButton as Integer = AF3_JoyButtonPress
  Return iif(CheckButton And &B1000000000000000, AF3_GP_Active, 0) 
End Function
