void BuildVector1PropertyField(Vector1ShaderProperty property)
        {
            switch (property.floatType)
            {
            case FloatType.Slider:
            {
                float min = Mathf.Min(property.value, property.rangeValues.x);
                float max = Mathf.Max(property.value, property.rangeValues.y);
                property.rangeValues = new Vector2(min, max);

                var defaultField = new FloatField {
                    value = property.value
                };
                var minField = new FloatField {
                    value = property.rangeValues.x
                };
                var maxField = new FloatField {
                    value = property.rangeValues.y
                };

                defaultField.RegisterValueChangedCallback(evt =>
                    {
                        property.value = (float)evt.newValue;
                        this.MarkDirtyRepaint();
                    });
                defaultField.Q("unity-text-input").RegisterCallback <FocusOutEvent>(evt =>
                    {
                        graph.owner.RegisterCompleteObjectUndo("Change Property Value");
                        float minValue       = Mathf.Min(property.value, property.rangeValues.x);
                        float maxValue       = Mathf.Max(property.value, property.rangeValues.y);
                        property.rangeValues = new Vector2(minValue, maxValue);
                        minField.value       = minValue;
                        maxField.value       = maxValue;
                        DirtyNodes();
                    });
                minField.RegisterValueChangedCallback(evt =>
                    {
                        graph.owner.RegisterCompleteObjectUndo("Change Range Property Minimum");
                        property.rangeValues = new Vector2((float)evt.newValue, property.rangeValues.y);
                        DirtyNodes();
                    });
                minField.Q("unity-text-input").RegisterCallback <FocusOutEvent>(evt =>
                    {
                        property.value     = Mathf.Max(Mathf.Min(property.value, property.rangeValues.y), property.rangeValues.x);
                        defaultField.value = property.value;
                        DirtyNodes();
                    });
                maxField.RegisterValueChangedCallback(evt =>
                    {
                        graph.owner.RegisterCompleteObjectUndo("Change Range Property Maximum");
                        property.rangeValues = new Vector2(property.rangeValues.x, (float)evt.newValue);
                        DirtyNodes();
                    });
                maxField.Q("unity-text-input").RegisterCallback <FocusOutEvent>(evt =>
                    {
                        property.value     = Mathf.Max(Mathf.Min(property.value, property.rangeValues.y), property.rangeValues.x);
                        defaultField.value = property.value;
                        DirtyNodes();
                    });

                AddRow("Default", defaultField);
                AddRow("Min", minField);
                AddRow("Max", maxField);
            }
            break;

            case FloatType.Integer:
            {
                property.value = (int)property.value;
                var defaultField = new IntegerField {
                    value = (int)property.value
                };
                defaultField.RegisterValueChangedCallback(evt =>
                    {
                        graph.owner.RegisterCompleteObjectUndo("Change Property Value");
                        property.value = (int)evt.newValue;
                        DirtyNodes();
                    });
                AddRow("Default", defaultField);
            }
            break;

            default:
            {
                var defaultField = new FloatField {
                    value = property.value
                };
                defaultField.RegisterValueChangedCallback(evt =>
                    {
                        graph.owner.RegisterCompleteObjectUndo("Change Property Value");
                        property.value = (float)evt.newValue;
                        DirtyNodes();
                    });
                AddRow("Default", defaultField);
            }
            break;
            }

            if (!graph.isSubGraph)
            {
                var modeField = new EnumField(property.floatType);
                modeField.RegisterValueChangedCallback(evt =>
                {
                    graph.owner.RegisterCompleteObjectUndo("Change Vector1 Mode");
                    property.floatType = (FloatType)evt.newValue;
                    Rebuild();
                });
                AddRow("Mode", modeField);
            }
        }
        void BuildVector1PropertyView(Vector1ShaderProperty floatProperty)
        {
            VisualElement[] rows = null;

            switch (floatProperty.floatType)
            {
            case FloatType.Slider:
            {
                float min = Mathf.Min(floatProperty.value, floatProperty.rangeValues.x);
                float max = Mathf.Max(floatProperty.value, floatProperty.rangeValues.y);
                floatProperty.rangeValues = new Vector2(min, max);

                var defaultField = new FloatField {
                    value = floatProperty.value
                };
                var minField = new FloatField {
                    value = floatProperty.rangeValues.x
                };
                var maxField = new FloatField {
                    value = floatProperty.rangeValues.y
                };

                defaultField.RegisterValueChangedCallback(evt =>
                    {
                        var value           = (float)evt.newValue;
                        floatProperty.value = value;
                        this.MarkDirtyRepaint();
                    });
                defaultField.Q("unity-text-input").RegisterCallback <FocusOutEvent>(evt =>
                    {
                        m_Graph.owner.RegisterCompleteObjectUndo("Change Property Value");
                        float minValue            = Mathf.Min(floatProperty.value, floatProperty.rangeValues.x);
                        float maxValue            = Mathf.Max(floatProperty.value, floatProperty.rangeValues.y);
                        floatProperty.rangeValues = new Vector2(minValue, maxValue);
                        minField.value            = minValue;
                        maxField.value            = maxValue;
                        DirtyNodes();
                    });
                minField.RegisterValueChangedCallback(evt =>
                    {
                        m_Graph.owner.RegisterCompleteObjectUndo("Change Range Property Minimum");
                        float newValue            = (float)evt.newValue;
                        floatProperty.rangeValues = new Vector2(newValue, floatProperty.rangeValues.y);
                        DirtyNodes();
                    });
                minField.Q("unity-text-input").RegisterCallback <FocusOutEvent>(evt =>
                    {
                        floatProperty.value = Mathf.Max(Mathf.Min(floatProperty.value, floatProperty.rangeValues.y), floatProperty.rangeValues.x);
                        defaultField.value  = floatProperty.value;
                        DirtyNodes();
                    });
                maxField.RegisterValueChangedCallback(evt =>
                    {
                        m_Graph.owner.RegisterCompleteObjectUndo("Change Range Property Maximum");
                        float newValue            = (float)evt.newValue;
                        floatProperty.rangeValues = new Vector2(floatProperty.rangeValues.x, newValue);
                        DirtyNodes();
                    });
                maxField.Q("unity-text-input").RegisterCallback <FocusOutEvent>(evt =>
                    {
                        floatProperty.value = Mathf.Max(Mathf.Min(floatProperty.value, floatProperty.rangeValues.y), floatProperty.rangeValues.x);
                        defaultField.value  = floatProperty.value;
                        DirtyNodes();
                    });
                rows    = new VisualElement[4];
                rows[0] = CreateRow("Default", defaultField);
                rows[2] = CreateRow("Min", minField);
                rows[3] = CreateRow("Max", maxField);
            }
            break;

            case FloatType.Integer:
            {
                floatProperty.value = (int)floatProperty.value;
                var defaultField = new IntegerField {
                    value = (int)floatProperty.value
                };
                defaultField.RegisterValueChangedCallback(evt =>
                    {
                        m_Graph.owner.RegisterCompleteObjectUndo("Change Property Value");
                        var value           = (int)evt.newValue;
                        floatProperty.value = value;
                        DirtyNodes();
                    });
                rows    = new VisualElement[2];
                rows[0] = CreateRow("Default", defaultField);
            }
            break;

            default:
            {
                var defaultField = new FloatField {
                    value = floatProperty.value
                };
                defaultField.RegisterValueChangedCallback(evt =>
                    {
                        m_Graph.owner.RegisterCompleteObjectUndo("Change Property Value");
                        var value           = (float)evt.newValue;
                        floatProperty.value = value;
                        DirtyNodes();
                    });
                rows    = new VisualElement[2];
                rows[0] = CreateRow("Default", defaultField);
            }
            break;
            }

            if (!m_Graph.isSubGraph)
            {
                var modeField = new EnumField(floatProperty.floatType);
                modeField.RegisterValueChangedCallback(evt =>
                {
                    m_Graph.owner.RegisterCompleteObjectUndo("Change Vector1 Mode");
                    var value = (FloatType)evt.newValue;
                    floatProperty.floatType = value;
                    if (rows != null)
                    {
                        RemoveElements(rows);
                    }
                    BuildVector1PropertyView(floatProperty);
                    this.MarkDirtyRepaint();
                });
                rows[1] = CreateRow("Mode", modeField);
            }

            if (rows == null)
            {
                return;
            }

            for (int i = 0; i < rows.Length; i++)
            {
                Add(rows[i]);
            }
        }
Exemple #3
0
        public BlackboardFieldPropertyView(BlackboardField blackboardField, AbstractMaterialGraph graph, IShaderProperty property)
        {
            styleSheets.Add(Resources.Load <StyleSheet>("Styles/ShaderGraphBlackboard"));
            m_BlackboardField = blackboardField;
            m_Graph           = graph;
            m_Property        = property;

            m_ExposedToogle = new Toggle();
            m_ExposedToogle.OnToggleChanged(evt =>
            {
                property.generatePropertyBlock = evt.newValue;
                if (property.generatePropertyBlock)
                {
                    m_BlackboardField.icon = BlackboardProvider.exposedIcon;
                }
                else
                {
                    m_BlackboardField.icon = null;
                }
                DirtyNodes(ModificationScope.Graph);
            });
            m_ExposedToogle.value = property.generatePropertyBlock;
            AddRow("Exposed", m_ExposedToogle);

            m_ReferenceNameField = new TextField(512, false, false, ' ');
            m_ReferenceNameField.styleSheets.Add(Resources.Load <StyleSheet>("Styles/PropertyNameReferenceField"));
            AddRow("Reference", m_ReferenceNameField);
            m_ReferenceNameField.value     = property.referenceName;
            m_ReferenceNameField.isDelayed = true;
            m_ReferenceNameField.RegisterValueChangedCallback(newName =>
            {
                if (m_ReferenceNameField.value != m_Property.referenceName)
                {
                    string newReferenceName        = m_Graph.SanitizePropertyReferenceName(newName.newValue, property.guid);
                    property.overrideReferenceName = newReferenceName;
                }
                m_ReferenceNameField.value = property.referenceName;

                if (string.IsNullOrEmpty(property.overrideReferenceName))
                {
                    m_ReferenceNameField.RemoveFromClassList("modified");
                }
                else
                {
                    m_ReferenceNameField.AddToClassList("modified");
                }

                DirtyNodes(ModificationScope.Graph);
                UpdateReferenceNameResetMenu();
            });

            if (!string.IsNullOrEmpty(property.overrideReferenceName))
            {
                m_ReferenceNameField.AddToClassList("modified");
            }

            if (property is Vector1ShaderProperty)
            {
                VisualElement floatRow   = new VisualElement();
                VisualElement intRow     = new VisualElement();
                VisualElement modeRow    = new VisualElement();
                VisualElement minRow     = new VisualElement();
                VisualElement maxRow     = new VisualElement();
                FloatField    floatField = null;

                var floatProperty = (Vector1ShaderProperty)property;

                if (floatProperty.floatType == FloatType.Integer)
                {
                    var field = new IntegerField {
                        value = (int)floatProperty.value
                    };
                    field.RegisterValueChangedCallback(intEvt =>
                    {
                        floatProperty.value = (float)intEvt.newValue;
                        DirtyNodes();
                    });
                    intRow = AddRow("Default", field);
                }
                else
                {
                    floatField = new FloatField {
                        value = floatProperty.value
                    };
                    floatField.RegisterValueChangedCallback(evt =>
                    {
                        floatProperty.value = (float)evt.newValue;
                        DirtyNodes();
                    });
                    floatRow = AddRow("Default", floatField);
                }

                var floatModeField = new EnumField((Enum)floatProperty.floatType);
                floatModeField.value = floatProperty.floatType;
                floatModeField.RegisterValueChangedCallback(evt =>
                {
                    if (floatProperty.floatType == (FloatType)evt.newValue)
                    {
                        return;
                    }
                    floatProperty           = (Vector1ShaderProperty)property;
                    floatProperty.floatType = (FloatType)evt.newValue;
                    switch (floatProperty.floatType)
                    {
                    case FloatType.Slider:
                        RemoveElements(new VisualElement[] { floatRow, intRow, modeRow, minRow, maxRow });
                        var field = new FloatField {
                            value = Mathf.Max(Mathf.Min(floatProperty.value, floatProperty.rangeValues.y), floatProperty.rangeValues.x)
                        };
                        floatProperty.value = (float)field.value;
                        field.RegisterValueChangedCallback(defaultEvt =>
                        {
                            floatProperty.value = Mathf.Max(Mathf.Min((float)defaultEvt.newValue, floatProperty.rangeValues.y), floatProperty.rangeValues.x);
                            field.value         = floatProperty.value;
                            DirtyNodes();
                        });
                        floatRow     = AddRow("Default", field);
                        field.value  = Mathf.Max(Mathf.Min(floatProperty.value, floatProperty.rangeValues.y), floatProperty.rangeValues.x);
                        modeRow      = AddRow("Mode", floatModeField);
                        var minField = new FloatField {
                            value = floatProperty.rangeValues.x
                        };
                        minField.RegisterValueChangedCallback(minEvt =>
                        {
                            floatProperty.rangeValues = new Vector2((float)minEvt.newValue, floatProperty.rangeValues.y);
                            floatProperty.value       = Mathf.Max(Mathf.Min(floatProperty.value, floatProperty.rangeValues.y), floatProperty.rangeValues.x);
                            field.value = floatProperty.value;
                            DirtyNodes();
                        });
                        minRow       = AddRow("Min", minField);
                        var maxField = new FloatField {
                            value = floatProperty.rangeValues.y
                        };
                        maxField.RegisterValueChangedCallback(maxEvt =>
                        {
                            floatProperty.rangeValues = new Vector2(floatProperty.rangeValues.x, (float)maxEvt.newValue);
                            floatProperty.value       = Mathf.Max(Mathf.Min(floatProperty.value, floatProperty.rangeValues.y), floatProperty.rangeValues.x);
                            field.value = floatProperty.value;
                            DirtyNodes();
                        });
                        maxRow = AddRow("Max", maxField);
                        break;

                    case FloatType.Integer:
                        RemoveElements(new VisualElement[] { floatRow, intRow, modeRow, minRow, maxRow });
                        var intField = new IntegerField {
                            value = (int)floatProperty.value
                        };
                        intField.RegisterValueChangedCallback(intEvt =>
                        {
                            floatProperty.value = (float)intEvt.newValue;
                            DirtyNodes();
                        });
                        intRow  = AddRow("Default", intField);
                        modeRow = AddRow("Mode", floatModeField);
                        break;

                    default:
                        RemoveElements(new VisualElement[] { floatRow, intRow, modeRow, minRow, maxRow });
                        field = new FloatField {
                            value = floatProperty.value
                        };
                        field.RegisterValueChangedCallback(defaultEvt =>
                        {
                            floatProperty.value = (float)defaultEvt.newValue;
                            DirtyNodes();
                        });
                        floatRow = AddRow("Default", field);
                        modeRow  = AddRow("Mode", floatModeField);
                        break;
                    }
                    DirtyNodes();
                });
                modeRow = AddRow("Mode", floatModeField);

                if (floatProperty.floatType == FloatType.Slider)
                {
                    var minField = new FloatField {
                        value = floatProperty.rangeValues.x
                    };
                    minField.RegisterValueChangedCallback(minEvt =>
                    {
                        floatProperty.rangeValues = new Vector2((float)minEvt.newValue, floatProperty.rangeValues.y);
                        floatProperty.value       = Mathf.Max(Mathf.Min(floatProperty.value, floatProperty.rangeValues.y), floatProperty.rangeValues.x);
                        floatField.value          = floatProperty.value;
                        DirtyNodes();
                    });
                    minRow = AddRow("Min", minField);
                    var maxField = new FloatField {
                        value = floatProperty.rangeValues.y
                    };
                    maxField.RegisterValueChangedCallback(maxEvt =>
                    {
                        floatProperty.rangeValues = new Vector2(floatProperty.rangeValues.x, (float)maxEvt.newValue);
                        floatProperty.value       = Mathf.Max(Mathf.Min(floatProperty.value, floatProperty.rangeValues.y), floatProperty.rangeValues.x);
                        floatField.value          = floatProperty.value;
                        DirtyNodes();
                    });
                    maxRow = AddRow("Max", maxField);
                }
            }
            else if (property is Vector2ShaderProperty)
            {
                var vectorProperty = (Vector2ShaderProperty)property;
                var field          = new Vector2Field {
                    value = vectorProperty.value
                };
                field.RegisterValueChangedCallback(evt =>
                {
                    vectorProperty.value = evt.newValue;
                    DirtyNodes();
                });
                AddRow("Default", field);
            }
            else if (property is Vector3ShaderProperty)
            {
                var vectorProperty = (Vector3ShaderProperty)property;
                var field          = new Vector3Field {
                    value = vectorProperty.value
                };
                field.RegisterValueChangedCallback(evt =>
                {
                    vectorProperty.value = evt.newValue;
                    DirtyNodes();
                });
                AddRow("Default", field);
            }
            else if (property is Vector4ShaderProperty)
            {
                var vectorProperty = (Vector4ShaderProperty)property;
                var field          = new Vector4Field {
                    value = vectorProperty.value
                };
                field.RegisterValueChangedCallback(evt =>
                {
                    vectorProperty.value = evt.newValue;
                    DirtyNodes();
                });
                AddRow("Default", field);
            }
            else if (property is ColorShaderProperty)
            {
                var colorProperty = (ColorShaderProperty)property;
                var colorField    = new ColorField {
                    value = property.defaultValue, showEyeDropper = false, hdr = colorProperty.colorMode == ColorMode.HDR
                };
                colorField.RegisterValueChangedCallback(evt =>
                {
                    colorProperty.value = evt.newValue;
                    DirtyNodes();
                });
                AddRow("Default", colorField);
                var colorModeField = new EnumField((Enum)colorProperty.colorMode);
                colorModeField.RegisterValueChangedCallback(evt =>
                {
                    if (colorProperty.colorMode == (ColorMode)evt.newValue)
                    {
                        return;
                    }
                    colorProperty.colorMode = (ColorMode)evt.newValue;
                    colorField.hdr          = colorProperty.colorMode == ColorMode.HDR;
                    colorField.MarkDirtyRepaint();
                    DirtyNodes();
                });
                AddRow("Mode", colorModeField);
            }
            else if (property is TextureShaderProperty)
            {
                var textureProperty = (TextureShaderProperty)property;
                var field           = new ObjectField {
                    value = textureProperty.value.texture, objectType = typeof(Texture)
                };
                field.RegisterValueChangedCallback(evt =>
                {
                    textureProperty.value.texture = (Texture)evt.newValue;
                    DirtyNodes();
                });
                AddRow("Default", field);
                var defaultModeField = new EnumField((Enum)textureProperty.defaultType);
                defaultModeField.RegisterValueChangedCallback(evt =>
                {
                    if (textureProperty.defaultType == (TextureShaderProperty.DefaultType)evt.newValue)
                    {
                        return;
                    }
                    textureProperty.defaultType = (TextureShaderProperty.DefaultType)evt.newValue;
                    DirtyNodes(ModificationScope.Graph);
                });
                AddRow("Mode", defaultModeField);
            }
            else if (property is Texture2DArrayShaderProperty)
            {
                var textureProperty = (Texture2DArrayShaderProperty)property;
                var field           = new ObjectField {
                    value = textureProperty.value.textureArray, objectType = typeof(Texture2DArray)
                };
                field.RegisterValueChangedCallback(evt =>
                {
                    textureProperty.value.textureArray = (Texture2DArray)evt.newValue;
                    DirtyNodes();
                });
                AddRow("Default", field);
            }
            else if (property is Texture3DShaderProperty)
            {
                var textureProperty = (Texture3DShaderProperty)property;
                var field           = new ObjectField {
                    value = textureProperty.value.texture, objectType = typeof(Texture3D)
                };
                field.RegisterValueChangedCallback(evt =>
                {
                    textureProperty.value.texture = (Texture3D)evt.newValue;
                    DirtyNodes();
                });
                AddRow("Default", field);
            }
            else if (property is CubemapShaderProperty)
            {
                var cubemapProperty = (CubemapShaderProperty)property;
                var field           = new ObjectField {
                    value = cubemapProperty.value.cubemap, objectType = typeof(Cubemap)
                };
                field.RegisterValueChangedCallback(evt =>
                {
                    cubemapProperty.value.cubemap = (Cubemap)evt.newValue;
                    DirtyNodes();
                });
                AddRow("Default", field);
            }
            else if (property is BooleanShaderProperty)
            {
                var booleanProperty = (BooleanShaderProperty)property;
                EventCallback <ChangeEvent <bool> > onBooleanChanged = evt =>
                {
                    booleanProperty.value = evt.newValue;
                    DirtyNodes();
                };
                var field = new Toggle();
                field.OnToggleChanged(onBooleanChanged);
                field.value = booleanProperty.value;
                AddRow("Default", field);
            }
//            AddRow("Type", new TextField());
//            AddRow("Exposed", new Toggle(null));
//            AddRow("Range", new Toggle(null));
//            AddRow("Default", new TextField());
//            AddRow("Tooltip", new TextField());


            AddToClassList("sgblackboardFieldPropertyView");

            UpdateReferenceNameResetMenu();
        }