soundvision/UnityProject/Assets/Editor/PdSpectrumBindEditor.cs

95 lines
3.3 KiB
C#
Raw Normal View History

2019-09-29 10:00:16 +00:00
using UnityEngine;
using UnityEditor;
namespace cylvester
{
[CustomEditor(typeof(PdSpectrumBind))]
class PdSpectrumBindEditor : Editor
{
private const int TextureWidth = 512;
private const int TextureHeight = 256;
private readonly string[] channels = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"
};
2019-09-29 19:53:47 +00:00
2019-09-29 10:00:16 +00:00
private ISpectrumGenerator spectrumGenerator_;
private IRectangularSelection rectangularSelection_;
private Rect paintSpace_;
2019-09-29 19:53:47 +00:00
private SerializedProperty selectionProperty_;
private SerializedProperty pdBackendProperty_;
2019-10-01 20:20:55 +00:00
2019-09-29 10:00:16 +00:00
public void OnEnable()
{
spectrumGenerator_ = new SpectrumGenerator(TextureWidth, TextureHeight);
2019-09-29 17:47:17 +00:00
rectangularSelection_ = new RectangularSelection(TextureWidth, TextureHeight);
2019-09-29 19:53:47 +00:00
pdBackendProperty_ = serializedObject.FindProperty("pdBackend");
selectionProperty_ = serializedObject.FindProperty("selection");
2019-09-29 10:00:16 +00:00
}
public override void OnInspectorGUI()
{
2019-09-29 19:53:47 +00:00
var behaviour = (IPdSpectrumBind)target;
EditorGUILayout.PropertyField(pdBackendProperty_);
2019-09-29 10:00:16 +00:00
GUILayout.Label("PureData Inputs", EditorStyles.boldLabel);
2019-09-29 19:53:47 +00:00
behaviour.Channel = EditorGUILayout.Popup("Input Channel", behaviour.Channel, channels);
2019-09-29 10:00:16 +00:00
RenderSpectrumExtractor(behaviour);
serializedObject.ApplyModifiedProperties();
2019-09-29 19:53:47 +00:00
}
private void RenderSpectrumExtractor(IPdSpectrumBind behaviour)
{
2019-10-01 20:20:55 +00:00
RenderSelection();
2019-09-29 10:00:16 +00:00
GUILayout.Space(5);
GUILayout.Label("Spectrum Extractor", EditorStyles.boldLabel);
var paintSpace = GUILayoutUtility.GetRect(TextureHeight, TextureWidth, TextureHeight, TextureHeight);
if (Event.current.type == EventType.Repaint)
{
paintSpace_ = paintSpace;
IPdArray spectrumArray = null;
if(Application.isPlaying)
spectrumArray = behaviour.GetPdArray(behaviour.Channel);
behaviour.Energy = spectrumGenerator_.Update(spectrumArray, selectionProperty_.rectValue);
2019-09-29 10:00:16 +00:00
GUI.DrawTexture(paintSpace_, spectrumGenerator_.Spectrum);
}
2019-09-29 17:47:17 +00:00
2019-09-29 10:00:16 +00:00
Repaint();
2019-10-01 20:20:55 +00:00
RenderExtractedEnergy(behaviour.Energy);
2019-09-29 10:00:16 +00:00
}
2019-09-29 19:53:47 +00:00
2019-10-01 20:20:55 +00:00
private void RenderSelection()
2019-09-29 19:53:47 +00:00
{
2019-10-01 20:20:55 +00:00
if (!Event.current.isMouse || Event.current.button != 0) return;
switch (Event.current.type)
2019-09-29 19:53:47 +00:00
{
2019-10-01 20:20:55 +00:00
case EventType.MouseDown:
{
rectangularSelection_.Start(Event.current.mousePosition);
break;
}
case EventType.MouseDrag:
2019-09-29 19:53:47 +00:00
{
2019-10-01 20:20:55 +00:00
selectionProperty_.rectValue = rectangularSelection_.Update(Event.current.mousePosition, ref paintSpace_);
break;
2019-09-29 19:53:47 +00:00
}
}
}
2019-10-01 20:20:55 +00:00
private void RenderExtractedEnergy(int energy)
{
GUILayout.BeginHorizontal();
GUILayout.Label("Extracted Energy", EditorStyles.boldLabel);
GUILayout.Label(energy.ToString());
GUILayout.EndHorizontal();
}
2019-09-29 10:00:16 +00:00
}
}