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 =
2019-10-16 18:02:08 +02:00
{ "Use Value" , "Use Constant" , "Use Variable" } ;
2019-04-08 16:14:50 +02:00
private static GUIStyle _popupStyle ;
2018-10-30 20:05:06 +01:00
2019-11-12 00:03:12 +01:00
public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
{
SerializedProperty _usage = property . FindPropertyRelative ( "_usage" ) ;
SerializedProperty _value = property . FindPropertyRelative ( "_value" ) ;
SerializedProperty _constant = property . FindPropertyRelative ( "_constant" ) ;
SerializedProperty _variable = property . FindPropertyRelative ( "_variable" ) ;
var usage = ( AtomReference . Usage ) _usage . intValue ;
var valueToUse = usage = = AtomReference . Usage . Value ? _value : usage = = AtomReference . Usage . Constant ? _constant : _variable ;
return EditorGUI . GetPropertyHeight ( valueToUse , label ) ;
}
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
2019-10-16 18:02:08 +02:00
SerializedProperty _usage = property . FindPropertyRelative ( "_usage" ) ;
SerializedProperty _value = property . FindPropertyRelative ( "_value" ) ;
SerializedProperty _constant = property . FindPropertyRelative ( "_constant" ) ;
SerializedProperty _variable = property . FindPropertyRelative ( "_variable" ) ;
2018-10-30 20:05:06 +01:00
// 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-10-16 18:02:08 +02:00
_usage . intValue = EditorGUI . Popup ( buttonRect , _usage . intValue , _popupOptions , _popupStyle ) ;
var usage = ( AtomReference . Usage ) _usage . intValue ;
2018-10-30 20:05:06 +01:00
2019-10-16 18:02:08 +02:00
var valueToUse = usage = = AtomReference . Usage . Value ? _value : usage = = AtomReference . Usage . Constant ? _constant : _variable ;
2018-10-30 20:05:06 +01:00
EditorGUI . PropertyField ( position ,
2019-10-16 18:02:08 +02:00
valueToUse ,
2018-10-30 20:05:06 +01:00
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
}