Skip to content

Commit ef5e83d

Browse files
authored
Merge pull request #53 from arimger/develop
Develop - 0.11.8
2 parents 600830b + 0d7de3d commit ef5e83d

File tree

15 files changed

+582
-30
lines changed

15 files changed

+582
-30
lines changed

Assets/Editor Toolbox/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 0.11.8 [15.09.2022]
2+
3+
### Added:
4+
- Utility methods for the Rect type
5+
- Utility methods for the EditorPrefs API
6+
7+
### Changed:
8+
- Possibility to define custom validation method in the EditorButton attribute
9+
110
## 0.11.7 [04.09.2022]
211

312
### Added:

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

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@ namespace Toolbox.Editor.Drawers
99
{
1010
public class EditorButtonAttributeDrawer : ToolboxDecoratorDrawer<EditorButtonAttribute>
1111
{
12+
private MethodInfo GetMethod(EditorButtonAttribute attribute, Object[] targetObjects, string methodName)
13+
{
14+
var methodInfo = ReflectionUtility.GetObjectMethod(methodName, targetObjects);
15+
if (methodInfo == null)
16+
{
17+
ToolboxEditorLog.AttributeUsageWarning(attribute, string.Format("{0} method not found.", methodName));
18+
return null;
19+
}
20+
21+
var parameters = methodInfo.GetParameters();
22+
if (parameters.Length > 0)
23+
{
24+
ToolboxEditorLog.AttributeUsageWarning(attribute, string.Format("{0} method has to be parameterless.", methodName));
25+
return null;
26+
}
27+
28+
return methodInfo;
29+
}
30+
31+
private static bool IsCoroutine(MethodInfo methodInfo)
32+
{
33+
return methodInfo.ReturnType == typeof(IEnumerator);
34+
}
35+
1236
private bool IsClickable(ButtonActivityType activityType)
1337
{
1438
switch (activityType)
@@ -26,26 +50,54 @@ private bool IsClickable(ButtonActivityType activityType)
2650
return true;
2751
}
2852

29-
private bool IsCoroutine(MethodInfo method)
53+
private bool IsClickable(EditorButtonAttribute attribute, Object[] targetObjects)
3054
{
31-
return method.ReturnType == typeof(IEnumerator);
55+
if (!IsClickable(attribute.ActivityType))
56+
{
57+
return false;
58+
}
59+
60+
var validateMethodName = attribute.ValidateMethodName;
61+
if (validateMethodName == null)
62+
{
63+
return true;
64+
}
65+
66+
var validateMethodInfo = GetMethod(attribute, targetObjects, validateMethodName);
67+
if (validateMethodInfo == null)
68+
{
69+
return true;
70+
}
71+
72+
var returnType = validateMethodInfo.ReturnType;
73+
if (returnType != typeof(bool))
74+
{
75+
ToolboxEditorLog.AttributeUsageWarning(attribute, "Validation method is invalid, return type has to be 'bool'.");
76+
return true;
77+
}
78+
79+
for (var i = 0; i < targetObjects.Length; i++)
80+
{
81+
var target = targetObjects[i];
82+
if (target == null)
83+
{
84+
continue;
85+
}
86+
87+
if (!(bool)validateMethodInfo.Invoke(target, null))
88+
{
89+
return false;
90+
}
91+
}
92+
93+
return true;
3294
}
3395

3496
private void CallMethods(EditorButtonAttribute attribute, Object[] targetObjects)
3597
{
36-
var methodInfo = ReflectionUtility.GetObjectMethod(attribute.MethodName, targetObjects);
37-
//validate method name (check if method exists)
98+
var methodInfo = GetMethod(attribute, targetObjects, attribute.MethodName);
3899
if (methodInfo == null)
39100
{
40-
ToolboxEditorLog.AttributeUsageWarning(attribute, string.Format("{0} method not found.", attribute.MethodName));
41-
return;
42-
}
43-
44-
//validate parameters count and log warning
45-
var parameters = methodInfo.GetParameters();
46-
if (parameters.Length > 0)
47-
{
48-
ToolboxEditorLog.AttributeUsageWarning(attribute, string.Format("{0} method has to be parameterless.", attribute.MethodName));
49101
return;
50102
}
51103

@@ -78,7 +130,7 @@ protected override void OnGuiCloseSafe(EditorButtonAttribute attribute)
78130
return;
79131
}
80132

81-
var disable = !IsClickable(attribute.ActivityType);
133+
var disable = !IsClickable(attribute, targetObjects);
82134
using (new EditorGUI.DisabledScope(disable))
83135
{
84136
var label = string.IsNullOrEmpty(attribute.ExtraLabel)

Assets/Editor Toolbox/Editor/ToolboxEditorSettingsEditor.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ private void OnEnable()
4747
currentTarget = target as ToolboxEditorSettings;
4848

4949
//internal properties cached by 'EditorPrefs'
50-
hierarchyAnimBool = new AnimBool(EditorPrefs.GetBool(string.Format("{0}.HierarchyEnabled", nameof(ToolboxEditorSettings)), false));
51-
projectAnimBool = new AnimBool(EditorPrefs.GetBool(string.Format("{0}.ProjectEnabled", nameof(ToolboxEditorSettings)), false));
52-
inspectorAnimBool = new AnimBool(EditorPrefs.GetBool(string.Format("{0}.InspectorEnabled", nameof(ToolboxEditorSettings)), false));
50+
hierarchyAnimBool = new AnimBool(ToolboxPrefs.GetBool(nameof(ToolboxEditorSettings), "HierarchyEnabled"));
51+
projectAnimBool = new AnimBool(ToolboxPrefs.GetBool(nameof(ToolboxEditorSettings), "ProjectEnabled"));
52+
inspectorAnimBool = new AnimBool(ToolboxPrefs.GetBool(nameof(ToolboxEditorSettings), "InspectorEnabled"));
5353

54-
enabledToShowDrawerType = EditorPrefs.GetInt(string.Format("{0}.PickedDrawerType", nameof(ToolboxEditorSettings)), 0);
54+
enabledToShowDrawerType = ToolboxPrefs.GetInt(nameof(ToolboxEditorSettings), "PickedDrawerType");
5555

5656
var repaintAction = new UnityAction(() =>
5757
{
@@ -131,10 +131,10 @@ private void OnEnable()
131131

132132
private void OnDisable()
133133
{
134-
EditorPrefs.SetBool(string.Format("{0}.HierarchyEnabled", nameof(ToolboxEditorSettings)), hierarchyAnimBool.target);
135-
EditorPrefs.SetBool(string.Format("{0}.ProjectEnabled", nameof(ToolboxEditorSettings)), projectAnimBool.target);
136-
EditorPrefs.SetBool(string.Format("{0}.InspectorEnabled", nameof(ToolboxEditorSettings)), inspectorAnimBool.target);
137-
EditorPrefs.SetInt(string.Format("{0}.PickedDrawerType", nameof(ToolboxEditorSettings)), enabledToShowDrawerType);
134+
ToolboxPrefs.SetBool(nameof(ToolboxEditorSettings), "HierarchyEnabled", hierarchyAnimBool.target);
135+
ToolboxPrefs.SetBool(nameof(ToolboxEditorSettings), "ProjectEnabled", projectAnimBool.target);
136+
ToolboxPrefs.SetBool(nameof(ToolboxEditorSettings), "InspectorEnabled", inspectorAnimBool.target);
137+
ToolboxPrefs.SetInt(nameof(ToolboxEditorSettings), "PickedDrawerType", enabledToShowDrawerType);
138138
}
139139

140140
private void DrawHierarchySettings()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
3+
using UnityEditor;
4+
5+
namespace Toolbox.Editor
6+
{
7+
/// <summary>
8+
/// Additional overlay for <see cref="EditorPrefs"/>.
9+
/// This utility class is Work in Progress.
10+
/// </summary>
11+
internal static class ToolboxPrefs
12+
{
13+
private static string GetKey(Type causer, string propertyName)
14+
{
15+
return GetKey(causer.Name, propertyName);
16+
}
17+
18+
private static string GetKey(string causer, string propertyName)
19+
{
20+
return string.Format("{0}{3}{1}{3}{2}", "Toolbox", causer, propertyName, ".");
21+
}
22+
23+
24+
public static void DeleteAll()
25+
{
26+
EditorPrefs.DeleteAll();
27+
}
28+
29+
public static bool GetBool(object causer, string propertyName, bool defaultValue = false)
30+
{
31+
return GetBool(causer.GetType(), propertyName, defaultValue);
32+
}
33+
34+
public static bool GetBool(Type causer, string propertyName, bool defaultValue = false)
35+
{
36+
var key = GetKey(causer, propertyName);
37+
return EditorPrefs.GetBool(key, defaultValue);
38+
}
39+
40+
public static void SetBool(object causer, string propertyName, bool value)
41+
{
42+
SetBool(causer.GetType(), propertyName, value);
43+
}
44+
45+
public static void SetBool(Type causer, string propertyName, bool value)
46+
{
47+
var key = GetKey(causer, propertyName);
48+
EditorPrefs.SetBool(key, value);
49+
}
50+
51+
public static int GetInt(object causer, string propertyName, int defaultValue = 0)
52+
{
53+
return GetInt(causer.GetType(), propertyName, defaultValue);
54+
}
55+
56+
public static int GetInt(Type causer, string propertyName, int defaultValue = 0)
57+
{
58+
var key = GetKey(causer, propertyName);
59+
return EditorPrefs.GetInt(key, defaultValue);
60+
}
61+
62+
public static void SetInt(object causer, string propertyName, int value)
63+
{
64+
SetInt(causer.GetType(), propertyName, value);
65+
}
66+
67+
public static void SetInt(Type causer, string propertyName, int value)
68+
{
69+
var key = GetKey(causer, propertyName);
70+
EditorPrefs.SetInt(key, value);
71+
}
72+
}
73+
}

Assets/Editor Toolbox/Editor/ToolboxPrefs.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/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,18 @@ public int var1;
313313
public int var1;
314314
```
315315
```csharp
316-
[EditorButton(nameof(MyMethod), "<b>My</b> Custom Label", activityType: ButtonActivityType.OnPlayMode)]
316+
[EditorButton(nameof(MyMethod), "<b>My</b> Custom Label", activityType: ButtonActivityType.OnPlayMode, ValidateMethodName = nameof(ValidationMethod))]
317317
public int var1;
318318

319319
private void MyMethod()
320320
{
321321
Debug.Log("MyMethod is invoked");
322322
}
323+
324+
private bool ValidationMethod()
325+
{
326+
return var1 == 0;
327+
}
323328
```
324329
```csharp
325330
[Highlight(0, 1, 0)]

Assets/Editor Toolbox/Runtime/Attributes/Toolbox/DecoratorAttributes/EditorButtonAttribute.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ public EditorButtonAttribute(string methodName, string extraLabel = null, Button
2020
}
2121

2222
public string MethodName { get; private set; }
23-
23+
/// <summary>
24+
/// If not <see langword="null"/> will be used to retrive validation method.
25+
/// Validation method will be used to disable/enable the button in the Inspector Window.
26+
/// </summary>
27+
public string ValidateMethodName { get; set; }
2428
public string ExtraLabel { get; private set; }
25-
2629
public string Tooltip { get; set; }
27-
2830
public ButtonActivityType ActivityType { get; private set; }
2931
}
3032

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using UnityEngine;
2+
3+
namespace Toolbox
4+
{
5+
public static class RectExtensions
6+
{
7+
public static Rect AlignRight(this Rect rect, float newWidth)
8+
{
9+
rect.xMin = rect.xMax - newWidth;
10+
return rect;
11+
}
12+
13+
public static Rect AlignLeft(this Rect rect, float newWidth)
14+
{
15+
rect.xMax = rect.xMin + newWidth;
16+
return rect;
17+
}
18+
19+
public static Rect AlignBottom(this Rect rect, float newHeight)
20+
{
21+
rect.yMin = rect.yMax - newHeight;
22+
return rect;
23+
}
24+
25+
public static Rect AlignTop(this Rect rect, float newHeight)
26+
{
27+
rect.yMax = rect.yMin + newHeight;
28+
return rect;
29+
}
30+
31+
public static Rect AlignCenterX(this Rect rect, float newWidth)
32+
{
33+
var offset = (rect.width - newWidth) / 2;
34+
rect.xMin += offset;
35+
rect.xMax -= offset;
36+
return rect;
37+
}
38+
39+
public static Rect AlignCenterY(this Rect rect, float newHeight)
40+
{
41+
var offset = (rect.height - newHeight) / 2;
42+
rect.yMin += offset;
43+
rect.yMax -= offset;
44+
return rect;
45+
}
46+
47+
public static Rect MoveByX(this Rect rect, float offset, float spacing = 0.0f)
48+
{
49+
rect.x += offset + spacing;
50+
return rect;
51+
}
52+
53+
public static Rect MoveByY(this Rect rect, float offset, float spacing = 0.0f)
54+
{
55+
rect.y += offset + spacing;
56+
return rect;
57+
}
58+
59+
public static Rect AddMax(this Rect rect, Vector2 range)
60+
{
61+
rect.xMax += range.x;
62+
rect.yMin += range.y;
63+
return rect;
64+
}
65+
66+
public static Rect AddMin(this Rect rect, Vector2 range)
67+
{
68+
rect.xMin += range.x;
69+
rect.yMin += range.y;
70+
return rect;
71+
}
72+
73+
public static Rect SubMax(this Rect rect, Vector2 range)
74+
{
75+
rect.xMax -= range.x;
76+
rect.yMin -= range.y;
77+
return rect;
78+
}
79+
80+
public static Rect SubMin(this Rect rect, Vector2 range)
81+
{
82+
rect.xMin -= range.x;
83+
rect.yMin -= range.y;
84+
return rect;
85+
}
86+
}
87+
}

Assets/Editor Toolbox/Runtime/Extensions/RectExtensions.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/Tests/Editor/Toolbox.Editor.Tests.asmdef

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{
22
"name": "Toolbox.Editor.Tests",
3+
"rootNamespace": "",
34
"references": [
45
"GUID:27619889b8ba8c24980f49ee34dbb44a",
56
"GUID:0acc523941302664db1f4e527237feb3",
67
"GUID:696a0953692949b40bec8730e64e1fdd",
78
"GUID:1b845e2499b39be4aab3e6ac2c8aa02a"
89
],
9-
"includePlatforms": [],
10+
"includePlatforms": [
11+
"Editor"
12+
],
1013
"excludePlatforms": [],
1114
"allowUnsafeCode": false,
1215
"overrideReferences": true,

0 commit comments

Comments
 (0)