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>
<Path>.idea</Path>
<Path>.vs</Path>
<Path>AudioSamples</Path>
<Path>Library</Path>
<Path>Temp</Path>
<Path>obj</Path>

View file

@ -16,6 +16,7 @@ namespace cylvester
private IRectangularSelection rectangularSelection_;
private Rect paintSpace_;
private SerializedProperty selectionProperty_;
private SerializedProperty pdBackendProperty_;
@ -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;
}
}

View file

@ -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;
}
}
}

View file

@ -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;
var fillPixel = magnitude > y;
var color = fillPixel ? Color.green : Color.black;
if (pdArray != null)
{
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)) &&
(mY - selectionRect.height < y && y < mY))
{
color.a = 1f;
if (fillPixel)
if (validPixel)
numPixels++;
}
else

View file

@ -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; }
}
}