Beispiel #1
0
        protected override void GUIUpdate()
        {
            EditorGUI.BeginChangeCheck();
            {
                EditorGUILayout.BeginVertical();
                {
                    EditorGUI.BeginChangeCheck();
                    filterMode = (FilterMode)EditorGUILayout.EnumPopup(filterMode);
                    if (EditorGUI.EndChangeCheck())
                    {
                        texture.filterMode = filterMode;
                    }
                    gradient = (Gradient)gradientField.Invoke(null, new object[] { "", gradient, null });
                    if (oldGradient != null && !gradient.Compare(oldGradient))
                    {
                        needUpdate = true;
                    }
                    if (e.type == EventType.Repaint)
                    {
                        update = false;
                    }
                }
                EditorGUILayout.EndVertical();

                if (e.type == EventType.KeyDown)
                {
                    if (e.keyCode == KeyCode.Return || e.keyCode == KeyCode.KeypadEnter || e.keyCode == KeyCode.Escape)
                    {
                        e.Use();
                    }
                }

                EditorGUIUtility.labelWidth = 100;
                debug = EditorGUILayout.Toggle("debug", debug);
            }
            if (EditorGUI.EndChangeCheck())
            {
                SendUpdate("SamplerSettingsUpdate");
            }

            if (needUpdate && e.type != EventType.Layout)
            {
                GUI.changed = true;
                needUpdate  = false;

                //for an unknown reason, EditorWindow.SendEvent dooes not works with gradient field
                //so here is a workaround to update the main window:
                if (windowToUpdate != null)
                {
                    update = true;
                    windowToUpdate.Repaint();
                }
            }

            oldGradient = (SerializableGradient)gradient;
        }
Beispiel #2
0
        public void Sampler2DPreview(GUIContent prefix, Sampler2D samp, bool update, bool settings = true, FilterMode fm = FilterMode.Bilinear)
        {
            int previewSize = (int)currentWindowRect.width - 20 - 20;             //padding + texture margin
            var e           = Event.current;

            if (samp == null)
            {
                return;
            }

            if (prefix != null && !String.IsNullOrEmpty(prefix.text))
            {
                EditorGUILayout.LabelField(prefix);
            }

            var fieldSettings = GetGUISettingData(() => {
                var state        = new PWGUISettings();
                state.filterMode = fm;
                state.debug      = false;
                state.gradient   = new SerializableGradient(
                    PWUtils.CreateGradient(
                        new KeyValuePair <float, Color>(0, Color.black),
                        new KeyValuePair <float, Color>(1, Color.white)
                        )
                    );
                return(state);
            });

            //recreated texture if it has been destoryed:
            if (fieldSettings.texture == null)
            {
                fieldSettings.texture            = new Texture2D(previewSize, previewSize, TextureFormat.RGBA32, false);
                fieldSettings.texture.filterMode = fieldSettings.filterMode;
            }

            //same for the gradient:
            if (fieldSettings.gradient == null || fieldSettings.gradient.alphaKeys == null)
            {
                fieldSettings.gradient = fieldSettings.serializableGradient;
            }

            Texture2D tex      = fieldSettings.texture;
            Gradient  gradient = fieldSettings.gradient;

            if (samp.size != tex.width)
            {
                tex.Resize(samp.size, samp.size, TextureFormat.RGBA32, false);
            }

            Rect previewRect = EditorGUILayout.GetControlRect(GUILayout.ExpandWidth(true), GUILayout.Height(0));

            if (previewRect.width > 2)
            {
                fieldSettings.savedRect = previewRect;
            }

            TexturePreview(tex, false, false, false);

            //draw the settings window
            if (settings && fieldSettings.active)
            {
                PWPopup.AddToRender(fieldSettings, "Sampler 2D settings", () => {
                    EditorGUILayout.BeginVertical();
                    {
                        EditorGUI.BeginChangeCheck();
                        fieldSettings.filterMode = (FilterMode)EditorGUILayout.EnumPopup(fieldSettings.filterMode);
                        if (EditorGUI.EndChangeCheck())
                        {
                            tex.filterMode = fieldSettings.filterMode;
                        }
                        gradient = (Gradient)gradientField.Invoke(null, new object[] { "", gradient, null });
                        if (!gradient.Compare(fieldSettings.serializableGradient))
                        {
                            fieldSettings.update = true;
                        }
                        fieldSettings.serializableGradient = (SerializableGradient)gradient;
                    }
                    EditorGUILayout.EndVertical();

                    if (e.type == EventType.KeyDown && fieldSettings.active)
                    {
                        if (e.keyCode == KeyCode.Return || e.keyCode == KeyCode.KeypadEnter || e.keyCode == KeyCode.Escape)
                        {
                            fieldSettings.InActive();
                            e.Use();
                        }
                    }

                    EditorGUIUtility.labelWidth = 100;
                    fieldSettings.debug         = EditorGUILayout.Toggle("debug", fieldSettings.debug);

                    if (GUILayout.Button("force update"))
                    {
                        fieldSettings.update = true;
                    }
                });
            }

            if (settings)
            {
                //draw the setting icon and manage his events
                int  icSettingsSize    = 16;
                int  icSettingsPadding = 4;
                Rect icSettingsRect    = new Rect(previewRect.x + previewRect.width - icSettingsSize - icSettingsPadding, previewRect.y + icSettingsPadding, icSettingsSize, icSettingsSize);

                GUI.DrawTexture(icSettingsRect, ic_settings);
                if (e.type == EventType.MouseDown && e.button == 0)
                {
                    if (icSettingsRect.Contains(e.mousePosition))
                    {
                        fieldSettings.Invert(null);
                        e.Use();
                    }
                    else if (fieldSettings.active)
                    {
                        fieldSettings.InActive();
                        e.Use();
                    }
                }
            }

            if (!settings && fieldSettings.texture.filterMode != fm)
            {
                fieldSettings.texture.filterMode = fm;
            }

            //update the texture with the gradient
            if (update || fieldSettings.update)
            {
                samp.Foreach((x, y, val) => {
                    tex.SetPixel(x, y, gradient.Evaluate(Mathf.Clamp01(val)));
                }, true);
                tex.Apply();
                fieldSettings.update = false;
            }

            if (fieldSettings.debug)
            {
                Vector2 pixelPos = e.mousePosition - fieldSettings.savedRect.position;

                pixelPos *= samp.size / fieldSettings.savedRect.width;

                EditorGUILayout.LabelField("Sampler2D min: " + samp.min + ", max: " + samp.max);

                if (pixelPos.x >= 0 && pixelPos.y >= 0 && pixelPos.x < samp.size && pixelPos.y < samp.size)
                {
                    if (e.isMouse)
                    {
                        e.Use();
                    }
                    EditorGUILayout.LabelField("(" + (int)pixelPos.x + ", " + (int)pixelPos.y + "): " + samp[(int)pixelPos.x, (int)pixelPos.y]);
                }
                else
                {
                    EditorGUILayout.LabelField("(NA, NA): NA");
                }
            }
        }