Skip to content

Commit 4cd14d0

Browse files
authored
Merge pull request #46 from arimger/develop
Develop - 0.11.3
2 parents 1076212 + 7180aa5 commit 4cd14d0

28 files changed

+288
-61
lines changed

.editorconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ csharp_preferred_modifier_order = private,protected,internal,public,static,exter
8282
csharp_prefer_braces = true:silent
8383
csharp_style_deconstructed_variable_declaration = true:suggestion
8484
csharp_prefer_simple_default_expression = true:suggestion
85-
csharp_style_pattern_local_over_anonymous_function = true:suggestion
86-
csharp_style_inlined_variable_declaration = true:suggestion
85+
csharp_style_pattern_local_over_anonymous_function = true:silent
86+
csharp_style_inlined_variable_declaration = true:silent
8787
###############################
8888
# C# Formatting Rules #
8989
###############################

Assets/Editor Toolbox/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 0.11.3 [05.06.2022]
2+
3+
### Added:
4+
- 'ApplyCondition' property to decorators (possibility to apply property condition state (disabled/hidden) to associated decorators
5+
- Begin/EndVerticalAttribute
6+
- Optional labels property ('OptionHandle') for the PresetAttribute
7+
8+
### Changed:
9+
- Fix overriding labels in ReorderableLists
10+
- Fix drawing MinMaxSlider in indentations
11+
112
## 0.11.1 [08.05.2022]
213

314
### Added:

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace Toolbox.Editor.Drawers
55
{
6+
using Toolbox.Editor.Internal;
7+
68
[CustomPropertyDrawer(typeof(MinMaxSliderAttribute))]
79
public class MinMaxSliderAttributeDrawer : PropertyDrawerBase
810
{
@@ -20,7 +22,11 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
2022

2123
label = EditorGUI.BeginProperty(position, label, property);
2224
EditorGUI.BeginChangeCheck();
23-
ToolboxEditorGui.DrawMinMaxSlider(position, label, ref xValue, ref yValue, minValue, maxValue);
25+
using (new ZeroIndentScope())
26+
{
27+
ToolboxEditorGui.DrawMinMaxSlider(position, label, ref xValue, ref yValue, minValue, maxValue);
28+
}
29+
2430
if (EditorGUI.EndChangeCheck())
2531
{
2632
property.vector2Value = new Vector2(xValue, yValue);

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

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections;
1+
using System.Collections;
32

43
using UnityEditor;
54
using UnityEngine;
@@ -9,30 +8,19 @@ namespace Toolbox.Editor.Drawers
98
[CustomPropertyDrawer(typeof(PresetAttribute))]
109
public class PresetAttributeDrawer : PropertyDrawerBase
1110
{
12-
protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
11+
private IList GetPresetList(object parentObject, string sourceHandle, SerializedProperty property)
1312
{
14-
return base.GetPropertyHeightSafe(property, label);
15-
}
16-
17-
protected override void OnGUISafe(Rect position, SerializedProperty property, GUIContent label)
18-
{
19-
//NOTE: this implementation does not support multiple different sources
20-
var sourceHandle = Attribute.SourceHandle;
21-
var declaringObject = property.GetDeclaringObject();
22-
//extract (if available) the real preset value
23-
if (!ValueExtractionHelper.TryGetValue(sourceHandle, declaringObject, out var sourceValue))
13+
if (!ValueExtractionHelper.TryGetValue(sourceHandle, parentObject, out var sourceValue))
2414
{
2515
ToolboxEditorLog.MemberNotFoundWarning(attribute, property, sourceHandle);
26-
EditorGUI.PropertyField(position, property, label);
27-
return;
16+
return null;
2817
}
2918

3019
if (!(sourceValue is IList presetList))
3120
{
3221
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
33-
string.Format("Preset ({0}) has to be a one-dimensional collection (array or list).", sourceHandle));
34-
EditorGUI.PropertyField(position, property, label);
35-
return;
22+
string.Format("Values preset ({0}) has to be a one-dimensional collection (array or list).", sourceHandle));
23+
return null;
3624
}
3725

3826
var sourceType = sourceValue.GetType();
@@ -44,36 +32,96 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
4432
{
4533
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
4634
"Type mismatch between serialized property and given Preset.");
47-
EditorGUI.PropertyField(position, property, label);
48-
return;
35+
return null;
4936
}
5037

38+
return presetList;
39+
}
40+
41+
private string[] GetOptions(IList presetList)
42+
{
5143
var itemsCount = presetList.Count;
52-
var objects = new object[itemsCount];
5344
var options = new string[itemsCount];
5445
for (var i = 0; i < itemsCount; i++)
5546
{
56-
objects[i] = presetList[i];
5747
options[i] = presetList[i]?.ToString();
5848
}
5949

60-
var value = property.GetProperValue(fieldInfo, declaringObject);
61-
var index = Array.IndexOf(objects, value);
50+
return options;
51+
}
52+
53+
private string[] GetOptions(IList presetList, object parentObject, string optionHandle, SerializedProperty property)
54+
{
55+
if (string.IsNullOrEmpty(optionHandle))
56+
{
57+
return GetOptions(presetList);
58+
}
59+
60+
if (!ValueExtractionHelper.TryGetValue(optionHandle, parentObject, out var optionValue))
61+
{
62+
ToolboxEditorLog.MemberNotFoundWarning(attribute, property, optionHandle);
63+
return GetOptions(presetList);
64+
}
65+
66+
if (!(optionValue is IList optionList))
67+
{
68+
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
69+
string.Format("Options preset ({0}) has to be a one-dimensional collection (array or list).", optionHandle));
70+
return GetOptions(presetList);
71+
}
72+
73+
var presetsCount = presetList.Count;
74+
var optionsCount = optionList.Count;
75+
var options = new string[presetsCount];
76+
for (int i = 0; i < presetsCount; i++)
77+
{
78+
options[i] = i < optionsCount
79+
? optionList[i]?.ToString()
80+
: presetList[i]?.ToString();
81+
}
82+
83+
return options;
84+
}
85+
86+
87+
protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
88+
{
89+
return base.GetPropertyHeightSafe(property, label);
90+
}
91+
92+
protected override void OnGUISafe(Rect position, SerializedProperty property, GUIContent label)
93+
{
94+
//NOTE: this implementation does not support multiple different sources
95+
var sourceHandle = Attribute.SourceHandle;
96+
var optionHandle = Attribute.OptionHandle;
97+
var parentObject = property.GetDeclaringObject();
98+
var presetList = GetPresetList(parentObject, sourceHandle, property);
99+
if (presetList == null)
100+
{
101+
EditorGUI.PropertyField(position, property, label);
102+
return;
103+
}
104+
105+
var options = GetOptions(presetList, parentObject, optionHandle, property);
106+
107+
var value = property.GetProperValue(fieldInfo, parentObject);
108+
var index = presetList.IndexOf(value);
62109

63110
//begin the true property
64111
label = EditorGUI.BeginProperty(position, label, property);
65112
EditorGUI.BeginChangeCheck();
113+
66114
//get selected preset value
67115
index = EditorGUI.Popup(position, label, index, EditorGUIUtility.TrTempContent(options));
68-
index = Mathf.Clamp(index, 0, itemsCount - 1);
116+
index = Mathf.Clamp(index, 0, presetList.Count - 1);
69117
if (EditorGUI.EndChangeCheck())
70118
{
71119
//udpate property value using previously cached FieldInfo and picked value
72120
//there is no cleaner way to do it, since we don't really know what kind of
73121
//serialized property we are updating
74122

75123
property.serializedObject.Update();
76-
property.SetProperValue(fieldInfo, objects[index]);
124+
property.SetProperValue(fieldInfo, presetList[index]);
77125
property.serializedObject.ApplyModifiedProperties();
78126
//handle situation when updating multiple different targets
79127
property.serializedObject.SetIsDifferentCacheDirty();
@@ -85,10 +133,10 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
85133

86134
public override bool IsPropertyValid(SerializedProperty property)
87135
{
88-
var declaringObject = property.GetDeclaringObject();
136+
var parentObject = property.GetDeclaringObject();
89137
//NOTE: reflection won't work properly on nested structs because of boxing
90138
//TODO: handle this case
91-
return !declaringObject.GetType().IsValueType;
139+
return !parentObject.GetType().IsValueType;
92140
}
93141

94142

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using UnityEngine;
2+
3+
namespace Toolbox.Editor.Drawers
4+
{
5+
public class BeginVerticalAttributeDrawer : ToolboxDecoratorDrawer<BeginVerticalAttribute>
6+
{
7+
protected override void OnGuiBeginSafe(BeginVerticalAttribute attribute)
8+
{
9+
ToolboxLayoutHelper.BeginVertical();
10+
}
11+
}
12+
}

Assets/Editor Toolbox/Editor/Drawers/Toolbox/Decorator/BeginVerticalAttributeDrawer.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.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using UnityEngine;
2+
3+
namespace Toolbox.Editor.Drawers
4+
{
5+
public class EndVerticalAttributeDrawer : ToolboxDecoratorDrawer<EndVerticalAttribute>
6+
{
7+
protected override void OnGuiCloseSafe(EndVerticalAttribute attribute)
8+
{
9+
ToolboxLayoutHelper.CloseVertical();
10+
}
11+
}
12+
}

Assets/Editor Toolbox/Editor/Drawers/Toolbox/Decorator/EndVerticalAttributeDrawer.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/Internal/ReorderableListBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ public void RemoveElement(int index)
497497
/// </summary>
498498
public virtual void DoList()
499499
{
500-
DoList(null);
500+
DoList(TitleLabel);
501501
}
502502

503503
/// <summary>

Assets/Editor Toolbox/Editor/ToolboxEditorGui.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,21 +205,19 @@ public static void DrawMinMaxSlider(Rect rect, string label, ref float xValue, r
205205

206206
public static void DrawMinMaxSlider(Rect rect, GUIContent label, ref float xValue, ref float yValue, float minValue, float maxValue)
207207
{
208-
var padding = 8.0f;
209-
var labelWidth = EditorGUIUtility.labelWidth;
208+
rect = EditorGUI.PrefixLabel(rect, label);
209+
210210
var fieldWidth = EditorGUIUtility.fieldWidth;
211-
212-
var minFieldRect = new Rect(rect.xMin + labelWidth, rect.y, fieldWidth, rect.height);
211+
var minFieldRect = new Rect(rect.xMin, rect.y, fieldWidth, rect.height);
213212
var maxFieldRect = new Rect(rect.xMax - fieldWidth, rect.y, fieldWidth, rect.height);
214-
var labelRect = new Rect(rect.x, rect.y, labelWidth, rect.height);
213+
215214
//set slider rect between min and max fields + additional padding
216-
var sliderRect = new Rect(rect.x + labelWidth + fieldWidth + padding,
217-
rect.y,
218-
rect.width - labelWidth - fieldWidth * 2 - padding * 2,
219-
rect.height);
215+
var spacing = 8.0f;
216+
var sliderRect = Rect.MinMaxRect(minFieldRect.xMax + spacing,
217+
rect.yMin,
218+
maxFieldRect.xMin - spacing,
219+
rect.yMax);
220220

221-
//begin drawing using GUI methods
222-
EditorGUI.LabelField(labelRect, label);
223221
EditorGUI.BeginChangeCheck();
224222
xValue = EditorGUI.FloatField(minFieldRect, xValue);
225223
yValue = EditorGUI.FloatField(maxFieldRect, yValue);

0 commit comments

Comments
 (0)