Skip to content

Commit

Permalink
rewrote ValueReferenceDrawer
Browse files Browse the repository at this point in the history
  • Loading branch information
jheiling committed Feb 19, 2018
1 parent 133d93f commit a5f9ef7
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 21 deletions.
26 changes: 26 additions & 0 deletions Code/Common/Editor/QuaternionValueReferenceDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using UnityEngine;
using UnityEditor;



namespace Signals.Common
{
[CustomPropertyDrawer(typeof(QuaternionValueReference))]
public class QuaternionValueReferenceDrawer : ValueReferenceDrawer
{
static Vector4 QuaternionToVector4(Quaternion quaternion)
{
return new Vector4(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
}

static Quaternion Vector4ToQuaternion(Vector4 vector)
{
return new Quaternion(vector.x, vector.y, vector.z, vector.w);
}

protected override void LocalValueField(Rect position, SerializedProperty localValue)
{
localValue.quaternionValue = Vector4ToQuaternion(EditorGUI.Vector4Field(position, GUIContent.none, QuaternionToVector4(localValue.quaternionValue)));
}
}
}
13 changes: 13 additions & 0 deletions Code/Common/Editor/QuaternionValueReferenceDrawer.cs.meta

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

9 changes: 9 additions & 0 deletions Code/Common/Editor/Vector2IntValueReferenceDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using UnityEditor;



namespace Signals.Common
{
[CustomPropertyDrawer(typeof(Vector2IntValueReference))]
public class Vector2IntValueReferenceDrawer : ValueReferenceDrawer { }
}
13 changes: 13 additions & 0 deletions Code/Common/Editor/Vector2IntValueReferenceDrawer.cs.meta

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

9 changes: 9 additions & 0 deletions Code/Common/Editor/Vector2ValueReferenceDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using UnityEditor;



namespace Signals.Common
{
[CustomPropertyDrawer(typeof(Vector2ValueReference))]
public class Vector2ValueReferenceDrawer : ValueReferenceDrawer { }
}
13 changes: 13 additions & 0 deletions Code/Common/Editor/Vector2ValueReferenceDrawer.cs.meta

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

9 changes: 9 additions & 0 deletions Code/Common/Editor/Vector3IntValueReferenceDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using UnityEditor;



namespace Signals.Common
{
[CustomPropertyDrawer(typeof(Vector3IntValueReference))]
public class Vector3IntValueReferenceDrawer : ValueReferenceDrawer { }
}
13 changes: 13 additions & 0 deletions Code/Common/Editor/Vector3IntValueReferenceDrawer.cs.meta

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

9 changes: 9 additions & 0 deletions Code/Common/Editor/Vector3ValueReferenceDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using UnityEditor;



namespace Signals.Common
{
[CustomPropertyDrawer(typeof(Vector3ValueReference))]
public class Vector3ValueReferenceDrawer : ValueReferenceDrawer { }
}
13 changes: 13 additions & 0 deletions Code/Common/Editor/Vector3ValueReferenceDrawer.cs.meta

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

16 changes: 16 additions & 0 deletions Code/Common/Editor/Vector4ValueReferenceDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using UnityEngine;
using UnityEditor;



namespace Signals.Common
{
[CustomPropertyDrawer(typeof(Vector4ValueReference))]
public class Vector4ValueReferenceDrawer : ValueReferenceDrawer
{
protected override void LocalValueField(Rect position, SerializedProperty localValue)
{
localValue.vector4Value = EditorGUI.Vector4Field(position, GUIContent.none, localValue.vector4Value);
}
}
}
13 changes: 13 additions & 0 deletions Code/Common/Editor/Vector4ValueReferenceDrawer.cs.meta

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

55 changes: 34 additions & 21 deletions Code/Editor/ValueReferenceDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,55 @@ namespace Signals
/// </summary>
public abstract class ValueReferenceDrawer : PropertyDrawer
{
readonly static string[] popupOptions = { "Use Signal Value", "Use Local Value" };
static GUIStyle popupStyle;
readonly static string[] _popupOptions = { "Use Signal Value", "Use Local Value" };

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
readonly static GUIStyle _popupStyle = new GUIStyle(GUI.skin.GetStyle("PaneOptions"))
{
if (popupStyle == null)
{
popupStyle = new GUIStyle(GUI.skin.GetStyle("PaneOptions"));
popupStyle.imagePosition = ImagePosition.ImageOnly;
}
imagePosition = ImagePosition.ImageOnly
};

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

EditorGUI.BeginChangeCheck();

var popupPosition = new Rect(position.x, position.y + _popupStyle.margin.top, _popupStyle.fixedWidth + _popupStyle.margin.right, position.height);
var useLocalValue = property.FindPropertyRelative("_useLocalValue");
var signal = property.FindPropertyRelative("_signal");
var localValue = property.FindPropertyRelative("_localValue");
useLocalValue.boolValue = EditorGUI.Popup(popupPosition, useLocalValue.boolValue ? 1 : 0, _popupOptions, _popupStyle) == 1;

var buttonRect = new Rect(position);
buttonRect.yMin += popupStyle.margin.top;
buttonRect.width = popupStyle.fixedWidth + popupStyle.margin.right;
position.xMin = buttonRect.xMax;
position.xMin = popupPosition.xMax;
if (useLocalValue.boolValue) LocalValueField(position, property.FindPropertyRelative("_localValue"));
else EditorGUI.PropertyField(position, property.FindPropertyRelative("_signal"), GUIContent.none);

var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
if (EditorGUI.EndChangeCheck()) property.serializedObject.ApplyModifiedProperties();

useLocalValue.boolValue = EditorGUI.Popup(buttonRect, useLocalValue.boolValue ? 1 : 0, popupOptions, popupStyle) == 1;
EditorGUI.EndProperty();
}

EditorGUI.PropertyField(position, useLocalValue.boolValue ? localValue : signal, GUIContent.none);
/// <summary>
/// Override this method to implement a custom inspector for the <see cref="ValueReference.LocalValue"/>.
/// </summary>
/// <param name="position">The field's position.</param>
/// <param name="localValue">The SerializedProperty containing the value.</param>
protected virtual void LocalValueField(Rect position, SerializedProperty localValue)
{
EditorGUI.PropertyField(position, localValue, GUIContent.none, true);
}

if (EditorGUI.EndChangeCheck()) property.serializedObject.ApplyModifiedProperties();
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return property.FindPropertyRelative("_useLocalValue").boolValue ? GetLocalValueFieldHeight() : EditorGUIUtility.singleLineHeight;
}

EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
/// <summary>
/// Override this if your <see cref="LocalValueField"/> is higher than one line.
/// </summary>
/// <returns>The <see cref="LocalValueField"/>'s height.</returns>
protected virtual float GetLocalValueFieldHeight()
{
return EditorGUIUtility.singleLineHeight;
}
}
}

0 comments on commit a5f9ef7

Please sign in to comment.