eye-therapy-2 / Assets / Scripts / Other / GeneralInputManager.cs
GeneralInputManager.cs
Raw
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Events;
using System;
using System.Collections.Generic;

[Serializable]
public class InputEvent : UnityEvent<InputAction.CallbackContext> { }

public enum VRControlMode
{
    Basic,
    Advanced,

    Count
}

public class GeneralInputManager : MonoBehaviour, IListener
{
    private int vrControlMode = 1;
    private InputActions input;

    private void Awake()
    {
        vrControlMode = PlayerPrefs.GetInt("VRControlMode", 1);
        input = new InputActions();
        EventManager.Instance.AddListener(EVENT_TYPE.ON_VR_CONTROLMODE_SWITCHED, this);
    }

    private void OnEnable()
    {
        input.General.Enable();

        input.General.SwitchVRControlMode.performed += OnVRModeSwitched;
    }

    private void OnDisable()
    {
        input.General.SwitchVRControlMode.performed -= OnVRModeSwitched;

        input.General.Disable();
    }

    public void OnVRModeSwitched(InputAction.CallbackContext ctx)
    {
        SwitchVRControlMode();
        EventManager.Instance.PostNotification<int>(EVENT_TYPE.ON_VR_CONTROLMODE_SWITCHED, this, vrControlMode);
    }

    private void SwitchVRControlMode(int newVRControlMode = (int)VRControlMode.Count)
    {
        if (newVRControlMode == (int)VRControlMode.Count)
        {
            vrControlMode = Mathf.Abs(vrControlMode - 1);
        }
        else
        {
            vrControlMode = newVRControlMode;
        }
        PlayerPrefs.SetInt("VRControlMode", vrControlMode);
    }

    public void OnEvent<T>(EVENT_TYPE eventType, Component Sender, T param = default)
    {
        switch(eventType)
        {
            case EVENT_TYPE.ON_VR_CONTROLMODE_SWITCHED:
                if(Sender != this)
                {
                    if (EqualityComparer<T>.Default.Equals(param, default(T)))
                        SwitchVRControlMode();
                    else
                        SwitchVRControlMode((dynamic)param);
                }
                break;
        }
    }
}