/// <summary>
        /// Initializes a new instance of the <see cref="ModelSerializedFieldField{TValue}"/> class.
        /// </summary>
        /// <param name="dispatcher">The dispatcher to use to dispatch commands when the field is edited.</param>
        /// <param name="model">The model that owns the field.</param>
        /// <param name="fieldName">The field name.</param>
        public ModelSerializedFieldField(
            Dispatcher dispatcher,
            IGraphElementModel model,
            string fieldName)
            : base(dispatcher, model, fieldName, null)
        {
            m_ValueGetter = MakeFieldValueGetter(Model, fieldName);

            switch (m_Field)
            {
            case null:
                break;

            case PopupField <string> _:
                m_Field.RegisterCallback <ChangeEvent <string>, ModelPropertyField <TValue> >(
                    (e, f) =>
                {
                    var newValue = Enum.Parse(typeof(TValue), e.newValue);
                    var command  = new SetModelFieldCommand(newValue, f.Model, fieldName);
                    f.Dispatcher.Dispatch(command);
                }, this);
                break;

            default:
                m_Field.RegisterCallback <ChangeEvent <TValue>, ModelPropertyField <TValue> >(
                    (e, f) =>
                {
                    var command = new SetModelFieldCommand(e.newValue, f.Model, fieldName);
                    f.Dispatcher.Dispatch(command);
                }, this);
                break;
            }
        }
예제 #2
0
        /// <summary>
        /// Default command handler
        /// </summary>
        /// <param name="graphToolState">The state to modify.</param>
        /// <param name="command">The command to apply to the state.</param>
        public static void DefaultCommandHandler(GraphToolState graphToolState, SetModelFieldCommand command)
        {
            graphToolState.PushUndo(command);

            if (command.Models != null)
            {
                using (var updater = graphToolState.GraphViewState.UpdateScope)
                {
                    foreach (var model in command.Models)
                    {
                        var target = model is IHasInspectorSurrogate hasInspectorSurrogate ? hasInspectorSurrogate.Surrogate : model;
                        if (target != null)
                        {
                            var fieldInfo = SerializedFieldsInspector.GetInspectableField(target, command.FieldName);
                            fieldInfo?.SetValue(target, command.Value);
                        }
                    }

                    updater.MarkChanged(command.Models);
                }
            }
        }