summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/VRCSDK/SDK3/Editor/VRCMidiWindow.cs
blob: c292408435e1a6341e3c881b457340789c8da35d (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
88
89
90
91
92
93
94
95
96
97
98
99
#if UNITY_2019_3_OR_NEWER
using UnityEditor.UIElements;
using UnityEngine.UIElements;
using UnityEngine.UIElements.StyleSheets;
#else
using UnityEditor.Experimental.UIElements;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleEnums;
#endif
using System.Linq;
using UnityEditor;
using UnityEngine;
using VRC.SDKBase.Midi;

namespace VRC.SDK3.Midi
{
#if UNITY_STANDALONE_WIN
    public class VRCMidiWindow : EditorWindow
    {
        private VisualElement _rootView;
        private TextField _deviceNameField;

        public const string DEVICE_NAME_STRING = "VRC.SDK3.Midi.Device";
#if (UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN) && !UNITY_ANDROID
        [MenuItem("VRChat SDK/Utilities/Midi")]
        private static void ShowWindow()
        {
            VRCMidiWindow foo = CreateInstance(typeof(VRCMidiWindow)) as VRCMidiWindow;
            // ReSharper disable once PossibleNullReferenceException
            foo.titleContent = new GUIContent("Midi");
            foo.maxSize = new Vector2(256, 80);
            foo.ShowUtility();
        }

        private void OnEnable()
        {
#if UNITY_2019_3_OR_NEWER
            _rootView = rootVisualElement;
#else
            _rootView = this.GetRootVisualContainer();
#endif
            _rootView.Add(new Label("Midi Settings")
            {
                style =
                {
                    fontSize = 18,
                    marginTop = 10,
                    marginBottom = 10,
                }
            });

            // Container for Device Name label and field
            VisualElement deviceNameContainer = new VisualElement() {style = {flexDirection = FlexDirection.Row}};
            _rootView.Add(deviceNameContainer);
            
            // Label for Field
            deviceNameContainer.Add(new Label("Device Name"));

            // Input Name Field
            _deviceNameField = new TextField()
            {
                isDelayed = true,
                value = EditorPrefs.GetString(DEVICE_NAME_STRING),
                style = { flexGrow = 1 },
            };
#if UNITY_2019_3_OR_NEWER
            _deviceNameField.RegisterValueChangedCallback(
#else
             _deviceNameField.OnValueChanged(
#endif
                evt => EditorPrefs.SetString(DEVICE_NAME_STRING, evt.newValue));

            // Get available device names
            VRCPortMidiInput midi = new VRCPortMidiInput();
            var deviceNames = midi.GetDeviceNames().ToList();

            // Add blank device name to use if specified device not found
            deviceNames.Insert(0, "");
            string currentDeviceValue = EditorPrefs.GetString(DEVICE_NAME_STRING);
            string defaultValue = deviceNames.Contains(currentDeviceValue) ? currentDeviceValue : "";

            // Create and add device popup
            var deviceNamePopupField = new PopupField<string>(deviceNames, defaultValue)
            {
                style = {flexGrow = 1},
                name = "midiDevicePopUp",
            };
#if UNITY_2019_3_OR_NEWER
            deviceNamePopupField.RegisterValueChangedCallback(
#else
             deviceNamePopupField.OnValueChanged(
#endif
                evt => EditorPrefs.SetString(DEVICE_NAME_STRING, evt.newValue));
            deviceNameContainer.Add(deviceNamePopupField);
        }
#endif
    }
#endif
}