soundvision/UnityProject/Assets/Editor/PdConsole.cs

100 lines
2.8 KiB
C#
Raw Normal View History

using System;
using UnityEditor;
2019-09-28 18:17:39 +00:00
namespace cylvester
{
public class PdConsole : EditorWindow
{
private const int NumChannels = 16;
2019-09-28 18:17:39 +00:00
private IEditorToggle dspToggle_;
private ITogglePresenter togglePresenter_;
private Action onDspToggleStateChanged_;
2019-09-28 18:17:39 +00:00
private IPdBackend pdBackend_;
2019-09-28 19:21:58 +00:00
private LevelMeter[] levelMeters_;
private PdArray levelMeterArray_;
2019-09-28 18:17:39 +00:00
[MenuItem("SoundVision/Pd console %#p")]
static void Init()
{
var window = (PdConsole)GetWindow(typeof(PdConsole));
window.Show();
}
private void Awake()
2019-09-28 18:29:31 +00:00
{
var foundObjects = FindObjectsOfType(typeof(PdBackend));
if (foundObjects.Length != 1)
return;
pdBackend_ = (IPdBackend) foundObjects[0];
2019-09-28 18:17:39 +00:00
dspToggle_ = new EditorToggle();
2019-09-28 18:29:31 +00:00
onDspToggleStateChanged_ = () =>
{
if (pdBackend_.State)
{
levelMeterArray_ = new PdArray("levelmeters", NumChannels);
for (var i = 0; i < NumChannels; ++i)
levelMeters_[i] = new LevelMeter(i, levelMeterArray_);
}
else
levelMeterArray_.Dispose();
};
pdBackend_.StateChanged += onDspToggleStateChanged_;
2019-09-28 18:29:31 +00:00
togglePresenter_ = new TogglePresenter(dspToggle_, pdBackend_);
levelMeters_ = new LevelMeter[NumChannels];
}
private void OnDestroy()
{
levelMeterArray_?.Dispose();
dspToggle_.ToggleStateChanged -= onDspToggleStateChanged_;
2019-09-28 18:17:39 +00:00
}
2019-09-28 18:29:31 +00:00
private void OnGUI ()
2019-09-28 18:17:39 +00:00
{
if(!ValidatePdBackend())
2019-09-28 18:29:31 +00:00
return;
2019-09-28 19:21:58 +00:00
EditorGUILayout.Space();
2019-09-28 18:17:39 +00:00
dspToggle_.State = EditorGUILayout.Toggle("Pure Data Process", dspToggle_.State);
if(!CheckProcessingState())
return;
2019-09-28 19:21:58 +00:00
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
levelMeterArray_.Update();
2019-09-28 19:21:58 +00:00
foreach (var levelMeter in levelMeters_)
levelMeter.Render();
EditorGUILayout.EndHorizontal();
Repaint();
2019-09-28 18:17:39 +00:00
}
private bool ValidatePdBackend()
{
if (pdBackend_ != null)
return true;
EditorGUILayout.LabelField("No Pd backend found in the scene");
return false;
}
2019-09-28 18:29:31 +00:00
private bool CheckProcessingState()
2019-09-28 18:29:31 +00:00
{
if (pdBackend_.State)
return true;
EditorGUILayout.LabelField("Pd process is currently inactive");
return false;
2019-09-28 18:29:31 +00:00
}
2019-09-28 18:17:39 +00:00
}
}