From 757df84bdbf60a2ee631d43e9601ea047206765d Mon Sep 17 00:00:00 2001 From: Chikashi Miyama Date: Tue, 1 Oct 2019 19:30:17 +0200 Subject: [PATCH] retain rectangular selection in playmode --- .../.idea.UnityProject/.idea/indexLayout.xml | 1 + .../Assets/Editor/PdSpectrumBindEditor.cs | 27 ++++++++++--------- .../Assets/Editor/RectangularSelection.cs | 7 +++-- .../Assets/Editor/SpectrumGenerator.cs | 20 +++++++++----- .../Scripts/PdConnection/PdSpectrumBind.cs | 4 +-- 5 files changed, 35 insertions(+), 24 deletions(-) diff --git a/UnityProject/.idea/.idea.UnityProject/.idea/indexLayout.xml b/UnityProject/.idea/.idea.UnityProject/.idea/indexLayout.xml index f04c30d..02eb9d0 100644 --- a/UnityProject/.idea/.idea.UnityProject/.idea/indexLayout.xml +++ b/UnityProject/.idea/.idea.UnityProject/.idea/indexLayout.xml @@ -15,6 +15,7 @@ .idea .vs + AudioSamples Library Temp obj diff --git a/UnityProject/Assets/Editor/PdSpectrumBindEditor.cs b/UnityProject/Assets/Editor/PdSpectrumBindEditor.cs index fae4ccd..d00f96e 100644 --- a/UnityProject/Assets/Editor/PdSpectrumBindEditor.cs +++ b/UnityProject/Assets/Editor/PdSpectrumBindEditor.cs @@ -15,9 +15,10 @@ namespace cylvester private ISpectrumGenerator spectrumGenerator_; private IRectangularSelection rectangularSelection_; private Rect paintSpace_; - - private SerializedProperty pdBackendProperty_; + private SerializedProperty selectionProperty_; + private SerializedProperty pdBackendProperty_; + public void OnEnable() { @@ -25,21 +26,21 @@ namespace cylvester rectangularSelection_ = new RectangularSelection(TextureWidth, TextureHeight); pdBackendProperty_ = serializedObject.FindProperty("pdBackend"); - + selectionProperty_ = serializedObject.FindProperty("selection"); + } public override void OnInspectorGUI() { - var behaviour = (IPdSpectrumBind)target; EditorGUILayout.PropertyField(pdBackendProperty_); - serializedObject.ApplyModifiedProperties(); GUILayout.Label("PureData Inputs", EditorStyles.boldLabel); behaviour.Channel = EditorGUILayout.Popup("Input Channel", behaviour.Channel, channels); - if (Application.isPlaying && pdBackendProperty_ != null) - RenderSpectrumExtractor(behaviour); + RenderSpectrumExtractor(behaviour); + serializedObject.ApplyModifiedProperties(); + } private void RenderSpectrumExtractor(IPdSpectrumBind behaviour) @@ -53,9 +54,12 @@ namespace cylvester if (Event.current.type == EventType.Repaint) { paintSpace_ = paintSpace; - var spectrumArray = behaviour.GetPdArray(behaviour.Channel); - - behaviour.Energy = spectrumGenerator_.Update(spectrumArray.Data, ref behaviour.Selection); + + IPdArray spectrumArray = null; + if(Application.isPlaying) + spectrumArray = behaviour.GetPdArray(behaviour.Channel); + + behaviour.Energy = spectrumGenerator_.Update(spectrumArray, selectionProperty_.rectValue); GUI.DrawTexture(paintSpace_, spectrumGenerator_.Spectrum); } @@ -80,8 +84,7 @@ namespace cylvester } case EventType.MouseDrag: { - rectangularSelection_.Update(Event.current.mousePosition, - ref paintSpace_, ref behaviour.Selection); + selectionProperty_.rectValue = rectangularSelection_.Update(Event.current.mousePosition, ref paintSpace_); break; } } diff --git a/UnityProject/Assets/Editor/RectangularSelection.cs b/UnityProject/Assets/Editor/RectangularSelection.cs index 35b0bd4..4eb76be 100644 --- a/UnityProject/Assets/Editor/RectangularSelection.cs +++ b/UnityProject/Assets/Editor/RectangularSelection.cs @@ -5,7 +5,7 @@ namespace cylvester interface IRectangularSelection { void Start(Vector2 mousePosition); - void Update(Vector2 mousePosition, ref Rect paintSpace, ref Rect selectionRect); + Rect Update(Vector2 mousePosition, ref Rect paintSpace); } public class RectangularSelection : IRectangularSelection @@ -28,7 +28,7 @@ namespace cylvester selectedArea_.y = mousePosition.y; } - public void Update(Vector2 mousePosition, ref Rect paintSpace, ref Rect selectionRect) + public Rect Update(Vector2 mousePosition, ref Rect paintSpace) { selectedArea_.width = mousePosition.x - selectedArea_.x; selectedArea_.height = mousePosition.y - selectedArea_.y; @@ -37,10 +37,13 @@ namespace cylvester var width = selectedArea_.width / paintSpace.width; var height = selectedArea_.height / paintSpace.height; + var selectionRect = new Rect(); selectionRect.x = xPos * textureWidth_; selectionRect.y = yPos * textureHeight_; selectionRect.width = width * textureWidth_; selectionRect.height = height * textureHeight_; + + return selectionRect; } } } \ No newline at end of file diff --git a/UnityProject/Assets/Editor/SpectrumGenerator.cs b/UnityProject/Assets/Editor/SpectrumGenerator.cs index ca57c25..0478b2d 100644 --- a/UnityProject/Assets/Editor/SpectrumGenerator.cs +++ b/UnityProject/Assets/Editor/SpectrumGenerator.cs @@ -5,7 +5,7 @@ namespace cylvester interface ISpectrumGenerator { Texture2D Spectrum { get; } - int Update(float[] fftData, ref Rect selectionRect); + int Update(IPdArray pdArray, Rect selectionRect); } public class SpectrumGenerator : ISpectrumGenerator @@ -18,23 +18,29 @@ namespace cylvester texture_ = new Texture2D(width, height); } - public int Update(float[] fftData, ref Rect selectionRect) + public int Update(IPdArray pdArray, Rect selectionRect) { var numPixels = 0; for (var x = 0; x < texture_.width; x++) { - var magnitude = fftData[x] * 20f; for (var y = 0; y < texture_.height; y++) { - var mY = texture_.height - selectionRect.y; + var color = Color.black; + var validPixel = false; + + if (pdArray != null) + { + var magnitude = pdArray.Data[x] * 20f; + validPixel = magnitude > y; + color = validPixel ? Color.green : Color.black; + } - var fillPixel = magnitude > y; - var color = fillPixel ? Color.green : Color.black; + var mY = texture_.height - selectionRect.y; if ((selectionRect.x < x && x < (selectionRect.x + selectionRect.width)) && (mY - selectionRect.height < y && y < mY)) { color.a = 1f; - if (fillPixel) + if (validPixel) numPixels++; } else diff --git a/UnityProject/Assets/Scripts/PdConnection/PdSpectrumBind.cs b/UnityProject/Assets/Scripts/PdConnection/PdSpectrumBind.cs index 1175e60..7f3aa03 100644 --- a/UnityProject/Assets/Scripts/PdConnection/PdSpectrumBind.cs +++ b/UnityProject/Assets/Scripts/PdConnection/PdSpectrumBind.cs @@ -6,14 +6,13 @@ namespace cylvester { IPdArray GetPdArray(int index); int Channel { get; set; } - ref Rect Selection { get; } int Energy { get; set; } } public class PdSpectrumBind : MonoBehaviour, IPdSpectrumBind { [SerializeField] private PdBackend pdBackend; - private Rect selection_; + [SerializeField] private Rect selection; public IPdArray GetPdArray(int index) { @@ -21,7 +20,6 @@ namespace cylvester } public int Channel { get; set; } - public ref Rect Selection => ref selection_; public int Energy { get; set; } } } \ No newline at end of file