2018-10-30 20:05:06 +01:00
using UnityEditor ;
using UnityEngine ;
2019-09-27 17:48:18 +02:00
namespace UnityAtoms.Editor
2018-10-30 20:05:06 +01:00
{
2019-10-15 19:19:44 +02:00
/// <summary>
/// A custom property drawer for References. Makes it possible to choose between a Variable and a constant value (not a Atom Contant, but a regular value).
/// </summary>
2019-04-17 13:05:23 +02:00
2019-09-25 21:05:06 +02:00
[CustomPropertyDrawer(typeof(AtomReference), true)]
public class AtomReferenceDrawer : PropertyDrawer
2018-10-30 20:05:06 +01:00
{
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 ( ) ;
}
2019-03-17 23:43:20 +01:00
2018-10-30 20:05:06 +01:00
}
2019-04-07 11:10:09 +02:00
}