Пример #1
0
        public static void DisableDefaultPortDrawer(OdinDrawer drawer)
        {
            var defaultDrawer = drawer.Property.GetActiveDrawerChain().BakedDrawerArray.FirstOrDefault(x => x.GetType().ImplementsOpenGenericClass(typeof(DefaultNodePortDrawer <>)));

            if (defaultDrawer != null)
            {
                defaultDrawer.SkipWhenDrawing = true;
            }
        }
        public static void HandleIfAttributesCondition(OdinDrawer drawer, InspectorProperty property, string memberName, object value, out bool result, out string errorMessage)
        {
            var context = property.Context.Get(drawer, "IfAttributeContext", (IfAttributesContext)null);

            if (context.Value == null)
            {
                context.Value = new IfAttributesContext();
                MemberInfo memberInfo = property.ParentType
                                        .FindMember()
                                        .IsNamed(memberName)
                                        .HasNoParameters()
                                        .GetMember(out context.Value.ErrorMessage);

                if (memberInfo != null)
                {
                    string name = (memberInfo is MethodInfo) ? memberInfo.Name + "()" : memberInfo.Name;

                    if (memberInfo.GetReturnType() == typeof(bool))
                    {
                        if (memberInfo.IsStatic())
                        {
                            context.Value.StaticMemberGetter = DeepReflection.CreateValueGetter <bool>(property.ParentType, name);
                        }
                        else
                        {
                            context.Value.InstanceMemberGetter = DeepReflection.CreateWeakInstanceValueGetter <bool>(property.ParentType, name);
                        }
                    }
                    else
                    {
                        if (value == null)
                        {
                            context.Value.ErrorMessage = "An member with a non-bool value was referenced, but no value was specified in the EnabledIf attribute.";
                        }
                        else
                        {
                            if (memberInfo.IsStatic())
                            {
                                context.Value.StaticObjectMemberGetter = DeepReflection.CreateValueGetter <object>(property.ParentType, name);
                            }
                            else
                            {
                                context.Value.InstanceObjectMemberGetter = DeepReflection.CreateWeakInstanceValueGetter <object>(property.ParentType, name);
                            }
                        }
                    }
                }
            }
            errorMessage = context.Value.ErrorMessage;

            if (Event.current.type != EventType.Layout)
            {
                result = context.Value.Result;
                return;
            }

            context.Value.Result = false;

            if (context.Value.ErrorMessage == null)
            {
                if (context.Value.InstanceMemberGetter != null)
                {
                    for (int i = 0; i < property.ParentValues.Count; i++)
                    {
                        if (context.Value.InstanceMemberGetter(property.ParentValues[i]))
                        {
                            context.Value.Result = true;
                            break;
                        }
                    }
                }
                else if (context.Value.InstanceObjectMemberGetter != null)
                {
                    for (int i = 0; i < property.ParentValues.Count; i++)
                    {
                        var val = context.Value.InstanceObjectMemberGetter(property.ParentValues[i]);
                        if (Equals(val, value))
                        {
                            context.Value.Result = true;
                            break;
                        }
                    }
                }
                else if (context.Value.StaticObjectMemberGetter != null)
                {
                    var val = context.Value.StaticObjectMemberGetter();
                    if (Equals(val, value))
                    {
                        context.Value.Result = true;
                    }
                }
                else if (context.Value.StaticMemberGetter != null)
                {
                    if (context.Value.StaticMemberGetter())
                    {
                        context.Value.Result = true;
                    }
                }
            }

            result = context.Value.Result;
        }
Пример #3
0
        public static float Draw <T>(OdinDrawer drawerInstance, IPropertyValueEntry <T> entry, float progress, ProgressBarAttribute attribute, GUIContent label)
        {
            PropertyContext <ProgressBarContext <T> > contextBuffer;

            if (entry.Context.Get <ProgressBarContext <T> >(drawerInstance, "ProgressBarContext", out contextBuffer))
            {
                var parentType = entry.Property.FindParent(PropertyValueCategory.Member, true).ParentType;
                contextBuffer.Value = new ProgressBarContext <T>();

                if (!attribute.ColorMember.IsNullOrWhitespace())
                {
                    MemberInfo member;
                    if (MemberFinder.Start(parentType)
                        .IsNamed(attribute.ColorMember)
                        .HasReturnType <Color>()
                        .TryGetMember(out member, out contextBuffer.Value.ErrorMessage))
                    {
                        if (member is FieldInfo || member is PropertyInfo)
                        {
                            if (member.IsStatic())
                            {
                                contextBuffer.Value.StaticColorGetter = DeepReflection.CreateValueGetter <Color>(parentType, attribute.ColorMember);
                            }
                            else
                            {
                                contextBuffer.Value.InstanceColorGetter = DeepReflection.CreateWeakInstanceValueGetter <Color>(parentType, attribute.ColorMember);
                            }
                        }
                        else if (member is MethodInfo)
                        {
                            if (member.IsStatic())
                            {
                                contextBuffer.Value.ErrorMessage = "Static method members are currently not supported.";
                            }
                            else
                            {
                                var method = member as MethodInfo;
                                var p      = method.GetParameters();

                                if (p.Length == 0)
                                {
                                    contextBuffer.Value.InstanceColorMethod = EmitUtilities.CreateWeakInstanceMethodCallerFunc <Color>(method);
                                }
                                else if (p.Length == 1 && p[0].ParameterType == typeof(T))
                                {
                                    contextBuffer.Value.InstanceColorParameterMethod = EmitUtilities.CreateWeakInstanceMethodCallerFunc <T, Color>(method);
                                }
                            }
                        }
                        else
                        {
                            contextBuffer.Value.ErrorMessage = "Unsupported member type.";
                        }
                    }
                }
                if (!attribute.BackgroundColorMember.IsNullOrWhitespace())
                {
                    MemberInfo member;
                    if (MemberFinder.Start(parentType)
                        .IsNamed(attribute.BackgroundColorMember)
                        .HasReturnType <Color>()
                        .TryGetMember(out member, out contextBuffer.Value.ErrorMessage))
                    {
                        if (member is FieldInfo || member is PropertyInfo)
                        {
                            if (member.IsStatic())
                            {
                                contextBuffer.Value.StaticBackgroundColorGetter = DeepReflection.CreateValueGetter <Color>(parentType, attribute.BackgroundColorMember);
                            }
                            else
                            {
                                contextBuffer.Value.InstanceBackgroundColorGetter = DeepReflection.CreateWeakInstanceValueGetter <Color>(parentType, attribute.BackgroundColorMember);
                            }
                        }
                        else if (member is MethodInfo)
                        {
                            if (member.IsStatic())
                            {
                                contextBuffer.Value.ErrorMessage = "Static method members are currently not supported.";
                            }
                            else
                            {
                                var method = member as MethodInfo;
                                var p      = method.GetParameters();

                                if (p.Length == 0)
                                {
                                    contextBuffer.Value.InstanceBackgroundColorMethod = EmitUtilities.CreateWeakInstanceMethodCallerFunc <Color>(method);
                                }
                                else if (p.Length == 1 && p[0].ParameterType == typeof(T))
                                {
                                    contextBuffer.Value.InstanceBackgroundColorParameterMethod = EmitUtilities.CreateWeakInstanceMethodCallerFunc <T, Color>(method);
                                }
                            }
                        }
                        else
                        {
                            contextBuffer.Value.ErrorMessage = "Unsupported member type.";
                        }
                    }
                }
            }

            var context = contextBuffer.Value;

            // Error message
            if (context.ErrorMessage != null)
            {
                AllEditorGUI.ErrorMessageBox(context.ErrorMessage);
            }

            // Construct rect.
            Rect rect;

            if (label != null)
            {
                rect = EditorGUILayout.GetControlRect(true, attribute.Height > EditorGUIUtility.singleLineHeight ? attribute.Height : EditorGUIUtility.singleLineHeight);
                rect = EditorGUI.PrefixLabel(rect, label);
                rect = rect.AlignMiddle(attribute.Height);
            }
            else
            {
                rect = EditorGUILayout.GetControlRect(false, attribute.Height);
                GUIHelper.IndentRect(ref rect);
            }

            // Draw
            if (Event.current.type == EventType.Repaint)
            {
                var parent = entry.Property.FindParent(PropertyValueCategory.Member, true).ParentValues[0];

                Color color =
                    context.StaticColorGetter != null?context.StaticColorGetter() :
                        context.InstanceColorGetter != null?context.InstanceColorGetter(parent) :
                            context.InstanceColorMethod != null?context.InstanceColorMethod(parent) :
                                context.InstanceColorParameterMethod != null?context.InstanceColorParameterMethod(parent, entry.SmartValue) :
                                    new Color(attribute.R, attribute.G, attribute.B, 1f);

                Color backgroundColor =
                    context.StaticBackgroundColorGetter != null?context.StaticBackgroundColorGetter() :
                        context.InstanceBackgroundColorGetter != null?context.InstanceBackgroundColorGetter(parent) :
                            context.InstanceBackgroundColorMethod != null?context.InstanceBackgroundColorMethod(parent) :
                                context.InstanceBackgroundColorParameterMethod != null?context.InstanceBackgroundColorParameterMethod(parent, entry.SmartValue) :
                                    new Color(0.16f, 0.16f, 0.16f, 1f);

                AllEditorGUI.DrawSolidRect(rect, backgroundColor);
                AllEditorGUI.DrawSolidRect(rect.AlignLeft(rect.width * Mathf.Clamp01(progress)), color);
                AllEditorGUI.DrawBorders(rect, 1, new Color(0.16f, 0.16f, 0.16f, 1f));
            }

            if (GUI.enabled)
            {
                int controlID = GUIUtility.GetControlID(FocusType.Passive);
                if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && rect.Contains(Event.current.mousePosition) ||
                    GUIUtility.hotControl == controlID && (Event.current.type == EventType.MouseMove || Event.current.type == EventType.MouseDrag))
                {
                    Event.current.Use();
                    GUIUtility.hotControl = controlID;
                    GUIHelper.RequestRepaint();
                    GUI.changed = true;

                    progress = (Event.current.mousePosition.x - rect.xMin) / rect.width;
                }
                else if (GUIUtility.hotControl == controlID && Event.current.rawType == EventType.MouseUp)
                {
                    GUIUtility.hotControl = 0;
                }
            }

            return(progress);
        }
Пример #4
0
        public static void HandleIfAttributesCondition(OdinDrawer drawer, InspectorProperty property, string memberName, object value, out bool result, out string errorMessage)
        {
            var context = property.Context.Get(drawer, "IfAttributeContext", (IfAttributesContext)null);

            if (context.Value == null)
            {
                Type returnType;

                context.Value = new IfAttributesContext();

                // TODO: Expressions temporarily disabled.
                //if (memberName != null && memberName.Length > 0 && memberName[0] == '$')
                //{
                //    var expression = memberName.Substring(1);

                //    context.Value.expressionIsStatic = property.ParentValueProperty == null && property.Tree.IsStatic;
                //    context.Value.ExpressionMethod = ExpressionCompilerUtility.CompileExpression(expression, context.Value.expressionIsStatic, property.ParentType, out context.Value.ErrorMessage);

                //    returnType = context.Value.ExpressionMethod != null ? context.Value.ExpressionMethod.Method.ReturnType : null;
                //}
                //else
                {
                    returnType = null;
                    MemberInfo memberInfo = property.ParentType
                                            .FindMember()
                                            .IsNamed(memberName)
                                            .HasNoParameters()
                                            .GetMember(out context.Value.ErrorMessage);

                    if (memberInfo != null)
                    {
                        string name = (memberInfo is MethodInfo) ? memberInfo.Name + "()" : memberInfo.Name;

                        if (memberInfo.GetReturnType() == typeof(bool))
                        {
                            if (memberInfo.IsStatic())
                            {
                                context.Value.StaticMemberGetter = DeepReflection.CreateValueGetter <bool>(property.ParentType, name);
                            }
                            else
                            {
                                context.Value.InstanceMemberGetter = DeepReflection.CreateWeakInstanceValueGetter <bool>(property.ParentType, name);
                            }
                        }
                        else
                        {
                            if (memberInfo.IsStatic())
                            {
                                context.Value.StaticObjectMemberGetter = DeepReflection.CreateValueGetter <object>(property.ParentType, name);
                            }
                            else
                            {
                                context.Value.InstanceObjectMemberGetter = DeepReflection.CreateWeakInstanceValueGetter <object>(property.ParentType, name);
                            }
                        }

                        returnType = memberInfo.GetReturnType();
                    }
                }

                if (returnType != null) // Should only be null in case of errors.
                {
                    context.Value.UseNullComparison = returnType != typeof(string) && (returnType.IsClass || returnType.IsInterface);
                }
            }
            errorMessage = context.Value.ErrorMessage;

            if (Event.current.type != EventType.Layout)
            {
                result = context.Value.Result;
                return;
            }

            context.Value.Result = false;

            if (context.Value.ErrorMessage == null)
            {
                // TODO: Expressions temporarily disabled.
                //if (context.Value.ExpressionMethod != null)
                //{
                //    for (int i = 0; i < property.ParentValues.Count; i++)
                //    {
                //        object val;
                //        if (context.Value.expressionIsStatic)
                //        {
                //            val = context.Value.ExpressionMethod.DynamicInvoke();
                //        }
                //        else
                //        {
                //            val = context.Value.ExpressionMethod.DynamicInvoke(property.ParentValues[i]);
                //        }

                //        if (context.Value.UseNullComparison)
                //        {
                //            if (val is UnityEngine.Object)
                //            {
                //                // Unity objects can be 'fake null', and to detect that we have to test the value as a Unity object.
                //                if (((UnityEngine.Object)val) != null)
                //                {
                //                    context.Value.Result = true;
                //                    break;
                //                }
                //            }
                //            else if (val != null)
                //            {
                //                context.Value.Result = true;
                //                break;
                //            }
                //        }
                //        else if (val is bool)
                //        {
                //            context.Value.Result = (bool)val;
                //            break;
                //        }
                //        else if (Equals(val, value))
                //        {
                //            context.Value.Result = true;
                //            break;
                //        }
                //    }
                //}
                //else
                if (context.Value.InstanceMemberGetter != null)
                {
                    for (int i = 0; i < property.ParentValues.Count; i++)
                    {
                        if (context.Value.InstanceMemberGetter(property.ParentValues[i]))
                        {
                            context.Value.Result = true;
                            break;
                        }
                    }
                }
                else if (context.Value.StaticMemberGetter != null)
                {
                    if (context.Value.StaticMemberGetter())
                    {
                        context.Value.Result = true;
                    }
                }
                else if (context.Value.InstanceObjectMemberGetter != null)
                {
                    for (int i = 0; i < property.ParentValues.Count; i++)
                    {
                        var val = context.Value.InstanceObjectMemberGetter(property.ParentValues[i]);
                        if (context.Value.UseNullComparison)
                        {
                            if (val is UnityEngine.Object)
                            {
                                // Unity objects can be 'fake null', and to detect that we have to test the value as a Unity object.
                                if (((UnityEngine.Object)val) != null)
                                {
                                    context.Value.Result = true;
                                    break;
                                }
                            }
                            else if (val != null)
                            {
                                context.Value.Result = true;
                                break;
                            }
                        }
                        else if (Equals(val, value))
                        {
                            context.Value.Result = true;
                            break;
                        }
                    }
                }
                else if (context.Value.StaticObjectMemberGetter != null)
                {
                    var val = context.Value.StaticObjectMemberGetter();
                    if (context.Value.UseNullComparison)
                    {
                        if (val is UnityEngine.Object)
                        {
                            // Unity objects can be 'fake null', and to detect that we have to test the value as a Unity object.
                            if (((UnityEngine.Object)val) != null)
                            {
                                context.Value.Result = true;
                            }
                        }
                        else if (val != null)
                        {
                            context.Value.Result = true;
                        }
                    }
                    else if (Equals(val, value))
                    {
                        context.Value.Result = true;
                    }
                }
            }

            result = context.Value.Result;
        }
Пример #5
0
        internal static ProgressBarContext <T> GetContext(OdinDrawer drawer, IPropertyValueEntry <T> entry, ProgressBarAttribute attribute)
        {
            PropertyContext <ProgressBarContext <T> > context;

            if (entry.Context.Get(drawer, "ProgressBarContext", out context))
            {
                context.Value = new ProgressBarContext <T>();
                var parentType = entry.Property.FindParent(PropertyValueCategory.Member, true).ParentType;

                // Foreground color member.
                if (!attribute.ColorMember.IsNullOrWhitespace())
                {
                    MemberInfo member;
                    if (MemberFinder.Start(parentType)
                        .IsNamed(attribute.ColorMember)
                        .HasReturnType <Color>()
                        .TryGetMember(out member, out context.Value.ErrorMessage))
                    {
                        if (member is FieldInfo || member is PropertyInfo)
                        {
                            if (member.IsStatic())
                            {
                                context.Value.StaticColorGetter = DeepReflection.CreateValueGetter <Color>(parentType, attribute.ColorMember);
                            }
                            else
                            {
                                context.Value.InstanceColorGetter = DeepReflection.CreateWeakInstanceValueGetter <Color>(parentType, attribute.ColorMember);
                            }
                        }
                        else if (member is MethodInfo)
                        {
                            if (member.IsStatic())
                            {
                                context.Value.ErrorMessage = "Static method members are currently not supported.";
                            }
                            else
                            {
                                var method = member as MethodInfo;
                                var p      = method.GetParameters();

                                if (p.Length == 0)
                                {
                                    context.Value.InstanceColorMethod = EmitUtilities.CreateWeakInstanceMethodCallerFunc <Color>(method);
                                }
                                else if (p.Length == 1 && p[0].ParameterType == typeof(T))
                                {
                                    context.Value.InstanceColorParameterMethod = EmitUtilities.CreateWeakInstanceMethodCallerFunc <T, Color>(method);
                                }
                            }
                        }
                        else
                        {
                            context.Value.ErrorMessage = "Unsupported member type.";
                        }
                    }
                }

                // Background color member.
                if (!attribute.BackgroundColorMember.IsNullOrWhitespace())
                {
                    MemberInfo member;
                    if (MemberFinder.Start(parentType)
                        .IsNamed(attribute.BackgroundColorMember)
                        .HasReturnType <Color>()
                        .TryGetMember(out member, out context.Value.ErrorMessage))
                    {
                        if (member is FieldInfo || member is PropertyInfo)
                        {
                            if (member.IsStatic())
                            {
                                context.Value.StaticBackgroundColorGetter = DeepReflection.CreateValueGetter <Color>(parentType, attribute.BackgroundColorMember);
                            }
                            else
                            {
                                context.Value.InstanceBackgroundColorGetter = DeepReflection.CreateWeakInstanceValueGetter <Color>(parentType, attribute.BackgroundColorMember);
                            }
                        }
                        else if (member is MethodInfo)
                        {
                            if (member.IsStatic())
                            {
                                context.Value.ErrorMessage = "Static method members are currently not supported.";
                            }
                            else
                            {
                                var method = member as MethodInfo;
                                var p      = method.GetParameters();

                                if (p.Length == 0)
                                {
                                    context.Value.InstanceBackgroundColorMethod = EmitUtilities.CreateWeakInstanceMethodCallerFunc <Color>(method);
                                }
                                else if (p.Length == 1 && p[0].ParameterType == typeof(T))
                                {
                                    context.Value.InstanceBackgroundColorParameterMethod = EmitUtilities.CreateWeakInstanceMethodCallerFunc <T, Color>(method);
                                }
                            }
                        }
                        else
                        {
                            context.Value.ErrorMessage = "Unsupported member type.";
                        }
                    }
                }

                // Custom value string getter
                if (attribute.CustomValueStringMember != null && attribute.CustomValueStringMember.Length > 0)
                {
                    string member = attribute.CustomValueStringMember;
                    if (attribute.CustomValueStringMember[0] != '$')
                    {
                        member = "$" + attribute.CustomValueStringMember;
                    }

                    context.Value.CustomValueLabelGetter = new StringMemberHelper(
                        entry.Property.ParentType,
                        member,
                        ref context.Value.ErrorMessage);
                }
            }

            return(context.Value);
        }