Skip to content

Commit 5637471

Browse files
authored
Merge pull request #52 from arimger/develop
Develop - 0.11.7
2 parents dd74c5b + 0bcd963 commit 5637471

22 files changed

+245
-40
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.11.7 [04.09.2022]
2+
3+
### Added:
4+
- Possibility to force default (built-in) behaviour for arrays/list
5+
6+
### Changed:
7+
- Improvements to EditorButton labels
8+
- Improvements to type fields while drawing [ReferencePicker]-based properties
9+
- Types returned by the TypeUtilities can now be sorted (additional property for the TypeConstraintContext class)
10+
111
## 0.11.5 [18.08.2022]
212

313
### Added:

Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedTypeDrawer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private void UpdateConstraint(TypeConstraintAttribute attribute)
5353
sharedConstraint.ApplyTarget(attribute.AssemblyType);
5454
if (sharedConstraint is TypeConstraintStandard constraint)
5555
{
56+
constraint.IsOrdered = attribute.OrderTypes;
5657
constraint.AllowAbstract = attribute.AllowAbstract;
5758
constraint.AllowObsolete = attribute.AllowObsolete;
5859
constraint.Settings = attribute.TypeSettings;

Assets/Editor Toolbox/Editor/Drawers/Toolbox/Decorator/EditorButtonAttributeDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected override void OnGuiCloseSafe(EditorButtonAttribute attribute)
8282
using (new EditorGUI.DisabledScope(disable))
8383
{
8484
var label = string.IsNullOrEmpty(attribute.ExtraLabel)
85-
? attribute.MethodName
85+
? ObjectNames.NicifyVariableName(attribute.MethodName)
8686
: attribute.ExtraLabel;
8787
var tooltip = attribute.Tooltip;
8888
var content = new GUIContent(label, tooltip);

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

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ namespace Toolbox.Editor.Drawers
1010

1111
public class ReferencePickerAttributeDrawer : ToolboxSelfPropertyDrawer<ReferencePickerAttribute>
1212
{
13+
private const float neededLabelWidth = 100.0f;
14+
private const float labelWidthOffset = -80.0f;
15+
1316
private static readonly TypeConstraintContext sharedConstraint = new TypeConstraintReference(null);
1417
private static readonly TypeAppearanceContext sharedAppearance = new TypeAppearanceContext(sharedConstraint, TypeGrouping.None, true);
1518
private static readonly TypeField typeField = new TypeField(sharedConstraint, sharedAppearance);
@@ -20,11 +23,27 @@ private void UpdateContexts(ReferencePickerAttribute attribute)
2023
sharedAppearance.TypeGrouping = attribute.TypeGrouping;
2124
}
2225

23-
private void CreateTypeProperty(SerializedProperty property, Type parentType)
26+
private Type GetParentType(SerializedProperty property, ReferencePickerAttribute attribute)
27+
{
28+
property.GetFieldInfo(out Type propertyType);
29+
var candidateType = attribute.ParentType;
30+
if (candidateType != null)
31+
{
32+
if (propertyType.IsAssignableFrom(candidateType))
33+
{
34+
return candidateType;
35+
}
36+
37+
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
38+
$"Provided {nameof(attribute.ParentType)} ({candidateType}) cannot be used because it's not assignable from: '{propertyType}'");
39+
}
40+
41+
return propertyType;
42+
}
43+
44+
private void CreateTypeProperty(Rect position, SerializedProperty property, Type parentType)
2445
{
2546
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
26-
var position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
27-
position = EditorGUI.IndentedRect(position);
2847
typeField.OnGui(position, true, (type) =>
2948
{
3049
try
@@ -61,39 +80,48 @@ private void UpdateTypeProperty(SerializedProperty property, Type referenceType)
6180
property.serializedObject.ApplyModifiedProperties();
6281
}
6382

64-
private Type GetParentType(SerializedProperty property, ReferencePickerAttribute attribute)
83+
private Rect PrepareTypePropertyPosition(in Rect labelPosition, in Rect inputPosition, bool isPropertyExpanded)
6584
{
66-
property.GetFieldInfo(out Type propertyType);
67-
var candidateType = attribute.ParentType;
68-
if (candidateType != null)
85+
var position = new Rect(inputPosition);
86+
var baseLabelWidth = EditorGUIUtility.labelWidth + labelWidthOffset;
87+
var realLabelWidth = labelPosition.width;
88+
var labelWidth = Mathf.Max(baseLabelWidth, realLabelWidth);
89+
if (isPropertyExpanded)
6990
{
70-
if (propertyType.IsAssignableFrom(candidateType))
91+
//property is expanded and we have place to move it to the next row
92+
if (labelWidth < neededLabelWidth)
7193
{
72-
return candidateType;
94+
position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
95+
position = EditorGUI.IndentedRect(position);
96+
return position;
7397
}
74-
75-
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
76-
$"Provided {nameof(attribute.ParentType)} ({candidateType}) cannot be used because it's not assignable from: '{propertyType}'");
7798
}
7899

79-
return propertyType;
100+
//adjust position to already rendered label
101+
position.xMin += labelWidth;
102+
return position;
80103
}
81104

105+
82106
protected override void OnGuiSafe(SerializedProperty property, GUIContent label, ReferencePickerAttribute attribute)
83107
{
84108
using (var propertyScope = new PropertyScope(property, label))
85109
{
86-
if (!propertyScope.IsVisible)
87-
{
88-
return;
89-
}
90-
91110
UpdateContexts(attribute);
92-
var parentType = GetParentType(property, attribute);
93111

112+
var isPropertyExpanded = propertyScope.IsVisible;
94113
EditorGUI.indentLevel++;
95-
CreateTypeProperty(property, parentType);
96-
ToolboxEditorGui.DrawPropertyChildren(property);
114+
var labelRect = propertyScope.LabelRect;
115+
var inputRect = propertyScope.InputRect;
116+
var position = PrepareTypePropertyPosition(in labelRect, in inputRect, isPropertyExpanded);
117+
118+
var parentType = GetParentType(property, attribute);
119+
CreateTypeProperty(position, property, parentType);
120+
if (isPropertyExpanded)
121+
{
122+
ToolboxEditorGui.DrawPropertyChildren(property);
123+
}
124+
97125
EditorGUI.indentLevel--;
98126
}
99127
}

Assets/Editor Toolbox/Editor/Internal/PropertyScope.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ internal class PropertyScope : IDisposable
1717
public PropertyScope(SerializedProperty property, GUIContent label)
1818
{
1919
this.property = property;
20-
ToolboxEditorGui.BeginProperty(property, ref label, out var labelRect);
21-
HandleEvents(labelRect);
22-
TryDrawLabel(labelRect, label);
20+
ToolboxEditorGui.BeginProperty(property, ref label, out var rect);
21+
HandleEvents(rect);
22+
TryDrawLabel(rect, label);
2323
}
2424

2525

@@ -33,8 +33,14 @@ private void HandleEvents(Rect rect)
3333

3434
private void TryDrawLabel(Rect rect, GUIContent label)
3535
{
36+
LabelRect = rect;
37+
InputRect = rect;
3638
if (property.hasChildren)
3739
{
40+
var size = EditorStyles.label.CalcSize(label);
41+
size.x = Mathf.Max(EditorGuiUtility.FoldoutOffset, size.x);
42+
rect.xMax = rect.xMin + size.x;
43+
LabelRect = rect;
3844
property.isExpanded = EditorGUI.Foldout(rect, property.isExpanded, label, true);
3945
}
4046
else
@@ -51,5 +57,7 @@ public void Dispose()
5157

5258

5359
public bool IsVisible => property.isExpanded;
60+
public Rect LabelRect { get; private set; }
61+
public Rect InputRect { get; private set; }
5462
}
55-
}
63+
}

Assets/Editor Toolbox/Editor/Internal/TypeConstraintContext.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,21 @@ public virtual void ApplyTarget(Type type)
3232

3333
public override bool Equals(object obj)
3434
{
35-
return obj is TypeConstraintContext constraint &&
36-
EqualityComparer<Type>.Default.Equals(targetType, constraint.targetType);
35+
return obj is TypeConstraintContext context &&
36+
EqualityComparer<Type>.Default.Equals(TargetType, context.TargetType) &&
37+
IsOrdered == context.IsOrdered;
3738
}
3839

3940
public override int GetHashCode()
4041
{
41-
return 1673078848 + EqualityComparer<Type>.Default.GetHashCode(targetType);
42+
var hashCode = -509589530;
43+
hashCode = hashCode * -1521134295 + EqualityComparer<Type>.Default.GetHashCode(TargetType);
44+
hashCode = hashCode * -1521134295 + IsOrdered.GetHashCode();
45+
return hashCode;
4246
}
4347

4448

4549
public Type TargetType => targetType;
50+
public bool IsOrdered { get; set; } = true;
4651
}
4752
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Toolbox.Editor
2+
{
3+
internal static class ToolboxDefines
4+
{
5+
internal const string defaultListsDefine = "TOOLBOX_FORCE_DEFAULT_LISTS";
6+
}
7+
}

Assets/Editor Toolbox/Editor/ToolboxDefines.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/ToolboxDrawerModule.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ namespace Toolbox.Editor
1010
{
1111
using Toolbox.Editor.Drawers;
1212

13+
//TODO:
14+
//1. dedicated class to initialize and hold drawer-related data
15+
//2. dedicated class used for settings initialization
16+
//3. separate logic for resettings active drawers
17+
1318
internal static class ToolboxDrawerModule
1419
{
1520
[InitializeOnLoadMethod]
@@ -244,6 +249,7 @@ internal static void UpdateDrawers(IToolboxInspectorSettings settings)
244249
//create all type-only-related drawers
245250
PrepareTargetTypeDrawers(settings);
246251

252+
HandleDefaultLists(settings.ForceDefaultLists);
247253
//log errors into console only once
248254
validationEnabled = false;
249255
}
@@ -268,6 +274,18 @@ internal static bool HasTargetTypeDrawer(Type type)
268274
return targetTypeDrawers.ContainsKey(type.IsGenericType ? type.GetGenericTypeDefinition() : type);
269275
}
270276

277+
internal static void HandleDefaultLists(bool value)
278+
{
279+
if (value)
280+
{
281+
ScriptingUtility.AppendDefine(ToolboxDefines.defaultListsDefine);
282+
}
283+
else
284+
{
285+
ScriptingUtility.RemoveDefine(ToolboxDefines.defaultListsDefine);
286+
}
287+
}
288+
271289
internal static ToolboxDecoratorDrawerBase GetDecoratorDrawer<T>(T attribute) where T : ToolboxDecoratorAttribute
272290
{
273291
return GetDecoratorDrawer(attribute.GetType());
@@ -412,7 +430,7 @@ internal static ToolboxPropertyHandler GetPropertyHandler(SerializedProperty pro
412430

413431
//TODO:
414432
//NOTE: unfortunately there is no valid, non-reflection way to check if property has a custom native drawer
415-
private readonly static MethodInfo getDrawerTypeForTypeMethod =
433+
private static readonly MethodInfo getDrawerTypeForTypeMethod =
416434
ReflectionUtility.GetEditorMethod("UnityEditor.ScriptAttributeUtility", "GetDrawerTypeForType",
417435
BindingFlags.NonPublic | BindingFlags.Static);
418436
}

Assets/Editor Toolbox/Editor/ToolboxEditorSettings.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ internal interface IToolboxInspectorSettings
4343
void SetAllPossibleTargetTypeDrawers();
4444

4545
bool UseToolboxDrawers { get; }
46+
bool ForceDefaultLists { get; }
4647

4748
List<SerializedType> DecoratorDrawerHandlers { get; }
4849
List<SerializedType> ConditionDrawerHandlers { get; }
@@ -81,8 +82,11 @@ internal class ToolboxEditorSettings : ScriptableObject, IToolboxGeneralSettings
8182
[SerializeField, ReorderableList(ListStyle.Boxed)]
8283
private List<FolderData> customFolders = new List<FolderData>();
8384

84-
[SerializeField]
85+
[SerializeField, Tooltip("Set to false if you don't want to use Toolbox attributes and related features.")]
8586
private bool useToolboxDrawers = true;
87+
[SerializeField, Tooltip("By default, Inspectors will use the built-in version of the list instead of the Toolbox-based one. " +
88+
"Keep in mind that built-in properties don't support Toolbox attributes. \n\n Changing this property will recompile the code.")]
89+
private bool forceDefaultLists;
8690

8791
[SerializeField, ReorderableList(ListStyle.Boxed), ClassExtends(typeof(ToolboxDecoratorDrawer<>))]
8892
private List<SerializedType> decoratorDrawerHandlers = new List<SerializedType>();
@@ -327,6 +331,12 @@ public bool UseToolboxDrawers
327331
set => useToolboxDrawers = value;
328332
}
329333

334+
public bool ForceDefaultLists
335+
{
336+
get => forceDefaultLists;
337+
set => forceDefaultLists = value;
338+
}
339+
330340
public List<SerializedType> DecoratorDrawerHandlers
331341
{
332342
get => decoratorDrawerHandlers;
@@ -357,7 +367,6 @@ public List<SerializedType> TargetTypeDrawerHandlers
357367
set => targetTypeDrawerHandlers = value;
358368
}
359369

360-
361370
private static class Defaults
362371
{
363372
internal const float largeFolderIconScaleDefault = 0.8f;

0 commit comments

Comments
 (0)