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 pdBackendProperty_;
|
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
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_);
|
|
|
|
|
serializedObject.ApplyModifiedProperties();
|
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
|
|
|
|
|
2019-09-29 19:53:47 +00:00
|
|
|
|
if (Application.isPlaying && pdBackendProperty_ != null)
|
|
|
|
|
RenderSpectrumExtractor(behaviour);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RenderSpectrumExtractor(IPdSpectrumBind behaviour)
|
|
|
|
|
{
|
|
|
|
|
UpdateSelection(behaviour);
|
|
|
|
|
|
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;
|
2019-09-29 19:53:47 +00:00
|
|
|
|
var spectrumArray = behaviour.GetPdArray(behaviour.Channel);
|
|
|
|
|
|
|
|
|
|
behaviour.Energy = spectrumGenerator_.Update(spectrumArray.Data, ref behaviour.Selection);
|
2019-09-29 10:00:16 +00:00
|
|
|
|
GUI.DrawTexture(paintSpace_, spectrumGenerator_.Spectrum);
|
|
|
|
|
}
|
2019-09-29 17:47:17 +00:00
|
|
|
|
|
|
|
|
|
GUILayout.BeginHorizontal();
|
|
|
|
|
GUILayout.Label("Extracted Energy", EditorStyles.boldLabel);
|
|
|
|
|
GUILayout.Label(behaviour.Energy.ToString());
|
|
|
|
|
GUILayout.EndHorizontal();
|
2019-09-29 10:00:16 +00:00
|
|
|
|
|
|
|
|
|
Repaint();
|
|
|
|
|
}
|
2019-09-29 19:53:47 +00:00
|
|
|
|
|
|
|
|
|
private void UpdateSelection(IPdSpectrumBind behaviour)
|
|
|
|
|
{
|
|
|
|
|
if (Event.current.isMouse && Event.current.button == 0)
|
|
|
|
|
{
|
|
|
|
|
switch (Event.current.type)
|
|
|
|
|
{
|
|
|
|
|
case EventType.MouseDown:
|
|
|
|
|
{
|
|
|
|
|
rectangularSelection_.Start(Event.current.mousePosition);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case EventType.MouseDrag:
|
|
|
|
|
{
|
|
|
|
|
rectangularSelection_.Update(Event.current.mousePosition,
|
|
|
|
|
ref paintSpace_, ref behaviour.Selection);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-29 10:00:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|