Untitled

 avatar
unknown
fsharp
2 years ago
2.1 kB
6
Indexable
namespace Util

open Godot

module InputReader =

    type Device =
        | MouseAndKeyboard = 0
        | Gamepad = 1

    let DisplayNames =
        [|
            "Mouse and Keyboard"
            "Gamepad"
        |]

    let GetCurrentDeviceDisplay
        (currentDevice: Device)
        (lastInputEvent: InputEvent)
        (lastGamepadMove: Vector2)
        (lastGamepadLook: Vector2)
        (lastDisplayName: string) =
        match currentDevice with
        | Device.MouseAndKeyboard -> DisplayNames[(int)Device.MouseAndKeyboard]
        | Device.Gamepad ->
            match lastInputEvent with
            | null -> failwith "Invalid Input Event"
            | :? InputEventJoypadButton -> DisplayNames[(int)Device.Gamepad]
            | :? InputEventJoypadMotion ->
                let gamepadMovedEnough =
                    let gamepadMotion = lastInputEvent :?> InputEventJoypadMotion
                    let IsInputDifferenceExceedingTolerance (lastInput: float32) =
                        let stickTolerance = 0.1f
                        Mathf.Abs(gamepadMotion.AxisValue - lastInput) > stickTolerance
                    match gamepadMotion.Axis with
                    | JoyAxis.LeftX -> IsInputDifferenceExceedingTolerance lastGamepadMove.X
                    | JoyAxis.LeftY -> IsInputDifferenceExceedingTolerance lastGamepadMove.Y
                    | JoyAxis.RightX -> IsInputDifferenceExceedingTolerance lastGamepadLook.X
                    | JoyAxis.RightY -> IsInputDifferenceExceedingTolerance lastGamepadLook.Y
                    | JoyAxis.TriggerLeft
                    | JoyAxis.TriggerRight ->
                        let triggerTolerance = 0.1f
                        gamepadMotion.AxisValue > triggerTolerance
                    | _ -> false
                if gamepadMovedEnough then
                    DisplayNames[(int)Device.Gamepad]
                else
                    lastDisplayName
            | _ -> failwith "Invalid Input Event"
        | _ -> System.ArgumentOutOfRangeException() |> raise
Editor is loading...
Leave a Comment