Skip to content

Commit

Permalink
#65 - Fix indent and ui state issues of drawer
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamRamberg committed Oct 20, 2019
1 parent a496c19 commit 0ad40a6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

_The release notes were introduced halfway through the work with version `2.0.0`. The list below might therefore not me 100% complete._

## 🐛 Bug fixes

- Fix indent and ui state issues of the `AtomDrawer<T>`

## 💥 Breaking changes

- The repo has been split up to 6 different packages: core, mobile, mono-hooks, scene-mgmt, tags and ui
Expand Down
70 changes: 47 additions & 23 deletions Packages/Core/Editor/Drawers/AtomDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if UNITY_2019_1_OR_NEWER
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

Expand All @@ -10,28 +11,47 @@ namespace UnityAtoms.Editor
/// <typeparam name="T">The type of Atom the property drawer should apply to.</typeparam>
public abstract class AtomDrawer<T> : PropertyDrawer where T : ScriptableObject
{
private bool _userClickedToCreateAtom = false;
private string _nameOfNewAtom = "";
private string _warningText = "";
class DrawerData
{
public bool UserClickedToCreateAtom = false;
public string NameOfNewAtom = "";
public string WarningText = "";
}

private Dictionary<string, DrawerData> _perPropertyViewData = new Dictionary<string, DrawerData>();

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var isCreatingSO = _userClickedToCreateAtom && property.objectReferenceValue == null;
if (!isCreatingSO || _warningText.Length <= 0) return base.GetPropertyHeight(property, label);
DrawerData drawerData = GetDrawerData(property.propertyPath);
var isCreatingSO = drawerData.UserClickedToCreateAtom && property.objectReferenceValue == null;
if (!isCreatingSO || drawerData.WarningText.Length <= 0) return base.GetPropertyHeight(property, label);
return base.GetPropertyHeight(property, label) * 2 + 4f;
}

private DrawerData GetDrawerData(string propertyPath)
{
DrawerData drawerData;
if (!_perPropertyViewData.TryGetValue(propertyPath, out drawerData))
{
drawerData = new DrawerData();
_perPropertyViewData[propertyPath] = drawerData;
}
return drawerData;
}

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var isCreatingSO = _userClickedToCreateAtom && property.objectReferenceValue == null;
var restWidth = _userClickedToCreateAtom ? 50 : 58;
var gutter = _userClickedToCreateAtom ? 2f : 6f;
EditorGUI.BeginProperty(position, label, property);

DrawerData drawerData = GetDrawerData(property.propertyPath);

var isCreatingSO = drawerData.UserClickedToCreateAtom && property.objectReferenceValue == null;
var restWidth = drawerData.UserClickedToCreateAtom ? 50 : 58;
var gutter = drawerData.UserClickedToCreateAtom ? 2f : 6f;
Rect restRect = new Rect();
Rect warningRect = new Rect();

EditorGUI.BeginProperty(position, label, property);

if (_warningText.Length > 0)
if (drawerData.WarningText.Length > 0)
{
position = IMGUIUtils.SnipRectV(position, EditorGUIUtility.singleLineHeight, out warningRect, 2f);
}
Expand All @@ -46,9 +66,12 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), isCreatingSO ? new GUIContent("Name of New Atom") : label);
GUI.color = defaultGUIColor;

var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;

if (isCreatingSO)
{
_nameOfNewAtom = EditorGUI.TextField(position, _nameOfNewAtom);
drawerData.NameOfNewAtom = EditorGUI.TextField(position, drawerData.NameOfNewAtom);
}
else
{
Expand All @@ -64,13 +87,13 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
Rect firstButtonRect = IMGUIUtils.SnipRectH(restRect, restRect.width - buttonWidth, out secondButtonRect, gutter);
if (GUI.Button(firstButtonRect, ""))
{
if (_nameOfNewAtom.Length > 0)
if (drawerData.NameOfNewAtom.Length > 0)
{
try
{
// Create asset
T so = ScriptableObject.CreateInstance<T>();
AssetDatabase.CreateAsset(so, "Assets/" + _nameOfNewAtom + ".asset");
AssetDatabase.CreateAsset(so, "Assets/" + drawerData.NameOfNewAtom + ".asset");
AssetDatabase.SaveAssets();
// Assign the newly created SO
property.objectReferenceValue = so;
Expand All @@ -80,35 +103,36 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
Debug.LogError("Not able to create Atom");
}

_userClickedToCreateAtom = false;
_warningText = "";
drawerData.UserClickedToCreateAtom = false;
drawerData.WarningText = "";
}
else
{
_warningText = "Name of new Atom must be specified!";
drawerData.WarningText = "Name of new Atom must be specified!";
}
}
if (GUI.Button(secondButtonRect, ""))
{
_userClickedToCreateAtom = false;
_warningText = "";
drawerData.UserClickedToCreateAtom = false;
drawerData.WarningText = "";
}

if (_warningText.Length > 0)
if (drawerData.WarningText.Length > 0)
{
EditorGUI.HelpBox(warningRect, _warningText, MessageType.Warning);
EditorGUI.HelpBox(warningRect, drawerData.WarningText, MessageType.Warning);
}
}
else
{
if (GUI.Button(restRect, "Create"))
{
_nameOfNewAtom = "";
_userClickedToCreateAtom = true;
drawerData.NameOfNewAtom = "";
drawerData.UserClickedToCreateAtom = true;
}
}
}

EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
}
Expand Down

0 comments on commit 0ad40a6

Please sign in to comment.