コード例 #1
0
        private void ReBindValidatedProperty(IValidated property, string propertyName)
        {
            if (property == null || property.Validator == null)
            {
                return;
            }

            var validator = property.Validator;

            Validator oldValidator;

            if (_subscribedPropertyValidators.TryGetValue(propertyName, out oldValidator) &&
                validator == oldValidator)
            {
                return;
            }

            if (oldValidator != null)
            {
                oldValidator.PropertyChanged -= OnPropertyValidatorValidityChanged;
            }

            validator.PropertyChanged += OnPropertyValidatorValidityChanged;
            _subscribedPropertyValidators[propertyName] = validator;

            validator.ValidateAll(); // enforce validation on property to notify parent model
        }
コード例 #2
0
 internal static void CopyProperties(IValidated fromControl, IValidated toControl)
 {
     toControl.Enabled               = fromControl.Enabled;
     toControl.ErrorMessage          = fromControl.ErrorMessage;
     toControl.Required              = fromControl.Required;
     toControl.ShowRequired          = fromControl.ShowRequired;
     toControl.TabIndex              = fromControl.TabIndex;
     toControl.ValidationGroup       = fromControl.ValidationGroup;
     toControl.ValidatorInitialValue = fromControl.ValidatorInitialValue;
     toControl.Theme = fromControl.Theme;
 }
コード例 #3
0
        protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
        {
            Control control = null;

            if (this.EditMode || this.InsertMode)
            {
                ChangeTextType();
                switch (this.TextType)
                {
                case TextFieldType.Date:
                case TextFieldType.DateTime:
                case TextFieldType.Time:
                case TextFieldType.DatePicker:
                case TextFieldType.DateTimePicker:
                case TextFieldType.TimePicker:
                    m_DatePicker.ReadOnly = ReadOnly;
                    control = m_DatePicker;
                    break;

                default:
                    m_TextBox.ReadOnly = ReadOnly;
                    control            = m_TextBox;
                    break;
                }

                control.Visible = Visible;

                IValidated ctl = control as IValidated;
                if (ctl != null)
                {
                    ctl.Enabled               = Enabled;
                    ctl.ErrorMessage          = ErrorMessage;
                    ctl.Required              = Required;
                    ctl.ValidationGroup       = ValidationGroup;
                    ctl.ValidatorInitialValue = ValidatorInitialValue;
                    ctl.TabIndex              = TabIndex;
                }

                if (cell != null)
                {
                    cell.Controls.Add(control);
                }
            }
            else
            {
                control = cell;
            }

            if (control != null && Visible)
            {
                control.DataBinding += new EventHandler(OnBindingField);
            }
        }
コード例 #4
0
        public void ShowValidationMessages()
        {
            IValidated target = (IValidated)this.target;

            // Enable rich text in help boxes. Store original so we can revert since this might be a "hack".
            var styleRichText = GUI.skin.GetStyle("HelpBox").richText;

            GUI.skin.GetStyle("HelpBox").richText = true;

            // This is a static list so we need to clear it before use. Not sure if this will ever be a threaded
            // operation which would be an issue.
            foreach (var messages in ValidatedHelper.messages)
            {
                messages.Clear();
            }

            // OceanRenderer isn't a hard requirement for validation to work. Null needs to be handled in each
            // component.
            target.Validate(FindObjectOfType <OceanRenderer>(), ValidatedHelper.HelpBox);

            // We only want space before and after the list of help boxes. We don't want space between.
            var needsSpaceAbove = true;
            var needsSpaceBelow = false;

            // We loop through in reverse order so errors appears at the top.
            for (var messageTypeIndex = 0; messageTypeIndex < ValidatedHelper.messages.Length; messageTypeIndex++)
            {
                var messages = ValidatedHelper.messages[messageTypeIndex];

                if (messages.Count > 0)
                {
                    if (needsSpaceAbove)
                    {
                        // Double space looks good at top.
                        EditorGUILayout.Space();
                        EditorGUILayout.Space();
                        needsSpaceAbove = false;
                    }

                    needsSpaceBelow = true;

                    // Map Validated.MessageType to HelpBox.MessageType.
                    var messageType = (MessageType)ValidatedHelper.messages.Length - messageTypeIndex;

                    if (_groupMessages)
                    {
                        // We join the messages together to reduce vertical space since HelpBox has padding, borders etc.
                        var joinedMessage = messages[0];
                        // Format as list if we have more than one message.
                        if (messages.Count > 1)
                        {
                            joinedMessage = $"- {joinedMessage}";
                        }

                        for (var messageIndex = 1; messageIndex < messages.Count; messageIndex++)
                        {
                            joinedMessage += $"\n- {messages[messageIndex]}";
                        }

                        EditorGUILayout.HelpBox(joinedMessage, messageType);
                    }
                    else
                    {
                        foreach (var message in messages)
                        {
                            EditorGUILayout.HelpBox(message, messageType);
                        }
                    }
                }
            }

            if (needsSpaceBelow)
            {
                EditorGUILayout.Space();
            }

            // Revert skin since it persists.
            GUI.skin.GetStyle("HelpBox").richText = styleRichText;
        }
コード例 #5
0
 public void RemoveValidator(IValidated validated)
 {
     _validateds.RemoveAll(subWeak => !subWeak.IsAlive || subWeak.Target == validated);
 }
コード例 #6
0
 public void AddValidator(IValidated validated)
 {
     _validateds.Add(new WeakReference(validated));
 }
コード例 #7
0
        public void ShowValidationMessages()
        {
            IValidated target = (IValidated)this.target;

            // Enable rich text in help boxes. Store original so we can revert since this might be a "hack".
            var styleRichText = GUI.skin.GetStyle("HelpBox").richText;

            GUI.skin.GetStyle("HelpBox").richText = true;

            // This is a static list so we need to clear it before use. Not sure if this will ever be a threaded
            // operation which would be an issue.
            foreach (var messages in ValidatedHelper.messages)
            {
                messages.Clear();
            }

            // OceanRenderer isn't a hard requirement for validation to work. Null needs to be handled in each
            // component.
            target.Validate(FindObjectOfType <OceanRenderer>(), ValidatedHelper.HelpBox);

            // We only want space before and after the list of help boxes. We don't want space between.
            var needsSpaceAbove = true;
            var needsSpaceBelow = false;

            // We loop through in reverse order so errors appears at the top.
            for (var messageTypeIndex = 0; messageTypeIndex < ValidatedHelper.messages.Length; messageTypeIndex++)
            {
                var messages = ValidatedHelper.messages[messageTypeIndex];

                if (messages.Count > 0)
                {
                    if (needsSpaceAbove)
                    {
                        // Double space looks good at top.
                        EditorGUILayout.Space();
                        EditorGUILayout.Space();
                        needsSpaceAbove = false;
                    }

                    needsSpaceBelow = true;

                    // Map Validated.MessageType to HelpBox.MessageType.
                    var messageType = (MessageType)ValidatedHelper.messages.Length - messageTypeIndex;

                    if (_groupMessages)
                    {
                        // We join the messages together to reduce vertical space since HelpBox has padding, borders etc.
                        var joinedMessage = messages[0]._message;
                        // Format as list if we have more than one message.
                        if (messages.Count > 1)
                        {
                            joinedMessage = $"- {joinedMessage}";
                        }

                        for (var messageIndex = 1; messageIndex < messages.Count; messageIndex++)
                        {
                            joinedMessage += $"\n- {messages[messageIndex]}";
                        }

                        EditorGUILayout.HelpBox(joinedMessage, messageType);
                    }
                    else
                    {
                        foreach (var message in messages)
                        {
                            EditorGUILayout.BeginHorizontal();
                            EditorGUILayout.HelpBox(message._message, messageType);

                            // Jump to object button.
                            if (message._object != null)
                            {
                                // Selection.activeObject can be message._object.gameObject instead of the component
                                // itself. We soft cast to MonoBehaviour to get the gameObject for comparison.
                                // Alternatively, we could always pass gameObject instead of "this".
                                var casted = message._object as MonoBehaviour;

                                if (Selection.activeObject != message._object && (casted == null || casted.gameObject != Selection.activeObject))
                                {
                                    if (s_jumpButtonContent == null)
                                    {
                                        s_jumpButtonContent = new GUIContent(EditorGUIUtility.FindTexture("scenepicking_pickable_hover@2x"), "Jump to object to resolve issue");
                                    }

                                    if (GUILayout.Button(s_jumpButtonContent, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true)))
                                    {
                                        Selection.activeObject = message._object;
                                    }
                                }
                            }

                            // Fix the issue button.
                            if (message._action != null)
                            {
                                if (s_fixButtonContent == null)
                                {
                                    s_fixButtonContent = new GUIContent(EditorGUIUtility.FindTexture("SceneViewTools@2x"), "Fix the issue");
                                }

                                if (GUILayout.Button(s_fixButtonContent, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true)))
                                {
                                    var serializedObject = new SerializedObject(message._object);
                                    message._action.Invoke(serializedObject);
                                    if (serializedObject.ApplyModifiedProperties())
                                    {
                                        // SerializedObject does this for us, but gives the history item a nicer label.
                                        Undo.RecordObject(message._object, $"Fix for {message._object.name}");
                                    }
                                }
                            }

                            EditorGUILayout.EndHorizontal();
                        }
                    }
                }
            }

            if (needsSpaceBelow)
            {
                EditorGUILayout.Space();
            }

            // Revert skin since it persists.
            GUI.skin.GetStyle("HelpBox").richText = styleRichText;
        }
コード例 #8
0
 /// <inheritdoc />
 public void UnValidated(IValidated validated)
 {
     _validator.Remove(validated);
 }
コード例 #9
0
 /// <inheritdoc />
 public void Validated(IValidated validated)
 {
     _validator.Add(validated);
 }