|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +using UnityEditor; |
| 5 | + |
| 6 | +namespace Toolbox.Editor |
| 7 | +{ |
| 8 | + public class ToolboxEditorDrawer : IToolboxEditorDrawer |
| 9 | + { |
| 10 | + private readonly HashSet<string> propertiesToIgnore = new HashSet<string>(); |
| 11 | + |
| 12 | + //TODO: refactor |
| 13 | + /// <inheritdoc /> |
| 14 | + public void DrawToolboxProperty(SerializedProperty property) |
| 15 | + { |
| 16 | + var propertyPath = property.propertyPath; |
| 17 | + if (propertiesToIgnore.Contains(propertyPath)) |
| 18 | + { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + ToolboxEditorGui.DrawToolboxProperty(property); |
| 23 | + } |
| 24 | + |
| 25 | + /// <inheritdoc /> |
| 26 | + public void DrawDefaultProperty(SerializedProperty property) |
| 27 | + { |
| 28 | + var propertyPath = property.propertyPath; |
| 29 | + if (propertiesToIgnore.Contains(propertyPath)) |
| 30 | + { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + ToolboxEditorGui.DrawNativeProperty(property); |
| 35 | + } |
| 36 | + |
| 37 | + /// <inheritdoc /> |
| 38 | + public void DrawToolboxInspector(SerializedObject serializedObject) |
| 39 | + { |
| 40 | + if (!ToolboxDrawerModule.ToolboxDrawersAllowed) |
| 41 | + { |
| 42 | + DrawDefaultInspector(serializedObject); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + //TODO: how to handle default/toolbox drawers |
| 47 | + //Action<SerializedProperty> drawAction = ToolboxDrawerModule.ToolboxDrawersAllowed |
| 48 | + // ? (Action<SerializedProperty>)DrawToolboxProperty |
| 49 | + // : DrawDefaultProperty; |
| 50 | + serializedObject.UpdateIfRequiredOrScript(); |
| 51 | + var property = serializedObject.GetIterator(); |
| 52 | + if (property.NextVisible(true)) |
| 53 | + { |
| 54 | + var isScript = PropertyUtility.IsDefaultScriptProperty(property); |
| 55 | + using (new EditorGUI.DisabledScope(isScript)) |
| 56 | + { |
| 57 | + DrawToolboxProperty(property.Copy()); |
| 58 | + } |
| 59 | + |
| 60 | + while (property.NextVisible(false)) |
| 61 | + { |
| 62 | + DrawToolboxProperty(property.Copy()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + serializedObject.ApplyModifiedProperties(); |
| 67 | + } |
| 68 | + |
| 69 | + //TODO: |
| 70 | + /// <inheritdoc /> |
| 71 | + public void DrawDefaultInspector(SerializedObject serializedObject) |
| 72 | + { |
| 73 | + serializedObject.UpdateIfRequiredOrScript(); |
| 74 | + var property = serializedObject.GetIterator(); |
| 75 | + if (property.NextVisible(true)) |
| 76 | + { |
| 77 | + var isScript = PropertyUtility.IsDefaultScriptProperty(property); |
| 78 | + using (new EditorGUI.DisabledScope(isScript)) |
| 79 | + { |
| 80 | + EditorGUILayout.PropertyField(property, true); |
| 81 | + } |
| 82 | + |
| 83 | + while (property.NextVisible(false)) |
| 84 | + { |
| 85 | + EditorGUILayout.PropertyField(property, true); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + serializedObject.ApplyModifiedProperties(); |
| 90 | + } |
| 91 | + |
| 92 | + /// <inheritdoc /> |
| 93 | + public void IgnoreProperty(SerializedProperty property) |
| 94 | + { |
| 95 | + IgnoreProperty(property.propertyPath); |
| 96 | + } |
| 97 | + |
| 98 | + /// <inheritdoc /> |
| 99 | + public void IgnoreProperty(string propertyPath) |
| 100 | + { |
| 101 | + propertiesToIgnore.Add(propertyPath); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments