2019-09-28 22:19:35 +00:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEditor;
|
2019-09-28 18:17:39 +00:00
|
|
|
|
|
|
|
|
|
namespace cylvester
|
|
|
|
|
{
|
|
|
|
|
public class PdConsole : EditorWindow
|
|
|
|
|
{
|
2019-09-28 22:19:35 +00:00
|
|
|
|
private const int NumChannels = 16;
|
|
|
|
|
|
2019-09-28 18:17:39 +00:00
|
|
|
|
private IEditorToggle dspToggle_;
|
|
|
|
|
private ITogglePresenter togglePresenter_;
|
2019-09-28 22:19:35 +00:00
|
|
|
|
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_;
|
2019-09-28 22:19:35 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-28 22:19:35 +00:00
|
|
|
|
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
|
|
|
|
|
2019-09-28 22:19:35 +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_);
|
2019-09-28 22:19:35 +00:00
|
|
|
|
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
|
|
|
|
{
|
2019-09-28 22:19:35 +00:00
|
|
|
|
if(!ValidatePdBackend())
|
2019-09-28 18:29:31 +00:00
|
|
|
|
return;
|
2019-09-28 22:19:35 +00:00
|
|
|
|
|
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);
|
2019-09-28 22:19:35 +00:00
|
|
|
|
|
|
|
|
|
if(!CheckProcessingState())
|
|
|
|
|
return;
|
2019-09-28 19:21:58 +00:00
|
|
|
|
|
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
2019-09-28 22:19:35 +00:00
|
|
|
|
|
|
|
|
|
levelMeterArray_.Update();
|
2019-09-28 19:21:58 +00:00
|
|
|
|
foreach (var levelMeter in levelMeters_)
|
|
|
|
|
levelMeter.Render();
|
|
|
|
|
|
|
|
|
|
EditorGUILayout.EndHorizontal();
|
2019-09-28 22:19:35 +00:00
|
|
|
|
|
|
|
|
|
Repaint();
|
2019-09-28 18:17:39 +00:00
|
|
|
|
|
|
|
|
|
}
|
2019-09-28 22:19:35 +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
|
|
|
|
|
2019-09-28 22:19:35 +00:00
|
|
|
|
private bool CheckProcessingState()
|
2019-09-28 18:29:31 +00:00
|
|
|
|
{
|
2019-09-28 22:19:35 +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 22:19:35 +00:00
|
|
|
|
|
2019-09-28 18:17:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|