2018-10-30 20:05:06 +01:00
|
|
|
using UnityEditor;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace UnityAtoms
|
|
|
|
{
|
|
|
|
public abstract class ReferenceDrawer : PropertyDrawer
|
|
|
|
{
|
2019-04-08 16:14:50 +02:00
|
|
|
private static readonly string[] _popupOptions =
|
2018-10-30 20:05:06 +01:00
|
|
|
{ "Use Constant", "Use Variable" };
|
|
|
|
|
2019-04-08 16:14:50 +02:00
|
|
|
private static GUIStyle _popupStyle;
|
2018-10-30 20:05:06 +01:00
|
|
|
|
|
|
|
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
|
|
|
{
|
2019-04-08 16:14:50 +02:00
|
|
|
if (_popupStyle == null)
|
2018-10-30 20:05:06 +01:00
|
|
|
{
|
2019-04-08 16:14:50 +02:00
|
|
|
_popupStyle = new GUIStyle(GUI.skin.GetStyle("PaneOptions"));
|
|
|
|
_popupStyle.imagePosition = ImagePosition.ImageOnly;
|
2018-10-30 20:05:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
label = EditorGUI.BeginProperty(position, label, property);
|
|
|
|
position = EditorGUI.PrefixLabel(position, label);
|
|
|
|
|
|
|
|
EditorGUI.BeginChangeCheck();
|
|
|
|
|
|
|
|
// Get properties
|
|
|
|
SerializedProperty useConstant = property.FindPropertyRelative("UseConstant");
|
|
|
|
SerializedProperty constantValue = property.FindPropertyRelative("ConstantValue");
|
|
|
|
SerializedProperty variable = property.FindPropertyRelative("Variable");
|
|
|
|
|
|
|
|
// Calculate rect for configuration button
|
|
|
|
Rect buttonRect = new Rect(position);
|
2019-04-08 16:14:50 +02:00
|
|
|
buttonRect.yMin += _popupStyle.margin.top;
|
|
|
|
buttonRect.width = _popupStyle.fixedWidth + _popupStyle.margin.right;
|
2018-10-30 20:05:06 +01:00
|
|
|
position.xMin = buttonRect.xMax;
|
|
|
|
|
|
|
|
// Store old indent level and set it to 0, the PrefixLabel takes care of it
|
|
|
|
int indent = EditorGUI.indentLevel;
|
|
|
|
EditorGUI.indentLevel = 0;
|
|
|
|
|
2019-04-08 16:14:50 +02:00
|
|
|
int result = EditorGUI.Popup(buttonRect, useConstant.boolValue ? 0 : 1, _popupOptions, _popupStyle);
|
2018-10-30 20:05:06 +01:00
|
|
|
|
|
|
|
useConstant.boolValue = result == 0;
|
|
|
|
|
|
|
|
EditorGUI.PropertyField(position,
|
|
|
|
useConstant.boolValue ? constantValue : variable,
|
|
|
|
GUIContent.none);
|
|
|
|
|
|
|
|
if (EditorGUI.EndChangeCheck())
|
|
|
|
property.serializedObject.ApplyModifiedProperties();
|
|
|
|
|
|
|
|
EditorGUI.indentLevel = indent;
|
|
|
|
EditorGUI.EndProperty();
|
|
|
|
}
|
|
|
|
|
|
|
|
[CustomPropertyDrawer(typeof(BoolReference))]
|
2019-04-07 16:03:16 +02:00
|
|
|
public sealed class BoolReferenceDrawer : ReferenceDrawer { }
|
2018-10-30 20:05:06 +01:00
|
|
|
|
|
|
|
[CustomPropertyDrawer(typeof(ColorReference))]
|
2019-04-07 16:03:16 +02:00
|
|
|
public sealed class ColorReferenceDrawer : ReferenceDrawer { }
|
2018-10-30 20:05:06 +01:00
|
|
|
|
|
|
|
[CustomPropertyDrawer(typeof(FloatReference))]
|
2019-04-07 16:03:16 +02:00
|
|
|
public sealed class FloatReferenceDrawer : ReferenceDrawer { }
|
2018-10-30 20:05:06 +01:00
|
|
|
|
|
|
|
[CustomPropertyDrawer(typeof(IntReference))]
|
2019-04-07 16:03:16 +02:00
|
|
|
public sealed class IntReferenceDrawer : ReferenceDrawer { }
|
2018-10-30 20:05:06 +01:00
|
|
|
|
2019-03-17 23:43:20 +01:00
|
|
|
[CustomPropertyDrawer(typeof(StringReference))]
|
2019-04-07 16:03:16 +02:00
|
|
|
public sealed class StringReferenceDrawer : ReferenceDrawer { }
|
2019-03-17 23:43:20 +01:00
|
|
|
|
2018-10-30 20:05:06 +01:00
|
|
|
[CustomPropertyDrawer(typeof(Vector2Reference))]
|
2019-04-07 16:03:16 +02:00
|
|
|
public sealed class Vector2ReferenceDrawer : ReferenceDrawer { }
|
2018-10-30 20:05:06 +01:00
|
|
|
|
|
|
|
[CustomPropertyDrawer(typeof(Vector3Reference))]
|
2019-04-07 16:03:16 +02:00
|
|
|
public sealed class Vector3ReferenceDrawer : ReferenceDrawer { }
|
2018-10-30 20:05:06 +01:00
|
|
|
}
|
2019-04-07 11:10:09 +02:00
|
|
|
}
|