public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (_attribute == null)
            {
                Initialize(property);
            }
            if (_values.IsNullOrEmpty() || _selectedValueIndex < 0)
            {
                EditorGUI.PropertyField(position, property, label);
                return;
            }

            if (!_valueFound && _selectedValueIndex == 0)
            {
                MyGUI.DrawColouredRect(position, MyGUI.Colors.Yellow);
            }

            EditorGUI.BeginChangeCheck();
            _selectedValueIndex = EditorGUI.Popup(position, label.text, _selectedValueIndex, _names);
            if (EditorGUI.EndChangeCheck())
            {
                fieldInfo.SetValue(property.serializedObject.targetObject, _values[_selectedValueIndex]);
                property.serializedObject.ApplyModifiedProperties();
                EditorUtility.SetDirty(property.serializedObject.targetObject);
            }
        }
Пример #2
0
        private void DrawPresetColourLine(Rect rect, Preset preset)
        {
            var cRect = new Rect(rect);

            cRect.width   = 6;
            cRect.height -= 2;

            Color color = MyGUI.Colors.Brown;

            if (preset == null)
            {
                color = Color.red;
            }
            else
            {
                var presetType = preset.GetTargetTypeName();
                if (presetType.Contains("Texture"))
                {
                    color = MyGUI.Colors.Blue;
                }
                else if (presetType.Contains("Audio"))
                {
                    color = MyGUI.Colors.Red;
                }
            }

            MyGUI.DrawColouredRect(cRect, color);
            EditorGUI.LabelField(cRect, GUIContent.none);
        }
Пример #3
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (!property.IsNumerical())
            {
                MyGUI.DrawColouredRect(position, MyGUI.Red);
                EditorGUI.LabelField(position, new GUIContent("", "[PositiveValueOnly] used with non-numeric property"));
            }
            else
            {
                if (HandleNegativeValues(property))
                {
                    property.serializedObject.ApplyModifiedProperties();
                }
            }

            EditorGUI.PropertyField(position, property, label, true);
        }
Пример #4
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.propertyType != SerializedPropertyType.String)
            {
                MyGUI.DrawColouredRect(position, MyGUI.Colors.Red);
                EditorGUI.LabelField(position, new GUIContent("", "[CharacterRangeAttribute] used with non-string property"));
            }
            else
            {
                var charactersRange = (CharactersRangeAttribute)attribute;
                var mode            = charactersRange.Mode;
                var ignoreCase      = charactersRange.IgnoreCase;
                var filter          = charactersRange.Characters;
                var allow           = mode == CharacterRangeMode.Allow || mode == CharacterRangeMode.WarningIfNotMatch;
                var warning         = mode == CharacterRangeMode.WarningIfAny || mode == CharacterRangeMode.WarningIfNotMatch;

                if (ignoreCase)
                {
                    filter = filter.ToUpper();
                }
                var filteredCharacters = property.stringValue.Distinct()
                                         .Where(c =>
                {
                    if (ignoreCase)
                    {
                        c = char.ToUpper(c);
                    }
                    return(filter.Contains(c) ^ allow);
                });

                DrawWarning();
                position.width -= 20;

                property.stringValue = EditorGUI.TextField(position, label, property.stringValue);
                DrawTooltip();

                if (!warning)
                {
                    property.stringValue = filteredCharacters.Aggregate(
                        property.stringValue,
                        (p, c) => p.Replace(c.ToString(), ""));
                }

                property.serializedObject.ApplyModifiedProperties();


                void DrawWarning()
                {
                    if (!warning)
                    {
                        return;
                    }

                    bool ifMatch    = mode == CharacterRangeMode.WarningIfAny;
                    bool ifNotMatch = mode == CharacterRangeMode.WarningIfNotMatch;

                    bool anyFiltered = filteredCharacters.Any();
                    bool warn        = (ifMatch && anyFiltered || ifNotMatch && anyFiltered);

                    if (property.stringValue.Length == 0)
                    {
                        warn = false;
                    }

                    MyGUI.DrawColouredRect(position, warn ? MyGUI.Colors.Yellow : Color.clear);
                }

                void DrawTooltip()
                {
                    string tooltip = "Characters range ";

                    if (mode == CharacterRangeMode.Allow || mode == CharacterRangeMode.WarningIfNotMatch)
                    {
                        tooltip += "is allowed:";
                    }
                    else
                    {
                        tooltip += "not allowed:";
                    }
                    tooltip += $"\n[{filter}]";

                    position.x    += position.width + 2;
                    position.width = 18;
                    var tooltipContent = new GUIContent(MyGUI.EditorIcons.Help);

                    tooltipContent.tooltip = tooltip;
                    EditorGUI.LabelField(position, tooltipContent, EditorStyles.label);
                }
            }
        }
Пример #5
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.propertyType != SerializedPropertyType.String)
            {
                MyGUI.DrawColouredRect(position, MyGUI.Colors.Red);
                EditorGUI.LabelField(position, new GUIContent("", "[RegexStringAttribute] used with non-string property"));
            }
            else
            {
                var  regex            = (RegexStringAttribute)attribute;
                var  mode             = regex.AttributeMode;
                bool ifMatch          = mode == RegexStringMode.WarningIfMatch;
                bool ifNotMatch       = mode == RegexStringMode.WarningIfNotMatch;
                bool anyMatching      = regex.Regex.IsMatch(property.stringValue);
                bool warn             = (ifMatch && anyMatching) || (ifNotMatch && !anyMatching);
                var  originalPosition = position;

                DrawWarning();
                position.width -= 20;
                EditorGUI.PropertyField(position, property, label, true);
                DrawTooltip();

                if (GUI.changed)
                {
                    if (mode == RegexStringMode.Replace)
                    {
                        OnReplace();
                    }
                    if (mode == RegexStringMode.Match)
                    {
                        OnKeepMatching();
                    }
                }

                if (warn)
                {
                    GUILayoutUtility.GetRect(GUIContent.none, EditorStyles.objectField);
                    position    = originalPosition;
                    position.y += EditorGUIUtility.singleLineHeight;
                    DrawWarning();
                    position.x += EditorGUIUtility.labelWidth;
                    var warningContent = new GUIContent("Regex rule violated!");
                    EditorGUI.LabelField(position, warningContent, EditorStyles.miniBoldLabel);
                }

                property.serializedObject.ApplyModifiedProperties();


                void OnReplace() => property.stringValue = regex.Regex.Replace(property.stringValue, "");
                void OnKeepMatching() => property.stringValue = regex.Regex.KeepMatching(property.stringValue);

                void DrawWarning()
                {
                    if (!ifMatch && !ifNotMatch)
                    {
                        return;
                    }
                    MyGUI.DrawColouredRect(position, warn ? MyGUI.Colors.Yellow : Color.clear);
                }

                void DrawTooltip()
                {
                    string tooltip = "Regex field: ";

                    if (mode == RegexStringMode.Match || mode == RegexStringMode.WarningIfNotMatch)
                    {
                        tooltip += "match expression";
                    }
                    else
                    {
                        tooltip += "remove expression";
                    }
                    tooltip += $"\n[{regex.Regex}]";

                    position.x    += position.width + 2;
                    position.width = 18;
                    var tooltipContent = new GUIContent(MyGUI.EditorIcons.Help);

                    tooltipContent.tooltip = tooltip;
                    EditorGUI.LabelField(position, tooltipContent, EditorStyles.label);
                }
            }
        }