retain rectangular selection in playmode

This commit is contained in:
Chikashi Miyama 2019-10-01 19:30:17 +02:00
parent cee612af5b
commit 757df84bdb
5 changed files with 35 additions and 24 deletions

View file

@ -15,6 +15,7 @@
<explicitExcludes> <explicitExcludes>
<Path>.idea</Path> <Path>.idea</Path>
<Path>.vs</Path> <Path>.vs</Path>
<Path>AudioSamples</Path>
<Path>Library</Path> <Path>Library</Path>
<Path>Temp</Path> <Path>Temp</Path>
<Path>obj</Path> <Path>obj</Path>

View file

@ -16,6 +16,7 @@ namespace cylvester
private IRectangularSelection rectangularSelection_; private IRectangularSelection rectangularSelection_;
private Rect paintSpace_; private Rect paintSpace_;
private SerializedProperty selectionProperty_;
private SerializedProperty pdBackendProperty_; private SerializedProperty pdBackendProperty_;
@ -25,21 +26,21 @@ namespace cylvester
rectangularSelection_ = new RectangularSelection(TextureWidth, TextureHeight); rectangularSelection_ = new RectangularSelection(TextureWidth, TextureHeight);
pdBackendProperty_ = serializedObject.FindProperty("pdBackend"); pdBackendProperty_ = serializedObject.FindProperty("pdBackend");
selectionProperty_ = serializedObject.FindProperty("selection");
} }
public override void OnInspectorGUI() public override void OnInspectorGUI()
{ {
var behaviour = (IPdSpectrumBind)target; var behaviour = (IPdSpectrumBind)target;
EditorGUILayout.PropertyField(pdBackendProperty_); EditorGUILayout.PropertyField(pdBackendProperty_);
serializedObject.ApplyModifiedProperties();
GUILayout.Label("PureData Inputs", EditorStyles.boldLabel); GUILayout.Label("PureData Inputs", EditorStyles.boldLabel);
behaviour.Channel = EditorGUILayout.Popup("Input Channel", behaviour.Channel, channels); 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) private void RenderSpectrumExtractor(IPdSpectrumBind behaviour)
@ -53,9 +54,12 @@ namespace cylvester
if (Event.current.type == EventType.Repaint) if (Event.current.type == EventType.Repaint)
{ {
paintSpace_ = paintSpace; 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); GUI.DrawTexture(paintSpace_, spectrumGenerator_.Spectrum);
} }
@ -80,8 +84,7 @@ namespace cylvester
} }
case EventType.MouseDrag: case EventType.MouseDrag:
{ {
rectangularSelection_.Update(Event.current.mousePosition, selectionProperty_.rectValue = rectangularSelection_.Update(Event.current.mousePosition, ref paintSpace_);
ref paintSpace_, ref behaviour.Selection);
break; break;
} }
} }

View file

@ -5,7 +5,7 @@ namespace cylvester
interface IRectangularSelection interface IRectangularSelection
{ {
void Start(Vector2 mousePosition); 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 public class RectangularSelection : IRectangularSelection
@ -28,7 +28,7 @@ namespace cylvester
selectedArea_.y = mousePosition.y; 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_.width = mousePosition.x - selectedArea_.x;
selectedArea_.height = mousePosition.y - selectedArea_.y; selectedArea_.height = mousePosition.y - selectedArea_.y;
@ -37,10 +37,13 @@ namespace cylvester
var width = selectedArea_.width / paintSpace.width; var width = selectedArea_.width / paintSpace.width;
var height = selectedArea_.height / paintSpace.height; var height = selectedArea_.height / paintSpace.height;
var selectionRect = new Rect();
selectionRect.x = xPos * textureWidth_; selectionRect.x = xPos * textureWidth_;
selectionRect.y = yPos * textureHeight_; selectionRect.y = yPos * textureHeight_;
selectionRect.width = width * textureWidth_; selectionRect.width = width * textureWidth_;
selectionRect.height = height * textureHeight_; selectionRect.height = height * textureHeight_;
return selectionRect;
} }
} }
} }

View file

@ -5,7 +5,7 @@ namespace cylvester
interface ISpectrumGenerator interface ISpectrumGenerator
{ {
Texture2D Spectrum { get; } Texture2D Spectrum { get; }
int Update(float[] fftData, ref Rect selectionRect); int Update(IPdArray pdArray, Rect selectionRect);
} }
public class SpectrumGenerator : ISpectrumGenerator public class SpectrumGenerator : ISpectrumGenerator
@ -18,23 +18,29 @@ namespace cylvester
texture_ = new Texture2D(width, height); texture_ = new Texture2D(width, height);
} }
public int Update(float[] fftData, ref Rect selectionRect) public int Update(IPdArray pdArray, Rect selectionRect)
{ {
var numPixels = 0; var numPixels = 0;
for (var x = 0; x < texture_.width; x++) for (var x = 0; x < texture_.width; x++)
{ {
var magnitude = fftData[x] * 20f;
for (var y = 0; y < texture_.height; y++) for (var y = 0; y < texture_.height; y++)
{ {
var mY = texture_.height - selectionRect.y; var color = Color.black;
var validPixel = false;
var fillPixel = magnitude > y; if (pdArray != null)
var color = fillPixel ? Color.green : Color.black; {
var magnitude = pdArray.Data[x] * 20f;
validPixel = magnitude > y;
color = validPixel ? Color.green : Color.black;
}
var mY = texture_.height - selectionRect.y;
if ((selectionRect.x < x && x < (selectionRect.x + selectionRect.width)) && if ((selectionRect.x < x && x < (selectionRect.x + selectionRect.width)) &&
(mY - selectionRect.height < y && y < mY)) (mY - selectionRect.height < y && y < mY))
{ {
color.a = 1f; color.a = 1f;
if (fillPixel) if (validPixel)
numPixels++; numPixels++;
} }
else else

View file

@ -6,14 +6,13 @@ namespace cylvester
{ {
IPdArray GetPdArray(int index); IPdArray GetPdArray(int index);
int Channel { get; set; } int Channel { get; set; }
ref Rect Selection { get; }
int Energy { get; set; } int Energy { get; set; }
} }
public class PdSpectrumBind : MonoBehaviour, IPdSpectrumBind public class PdSpectrumBind : MonoBehaviour, IPdSpectrumBind
{ {
[SerializeField] private PdBackend pdBackend; [SerializeField] private PdBackend pdBackend;
private Rect selection_; [SerializeField] private Rect selection;
public IPdArray GetPdArray(int index) public IPdArray GetPdArray(int index)
{ {
@ -21,7 +20,6 @@ namespace cylvester
} }
public int Channel { get; set; } public int Channel { get; set; }
public ref Rect Selection => ref selection_;
public int Energy { get; set; } public int Energy { get; set; }
} }
} }