Untitled

mail@pastecode.io avatar
unknown
fsharp
7 months ago
2.1 kB
2
Indexable
Never
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
Leave a Comment