soundvision/UnityProject/Assets/Editor/PdSpectrumBindEditor.cs

103 lines
3.7 KiB
C#
Raw Normal View History

2019-09-29 10:00:16 +00:00
using UnityEngine;
using UnityEditor;
namespace cylvester
{
[CustomEditor(typeof(PdSpectrumBind))]
2019-10-31 09:48:04 +00:00
class PdSpectrumBindEditor : UnityEditor.Editor
2019-09-29 10:00:16 +00:00
{
2019-10-02 16:17:08 +00:00
private readonly string[] channels_ =
{
2019-09-29 10:00:16 +00:00
"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 IRectangularSelection rectangularSelection_;
2019-09-29 19:53:47 +00:00
private SerializedProperty selectionProperty_;
private SerializedProperty pdBackendProperty_;
2019-10-02 16:17:08 +00:00
private SerializedProperty energyChangedProperty_;
2019-10-04 13:35:23 +00:00
private SerializedProperty channelProperty_;
2019-10-02 16:17:08 +00:00
private Rect paintSpace_;
private ISpectrumGenerator spectrumGeneratorEditMode_;
2019-10-01 20:20:55 +00:00
2019-09-29 10:00:16 +00:00
public void OnEnable()
{
2019-10-04 13:35:23 +00:00
var behaviour = (PdSpectrumBind) target;
2019-09-29 19:53:47 +00:00
pdBackendProperty_ = serializedObject.FindProperty("pdBackend");
selectionProperty_ = serializedObject.FindProperty("selection");
2019-10-02 16:17:08 +00:00
energyChangedProperty_ = serializedObject.FindProperty("energyChanged");
2019-10-04 13:35:23 +00:00
channelProperty_ = serializedObject.FindProperty("channel");
2019-10-02 16:17:08 +00:00
rectangularSelection_ = new RectangularSelection(behaviour.TextureWidth, behaviour.TextureHeight);
spectrumGeneratorEditMode_ = new SpectrumGeneratorEditMode(behaviour.TextureWidth, behaviour.TextureHeight);
2019-09-29 10:00:16 +00:00
}
public override void OnInspectorGUI()
{
2019-10-04 13:35:23 +00:00
var behaviour = (PdSpectrumBind) target;
2019-09-29 19:53:47 +00:00
EditorGUILayout.PropertyField(pdBackendProperty_);
2019-09-29 10:00:16 +00:00
GUILayout.Label("PureData Inputs", EditorStyles.boldLabel);
2019-10-04 13:35:23 +00:00
channelProperty_.intValue = EditorGUILayout.Popup("Input Channel", channelProperty_.intValue, channels_);
2019-09-29 10:00:16 +00:00
2019-10-02 16:17:08 +00:00
GUILayout.Label("Callback", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(energyChangedProperty_);
2019-09-29 19:53:47 +00:00
2019-09-29 10:00:16 +00:00
GUILayout.Space(5);
GUILayout.Label("Spectrum Extractor", EditorStyles.boldLabel);
2019-10-02 16:17:08 +00:00
paintSpace_ = GUILayoutUtility.GetRect(behaviour.TextureWidth, behaviour.TextureWidth,
behaviour.TextureHeight, behaviour.TextureHeight);
2019-09-29 10:00:16 +00:00
2019-10-02 16:17:08 +00:00
UpdateSelection();
2019-09-29 10:00:16 +00:00
if (Event.current.type == EventType.Repaint)
{
2019-10-02 16:17:08 +00:00
// update selection
if (Application.isPlaying)
{
GUI.DrawTexture(paintSpace_, behaviour.Spectrum);
}
else
{
spectrumGeneratorEditMode_.Update(selectionProperty_.rectValue);
GUI.DrawTexture(paintSpace_, spectrumGeneratorEditMode_.Spectrum);
}
2019-09-29 10:00:16 +00:00
}
2019-10-02 16:17:08 +00:00
2019-09-29 10:00:16 +00:00
Repaint();
2019-10-02 16:17:08 +00:00
2019-10-01 20:20:55 +00:00
RenderExtractedEnergy(behaviour.Energy);
2019-10-02 16:17:08 +00:00
serializedObject.ApplyModifiedProperties();
2019-09-29 10:00:16 +00:00
}
2019-09-29 19:53:47 +00:00
2019-10-02 16:17:08 +00:00
private void UpdateSelection()
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;
}
2019-10-02 16:17:08 +00:00
2019-10-01 20:20:55 +00:00
case EventType.MouseDrag:
2019-09-29 19:53:47 +00:00
{
2019-10-02 16:17:08 +00:00
selectionProperty_.rectValue =
rectangularSelection_.Update(Event.current.mousePosition, ref paintSpace_);
2019-10-01 20:20:55 +00:00
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
}
2019-10-02 16:17:08 +00:00
}