Database Improvements

Brought in all the myriad improvements to the database tools from the Stream Pixels project
Added configurable nested entry grouping
This commit is contained in:
Reinier Vens 2021-04-07 01:27:35 +01:00
commit 968783bf9c
37 changed files with 865 additions and 580 deletions

View file

@ -12,7 +12,7 @@ public class AnimationCurveFloat4Inspector : BaseInspector<float4[]>
var val = value != null && value.Length > 0
? value.ToCurve()
: new AnimationCurve();
using (var h = new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
val = CurveField(val, Color.yellow, new Rect(0, 0, 1, 1));
@ -22,6 +22,25 @@ public class AnimationCurveFloat4Inspector : BaseInspector<float4[]>
}
}
public class BezierCurveInspector : BaseInspector<BezierCurve>
{
public override BezierCurve Inspect(string label, BezierCurve value, object parent, DatabaseInspector inspectorWindow)
{
var val = value != null && value.Keys != null && value.Keys.Length > 0
? value.ToCurve()
: new AnimationCurve();
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
val = CurveField(val, Color.yellow, new Rect(0, 0, 1, 1));
}
value.Keys = val.keys.Select(k => float4(k.time, k.value, k.inTangent, k.outTangent)).ToArray();
return value;
}
}
public class AnimationCurveInspect : BaseInspector<AnimationCurve>
{
public override AnimationCurve Inspect(string label, AnimationCurve value, object parent, DatabaseInspector inspectorWindow)

View file

@ -8,32 +8,35 @@ public class DatabaseLinkInspector : BaseInspector<Guid, InspectableDatabaseLink
{
public override Guid Inspect(string label, Guid value, object parent, DatabaseInspector inspectorWindow, InspectableDatabaseLinkAttribute link)
{
LabelField(label, EditorStyles.boldLabel);
var valueEntry = DatabaseInspector.CultCache.Get(value);
using (var h = new HorizontalScope(GUI.skin.box))
using (new HorizontalScope())
{
GUILayout.Label((valueEntry as INamedEntry)?.EntryName ?? valueEntry?.ID.ToString() ?? $"Assign a {link.EntryType.Name} by dragging from the list panel.");
if (h.rect.Contains(Event.current.mousePosition) && (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform))
GUILayout.Label(label, GUILayout.Width(width));
var valueEntry = DatabaseInspector.CultCache.Get(value);
using (var h = new HorizontalScope(GUI.skin.box))
{
var guid = (Guid) DragAndDrop.GetGenericData("Item");
var dragEntry = DatabaseInspector.CultCache.Get(guid);
var dragValid = dragEntry != null && link.EntryType.IsInstanceOfType(dragEntry) && dragEntry != valueEntry;
if(Event.current.type == EventType.DragUpdated)
DragAndDrop.visualMode = dragValid ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Rejected;
else if(Event.current.type == EventType.DragPerform)
GUILayout.Label((valueEntry as INamedEntry)?.EntryName ?? valueEntry?.ID.ToString() ?? $"Drag and drop: {DatabaseListView.FormatTypeName(link.EntryType.Name)}");
if (h.rect.Contains(Event.current.mousePosition) && (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform))
{
if (dragValid)
var guid = (Guid) DragAndDrop.GetGenericData("Item");
var dragEntry = DatabaseInspector.CultCache.Get(guid);
var dragValid = dragEntry != null && link.EntryType.IsInstanceOfType(dragEntry) && dragEntry != valueEntry;
if(Event.current.type == EventType.DragUpdated)
DragAndDrop.visualMode = dragValid ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Rejected;
else if(Event.current.type == EventType.DragPerform)
{
DragAndDrop.AcceptDrag();
GUI.changed = true;
return guid;
if (dragValid)
{
DragAndDrop.AcceptDrag();
GUI.changed = true;
return guid;
}
}
}
}
if (GUILayout.Button("-", GUILayout.Width((EditorGUIUtility.singleLineHeight - 3)*2), GUILayout.Height(EditorGUIUtility.singleLineHeight-3)))
{
return Guid.Empty;
if (GUILayout.Button("-", GUILayout.Width((EditorGUIUtility.singleLineHeight - 3)*2), GUILayout.Height(EditorGUIUtility.singleLineHeight-3)))
{
return Guid.Empty;
}
}
}
@ -46,13 +49,19 @@ public class DatabaseLinkObjectInspector<T> : BaseInspector<DatabaseLink<T>> whe
public override DatabaseLink<T> Inspect(string label, DatabaseLink<T> value, object parent, DatabaseInspector inspectorWindow)
{
if (value == null)
value = new DatabaseLink<T>{Cache = DatabaseInspector.CultCache};
LabelField(label, EditorStyles.boldLabel);
var valueEntry = value.Value;
using (var h = new HorizontalScope(GUI.skin.box))
value = new DatabaseLink<T>();
if(!string.IsNullOrEmpty(label))
{
GUILayout.Label((valueEntry as INamedEntry)?.EntryName ?? valueEntry?.ID.ToString() ?? $"Assign a {typeof(T).Name} by dragging from the list panel.");
GUILayout.BeginHorizontal();
GUILayout.Label(label, GUILayout.Width(width));
}
var valueEntry = value.Value;
using (var h = string.IsNullOrEmpty(label) ? new HorizontalScope() : new HorizontalScope(GUI.skin.box))
{
GUILayout.Label((valueEntry as INamedEntry)?.EntryName ?? valueEntry?.ID.ToString() ?? $"Drag and drop: {DatabaseListView.FormatTypeName(typeof(T).Name)}");
if (h.rect.Contains(Event.current.mousePosition) && (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform))
{
var guid = (Guid) DragAndDrop.GetGenericData("Item");
@ -70,11 +79,21 @@ public class DatabaseLinkObjectInspector<T> : BaseInspector<DatabaseLink<T>> whe
}
}
}
if (GUILayout.Button("-", GUILayout.Width((EditorGUIUtility.singleLineHeight - 3)*2), GUILayout.Height(EditorGUIUtility.singleLineHeight-3)))
if(value.LinkID != Guid.Empty)
{
value.LinkID = Guid.Empty;
if (GUILayout.Button("-",
GUILayout.Width((EditorGUIUtility.singleLineHeight - 3) * 2),
GUILayout.Height(EditorGUIUtility.singleLineHeight - 3)))
{
value.LinkID = Guid.Empty;
}
}
}
if(!string.IsNullOrEmpty(label))
{
GUILayout.EndHorizontal();
}
return value;
}
@ -92,11 +111,9 @@ public class DatabaseLinkListInspector : BaseInspector<List<Guid>, InspectableDa
DatabaseInspector inspectorWindow,
InspectableDatabaseLinkAttribute link)
{
Space();
LabelField(label, EditorStyles.boldLabel);
using (var v = new VerticalScope(GUI.skin.box))
{
GUILayout.Label(label, EditorStyles.boldLabel);
if (value.Count == 0)
{
using (new HorizontalScope(ListItemStyle))

View file

@ -7,10 +7,6 @@ using static UnityEditor.EditorGUILayout;
public class FloatDictionaryInspector : BaseInspector<Dictionary<Guid, float>, InspectableDatabaseLinkAttribute>
{
private bool _listItemStyle;
public GUIStyle ListItemStyle =>
(_listItemStyle = !_listItemStyle) ? DatabaseListView.ListStyleEven : DatabaseListView.ListStyleOdd;
public override Dictionary<Guid, float> Inspect(string label,
Dictionary<Guid, float> value,
object parent,
@ -24,14 +20,14 @@ public class FloatDictionaryInspector : BaseInspector<Dictionary<Guid, float>, I
{
if (value.Count == 0)
{
using (new HorizontalScope(ListItemStyle))
using (new HorizontalScope(DatabaseInspector.ListItemStyle))
{
GUILayout.Label("Drag from list to add item");
}
}
foreach (var ingredient in value.ToArray())
{
using (new HorizontalScope(ListItemStyle))
using (new HorizontalScope(DatabaseInspector.ListItemStyle))
{
var entry = DatabaseInspector.CultCache.Get(ingredient.Key);
if (entry == null)
@ -79,10 +75,6 @@ public class FloatDictionaryInspector : BaseInspector<Dictionary<Guid, float>, I
public class RangedFloatDictionaryInspector : BaseInspector<Dictionary<Guid, float>, InspectableDatabaseLinkAttribute, RangedFloatAttribute>
{
private bool _listItemStyle;
public GUIStyle ListItemStyle =>
(_listItemStyle = !_listItemStyle) ? DatabaseListView.ListStyleEven : DatabaseListView.ListStyleOdd;
public override Dictionary<Guid, float> Inspect(string label,
Dictionary<Guid, float> value,
object parent,
@ -97,14 +89,14 @@ public class RangedFloatDictionaryInspector : BaseInspector<Dictionary<Guid, flo
{
if (value.Count == 0)
{
using (new HorizontalScope(ListItemStyle))
using (new HorizontalScope(DatabaseInspector.ListItemStyle))
{
GUILayout.Label("Drag from list to add item");
}
}
foreach (var ingredient in value.ToArray())
{
using (new HorizontalScope(ListItemStyle))
using (new HorizontalScope(DatabaseInspector.ListItemStyle))
{
var entry = DatabaseInspector.CultCache.Get(ingredient.Key);
if (entry == null)
@ -152,10 +144,6 @@ public class RangedFloatDictionaryInspector : BaseInspector<Dictionary<Guid, flo
public class IntDictionaryInspector : BaseInspector<Dictionary<Guid, int>, InspectableDatabaseLinkAttribute>
{
private bool _listItemStyle;
public GUIStyle ListItemStyle =>
(_listItemStyle = !_listItemStyle) ? DatabaseListView.ListStyleEven : DatabaseListView.ListStyleOdd;
public override Dictionary<Guid, int> Inspect(string label,
Dictionary<Guid, int> value,
object parent,
@ -169,14 +157,14 @@ public class IntDictionaryInspector : BaseInspector<Dictionary<Guid, int>, Inspe
{
if (value.Count == 0)
{
using (var h = new HorizontalScope(ListItemStyle))
using (var h = new HorizontalScope(DatabaseInspector.ListItemStyle))
{
GUILayout.Label("Drag from list to add item");
}
}
foreach (var ingredient in value.ToArray())
{
using (var h = new HorizontalScope(ListItemStyle))
using (var h = new HorizontalScope(DatabaseInspector.ListItemStyle))
{
var entry = DatabaseInspector.CultCache.Get(ingredient.Key);
if (entry == null)

View file

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using static Unity.Mathematics.math;
using static UnityEditor.EditorGUILayout;
public class EnumInspector<T> : BaseInspector<List<T>, InspectableEnumValuesAttribute>
{
public override List<T> Inspect(string label, List<T> list, object parent, DatabaseInspector inspectorWindow, InspectableEnumValuesAttribute attribute)
{
var names = Enum.GetNames(attribute.EnumType);
list ??= new List<T>();
while(list.Count < names.Length) list.Add(Activator.CreateInstance<T>());
while(list.Count > names.Length) list.RemoveAt(list.Count-1);
for (var i = 0; i < names.Length; i++)
inspectorWindow.InspectMember(names[i], list[i], parent, typeof(T));
return list;
}
}
public class EnumDictionaryInspector<E, T> : BaseInspector<EnumDictionary<E, T>> where E : Enum
{
private static HashSet<object> _foldouts = new HashSet<object>();
public override EnumDictionary<E, T> Inspect(string label, EnumDictionary<E, T> dict, object parent, DatabaseInspector inspectorWindow)
{
var names = Enum.GetNames(typeof(E));
if (dict == null || dict.Values.Length != names.Length)
{
var newValue = new T[names.Length];
if(dict != null && dict.Values.Length > 0)
Array.Copy(dict.Values, newValue, min(dict.Values.Length, newValue.Length));
dict.Values = newValue;
}
using (new VerticalScope(GUI.skin.box))
{
if (Foldout(_foldouts.Contains(dict), label, true, EditorStyles.boldLabel))
_foldouts.Add(dict);
else _foldouts.Remove(dict);
if(_foldouts.Contains(dict))
{
for (var i = 0; i < names.Length; i++)
{
using (new VerticalScope(DatabaseInspector.ListItemStyle))
{
dict.Values[i] = (T) inspectorWindow.InspectMember(names[i], dict.Values[i], parent, typeof(T));
}
}
}
}
return dict;
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: da1b15e5564747958639c8f959226c74
timeCreated: 1617335525

View file

@ -14,13 +14,13 @@ public class FloatInspector : BaseInspector<float>
}
}
public class RangedFloatInspector : BaseInspector<float, RangedFloatInspectableAttribute>
public class RangedFloatInspector : BaseInspector<float, InspectableRangedFloatAttribute>
{
public override float Inspect(string label,
float value,
object parent,
DatabaseInspector inspectorWindow,
RangedFloatInspectableAttribute attribute)
InspectableRangedFloatAttribute attribute)
{
using (new HorizontalScope())
{

View file

@ -14,9 +14,9 @@ public class IntInspector : BaseInspector<int>
}
}
public class RangedIntInspector : BaseInspector<int, RangedIntInspectableAttribute>
public class RangedIntInspector : BaseInspector<int, InspectableRangedIntAttribute>
{
public override int Inspect(string label, int value, object parent, DatabaseInspector inspectorWindow, RangedIntInspectableAttribute attribute)
public override int Inspect(string label, int value, object parent, DatabaseInspector inspectorWindow, InspectableRangedIntAttribute attribute)
{
using (new HorizontalScope())
{

View file

@ -9,46 +9,49 @@ using static UnityEditor.EditorGUILayout;
public class ListInspector<T> : BaseInspector<List<T>>
{
private bool _listItemStyle;
private static HashSet<int> _listItemFoldouts = new HashSet<int>();
public GUIStyle ListItemStyle =>
(_listItemStyle = !_listItemStyle) ? DatabaseListView.ListStyleEven : DatabaseListView.ListStyleOdd;
private static HashSet<object> _foldouts = new HashSet<object>();
public override List<T> Inspect(string label, List<T> list, object parent, DatabaseInspector inspectorWindow)
{
var listType = typeof(T);
if (listType.GetCustomAttribute<InspectableAttribute>() != null)
var inspectable = listType.GetCustomAttribute<InspectableAttribute>();
// if (listType.GetCustomAttribute<InspectableAttribute>() != null)
// {
//Space();
using (new VerticalScope(GUI.skin.box))
{
Space();
using (new VerticalScope(GUI.skin.box))
if (list == null)
{
if (list == null)
list = new List<T>();
}
else
{
var sorted = true;
var order = int.MinValue;
foreach(var element in list)
{
list = new List<T>();
if (element == null)
{
list.Remove(element);
return list;
}
var elementType = element.GetType();
var elementOrder = elementType.GetCustomAttribute<OrderAttribute>()?.Order ?? 0;
if (elementOrder < order)
sorted = false;
order = elementOrder;
}
else
if (!sorted)
{
var sorted = true;
var order = int.MinValue;
foreach(var element in list)
{
if (element == null)
{
list.Remove(element);
return list;
}
var elementType = element.GetType();
var elementOrder = elementType.GetCustomAttribute<OrderAttribute>()?.Order ?? 0;
if (elementOrder < order)
sorted = false;
order = elementOrder;
}
if (!sorted)
{
list.Sort((x, y) => (x.GetType().GetCustomAttribute<OrderAttribute>()?.Order ?? 0).CompareTo(y.GetType().GetCustomAttribute<OrderAttribute>()?.Order ?? 0));
}
list.Sort((x, y) => (x.GetType().GetCustomAttribute<OrderAttribute>()?.Order ?? 0).CompareTo(y.GetType().GetCustomAttribute<OrderAttribute>()?.Order ?? 0));
}
}
if (Foldout(_foldouts.Contains(list), label, true, EditorStyles.boldLabel))
_foldouts.Add(list);
else _foldouts.Remove(list);
if (_foldouts.Contains(list))
{
foreach (var o in list)
{
if (o == null)
@ -64,78 +67,65 @@ public class ListInspector<T> : BaseInspector<List<T>>
GUI.backgroundColor = tint.TintColor.ToColor();
}
using (new VerticalScope(ListItemStyle))
using (new VerticalScope(DatabaseInspector.ListItemStyle))
{
using (new HorizontalScope())
{
//if (listType.IsInterface)
if(Foldout(_listItemFoldouts.Contains(o.GetHashCode()), o.ToString(), true))
_listItemFoldouts.Add(o.GetHashCode());
else
_listItemFoldouts.Remove(o.GetHashCode());
if (inspectable != null)
{
if (Foldout(_foldouts.Contains(o), o.ToString(), true, EditorStyles.boldLabel))
_foldouts.Add(o);
else
_foldouts.Remove(o);
}
else inspectorWindow.InspectMember("", o, parent, listType, true);
// var rect = GetControlRect(false,
// GUILayout.Width(EditorGUIUtility.singleLineHeight));
// GUI.DrawTexture(rect, Icons.Instance.minus, ScaleMode.StretchToFill, true, 1,
// LabelColor, 0, 0);
if (GUILayout.Button("-", GUILayout.Width((EditorGUIUtility.singleLineHeight - 3)*2), GUILayout.Height(EditorGUIUtility.singleLineHeight-3)))
{
list.Remove(o);
break;
}
}
if(_listItemFoldouts.Contains(o.GetHashCode()))
{
Space();
inspectorWindow.Inspect(o, true);
}
if(inspectable != null && _foldouts.Contains(o))
inspectorWindow.InspectMember("", o, parent, o.GetType(), true);
}
if(_tinted)
GUI.backgroundColor = originalColor;
}
using (new HorizontalScope(ListItemStyle))
using (new HorizontalScope(DatabaseInspector.ListItemStyle))
{
GUILayout.Label($"Add new {listType.Name}", GUILayout.ExpandWidth(true));
GUILayout.Label($"Add new {listType.GetFullName()}", GUILayout.ExpandWidth(true));
// var rect = GetControlRect(false, GUILayout.Width(EditorGUIUtility.singleLineHeight));
// GUI.DrawTexture(rect, Icons.Instance.plus, ScaleMode.StretchToFill, true, 1, LabelColor, 0, 0);
if (GUILayout.Button("+", GUILayout.Width((EditorGUIUtility.singleLineHeight - 3)*2), GUILayout.Height(EditorGUIUtility.singleLineHeight-3)))
{
if (listType.IsInterface)
if (listType.IsAbstract || listType.IsInterface)
{
var menu = new GenericMenu();
foreach (var childType in listType.GetAllInterfaceClasses())
foreach (var childType in listType.GetAllChildClasses().Where(t=>!t.IsAbstract && !t.IsInterface))
{
menu.AddItem(new GUIContent(childType.Name), false, () =>
{
list.Add((T)Activator.CreateInstance(childType));
});
}
menu.ShowAsContext();
}
else if (listType.IsAbstract)
{
var menu = new GenericMenu();
foreach (var childType in listType.GetAllChildClasses().Where(t=>!t.IsAbstract))
{
menu.AddItem(new GUIContent(childType.Name), false, () =>
{
list.Add((T)Activator.CreateInstance(childType));
var o = (T) Activator.CreateInstance(childType);
_foldouts.Add(o);
list.Add(o);
});
}
menu.ShowAsContext();
}
else
{
list.Add(Activator.CreateInstance<T>());
var o = Activator.CreateInstance<T>();
_foldouts.Add(o);
list.Add(o);
}
}
}
}
}
else Debug.Log($"Field \"{label}\" is a list of non-Inspectable type {listType.Name}. No inspector was generated.");
// }
// else Debug.Log($"Field \"{label}\" is a list of non-Inspectable type {listType.Name}. No inspector was generated.");
return list;
}

View file

@ -3,12 +3,11 @@ using UnityEditor;
using UnityEngine;
using static UnityEditor.EditorGUILayout;
public class Float2Inspector : BaseInspector<float2>
{
public override float2 Inspect(string label, float2 value, object parent, DatabaseInspector inspectorWindow)
{
using (new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label("X", GUILayout.Width(labelWidth));
@ -24,7 +23,7 @@ public class Float3Inspector : BaseInspector<float3>
{
public override float3 Inspect(string label, float3 value, object parent, DatabaseInspector inspectorWindow)
{
using (new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label("X", GUILayout.Width(labelWidth));
@ -38,11 +37,23 @@ public class Float3Inspector : BaseInspector<float3>
}
}
public class Float3ColorInspector : BaseInspector<float3, InspectableColorAttribute>
{
public override float3 Inspect(string label, float3 value, object parent, DatabaseInspector inspectorWindow, InspectableColorAttribute attribute)
{
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
return ColorField(value.ToColor()).ToFloat3();
}
}
}
public class Float4Inspector : BaseInspector<float4>
{
public override float4 Inspect(string label, float4 value, object parent, DatabaseInspector inspectorWindow)
{
using (new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label("X", GUILayout.Width(labelWidth));
@ -58,11 +69,23 @@ public class Float4Inspector : BaseInspector<float4>
}
}
public class Float4ColorInspector : BaseInspector<float4, InspectableColorAttribute>
{
public override float4 Inspect(string label, float4 value, object parent, DatabaseInspector inspectorWindow, InspectableColorAttribute attribute)
{
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
return ColorField(value.ToColor()).ToFloat4();
}
}
}
public class Int2Inspector : BaseInspector<int2>
{
public override int2 Inspect(string label, int2 value, object parent, DatabaseInspector inspectorWindow)
{
using (new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label("X", GUILayout.Width(labelWidth));
@ -86,7 +109,7 @@ public class Int3Inspector : BaseInspector<int3>
{
public override int3 Inspect(string label, int3 value, object parent, DatabaseInspector inspectorWindow)
{
using (new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label("X", GUILayout.Width(labelWidth));
@ -104,7 +127,7 @@ public class Int4Inspector : BaseInspector<int4>
{
public override int4 Inspect(string label, int4 value, object parent, DatabaseInspector inspectorWindow)
{
using (new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label("X", GUILayout.Width(labelWidth));