-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathInputDeviceSetting.cs.txt
More file actions
184 lines (151 loc) · 4.78 KB
/
InputDeviceSetting.cs.txt
File metadata and controls
184 lines (151 loc) · 4.78 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Linq;
using Assets.Metater;
using R3;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class InputDeviceSetting : MetaMb<InputDeviceSetting>, IInitableSetting
{
private static readonly string NoMicrophoneDetected = "No Microphone Detected";
public SerializableReactiveProperty<string> selectedDevice = new(null);
[Header("Colors")]
public Color missingColor; // no mic at all
public Color disconnectedColor; // saved mic is not connected
public Color nominalColor;
public Color missingTextColor;
public Color nominalTextColor;
[Header("UI")]
public TMP_Text text;
public TMP_Dropdown dropdown;
public Button resetButton;
private string PlayerPrefsKey => name;
private bool isInit = false;
private void Awake()
{
dropdown.onValueChanged.AddListener(OnDropdownValueChanged);
resetButton.onClick.AddListener(OnResetButtonClicked);
Init();
}
private void OnDropdownValueChanged(int i)
{
AudioSystem.Instance.PlayClickSfx();
string device = dropdown.options[i].text;
if (string.IsNullOrWhiteSpace(device) || device == NoMicrophoneDetected)
{
return;
}
selectedDevice.Value = device;
}
private void OnResetButtonClicked()
{
if (dropdown.options.Count != 0)
{
OnDropdownValueChanged(0);
}
}
public void Init()
{
if (isInit)
{
return;
}
isInit = true;
string defaultDevice = Microphone.devices.Length > 0 ? Microphone.devices[0] : null;
selectedDevice.Value = PlayerPrefs.GetString(PlayerPrefsKey, defaultDevice);
selectedDevice.Subscribe(selectedDevice =>
{
RefreshOptions(Microphone.devices, selectedDevice);
if (selectedDevice == null)
{
return;
}
PlayerPrefs.SetString(PlayerPrefsKey, selectedDevice);
}).AddTo(this);
}
public void Delta(string[] devices, string activeDevice)
{
Init();
text.text = activeDevice ?? NoMicrophoneDetected;
if (activeDevice == null)
{
text.color = missingTextColor;
}
else
{
text.color = nominalTextColor;
if (string.IsNullOrEmpty(selectedDevice.Value))
{
selectedDevice.Value = activeDevice;
}
}
RefreshOptions(devices, selectedDevice.Value);
}
private void RefreshOptions(string[] devices, string selectedDevice)
{
dropdown.ClearOptions();
if (devices.Length == 0)
{
if (selectedDevice == null)
{
SetGraphicState(GraphicState.Missing);
}
else
{
dropdown.AddOptions(new List<TMP_Dropdown.OptionData> { new(selectedDevice) });
SetGraphicState(GraphicState.Disconnected);
dropdown.SetValueWithoutNotify(0);
}
}
else
{
if (devices.Contains(selectedDevice))
{
dropdown.AddOptions(devices.Select(d => new TMP_Dropdown.OptionData(d)).ToList());
SetGraphicState(GraphicState.Nominal);
dropdown.SetValueWithoutNotify(DropdownIndexOf(selectedDevice));
}
else if (string.IsNullOrWhiteSpace(selectedDevice))
{
dropdown.AddOptions(devices.Select(d => new TMP_Dropdown.OptionData(d)).ToList());
SetGraphicState(GraphicState.Nominal);
dropdown.SetValueWithoutNotify(0);
this.selectedDevice.Value = devices[0];
}
else
{
dropdown.AddOptions(devices.Select(d => new TMP_Dropdown.OptionData(d)).ToList());
dropdown.AddOptions(new List<TMP_Dropdown.OptionData> { new(selectedDevice) });
SetGraphicState(GraphicState.Disconnected);
dropdown.SetValueWithoutNotify(devices.Length);
}
}
}
private int DropdownIndexOf(string device)
{
for (int i = 0; i < dropdown.options.Count; i++)
{
if (dropdown.options[i].text == device)
{
return i;
}
}
return -1; // Not found
}
private void SetGraphicState(GraphicState graphicState)
{
dropdown.targetGraphic.color = graphicState switch
{
GraphicState.Missing => missingColor,
GraphicState.Disconnected => disconnectedColor,
_ => nominalColor,
};
}
private enum GraphicState
{
Missing,
Disconnected,
Nominal
}
}