Skip to content

Commit

Permalink
Added InjectLink class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Keijiro Takahashi authored and Keijiro Takahashi committed Aug 5, 2014
1 parent 2645484 commit 1657adf
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 20 deletions.
99 changes: 99 additions & 0 deletions Assets/Reaktion/Editor/Internal/InjectorLinkDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// Reaktion - An audio reactive animation toolkit for Unity.
//
// Copyright (C) 2013, 2014 Keijiro Takahashi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using UnityEngine;
using UnityEditor;
using System.Collections;

namespace Reaktion {

// Custom editor UI for InjectorLink.
[CustomPropertyDrawer(typeof(InjectorLink))]
class InjectorLinkDrawer : PropertyDrawer
{
// Labels and values for the mode selector.
static GUIContent[] modeLabels = {
new GUIContent("Not Bound"),
new GUIContent("Auto Bind"),
new GUIContent("Bind By Reference"),
new GUIContent("Bind By Name")
};
static int[] modeValues = { 0, 1, 2, 3 };

int GetRowCount(SerializedProperty property)
{
// Fully expand if it has multiple mode values.
var propMode = property.FindPropertyRelative("_mode");
if (propMode.hasMultipleDifferentValues) return 3;

// Expand when by-reference and by-name mode.
var mode = (InjectorLink.Mode)propMode.enumValueIndex;
if (mode == InjectorLink.Mode.ByReference ||
mode == InjectorLink.Mode.ByName) return 2;

// Just one line.
return 1;
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var rowCount = GetRowCount(property);
return EditorGUIUtility.singleLineHeight * rowCount +
EditorGUIUtility.standardVerticalSpacing * (rowCount - 1);
}

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);

position.height = EditorGUIUtility.singleLineHeight;

// First line: mode selector.
var propMode = property.FindPropertyRelative("_mode");
EditorGUI.IntPopup(position, propMode, modeLabels, modeValues, label);
position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;

// Indent the line.
position.width -= 16;
position.x += 16;
EditorGUIUtility.labelWidth -= 16;

// Reference box.
var mode = (InjectorLink.Mode)propMode.enumValueIndex;
if (propMode.hasMultipleDifferentValues || mode == InjectorLink.Mode.ByReference)
{
EditorGUI.PropertyField(position, property.FindPropertyRelative("_reference"));
position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
}

// Name box.
if (propMode.hasMultipleDifferentValues || mode == InjectorLink.Mode.ByName)
EditorGUI.PropertyField(position, property.FindPropertyRelative("_name"));

// Update the link when it gets updated.
if (GUI.changed) property.FindPropertyRelative("_forceUpdate").boolValue = true;

EditorGUI.EndProperty();
}
}

} // namespace Reaktion
8 changes: 8 additions & 0 deletions Assets/Reaktion/Editor/Internal/InjectorLinkDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions Assets/Reaktion/Editor/Reaktor/ReaktorEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ namespace Reaktion {
public class ReaktorEditor : Editor
{
// Audio input settings.
SerializedProperty propAutoBind;
SerializedProperty propInjector;
SerializedProperty propAudioCurve;

Expand Down Expand Up @@ -98,7 +97,6 @@ public class ReaktorEditor : Editor
void OnEnable()
{
// Audio input settings.
propAutoBind = serializedObject.FindProperty("autoBind");
propInjector = serializedObject.FindProperty("injector");
propAudioCurve = serializedObject.FindProperty("audioCurve");

Expand Down Expand Up @@ -155,10 +153,7 @@ public override void OnInspectorGUI()
serializedObject.Update();

// Audio input settings.
EditorGUILayout.PropertyField(propAutoBind);

if (propAutoBind.hasMultipleDifferentValues || !propAutoBind.boolValue)
EditorGUILayout.PropertyField(propInjector);
EditorGUILayout.PropertyField(propInjector);

EditorGUILayout.PropertyField(propAudioCurve, labelCurve);

Expand Down
118 changes: 118 additions & 0 deletions Assets/Reaktion/Internal/InjectorLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//
// Reaktion - An audio reactive animation toolkit for Unity.
//
// Copyright (C) 2013, 2014 Keijiro Takahashi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using UnityEngine;
using System.Collections;

namespace Reaktion {

// A class used to reference an Injector from Reaktors.
[System.Serializable]
public class InjectorLink
{
// Link mode.
public enum Mode { Null, Automatic, ByReference, ByName }

[SerializeField] Mode _mode = Mode.Automatic;

public Mode mode {
get { return _mode; }
set { _mode = value; Update(); }
}

// Link-by-reference mode information.
[SerializeField] InjectorBase _reference;

public InjectorBase reference {
get { return _reference; }
set { _reference = value; Update(); }
}

// Link-by-name mode information.
[SerializeField] string _name;

public string name {
get { return _name; }
set { _name = value; Update(); }
}

// "Update the link" flag (exposed only for Editor).
[SerializeField] bool _forceUpdate;

// Master script.
MonoBehaviour master;

// Linked Injector.
InjectorBase injector;

// Get a output dB level from the Injector.
public float DbLevel {
get {
if (_forceUpdate) Update();
return injector ? injector.DbLevel : -1e12f;
}
}

// Initialization (should be called from the master script).
public void Initialize(MonoBehaviour master)
{
this.master = master;
Update();
}

// Update the link.
public void Update()
{
injector = FindInjector();
_forceUpdate = false;
}

// Find the linked injector.
InjectorBase FindInjector()
{
if (_mode == Mode.Automatic && master)
{
var r = master.GetComponent<InjectorBase>();
if (r) return r;

r = master.GetComponentInParent<InjectorBase>();
if (r) return r;

r = master.GetComponentInChildren<InjectorBase>();
if (r) return r;

return Object.FindObjectOfType<InjectorBase>();
}

if (_mode == Mode.ByReference) return _reference;

if (_mode == Mode.ByName)
{
var go = GameObject.Find(_name);
if (go) return go.GetComponent<InjectorBase>();
}

return null;
}
}

} // namespace Reaktion
8 changes: 8 additions & 0 deletions Assets/Reaktion/Internal/InjectorLink.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 3 additions & 14 deletions Assets/Reaktion/Reaktor/Reaktor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public class Reaktor : MonoBehaviour
{
#region Audio input settings

public bool autoBind = true;
public InjectorBase injector;
public InjectorLink injector;
public AnimationCurve audioCurve = AnimationCurve.Linear(0, 0, 1, 1);

#endregion
Expand Down Expand Up @@ -132,17 +131,7 @@ public static int ActiveInstanceCount {

void Start()
{
// Search for an injector.
if (autoBind)
{
injector = GetComponentInChildren<InjectorBase>();
if (injector == null)
{
injector = GetComponentInParent<InjectorBase>();
if (injector == null)
injector = FindObjectOfType<InjectorBase>();
}
}
injector.Initialize(this);

// Begins with the lowest level.
peak = lowerBound + dynamicRange + headroom;
Expand All @@ -155,7 +144,7 @@ void Update()
float input = 0.0f;

// Audio input.
rawInput = injector ? injector.DbLevel : -1e12f;
rawInput = injector.DbLevel;

// Check the peak level.
peak -= Time.deltaTime * falldown;
Expand Down

0 comments on commit 1657adf

Please sign in to comment.