Skip to content

Commit 8eb47f9

Browse files
authored
Merge pull request #105 from arimger/develop
Develop - 0.12.10
2 parents 4a72fd0 + 8dd946d commit 8eb47f9

26 files changed

+250
-115
lines changed

Assets/Editor Toolbox/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 0.12.0 [28.03.2024]
2+
3+
### Changed:
4+
- Fix fetching and initializing Toolbox features when assets are updating
5+
- Display ReorderableList element handle at the top of available space
6+
- Display "Collection is Empty" label for empty ReorderableLists
7+
- New visual appearance for the SerializedDictionary properties
8+
- Fix using Toolbox drawers in the SerializedDictionary properties
9+
- Change the default SceneView selector key to `Tab`
10+
111
## 0.12.9 [27.01.2024]
212

313
### Changed:

Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorage.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,31 @@ namespace Toolbox.Editor.Drawers
77
/// Internal system responsible for keeping and clearing data between <see cref="UnityEditor.Editor"/>s.
88
/// This small system works only for attribute-based drawers and should be defined as a static field.
99
/// </summary>
10-
/// <typeparam name="T">Key-related object.</typeparam>
11-
/// <typeparam name="T1">Data to store.</typeparam>
12-
/// <typeparam name="T2">Any type needed for storage item creation. Pass <see cref="EventArgs.Empty"/> if no additional data is needed.</typeparam>
13-
public abstract class DrawerDataStorage<T, T1, T2> : DrawerDataStorageBase
10+
/// <typeparam name="TKey">Key-related object.</typeparam>
11+
/// <typeparam name="TData">Data to store.</typeparam>
12+
/// <typeparam name="TArgs">Any type needed for storage item creation. Pass <see cref="EventArgs.Empty"/> if no additional data is needed.</typeparam>
13+
public abstract class DrawerDataStorage<TKey, TData, TArgs> : DrawerDataStorageBase
1414
{
15-
protected readonly Dictionary<string, T1> items = new Dictionary<string, T1>();
15+
protected readonly Dictionary<string, TData> items = new Dictionary<string, TData>();
1616

17-
protected readonly Func<T, T2, T1> createMethod;
18-
protected readonly Action<T1> removeMethod;
17+
protected readonly Func<TKey, TArgs, TData> createMethod;
18+
protected readonly Action<TData> removeMethod;
1919

20-
21-
public DrawerDataStorage(Func<T, T2, T1> createMethod) : this(createMethod, null)
20+
public DrawerDataStorage(Func<TKey, TArgs, TData> createMethod) : this(createMethod, null)
2221
{ }
2322

24-
public DrawerDataStorage(Func<T, T2, T1> createMethod, Action<T1> removeMethod)
23+
public DrawerDataStorage(Func<TKey, TArgs, TData> createMethod, Action<TData> removeMethod)
2524
{
2625
this.createMethod = createMethod;
2726
this.removeMethod = removeMethod;
2827
}
2928

30-
31-
protected abstract string GetKey(T context);
32-
29+
protected abstract string GetKey(TKey context);
3330

3431
/// <summary>
3532
/// Returns and if needed creates new item.
3633
/// </summary>
37-
public virtual T1 ReturnItem(T context, T2 args)
34+
public virtual TData ReturnItem(TKey context, TArgs args)
3835
{
3936
var key = GetKey(context);
4037
if (items.TryGetValue(key, out var item))
@@ -47,12 +44,12 @@ public virtual T1 ReturnItem(T context, T2 args)
4744
}
4845
}
4946

50-
public virtual T1 CreateItem(T context, T2 args)
47+
public virtual TData CreateItem(TKey context, TArgs args)
5148
{
5249
return CreateItem(context, args, true);
5350
}
5451

55-
public virtual T1 CreateItem(T context, T2 args, bool append)
52+
public virtual TData CreateItem(TKey context, TArgs args, bool append)
5653
{
5754
var item = createMethod(context, args);
5855
if (append)
@@ -63,13 +60,13 @@ public virtual T1 CreateItem(T context, T2 args, bool append)
6360
return item;
6461
}
6562

66-
public virtual T1 AppendItem(T context, T1 item)
63+
public virtual TData AppendItem(TKey context, TData item)
6764
{
6865
var key = GetKey(context);
6966
return items[key] = item;
7067
}
7168

72-
public virtual void ClearItem(T context)
69+
public virtual void ClearItem(TKey context)
7370
{
7471
var key = GetKey(context);
7572
if (removeMethod != null)

Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorageBase.cs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
1-
using System.Collections.Generic;
2-
3-
namespace Toolbox.Editor.Drawers
1+
namespace Toolbox.Editor.Drawers
42
{
53
public abstract class DrawerDataStorageBase
64
{
7-
static DrawerDataStorageBase()
8-
{
9-
ToolboxEditorHandler.OnEditorReload += () =>
10-
{
11-
for (var i = 0; i < storages.Count; i++)
12-
{
13-
storages[i].ClearItems();
14-
}
15-
};
16-
}
17-
18-
195
protected DrawerDataStorageBase()
206
{
21-
storages.Add(this);
7+
DrawerStorageManager.AppendStorage(this);
228
}
239

2410
~DrawerDataStorageBase()
2511
{
26-
storages.Remove(this);
12+
DrawerStorageManager.RemoveStorage(this);
2713
}
2814

29-
30-
private static readonly List<DrawerDataStorageBase> storages = new List<DrawerDataStorageBase>();
31-
32-
3315
/// <summary>
3416
/// Called each time Editor is completely destroyed.
3517
/// </summary>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Toolbox.Editor.Drawers
5+
{
6+
internal static class DrawerStorageManager
7+
{
8+
static DrawerStorageManager()
9+
{
10+
ToolboxEditorHandler.OnEditorReload -= ClearStorages;
11+
ToolboxEditorHandler.OnEditorReload += ClearStorages;
12+
}
13+
14+
private static readonly List<DrawerDataStorageBase> storages = new List<DrawerDataStorageBase>();
15+
16+
internal static void ClearStorages()
17+
{
18+
ClearStorages(null);
19+
}
20+
21+
internal static void ClearStorages(Func<DrawerDataStorageBase, bool> predicate)
22+
{
23+
for (var i = 0; i < storages.Count; i++)
24+
{
25+
var storage = storages[i];
26+
if (predicate != null && !predicate(storage))
27+
{
28+
continue;
29+
}
30+
31+
storage.ClearItems();
32+
}
33+
}
34+
35+
internal static void AppendStorage(DrawerDataStorageBase storage)
36+
{
37+
storages.Add(storage);
38+
}
39+
40+
internal static bool RemoveStorage(DrawerDataStorageBase storage)
41+
{
42+
return storages.Remove(storage);
43+
}
44+
}
45+
}

Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerStorageManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertyList/ReorderableListAttributeDrawer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ static ReorderableListAttributeDrawer()
1111
{
1212
storage = new PropertyDataStorage<ReorderableListBase, ReorderableListAttribute>(false, (p, a) =>
1313
{
14-
return ToolboxEditorGui.CreateList(p,
14+
var list = ToolboxEditorGui.CreateList(p,
1515
a.ListStyle,
1616
a.ElementLabel,
1717
a.FixedSize,
1818
a.Draggable,
1919
a.HasHeader,
2020
a.HasLabels,
2121
a.Foldable);
22+
return list;
2223
});
2324
}
2425

2526
private static readonly PropertyDataStorage<ReorderableListBase, ReorderableListAttribute> storage;
2627

27-
2828
/// <summary>
2929
/// Draws a <see cref="ReorderableList"/> if given property was previously cached or creates completely new instance.
3030
/// </summary>

Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelf/ReferencePickerAttributeDrawer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private Type GetParentType(ReferencePickerAttribute attribute, SerializedPropert
4343
private void CreateTypeProperty(SerializedProperty property, Type parentType, ReferencePickerAttribute attribute, Rect position)
4444
{
4545
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
46-
typeField.OnGui(position, true, (type) =>
46+
typeField.OnGui(position, attribute.AddTextSearchField, (type) =>
4747
{
4848
try
4949
{
@@ -78,6 +78,11 @@ private void UpdateTypeProperty(SerializedProperty property, Type targetType, Re
7878
property.serializedObject.Update();
7979
property.managedReferenceValue = obj;
8080
property.serializedObject.ApplyModifiedProperties();
81+
82+
//NOTE: fix for invalid cached properties, e.g. changing parent's managed reference can change available children
83+
// since we cannot check if cached property is "valid" we need to clear the whole cache
84+
//TODO: reverse it and provide dedicated event when a managed property is changed through a dedicated handler
85+
DrawerStorageManager.ClearStorages();
8186
}
8287

8388
private Rect PrepareTypePropertyPosition(bool hasLabel, in Rect labelPosition, in Rect inputPosition, bool isPropertyExpanded)

Assets/Editor Toolbox/Editor/Drawers/Toolbox/TargetType/SerializedDictionaryDrawer.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,19 @@ static SerializedDictionaryDrawer()
4646
list.drawElementCallback += (rect, index, isActive, isFocused) =>
4747
{
4848
var element = pairsProperty.GetArrayElementAtIndex(index);
49-
var content = list.GetElementContent(element, index);
49+
var kProperty = element.FindPropertyRelative("key");
50+
var vProperty = element.FindPropertyRelative("value");
5051

51-
using (var propertyScope = new PropertyScope(element, content))
52+
var content = list.GetElementContent(element, index);
53+
using (new EditorGUILayout.HorizontalScope())
5254
{
53-
if (!propertyScope.IsVisible)
54-
{
55-
return;
56-
}
57-
58-
//draw key/value children and use new, shortened labels
59-
EditorGUI.indentLevel++;
60-
EditorGUILayout.PropertyField(element.FindPropertyRelative("key"), new GUIContent("K"));
61-
EditorGUILayout.PropertyField(element.FindPropertyRelative("value"), new GUIContent("V"));
62-
EditorGUI.indentLevel--;
55+
var kOption = GUILayout.Width(Style.kGroupWidth);
56+
DrawDictionaryProperty(kProperty, Style.kLabel, Style.kLabelWidth, kOption);
57+
58+
var vLabel = vProperty.hasVisibleChildren
59+
? Style.vLabel
60+
: GUIContent.none;
61+
DrawDictionaryProperty(vProperty, vLabel, Style.vLabelWidth);
6362
}
6463
};
6564
return list;
@@ -68,6 +67,19 @@ static SerializedDictionaryDrawer()
6867

6968
private static readonly PropertyDataStorage<ReorderableListBase, CreationArgs> storage;
7069

70+
private static void DrawDictionaryProperty(SerializedProperty property, GUIContent label, float labelWidth, params GUILayoutOption[] options)
71+
{
72+
using (new EditorGUILayout.VerticalScope(Style.propertyGroupStyle, options))
73+
{
74+
var indent = property.hasVisibleChildren ? 1 : 0;
75+
using (new ChangeIndentScope(indent))
76+
{
77+
EditorGUIUtility.labelWidth = labelWidth;
78+
ToolboxEditorGui.DrawToolboxProperty(property, label);
79+
EditorGUIUtility.labelWidth = -1;
80+
}
81+
}
82+
}
7183

7284
public override void OnGui(SerializedProperty property, GUIContent label)
7385
{
@@ -92,11 +104,19 @@ public override bool UseForChildren()
92104
return false;
93105
}
94106

95-
96107
private static class Style
97108
{
109+
internal static readonly float kLabelWidth = 40.0f;
110+
internal static readonly float kGroupWidth = 200.0f;
111+
internal static readonly float vLabelWidth = 150.0f;
112+
98113
internal static readonly float warningIconOffset = 25.0f;
99114
internal static readonly string warningMessage = "keys are not unique, it will break deserialization";
115+
116+
internal static readonly GUIContent kLabel = new GUIContent("Key");
117+
internal static readonly GUIContent vLabel = new GUIContent("Value");
118+
119+
internal static readonly GUIStyle propertyGroupStyle = new GUIStyle(EditorStyles.helpBox);
100120
}
101121

102122
private struct CreationArgs
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using UnityEditor;
3+
4+
namespace Toolbox.Editor.Internal
5+
{
6+
internal class ChangeIndentScope : IDisposable
7+
{
8+
private readonly int indentChange;
9+
10+
public ChangeIndentScope(int indentChange)
11+
{
12+
this.indentChange = indentChange;
13+
Prepare(indentChange);
14+
}
15+
16+
public void Prepare(int indentChange)
17+
{
18+
EditorGUI.indentLevel += indentChange;
19+
}
20+
21+
public void Dispose()
22+
{
23+
EditorGUI.indentLevel -= indentChange;
24+
}
25+
}
26+
}

Assets/Editor Toolbox/Editor/Internal/ChangeIndentScope.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)