/// <summary>
        /// Draws the property.
        /// </summary>
        protected override void DrawPropertyLayout(InspectorProperty property, InfoBoxAttribute attribute, GUIContent label)
        {
            PropertyContext <InfoBoxContext> context = null;

            context = property.Context.Get(this, "Config_" + this.GetHashCode(), (InfoBoxContext)null);
            if (context.Value == null)
            {
                context.Value = new InfoBoxContext()
                {
                    MessageHelper = new StringMemberHelper(property.ParentType, attribute.Message)
                };

                context.Value.ErrorMessage = context.Value.MessageHelper.ErrorMessage;

                MemberInfo memberInfo;

                if (attribute.VisibleIf != null)
                {
                    // Parameter functions
                    if (property.ValueEntry != null &&
                        property.ParentType.FindMember()
                        .IsMethod()
                        .HasReturnType <bool>()
                        .HasParameters(property.ValueEntry.BaseValueType)
                        .IsNamed(attribute.VisibleIf)
                        .TryGetMember(out memberInfo, out context.Value.ErrorMessage))
                    {
                        if (context.Value.ErrorMessage == null)
                        {
                            if (memberInfo is MethodInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationParameterMethod = memberInfo as MethodInfo;
                                }
                                else
                                {
                                    context.Value.InstanceValidationParameterMethod = memberInfo as MethodInfo;
                                }
                            }
                            else
                            {
                                context.Value.ErrorMessage = "Invalid member type!";
                            }
                        }
                    }
                    // Fields, properties, and no-parameter functions.
                    else if (property.ParentType.FindMember()
                             .HasReturnType <bool>()
                             .HasNoParameters()
                             .IsNamed(attribute.VisibleIf)
                             .TryGetMember(out memberInfo, out context.Value.ErrorMessage))
                    {
                        if (context.Value.ErrorMessage == null)
                        {
                            if (memberInfo is FieldInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationCaller = EmitUtilities.CreateStaticFieldGetter <bool>(memberInfo as FieldInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValueGetter = EmitUtilities.CreateWeakInstanceFieldGetter(property.ParentType, memberInfo as FieldInfo);
                                }
                            }
                            else if (memberInfo is PropertyInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationCaller = EmitUtilities.CreateStaticPropertyGetter <bool>(memberInfo as PropertyInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValueGetter = EmitUtilities.CreateWeakInstancePropertyGetter(property.ParentType, memberInfo as PropertyInfo);
                                }
                            }
                            else if (memberInfo is MethodInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationCaller = (Func <bool>)Delegate.CreateDelegate(typeof(Func <bool>), memberInfo as MethodInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValidationMethodCaller = EmitUtilities.CreateWeakInstanceMethodCallerFunc <bool>(memberInfo as MethodInfo);
                                }
                            }
                            else
                            {
                                context.Value.ErrorMessage = "Invalid member type!";
                            }
                        }
                    }
                }
            }

            if (context.Value.ErrorMessage != null)
            {
                SirenixEditorGUI.ErrorMessageBox(context.Value.ErrorMessage);
            }
            else
            {
                if (Event.current.type == EventType.Layout)
                {
                    var parentValue = property.ParentValues[0];

                    try
                    {
                        context.Value.DrawMessageBox =
                            attribute.VisibleIf == null ||
                            (context.Value.StaticValidationParameterMethod != null && (bool)context.Value.StaticValidationParameterMethod.Invoke(null, new object[] { property.ValueEntry.WeakSmartValue })) ||
                            (context.Value.InstanceValidationParameterMethod != null && (bool)context.Value.InstanceValidationParameterMethod.Invoke(null, new object[] { property.ParentValues[0], property.ValueEntry.WeakSmartValue })) ||
                            (context.Value.InstanceValidationMethodCaller != null && context.Value.InstanceValidationMethodCaller(property.ParentValues[0])) ||
                            (context.Value.InstanceValueGetter != null && (bool)context.Value.InstanceValueGetter(ref parentValue)) ||
                            (context.Value.StaticValidationCaller != null && context.Value.StaticValidationCaller());
                    }
                    catch (System.Exception ex)
                    {
                        Debug.LogException(ex);
                    }
                }

                if (context.Value.DrawMessageBox)
                {
                    switch (attribute.InfoMessageType)
                    {
                    case InfoMessageType.None:
                        SirenixEditorGUI.MessageBox(context.Value.MessageHelper.GetString(property));
                        break;

                    case InfoMessageType.Info:
                        SirenixEditorGUI.InfoMessageBox(context.Value.MessageHelper.GetString(property));
                        break;

                    case InfoMessageType.Warning:
                        SirenixEditorGUI.WarningMessageBox(context.Value.MessageHelper.GetString(property));
                        break;

                    case InfoMessageType.Error:
                        SirenixEditorGUI.ErrorMessageBox(context.Value.MessageHelper.GetString(property));
                        break;

                    default:
                        SirenixEditorGUI.ErrorMessageBox("Unknown InfoBoxType: " + attribute.InfoMessageType.ToString());
                        break;
                    }
                }
            }

            this.CallNextDrawer(property, label);
        }
示例#2
0
        /// <summary>
        /// Initialized the drawer.
        /// </summary>
        protected override void Initialize()
        {
            MemberInfo member;

            // Min member
            if (this.Attribute.MinMember != null)
            {
                this.getterMinValue = new InspectorPropertyValueGetter <T>(this.Property, this.Attribute.MinMember);
                this.errorMessage   = this.getterMinValue.ErrorMessage;
            }

            // Max member
            if (this.Attribute.MaxMember != null)
            {
                this.getterMaxValue = new InspectorPropertyValueGetter <T>(this.Property, this.Attribute.MaxMember);
                this.errorMessage   = this.getterMaxValue.ErrorMessage;
            }

            // Foreground color member.
            if (!this.Attribute.ColorMember.IsNullOrWhitespace())
            {
                if (MemberFinder.Start(this.Property.ParentType)
                    .IsNamed(this.Attribute.ColorMember)
                    .HasReturnType <Color>()
                    .TryGetMember(out member, out this.errorMessage))
                {
                    if (member is FieldInfo || member is PropertyInfo)
                    {
                        if (member.IsStatic())
                        {
                            this.staticColorGetter = DeepReflection.CreateValueGetter <Color>(this.Property.ParentType, this.Attribute.ColorMember);
                        }
                        else
                        {
                            this.instanceColorGetter = DeepReflection.CreateWeakInstanceValueGetter <Color>(this.Property.ParentType, this.Attribute.ColorMember);
                        }
                    }
                    else if (member is MethodInfo)
                    {
                        if (member.IsStatic())
                        {
                            this.errorMessage = "Static method members are currently not supported.";
                        }
                        else
                        {
                            var method = member as MethodInfo;
                            var p      = method.GetParameters();

                            if (p.Length == 0)
                            {
                                this.instanceColorMethod = EmitUtilities.CreateWeakInstanceMethodCallerFunc <Color>(method);
                            }
                            else if (p.Length == 1 && p[0].ParameterType == typeof(T))
                            {
                                this.instanceColorParameterMethod = EmitUtilities.CreateWeakInstanceMethodCallerFunc <T, Color>(method);
                            }
                        }
                    }
                    else
                    {
                        this.errorMessage = "Unsupported member type.";
                    }
                }
            }

            // Background color member.
            if (!this.Attribute.BackgroundColorMember.IsNullOrWhitespace())
            {
                if (MemberFinder.Start(this.Property.ParentType)
                    .IsNamed(this.Attribute.BackgroundColorMember)
                    .HasReturnType <Color>()
                    .TryGetMember(out member, out this.errorMessage))
                {
                    if (member is FieldInfo || member is PropertyInfo)
                    {
                        if (member.IsStatic())
                        {
                            this.staticBackgroundColorGetter = DeepReflection.CreateValueGetter <Color>(this.Property.ParentType, this.Attribute.BackgroundColorMember);
                        }
                        else
                        {
                            this.instanceBackgroundColorGetter = DeepReflection.CreateWeakInstanceValueGetter <Color>(this.Property.ParentType, this.Attribute.BackgroundColorMember);
                        }
                    }
                    else if (member is MethodInfo)
                    {
                        if (member.IsStatic())
                        {
                            this.errorMessage = "Static method members are currently not supported.";
                        }
                        else
                        {
                            var method = member as MethodInfo;
                            var p      = method.GetParameters();

                            if (p.Length == 0)
                            {
                                this.instanceBackgroundColorMethod = EmitUtilities.CreateWeakInstanceMethodCallerFunc <Color>(method);
                            }
                            else if (p.Length == 1 && p[0].ParameterType == typeof(T))
                            {
                                this.instanceBackgroundColorParameterMethod = EmitUtilities.CreateWeakInstanceMethodCallerFunc <T, Color>(method);
                            }
                        }
                    }
                    else
                    {
                        this.errorMessage = "Unsupported member type.";
                    }
                }
            }

            // Custom value string getter
            if (this.Attribute.CustomValueStringMember != null && this.Attribute.CustomValueStringMember.Length > 0)
            {
                if (MemberFinder.Start(this.Property.ParentType)
                    .IsNamed(this.Attribute.CustomValueStringMember)
                    .HasReturnType <string>()
                    .TryGetMember(out member, out this.errorMessage))
                {
                    if (member is FieldInfo || member is PropertyInfo)
                    {
                        if (member.IsStatic())
                        {
                            this.staticGetValueLabel = DeepReflection.CreateValueGetter <string>(this.Property.ParentType, this.Attribute.CustomValueStringMember);
                        }
                        else
                        {
                            this.instanceGetValueLabel = DeepReflection.CreateWeakInstanceValueGetter <string>(this.Property.ParentType, this.Attribute.CustomValueStringMember);
                        }
                    }
                    else if (member is MethodInfo)
                    {
                        var method     = member as MethodInfo;
                        var parameters = method.GetParameters();

                        if (parameters.Length == 0)
                        {
                            string name = this.Attribute.CustomValueStringMember + "()";
                            if (member.IsStatic())
                            {
                                this.staticGetValueLabel = DeepReflection.CreateValueGetter <string>(this.Property.ParentType, name);
                            }
                            else
                            {
                                this.instanceGetValueLabel = DeepReflection.CreateWeakInstanceValueGetter <string>(this.Property.ParentType, name);
                            }
                        }
                        else if (parameters.Length == 1 && parameters[0].ParameterType == typeof(T))
                        {
                            if (member.IsStatic())
                            {
                                // TODO: This should be emitted.
                                this.staticGetValueLabelValue = (v) => (string)method.Invoke(null, new object[] { v });
                            }
                            else
                            {
                                // TODO: This should be emitted.
                                this.instanceGetValueLabelValue = (o, v) => (string)method.Invoke(o, new object[] { v });
                            }
                        }
                        else
                        {
                            this.errorMessage = "Was unable to find any string field or property or string method with no parameters or exactly one " + typeof(T).GetNiceName() + " parameter named " + this.Attribute.CustomValueStringMember;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        protected override void Initialize()
        {
            this.hideDetailedMessage = true;
            this.messageHelper       = new StringMemberHelper(this.Property, this.Attribute.Message);
            this.detailsHelper       = new StringMemberHelper(this.Property, this.Attribute.Details);
            this.errorMessage        = this.messageHelper.ErrorMessage ?? this.detailsHelper.ErrorMessage;

            if (this.Attribute.VisibleIf != null)
            {
                MemberInfo memberInfo;

                // Parameter functions
                if (this.Property.ValueEntry != null && this.Property.ParentType.FindMember()
                    .IsMethod()
                    .HasReturnType <bool>()
                    .HasParameters(this.Property.ValueEntry.BaseValueType)
                    .IsNamed(this.Attribute.VisibleIf)
                    .TryGetMember(out memberInfo, out this.errorMessage))
                {
                    if (this.errorMessage == null)
                    {
                        if (memberInfo is MethodInfo)
                        {
                            var method = memberInfo as MethodInfo;

                            if (memberInfo.IsStatic())
                            {
                                // TODO: Emit dis.
                                this.staticValidationParameterMethodCaller = p => (bool)method.Invoke(null, new object[] { p });
                            }
                            else
                            {
                                // TODO: Emit dis.
                                this.instanceValidationParameterMethodCaller = (i, p) => (bool)method.Invoke(i, new object[] { p });
                            }
                        }
                        else
                        {
                            this.errorMessage = "Invalid member type!";
                        }
                    }
                }

                // Fields, properties, and no-parameter functions.
                else if (this.Property.ParentType.FindMember()
                         .HasReturnType <bool>()
                         .HasNoParameters()
                         .IsNamed(this.Attribute.VisibleIf)
                         .TryGetMember(out memberInfo, out this.errorMessage))
                {
                    if (this.errorMessage == null)
                    {
                        if (memberInfo is FieldInfo)
                        {
                            if (memberInfo.IsStatic())
                            {
                                this.staticValidationCaller = EmitUtilities.CreateStaticFieldGetter <bool>(memberInfo as FieldInfo);
                            }
                            else
                            {
                                this.instanceValueGetter = EmitUtilities.CreateWeakInstanceFieldGetter(this.Property.ParentType, memberInfo as FieldInfo);
                            }
                        }
                        else if (memberInfo is PropertyInfo)
                        {
                            if (memberInfo.IsStatic())
                            {
                                this.staticValidationCaller = EmitUtilities.CreateStaticPropertyGetter <bool>(memberInfo as PropertyInfo);
                            }
                            else
                            {
                                this.instanceValueGetter = EmitUtilities.CreateWeakInstancePropertyGetter(this.Property.ParentType, memberInfo as PropertyInfo);
                            }
                        }
                        else if (memberInfo is MethodInfo)
                        {
                            if (memberInfo.IsStatic())
                            {
                                this.staticValidationCaller = (Func <bool>)Delegate.CreateDelegate(typeof(Func <bool>), memberInfo as MethodInfo);
                            }
                            else
                            {
                                this.instanceValidationMethodCaller = EmitUtilities.CreateWeakInstanceMethodCallerFunc <bool>(memberInfo as MethodInfo);
                            }
                        }
                        else
                        {
                            this.errorMessage = "Invalid member type!";
                        }
                    }
                }
            }
        }
示例#4
0
        /// <summary>
        /// Draws the property.
        /// </summary>
        protected override void DrawPropertyLayout(IPropertyValueEntry <T> entry, DetailedInfoBoxAttribute attribute, GUIContent label)
        {
            PropertyContext <InfoBoxContext> context = entry.Property.Context.Get(this, "Context", (InfoBoxContext)null);

            if (context.Value == null)
            {
                context.Value = new InfoBoxContext()
                {
                    MessageHelper = new StringMemberHelper(entry.ParentType, attribute.Message),
                    DetailsHelper = new StringMemberHelper(entry.ParentType, attribute.Details)
                };

                context.Value.ErrorMessage = context.Value.MessageHelper.ErrorMessage ?? context.Value.DetailsHelper.ErrorMessage;

                if (attribute.VisibleIf != null)
                {
                    MemberInfo memberInfo;

                    // Parameter functions
                    if (entry.ParentType.FindMember()
                        .IsMethod()
                        .HasReturnType <bool>()
                        .HasParameters <T>()
                        .IsNamed(attribute.VisibleIf)
                        .TryGetMember(out memberInfo, out context.Value.ErrorMessage))
                    {
                        if (context.Value.ErrorMessage == null)
                        {
                            if (memberInfo is MethodInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationParameterMethodCaller = (Func <T, bool>)Delegate.CreateDelegate(typeof(Func <T, bool>), memberInfo as MethodInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValidationParameterMethodCaller = EmitUtilities.CreateWeakInstanceMethodCaller <bool, T>(memberInfo as MethodInfo);
                                }
                            }
                            else
                            {
                                context.Value.ErrorMessage = "Invalid member type!";
                            }
                        }
                    }
                    // Fields, properties, and no-parameter functions.
                    else if (entry.ParentType.FindMember()
                             .HasReturnType <bool>()
                             .HasNoParameters()
                             .IsNamed(attribute.VisibleIf)
                             .TryGetMember(out memberInfo, out context.Value.ErrorMessage))
                    {
                        if (context.Value.ErrorMessage == null)
                        {
                            if (memberInfo is FieldInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationCaller = EmitUtilities.CreateStaticFieldGetter <bool>(memberInfo as FieldInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValueGetter = EmitUtilities.CreateWeakInstanceFieldGetter(entry.ParentType, memberInfo as FieldInfo);
                                }
                            }
                            else if (memberInfo is PropertyInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationCaller = EmitUtilities.CreateStaticPropertyGetter <bool>(memberInfo as PropertyInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValueGetter = EmitUtilities.CreateWeakInstancePropertyGetter(entry.ParentType, memberInfo as PropertyInfo);
                                }
                            }
                            else if (memberInfo is MethodInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationCaller = (Func <bool>)Delegate.CreateDelegate(typeof(Func <bool>), memberInfo as MethodInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValidationMethodCaller = EmitUtilities.CreateWeakInstanceMethodCallerFunc <bool>(memberInfo as MethodInfo);
                                }
                            }
                            else
                            {
                                context.Value.ErrorMessage = "Invalid member type!";
                            }
                        }
                    }
                }
            }

            if (context.Value.ErrorMessage != null)
            {
                AllEditorGUI.ErrorMessageBox(context.Value.ErrorMessage);
            }
            else
            {
                if (Event.current.type == EventType.Layout)
                {
                    var parentValue = entry.Property.ParentValues[0];

                    try
                    {
                        if (entry.Property.ValueEntry != null)
                        {
                            context.Value.DrawMessageBox =
                                attribute.VisibleIf == null ||
                                (context.Value.StaticValidationParameterMethodCaller != null && context.Value.StaticValidationParameterMethodCaller(entry.SmartValue)) ||
                                (context.Value.InstanceValidationParameterMethodCaller != null && context.Value.InstanceValidationParameterMethodCaller(entry.Property.ParentValues[0], entry.SmartValue)) ||
                                (context.Value.InstanceValidationMethodCaller != null && context.Value.InstanceValidationMethodCaller(entry.Property.ParentValues[0])) ||
                                (context.Value.InstanceValueGetter != null && (bool)context.Value.InstanceValueGetter(ref parentValue)) ||
                                (context.Value.StaticValidationCaller != null && context.Value.StaticValidationCaller());
                        }
                    }
                    catch (System.Exception ex)
                    {
                        Debug.LogException(ex);
                    }
                }

                if (context.Value.DrawMessageBox)
                {
                    var foldedConfig = entry.Property.Context.GetPersistent <bool>(this, "InfoBoxExpanded", true);

                    switch (attribute.InfoMessageType)
                    {
                    case InfoMessageType.None:
                        foldedConfig.Value = AllEditorGUI.DetailedMessageBox(context.Value.MessageHelper.GetString(entry), context.Value.DetailsHelper.GetString(entry), UnityEditor.MessageType.None, foldedConfig.Value);
                        break;

                    case InfoMessageType.Info:
                        foldedConfig.Value = AllEditorGUI.DetailedMessageBox(context.Value.MessageHelper.GetString(entry), context.Value.DetailsHelper.GetString(entry), UnityEditor.MessageType.Info, foldedConfig.Value);
                        break;

                    case InfoMessageType.Warning:
                        foldedConfig.Value = AllEditorGUI.DetailedMessageBox(context.Value.MessageHelper.GetString(entry), context.Value.DetailsHelper.GetString(entry), UnityEditor.MessageType.Warning, foldedConfig.Value);
                        break;

                    case InfoMessageType.Error:
                        foldedConfig.Value = AllEditorGUI.DetailedMessageBox(context.Value.MessageHelper.GetString(entry), context.Value.DetailsHelper.GetString(entry), UnityEditor.MessageType.Error, foldedConfig.Value);
                        break;

                    default:
                        AllEditorGUI.ErrorMessageBox("Unknown InfoBoxType: " + attribute.InfoMessageType.ToString());
                        break;
                    }
                }
            }

            this.CallNextDrawer(entry.Property, label);
        }
示例#5
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);
        }
示例#6
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);
        }