protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            if (!IsNumber(property))
            {
                string message = string.Format("Field {0} is not a number", property.name);
                DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
                return;
            }

            ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property);
            var value          = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
            var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : string.Format("{0:0.00}", value);
            var fillPercentage = value / progressBarAttribute.MaxValue;
            var barLabel       = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + progressBarAttribute.MaxValue;
            var barColor       = progressBarAttribute.Color.GetColor();
            var labelColor     = Color.white;

            var  indentLength = NaughtyEditorGUI.GetIndentLength(rect);
            Rect barRect      = new Rect()
            {
                x      = rect.x + indentLength,
                y      = rect.y,
                width  = rect.width - indentLength,
                height = EditorGUIUtility.singleLineHeight
            };

            DrawBar(barRect, Mathf.Clamp01(fillPercentage), barLabel, barColor, labelColor);
        }
        private object GetMaxValue(SerializedProperty property, ProgressBarAttribute progressBarAttribute)
        {
            if (string.IsNullOrEmpty(progressBarAttribute.MaxValueName))
            {
                return(progressBarAttribute.MaxValue);
            }
            else
            {
                object target = PropertyUtility.GetTargetObjectWithProperty(property);

                FieldInfo valuesFieldInfo = ReflectionUtility.GetField(target, progressBarAttribute.MaxValueName);
                if (valuesFieldInfo != null)
                {
                    return(valuesFieldInfo.GetValue(target));
                }

                PropertyInfo valuesPropertyInfo = ReflectionUtility.GetProperty(target, progressBarAttribute.MaxValueName);
                if (valuesPropertyInfo != null)
                {
                    return(valuesPropertyInfo.GetValue(target));
                }

                MethodInfo methodValuesInfo = ReflectionUtility.GetMethod(target, progressBarAttribute.MaxValueName);
                if (methodValuesInfo != null &&
                    methodValuesInfo.ReturnType == typeof(float) &&
                    methodValuesInfo.GetParameters().Length == 0)
                {
                    return(methodValuesInfo.Invoke(target, null));
                }

                return(null);
            }
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            progressBarAttribute = (attribute as ProgressBarAttribute);
            switch (property.propertyType)
            {
            case SerializedPropertyType.Integer:
                property.intValue = (int)EditorGUIExtension.ProgressBar(position, property.intValue,
                                                                        progressBarAttribute.min,
                                                                        progressBarAttribute.max,
                                                                        string.Concat(property.intValue.ToString(), "(", property.displayName, ")")
                                                                        , true, progressBarAttribute.drawMinMaxValue);
                break;

            case SerializedPropertyType.Float:
                property.floatValue = EditorGUIExtension.ProgressBar(position, property.floatValue,
                                                                     progressBarAttribute.min,
                                                                     progressBarAttribute.max,
                                                                     string.Concat(property.floatValue.ToString("0.00"), "(", "property.displayName", ")"),
                                                                     true, progressBarAttribute.drawMinMaxValue);
                break;

            default:
                break;
            }
        }
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            if (property.propertyType != SerializedPropertyType.Float && property.propertyType != SerializedPropertyType.Integer)
            {
                EditorGUILayout.HelpBox("Field " + property.name + " is not a number", MessageType.Warning);
                return;
            }

            var value          = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
            var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : String.Format("{0:0.00}", value);

            ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property);
            var   position    = EditorGUILayout.GetControlRect();
            var   maxValue    = progressBarAttribute.MaxValue;
            float lineHight   = EditorGUIUtility.singleLineHeight;
            float padding     = EditorGUIUtility.standardVerticalSpacing;
            var   barPosition = new Rect(position.position.x, position.position.y, position.size.x, lineHight);

            var fillPercentage = value / maxValue;
            var barLabel       = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + maxValue;

            var color  = GetColor(progressBarAttribute.Color);
            var color2 = Color.white;

            DrawBar(barPosition, Mathf.Clamp01(fillPercentage), barLabel, color, color2);
        }
        // Make your own IMGUI based GUI for the property
        public override void OnGUI(Rect _position, SerializedProperty _property, GUIContent _label)
        {
            ProgressBarAttribute _attribute = (ProgressBarAttribute)attribute;

            // Set label if needed
            if (!string.IsNullOrEmpty(_attribute.Label))
            {
                _label.text = _attribute.Label;
            }

            // Draw editable or readonly progress bar
            if (_attribute.IsEditable)
            {
                if (string.IsNullOrEmpty(_attribute.MaxValueVariableName))
                {
                    EditorGUIEnhanced.EditableProgressBar(_position, _label, _property, _attribute.MaxValue, _attribute.Color.GetColor());
                }
                else
                {
                    EditorGUIEnhanced.EditableProgressBar(_position, _label, _property, _attribute.MaxValueVariableName, _attribute.Color.GetColor());
                }
            }
            else
            {
                if (string.IsNullOrEmpty(_attribute.MaxValueVariableName))
                {
                    EditorGUIEnhanced.ProgressBar(_position, _label, _property, _attribute.MaxValue, _attribute.Color.GetColor());
                }
                else
                {
                    EditorGUIEnhanced.ProgressBar(_position, _label, _property, _attribute.MaxValueVariableName, _attribute.Color.GetColor());
                }
            }
        }
Example #6
0
        /// <summary>
        /// Draws the property.
        /// </summary>
        protected override void DrawPropertyLayout(IPropertyValueEntry <T> entry, ProgressBarAttribute attribute, GUIContent label)
        {
            ProgressBarContext <T> context = GetContext(this, entry, attribute);

            // Display evt. error
            if (context.ErrorMessage != null)
            {
                SirenixEditorGUI.ErrorMessageBox(context.ErrorMessage);
            }

            ProgressBarConfig config = context.GetConfig(entry, attribute);

            // Construct a Rect based on the configured height of the field.
            Rect rect = EditorGUILayout.GetControlRect(label != null, config.Height < EditorGUIUtility.singleLineHeight ? EditorGUIUtility.singleLineHeight : config.Height);

            // Draw the field.
            EditorGUI.BeginChangeCheck();
            T value = this.DrawProgressBar(rect, label, entry.SmartValue, attribute, config, context.CustomValueLabelGetter != null ? context.CustomValueLabelGetter.GetString(entry) : null);

            // Apply evt. changes
            if (EditorGUI.EndChangeCheck())
            {
                entry.SmartValue = value;
            }
        }
        protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
        {
            ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property);
            var maxValue = GetMaxValue(property, progressBarAttribute);

            return(IsNumber(property) && maxValue is float
                   ?GetPropertyHeight(property)
                       : GetPropertyHeight(property) + GetHelpBoxHeight());
        }
Example #8
0
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(rect, label, property);

            if (!IsNumber(property))
            {
                string message = string.Format("Field {0} is not a number", property.name);
                DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
                return;
            }

            if (_cachedProgressBarAttribute == null)
            {
                _cachedProgressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property);
            }

            ProgressBarAttribute progressBarAttribute = _cachedProgressBarAttribute;
            var value          = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
            var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : string.Format("{0:0.00}", value);
            var maxValue       = GetMaxValue(property, progressBarAttribute);

            if (maxValue != null && IsNumber(maxValue))
            {
                var fillPercentage = value / CastToFloat(maxValue);
                var barLabel       = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + maxValue;
                var barColor       = progressBarAttribute.Color.GetColor();
                var labelColor     = Color.white;

                var  indentLength = NaughtyEditorGUI.GetIndentLength(rect);
                Rect barRect      = new Rect()
                {
                    x      = rect.x + indentLength,
                    y      = rect.y,
                    width  = rect.width - indentLength,
                    height = EditorGUIUtility.singleLineHeight
                };

                DrawBar(barRect, Mathf.Clamp01(fillPercentage), barLabel, barColor, labelColor);
            }
            else
            {
                string message = string.Format(
                    "The provided dynamic max value for the progress bar is not correct. Please check if the '{0}' is correct, or the return type is float/int",
                    nameof(progressBarAttribute.MaxValueName));

                DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
            }

            EditorGUI.EndProperty();
        }
        private void DrawBar(Rect position, float fillPercent, string label, Color barColor, Color labelColor,
                             ProgressBarAttribute attribute)
        {
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            var fillRect = new Rect(position.x, position.y, position.width * fillPercent, position.height);

            EditorGUI.DrawRect(position, new Color(0.13f, 0.13f, 0.13f));
            EditorGUI.DrawRect(fillRect, barColor);

            if (attribute.Option.Contains(FieldOption.HideLabel))
            {
                return;
            }

            // set alignment and cache the default
            var align = GUI.skin.label.alignment;

            GUI.skin.label.alignment = TextAnchor.UpperCenter;

            // set the color and cache the default
            var c = GUI.contentColor;

            GUI.contentColor = labelColor;

            // calculate the position
            var labelRect = new Rect(position.x, position.y - 2, position.width, position.height);

            // draw~
            if (attribute.Option.Contains(FieldOption.BoldLabel))
            {
                EditorGUI.DropShadowLabel(labelRect, label, EditorStyles.boldLabel);
            }
            else
            {
                EditorGUI.DropShadowLabel(labelRect, label);
            }

            // reset color and alignment
            GUI.contentColor         = c;
            GUI.skin.label.alignment = align;
        }
            public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
            {
                ProgressBarAttribute progressBar = (ProgressBarAttribute)attribute;

                float progress = 0;

                if (property.propertyType == SerializedPropertyType.Integer)
                {
                    progress = Mathf.Clamp01(((float)property.intValue - progressBar.leftBound) / progressBar.rightBound);
                }
                else if (property.propertyType == SerializedPropertyType.Float)
                {
                    progress = Mathf.Clamp01((property.floatValue - progressBar.leftBound) / progressBar.rightBound);
                }
                else
                {
                    GUI.Label(position, "Use ProgressBar with int and float values only.");
                    return;
                }

                EditorGUI.ProgressBar(position, progress, property.displayName);
            }
Example #11
0
        public ProgressBarConfig GetConfig(IPropertyValueEntry <T> entry, ProgressBarAttribute attribute)
        {
            var config = ProgressBarConfig.Default;

            config.Height              = attribute.Height;
            config.DrawValueLabel      = attribute.DrawValueLabelHasValue ? attribute.DrawValueLabel : (attribute.Segmented ? false : true);
            config.ValueLabelAlignment = attribute.ValueLabelAlignmentHasValue ? attribute.ValueLabelAlignment : (attribute.Segmented ? TextAlignment.Right : TextAlignment.Center);

            if (attribute.CustomValueStringMember != null)
            {
                // Do not draw default label.
                config.DrawValueLabel = false;
            }

            // No point in updating the color in non-repaint events.
            if (Event.current.type == EventType.Repaint)
            {
                var parent = entry.Property.FindParent(PropertyValueCategory.Member, true).ParentValues[0];

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

                config.BackgroundColor =
                    this.StaticBackgroundColorGetter != null?this.StaticBackgroundColorGetter() :
                        this.InstanceBackgroundColorGetter != null?this.InstanceBackgroundColorGetter(parent) :
                            this.InstanceBackgroundColorMethod != null?this.InstanceBackgroundColorMethod(parent) :
                                this.InstanceBackgroundColorParameterMethod != null?this.InstanceBackgroundColorParameterMethod(parent, entry.SmartValue) :
                                    config.BackgroundColor; // Use default if no other option available.
            }

            return(config);
        }
        /***************************
         *******   METHODS   *******
         **************************/

        // Specify how tall the GUI for this decorator is in pixels
        public override float GetPropertyHeight(SerializedProperty _property, GUIContent _label)
        {
            ProgressBarAttribute _attribute = (ProgressBarAttribute)attribute;

            return(_attribute.Height);
        }
 public override void SetUp()
 {
     AssignTestObject <ProgressBarExample>();
     progressBar = new ProgressBarAttribute();
 }
Example #14
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);
        }
Example #15
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);
        }
Example #16
0
 /// <summary>
 /// Draws a progress bar for a decimal property.
 /// </summary>
 protected override decimal DrawProgressBar(Rect rect, GUIContent label, decimal value, ProgressBarAttribute attribute, ProgressBarConfig config, string valueLabel)
 {
     if (attribute.Segmented)
     {
         return((decimal)SirenixEditorFields.SegmentedProgressBarField(rect, label, (long)value, (long)attribute.Min, (long)attribute.Max, config, valueLabel));
     }
     else
     {
         return((decimal)SirenixEditorFields.ProgressBarField(rect, label, (double)value, attribute.Min, attribute.Max, config, valueLabel));
     }
 }
Example #17
0
 /// <summary>
 /// Generic implementation of progress bar field drawing.
 /// </summary>
 protected abstract T DrawProgressBar(Rect rect, GUIContent label, T value, ProgressBarAttribute attribute, ProgressBarConfig config, string valueLabel);
Example #18
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            if (property.propertyType != SerializedPropertyType.Float && property.propertyType != SerializedPropertyType.Integer)
            {
                EditorGUILayout.HelpBox("Field " + property.name + " is not a number", MessageType.Warning);
                return;
            }

            var value          = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
            var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : String.Format("{0:0.##}", value);

            ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property);
            var position = EditorGUILayout.GetControlRect();
            var maxValue = progressBarAttribute.MaxValue;

            float lineHight   = EditorGUIUtility.singleLineHeight;
            float padding     = EditorGUIUtility.standardVerticalSpacing;
            var   barPosition = new Rect(position.position.x, position.position.y, position.size.x, lineHight);

            UnityEngine.Object target = PropertyUtility.GetTargetObject(property);

            // maxValueVar - if found so override maxValue
            var maxValueVar = progressBarAttribute.maxValueVar;

            if (maxValueVar.Trim().Length > 0)
            {
                // try to get field first
                FieldInfo maxValueFieldInfo = ReflectionUtility.GetField(target, maxValueVar);
                if (maxValueFieldInfo != null)
                {
                    if (maxValueFieldInfo.FieldType == typeof(int))
                    {
                        maxValue = (int)maxValueFieldInfo.GetValue(target);
                    }

                    if (maxValueFieldInfo.FieldType == typeof(float))
                    {
                        maxValue = (float)maxValueFieldInfo.GetValue(target);
                    }
                }
                else
                {
                    // if not get the property
                    PropertyInfo maxValuePropertyInfo = ReflectionUtility.GetProperty(target, maxValueVar);
                    if (maxValuePropertyInfo != null)
                    {
                        if (maxValuePropertyInfo.PropertyType == typeof(int))
                        {
                            maxValue = (int)maxValuePropertyInfo.GetValue(target);
                        }

                        if (maxValuePropertyInfo.PropertyType == typeof(float))
                        {
                            maxValue = (float)maxValuePropertyInfo.GetValue(target);
                        }
                    }
                }
            }

            var fillPercentage = value / maxValue;
            var barLabel       = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + maxValue;

            var color  = GetColor(progressBarAttribute.Color);
            var color2 = Color.white;

            DrawBar(barPosition, Mathf.Clamp01(fillPercentage), barLabel, color, color2);
        }
 public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
 {
     progressBarAttribute = (attribute as ProgressBarAttribute);
     return(progressBarAttribute.height);
 }