summaryrefslogtreecommitdiff
path: root/Assets/Samples/OpenXR Plugin/1.14.3/Controller/Scripts/ActionToButtonISX.cs
blob: 5860af39af972ec13928878f61cd110726a9549f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using UnityEngine.InputSystem;
using UnityEngine.UI;

namespace UnityEngine.XR.OpenXR.Samples.ControllerSample
{
    public class ActionToButtonISX : MonoBehaviour
    {
        [SerializeField]
        private InputActionReference m_ActionReference;
        public InputActionReference actionReference { get => m_ActionReference; set => m_ActionReference = value; }

        [SerializeField]
        Color enabledColor = Color.green;

        [SerializeField]
        Color disabledColor = Color.red;

        [SerializeField]
        Image image = null;

        Graphic graphic = null;
        Graphic[] graphics = new Graphic[] { };

        private void OnEnable()
        {
            if (image == null)
                Debug.LogWarning("ActionToButton Monobehaviour started without any associated image. This input will not be reported.", this);

            graphic = gameObject.GetComponent<Graphic>();
            graphics = gameObject.GetComponentsInChildren<Graphic>();
        }

        Type lastActiveType = null;

        void Update()
        {
            if (actionReference != null && actionReference.action != null && image != null && actionReference.action.enabled && actionReference.action.controls.Count > 0)
            {
                SetVisible(true);

                Type typeToUse = null;

                if (actionReference.action.activeControl != null)
                {
                    typeToUse = actionReference.action.activeControl.valueType;
                }
                else
                {
                    typeToUse = lastActiveType;
                }

                if (typeToUse == typeof(bool))
                {
                    lastActiveType = typeof(bool);
                    bool value = actionReference.action.ReadValue<bool>();
                    image.color = value ? enabledColor : disabledColor;
                }
                else if (typeToUse == typeof(float))
                {
                    lastActiveType = typeof(float);
                    float value = actionReference.action.ReadValue<float>();
                    image.color = value > 0.5 ? enabledColor : disabledColor;
                }
                else
                {
                    image.color = disabledColor;
                }
            }
            else
            {
                SetVisible(false);
            }
        }

        void SetVisible(bool visible)
        {
            if (graphic != null)
                graphic.enabled = visible;

            for (int i = 0; i < graphics.Length; i++)
            {
                graphics[i].enabled = visible;
            }
        }
    }
}