'*********************************************************
'* Controller.bi                               6/12/2021
'*
'* Contains all the code pretaining to controller setup
'* and settings
'*********************************************************




'*********************************************************
'* SetupController
'*
'* Called from main menu to bind buttons and set analog deadzone
'*********************************************************
Sub SetupController
    Do
    Loop Until InKey$ = ""
    Input "Enter Analog Stick dead-zone size (10 Default)"; Controller.DeadZone
    Do
    Loop Until InKey$ = ""
    Print "Push Button for 'A'"
    Controller.A = ButtonBind
    Print "Push Button for 'B'"
    Controller.B = ButtonBind
    Print "Push Button for 'Start'"
    Controller.Start = ButtonBind
    Controller.Enabled = 1
    Input "Configure D-Pad? (Y/N)"; Ans$
    Ans$ = UCase$(Ans$)
    If Ans$ = "Y" Then
        Print "Push Button for 'Left'"
        Controller.Up = ButtonBind
        Print "Push Button for 'Up'"
        Controller.Down = ButtonBind
        Print "Push Button for 'Down'"
        Controller.Left = ButtonBind
        Print "Push Button for 'Right'"
        Controller.Right = ButtonBind
    End If


    Call SaveController
End Sub




'*********************************************************
'* SaveController
'*
'* Saves the controller info.
'*********************************************************
Sub SaveController
    Open "Data\Controller.inf" For Random As #1
    Put #1, , Controller
    Close #1
End Sub


'*********************************************************
'* LoadController
'*
'* Loads the controller config into the controller object
'*********************************************************
Sub LoadController
    CALL PrintConsole("Engine -> Initializing Controller.", 1)

    OPEN "Data\Controller.inf" FOR Random AS #1
    Get #1, , Controller
    CLOSE #1
END SUB




'*********************************************************
'* ButtonBind
'*
'* Used to detect controller button press during controller setup
'*********************************************************

FUNCTION ButtonBind
    DO
        _LIMIT 10
        IF STRIG(0) = -1 THEN
            ButtonBind = 0
            EXIT DO
        ELSEIF STRIG(1) = -1 THEN
            ButtonBind = 1
            EXIT DO
        ELSEIF STRIG(2) = -1 THEN
            ButtonBind = 2
            EXIT DO
        ELSEIF STRIG(3) = -1 THEN
            ButtonBind = 3
            EXIT DO
        ELSEIF STRIG(4) = -1 THEN
            ButtonBind = 4
            EXIT DO
        ELSEIF STRIG(5) = -1 THEN
            ButtonBind = 5
            EXIT DO
        ELSEIF STRIG(6) = -1 THEN
            ButtonBind = 6
            EXIT DO
        ELSEIF STRIG(7) = -1 THEN
            ButtonBind = 7
            EXIT DO
        ELSEIF STRIG(8) = -1 THEN
            ButtonBind = 8
            EXIT DO
        ELSEIF STRIG(9) = -1 THEN
            ButtonBind = 9
            EXIT DO
        ELSEIF STRIG(10) = -1 THEN
            ButtonBind = 10
            EXIT DO
        END IF
    LOOP
END FUNCTION

