Exemple #1
0
        void TexturePreview(Rect previewRect, Texture tex, bool settings, bool settingsStorage, bool debug)
        {
            var e = Event.current;

            if (!settingsStorage)
            {
                EditorGUI.DrawPreviewTexture(previewRect, tex);
                if (debug)
                {
                    DisplayTextureDebug(previewRect, tex as Texture2D);
                }
                return;
            }

            //create or load texture settings
            var fieldSettings = GetGUISettingData(() => {
                var state         = new PWGUISettings();
                state.filterMode  = FilterMode.Bilinear;
                state.scaleMode   = ScaleMode.ScaleToFit;
                state.scaleAspect = 1;
                state.material    = null;
                state.debug       = false;
                return(state);
            });

            if (e.type == EventType.Repaint)
            {
                fieldSettings.savedRect = previewRect;
            }

            EditorGUI.DrawPreviewTexture(previewRect, tex, fieldSettings.material, fieldSettings.scaleMode, fieldSettings.scaleAspect);

            if (!settings)
            {
                return;
            }

            //render debug:
            if (fieldSettings.debug)
            {
                DisplayTextureDebug(fieldSettings.savedRect, tex as Texture2D);
            }

            //render the texture settings window
            if (fieldSettings.active)
            {
                PWPopup.AddToRender(fieldSettings, "Texture preview settings", () =>
                {
                    if (e.type == EventType.KeyDown)
                    {
                        if (e.keyCode == KeyCode.Return || e.keyCode == KeyCode.KeypadEnter || e.keyCode == KeyCode.Escape)
                        {
                            fieldSettings.InActive();
                            e.Use();
                        }
                    }

                    EditorGUIUtility.labelWidth = 80;
                    EditorGUI.BeginChangeCheck();
                    fieldSettings.filterMode = (FilterMode)EditorGUILayout.EnumPopup("filter mode", fieldSettings.filterMode);
                    if (EditorGUI.EndChangeCheck() || tex.filterMode != fieldSettings.filterMode)
                    {
                        tex.filterMode = fieldSettings.filterMode;
                    }
                    fieldSettings.scaleMode     = (ScaleMode)EditorGUILayout.EnumPopup("scale mode", fieldSettings.scaleMode);
                    fieldSettings.scaleAspect   = EditorGUILayout.FloatField("scale aspect", fieldSettings.scaleAspect);
                    fieldSettings.material      = (Material)EditorGUILayout.ObjectField("material", fieldSettings.material, typeof(Material), false);
                    fieldSettings.debug         = EditorGUILayout.Toggle("debug", fieldSettings.debug);
                    EditorGUIUtility.labelWidth = 0;
                }, 200);
            }

            int  icSettingsSize = 16;
            Rect icSettingsRect = new Rect(previewRect.x + previewRect.width - icSettingsSize, previewRect.y, icSettingsSize, icSettingsSize);

            GUI.DrawTexture(icSettingsRect, ic_settings);
            if (e.type == EventType.MouseDown && e.button == 0)
            {
                if (icSettingsRect.Contains(e.mousePosition))
                {
                    fieldSettings.Invert(0);
                    e.Use();
                }
                else if (fieldSettings.active)
                {
                    fieldSettings.InActive();
                    e.Use();
                }
            }
        }
Exemple #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");
                }
            }
        }
Exemple #3
0
        public void ColorPicker(GUIContent prefix, Rect rect, ref Color color, bool displayColorPreview = true, bool previewOnIcon = false)
        {
            var   e           = Event.current;
            Rect  iconRect    = rect;
            int   icColorSize = 18;
            Color localColor  = color;

            var fieldSettings = GetGUISettingData(() => {
                PWGUISettings colorSettings = new PWGUISettings();

                colorSettings.c = (SerializableColor)localColor;

                return(colorSettings);
            });

            if (fieldSettings.active)
            {
                int colorPickerWidth  = 170;
                int colorPickerHeight = 270;

                GUI.changed = true;
                PWPopup.AddToRender(fieldSettings, "Color picker", () =>
                {
                    if (e.type == EventType.KeyDown)
                    {
                        if (e.keyCode == KeyCode.Return || e.keyCode == KeyCode.KeypadEnter)
                        {
                            fieldSettings.InActive();
                            e.Use();
                        }
                        if (e.keyCode == KeyCode.Escape)
                        {
                            fieldSettings.c = (SerializableColor)(Color)fieldSettings.InActive();
                            e.Use();
                        }
                    }

                    //draw the color picker window
                    colorPickerStyle = GUI.skin.FindStyle("ColorPicker");
                    {
                        Rect localColorPickerRect = new Rect(Vector2.zero, new Vector2(colorPickerWidth, colorPickerHeight));
                        GUILayout.Label(colorPickerTexture, GUILayout.Width(150), GUILayout.Height(150));

                        Vector2 colorPickerMousePosition = e.mousePosition - new Vector2(colorPickerStyle.padding.left + 1, colorPickerStyle.padding.top + 5);

                        if (colorPickerMousePosition.x >= 0 && colorPickerMousePosition.y >= 0 && colorPickerMousePosition.x <= 150 && colorPickerMousePosition.y <= 150)
                        {
                            if (e.type == EventType.MouseDown || e.type == EventType.MouseDrag)
                            {
                                Vector2 textureCoord        = colorPickerMousePosition * (colorPickerTexture.width / 150f);
                                textureCoord.y              = colorPickerTexture.height - textureCoord.y;
                                fieldSettings.c             = (SerializableColor)colorPickerTexture.GetPixel((int)textureCoord.x, (int)textureCoord.y);
                                fieldSettings.thumbPosition = colorPickerMousePosition + new Vector2(6, 9);
                            }
                        }

                        Rect colorPickerThumbRect = new Rect(fieldSettings.thumbPosition, new Vector2(8, 8));
                        GUI.DrawTexture(colorPickerThumbRect, colorPickerThumb);

                        byte r, g, b, a;
                        PWColorPalette.ColorToByte(fieldSettings.c, out r, out g, out b, out a);
                        EditorGUIUtility.labelWidth = 20;
                        r = (byte)EditorGUILayout.IntSlider("R", r, 0, 255);
                        g = (byte)EditorGUILayout.IntSlider("G", g, 0, 255);
                        b = (byte)EditorGUILayout.IntSlider("B", b, 0, 255);
                        a = (byte)EditorGUILayout.IntSlider("A", a, 0, 255);
                        fieldSettings.c             = (SerializableColor)PWColorPalette.ByteToColor(r, g, b, a);
                        EditorGUIUtility.labelWidth = 0;

                        EditorGUILayout.Space();

                        //hex field
                        int hex = PWColorPalette.ColorToHex(fieldSettings.c, false);                         //get color without alpha
                        EditorGUIUtility.labelWidth = 80;
                        EditorGUI.BeginChangeCheck();
                        string hexColor = EditorGUILayout.TextField("Hex color", hex.ToString("X6"));
                        if (EditorGUI.EndChangeCheck())
                        {
                            a = 255;
                        }
                        EditorGUIUtility.labelWidth = 0;
                        Regex reg = new Regex(@"[^A-F0-9 -]");
                        hexColor  = reg.Replace(hexColor, "");
                        hexColor  = hexColor.Substring(0, Mathf.Min(hexColor.Length, 6));
                        if (hexColor == "")
                        {
                            hexColor = "0";
                        }
                        hex             = int.Parse(a.ToString("X2") + hexColor, System.Globalization.NumberStyles.HexNumber);
                        fieldSettings.c = (SerializableColor)PWColorPalette.HexToColor(hex, false);

                        if (e.isMouse && localColorPickerRect.Contains(e.mousePosition))
                        {
                            e.Use();
                        }
                    }
                }, colorPickerWidth);
            }

            color = fieldSettings.c;

            //draw the icon
            Rect colorPreviewRect = iconRect;

            if (displayColorPreview)
            {
                int width = (int)rect.width;
                int colorPreviewPadding = 5;

                Vector2 prefixSize = Vector2.zero;
                if (prefix != null && !String.IsNullOrEmpty(prefix.text))
                {
                    prefixSize    = GUI.skin.label.CalcSize(prefix);
                    prefixSize.x += 5;                     //padding of 5 pixels
                    colorPreviewRect.position += new Vector2(prefixSize.x, 0);
                    Rect prefixRect = new Rect(iconRect.position, prefixSize);
                    GUI.Label(prefixRect, prefix);
                }
                colorPreviewRect.size = new Vector2(width - icColorSize - prefixSize.x - colorPreviewPadding, 16);
                iconRect.position    += new Vector2(colorPreviewRect.width + prefixSize.x + colorPreviewPadding, 0);
                iconRect.size         = new Vector2(icColorSize, icColorSize);
                EditorGUIUtility.DrawColorSwatch(colorPreviewRect, color);
            }

            //actions if clicked on/outside of the icon
            if (previewOnIcon)
            {
                GUI.color = color;
            }
            GUI.DrawTexture(iconRect, ic_color);
            GUI.color = Color.white;
            if (e.type == EventType.MouseDown && e.button == 0)
            {
                if (iconRect.Contains(e.mousePosition) || colorPreviewRect.Contains(e.mousePosition))
                {
                    fieldSettings.Active(color);
                    e.Use();
                }
                else if (fieldSettings.active)
                {
                    fieldSettings.InActive();
                    e.Use();
                }
            }
        }