Exemple #1
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        ClampAttribute clamp = (ClampAttribute)base.attribute;

        if (property.propertyType == SerializedPropertyType.Float)
        {
            EditorGUI.BeginChangeCheck();
            float result = EditorGUI.FloatField(position, label, property.floatValue);
            if (EditorGUI.EndChangeCheck())
            {
                property.floatValue = Mathf.Clamp(result, clamp.mMin, clamp.mMax);
            }
        }
        else if (property.propertyType == SerializedPropertyType.Integer)
        {
            EditorGUI.BeginChangeCheck();
            int result = EditorGUI.IntField(position, label, property.intValue);
            if (EditorGUI.EndChangeCheck())
            {
                property.intValue = Mathf.Clamp(result, (int)clamp.mMin, (int)clamp.mMax);
            }
        }
        else
        {
            EditorGUI.LabelField(position, label.text, "Use Clamp with float or int.");
        }
    }
Exemple #2
0
        public string ViewOption(PropertyInfo option, bool allowTooltips = true)
        {
            var            result    = new StringBuilder();
            ClampAttribute clamp     = option.GetCustomAttribute <ClampAttribute>();
            string         id        = option.GetCustomAttribute <IdAttribute>()?.Id ?? option.Name.ToLower();
            string         title     = option.GetCustomAttribute <TitleAttribute>()?.Name ?? option.Name;
            string         summary   = option.GetCustomAttribute <DescriptionAttribute>()?.Content;
            string         value     = WriteOptionValue(option.GetValue(this, null));
            string         valueType = WriteOptionType(option);

            Type type = option.GetValue(this, null)?.GetType();

            if (allowTooltips)
            {
                string        tipBase  = $"config {id}";
                List <string> tooltips = option.GetCustomAttribute <TooltipAttribute>()?.Tips.ToList() ?? new List <string>();

                bool useFlags = type?.GetCustomAttribute <FlagsAttribute>() != null;

                if (clamp != null && type != null && type == typeof(int))
                {
                    if (clamp.HasMin)
                    {
                        tooltips.Add($"Type `{tipBase} --min` to set the lowest possible value.");
                    }

                    tooltips.Add($"Type `{tipBase} --max` to set the highest possible value.");
                }

                if (type != null && type.IsEnum)
                {
                    if (useFlags)
                    {
                        tooltips.Add($"Type `{tipBase} --clear` to clear all specified values.");
                        tooltips.Add($"Type `{tipBase} --all` to set all possible values.");
                        tooltips.Add($"You can chain values together by typing the sum of each flag you want to toggle.");
                        tooltips.Add($"Specifying an active flag will disable it.");
                    }
                }

                tooltips.Add($"Type `{tipBase} --default` to revert to its default value.");
                tooltips.Add($"Type `{tipBase} <value>` to update its current value.");

                result.AppendLine(Format.Tooltip(tooltips));
                result.AppendLine();
            }

            result.AppendLine($"> **{title}**");

            if (!string.IsNullOrWhiteSpace(summary))
            {
                result.AppendLine($"> {summary}");
            }

            result.AppendLine($"> Current Value: `{value}`");
            result.AppendLine($"> Value Type: `{valueType}`");

            if (clamp != null)
            {
                if (!clamp.HasMin)
                {
                    if (type == typeof(string))
                    {
                        result.AppendLine($"> Max Allowed Length: `{clamp.Max}`");
                    }
                    else
                    {
                        result.AppendLine($"> Limit: `{clamp.Max}`");
                    }
                }
                else
                {
                    result.AppendLine($"> Value Range: `{clamp.Min} to {clamp.Max}`");
                }
            }

            if (type != null && type.IsEnum)
            {
                List <string> names  = type.GetEnumNames().ToList();
                List <long>   values = type.GetEnumValues().Cast <object>().Select(Convert.ToInt64).ToList();

                IEnumerable <string> groups = names.Join(values,
                                                         a => names.IndexOf(a),
                                                         b => values.IndexOf(b),
                                                         (a, b) => $"{a} = {b}");

                if (groups.Any())
                {
                    result.AppendLine($"\n> **Values**\n```cs");
                    result.AppendJoin(",\n", groups);
                    result.Append("```");
                }
            }

            if (allowTooltips)
            {
                IEnumerable <string> details = option.GetCustomAttribute <DetailsAttribute>()?.Details;

                if (details?.Any() ?? false)
                {
                    result.AppendLine($"\n> 🛠️ **Details**");
                    result.AppendJoin("\n", details.Select(x => $"• {x}"));
                }
            }

            return(result.ToString());
        }