Skip to content

Commit

Permalink
Added Obj enum to customize class/struct fields
Browse files Browse the repository at this point in the history
  • Loading branch information
vexe committed Jul 23, 2015
1 parent d6470dd commit 455dcce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
27 changes: 24 additions & 3 deletions Assets/Plugins/Editor/Vexe/Drawers/API/Core/RecursiveDrawer.cs
Expand Up @@ -29,6 +29,7 @@ public class RecursiveDrawer : ObjectDrawer<object>
private Func<UnityObject[], UnityObject> _getDraggedObject;
private Func<Func<Type[]>, Action<Type>, string, Tab> _newTypeTab;
private Func<Func<UnityObject[]>, string, Tab> _newUnityTab;
private bool _disablePicker, _autoAlloc;

protected override void Initialize()
{
Expand Down Expand Up @@ -112,12 +113,29 @@ protected override void Initialize()
ArrayUtility.Add(ref systemTypes, memberType);

_tabs[idx] = _newTypeTab(() => systemTypes, TryCreateInstance, "System Type");

var display = attributes.GetAttribute<DisplayAttribute>();
if (display != null)
{
_autoAlloc = (display.ObjOpt & Obj.AutoAlloc) != 0;
_disablePicker = (display.ObjOpt & Obj.DisablePicker) != 0;
}
}

public override void OnGUI()
{
using (gui.Horizontal())
{
if (_autoAlloc && memberValue == null)
{
if (memberType.IsA<UnityObject>())
Debug.Log("Cannot automatically allocate memory for UnityObject member: " + member.NiceName);
else if (memberType.IsAbstract)
Debug.Log("Cannot automatically allocate memory for abstract member: " + member.NiceName);
else
memberValue = memberType.ActivatorInstance();
}

var isEmpty = string.IsNullOrEmpty(displayText);
var label = isEmpty ? string.Empty : displayText + " " + (foldout ? "^" : ">");
var value = member.Value;
Expand Down Expand Up @@ -172,7 +190,7 @@ public override void OnGUI()
{
var mb = unityObj as MonoBehaviour;
if (mb != null)
{
{
var monoscript = MonoScript.FromMonoBehaviour(mb);
var scriptPath = AssetDatabase.GetAssetPath(monoscript);
UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(scriptPath, 0);
Expand All @@ -195,12 +213,15 @@ public override void OnGUI()
gui.Cursor(thumbRect, MouseCursor.Link);

// Selection/thumb button
{
{
if (e.IsMouseContained(thumbRect) && e.IsMouseDown())
{
if (e.IsLMB())
{
SelectionWindow.Show("Select a `" + memberTypeName + "` object", _tabs);
if (_disablePicker)
memberValue = memberType.ActivatorInstance();
else
SelectionWindow.Show("Select a `" + memberTypeName + "` object", _tabs);
}
else if (e.IsRMB())
{
Expand Down
Expand Up @@ -56,12 +56,22 @@ public float Order
/// </summary>
public Dict DictOpt;

/// <summary>
/// Customizes the dispaly of class/struct fields
/// </summary>
public Obj ObjOpt;

public float? DisplayOrder;

public DisplayAttribute()
{
}

public DisplayAttribute(Obj objOpt)
{
this.ObjOpt = objOpt;
}

public DisplayAttribute(Seq seqOpt)
{
this.SeqOpt = seqOpt;
Expand All @@ -83,6 +93,20 @@ public DisplayAttribute(string formatLabel)
}
}

[Flags]
public enum Obj
{
/// <summary>
/// Automatically tries to allocate value for the member if it was null
/// </summary>
AutoAlloc = 1,

/// <summary>
/// Makes it not possible to show the selection window changing the object type
/// </summary>
DisablePicker = 1 << 1,
}

[Flags]
public enum Dict
{
Expand Down

0 comments on commit 455dcce

Please sign in to comment.