Example #1
0
        //------------------Helpers----------------------------

        public static void setActiveShader(Shader shader)
        {
            if (shader != activeShader)
            {
                activeShader         = shader;
                presetHandler        = new PresetHandler(shader);
                activeShaderMaterial = new Material(shader);
            }
        }
        public static void Popup(Rect activeation_rect, PresetHandler presetHandler)
        {
            Vector2 pos = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);

            pos.x = Mathf.Min(EditorWindow.focusedWindow.position.x + EditorWindow.focusedWindow.position.width - 250, pos.x);
            pos.y = Mathf.Min(EditorWindow.focusedWindow.position.y + EditorWindow.focusedWindow.position.height - 200, pos.y);

            if (window != null)
            {
                window.Close();
            }
            window          = ScriptableObject.CreateInstance <PresetPopup>();
            window.position = new Rect(pos.x, pos.y, 250, 200);
            window.Init(presetHandler);
            window.ShowPopup();
        }
Example #3
0
        void OnGUI()
        {
            if (shaders == null)
            {
                this.shaders = ShaderHelper.GetThryEditorShaderNames();
                if (Settings.activeShader != null)
                {
                    for (int i = 0; i < shaders.Length; i++)
                    {
                        if (shaders[i] == Settings.activeShader.name)
                        {
                            selectedShaderIndex = i;
                        }
                    }
                }
            }
            if (propertyBackground == null)
            {
                setupStyle();
            }
            Shader activeShader = Settings.activeShader;
            int    newIndex     = EditorGUILayout.Popup(selectedShaderIndex, shaders, GUILayout.MaxWidth(500));

            if (selectedShaderIndex == -1)
            {
                newIndex = 0;
            }
            if (newIndex != selectedShaderIndex)
            {
                selectedShaderIndex = newIndex;
                selectedPreset      = 0;
                Settings.setActiveShader(Shader.Find(shaders[selectedShaderIndex]));
                activeShader     = Settings.activeShader;
                reloadProperties = true;
            }
            if (activeShader != null)
            {
                PresetHandler presetHandler = Settings.presetHandler;
                if (presetHandler.shaderHasPresetPath())
                {
                    Dictionary <string, List <string[]> > presets = presetHandler.getPresets();
                    string[] presetStrings = new string[presets.Count + 1];
                    int      i             = 0;
                    foreach (KeyValuePair <string, List <string[]> > entry in presets)
                    {
                        presetStrings[i++] = entry.Key;
                    }
                    presetStrings[presets.Count] = "<New Preset>";
                    GUILayout.BeginHorizontal();
                    int newSelectedPreset = EditorGUILayout.Popup(selectedPreset, presetStrings, GUILayout.MaxWidth(500));
                    if (newSelectedPreset != selectedPreset || reloadProperties)
                    {
                        this.selectedPreset = newSelectedPreset;
                        if (newSelectedPreset == presetStrings.Length - 1)
                        {
                            newPreset     = true;
                            newPresetName = "<name>";
                            properties    = null;
                        }
                        else
                        {
                            this.properties = presetHandler.getPropertiesOfPreset(presetStrings[selectedPreset]);
                            List <string> unusedProperties = new List <string>();
                            foreach (string pName in presetHandler.getPropertyNames())
                            {
                                bool unused = true;
                                foreach (string[] p in this.properties)
                                {
                                    if (p[0] == pName)
                                    {
                                        unused = false;
                                    }
                                }
                                if (unused)
                                {
                                    unusedProperties.Add(pName);
                                }
                            }
                            this.unusedProperties = unusedProperties.ToArray();
                            reloadProperties      = false;
                            newPreset             = false;
                        }
                    }
                    if (GUILayout.Button("Delete", GUILayout.MaxWidth(80)))
                    {
                        presetHandler.removePreset(presetStrings[selectedPreset]);
                        reloadProperties = true;
                        Repaint();
                    }
                    GUILayout.EndHorizontal();
                    if (newPreset)
                    {
                        GUILayout.BeginHorizontal();
                        newPresetName = GUILayout.TextField(newPresetName, GUILayout.MaxWidth(150));
                        if (GUILayout.Button("Add Preset", GUILayout.MaxWidth(80)))
                        {
                            presetHandler.addNewPreset(newPresetName);
                            reloadProperties = true;
                            Repaint();
                            selectedPreset = presetStrings.Length - 1;
                        }
                        GUILayout.EndHorizontal();
                    }
                    scrollPos = GUILayout.BeginScrollView(scrollPos);
                    if (properties != null)
                    {
                        for (i = 0; i < properties.Count; i++)
                        {
                            if (i % 2 == 0)
                            {
                                GUILayout.BeginHorizontal(propertyBackground);
                            }
                            else
                            {
                                GUILayout.BeginHorizontal();
                            }
                            //properties[i][0] = GUILayout.TextField(properties[i][0], GUILayout.MaxWidth(200));
                            GUILayout.Label(properties[i][0], GUILayout.MaxWidth(150));

                            bool typeFound = false;
                            ShaderUtil.ShaderPropertyType propertyType = ShaderUtil.ShaderPropertyType.Float;
                            for (int p = 0; p < ShaderUtil.GetPropertyCount(activeShader); p++)
                            {
                                if (ShaderUtil.GetPropertyName(activeShader, p) == properties[i][0])
                                {
                                    propertyType = ShaderUtil.GetPropertyType(activeShader, p);
                                    typeFound    = true;
                                    break;
                                }
                            }
                            if (typeFound)
                            {
                                switch (propertyType)
                                {
                                case ShaderUtil.ShaderPropertyType.Color:
                                    float[] rgba = new float[4] {
                                        1, 1, 1, 1
                                    };
                                    string[] rgbaString = properties[i][1].Split(',');
                                    if (rgbaString.Length > 0)
                                    {
                                        float.TryParse(rgbaString[0], out rgba[0]);
                                    }
                                    if (rgbaString.Length > 1)
                                    {
                                        float.TryParse(rgbaString[1], out rgba[1]);
                                    }
                                    if (rgbaString.Length > 2)
                                    {
                                        float.TryParse(rgbaString[2], out rgba[2]);
                                    }
                                    if (rgbaString.Length > 3)
                                    {
                                        float.TryParse(rgbaString[3], out rgba[3]);
                                    }
                                    Color p = EditorGUI.ColorField(EditorGUILayout.GetControlRect(GUILayout.MaxWidth(204)), new GUIContent(), new Color(rgba[0], rgba[1], rgba[2], rgba[3]), true, true, true, new ColorPickerHDRConfig(0, 1000, 0, 1000));
                                    properties[i][1] = "" + p.r + "," + p.g + "," + p.b + "," + p.a;
                                    break;

                                case ShaderUtil.ShaderPropertyType.TexEnv:
                                    string[] guids   = AssetDatabase.FindAssets(properties[i][1]);
                                    Texture  texture = null;
                                    if (guids.Length > 0)
                                    {
                                        texture = AssetDatabase.LoadAssetAtPath <Texture>(AssetDatabase.GUIDToAssetPath(guids[0]));
                                    }
#pragma warning disable CS0618 // Type or member is obsolete
                                    texture = (Texture)EditorGUI.ObjectField(EditorGUILayout.GetControlRect(GUILayout.MaxWidth(100)), texture, typeof(Texture));
#pragma warning restore CS0618 // Type or member is obsolete
                                    if (texture != null)
                                    {
                                        properties[i][1] = texture.name;
                                    }
                                    GUILayout.Label("(" + properties[i][1] + ")", GUILayout.MaxWidth(100));
                                    break;

                                case ShaderUtil.ShaderPropertyType.Vector:
                                    Vector4 vector = Helper.stringToVector(properties[i][1]);
                                    vector           = EditorGUI.Vector4Field(EditorGUILayout.GetControlRect(GUILayout.MaxWidth(204)), "", vector);
                                    properties[i][1] = "" + vector.x + "," + vector.y + "," + vector.z + "," + vector.w;
                                    break;

                                default:
                                    properties[i][1] = GUILayout.TextField(properties[i][1], GUILayout.MaxWidth(204));
                                    break;
                                }
                            }
                            else
                            {
                                properties[i][1] = GUILayout.TextField(properties[i][1], GUILayout.MaxWidth(204));
                            }
                            if (GUILayout.Button("Delete", GUILayout.MaxWidth(80)))
                            {
                                properties.RemoveAt(i);
                                this.reloadProperties = true;
                                saveProperties(presetHandler, presetStrings);
                            }
                            GUILayout.EndHorizontal();
                        }
                        //new preset gui
                        GUILayout.BeginHorizontal();
                        addPropertyIndex = EditorGUILayout.Popup(addPropertyIndex, unusedProperties, GUILayout.MaxWidth(150));
                        if (GUILayout.Button("Add", GUILayout.MaxWidth(80)))
                        {
                            this.reloadProperties = true;
                            properties.Add(new string[] { unusedProperties[addPropertyIndex], "" });
                            saveProperties(presetHandler, presetStrings);
                        }
                        GUILayout.EndHorizontal();
                    }
                    GUILayout.EndScrollView();
                    if (GUILayout.Button("Save", GUILayout.MinWidth(50)))
                    {
                        saveProperties(presetHandler, presetStrings);
                    }
                    Event e = Event.current;
                    if (e.isKey)
                    {
                        if (Event.current.keyCode == (KeyCode.Return))
                        {
                            saveProperties(presetHandler, presetStrings);
                        }
                    }
                }
            }
        }
Example #4
0
 private void saveProperties(PresetHandler presetHandler, string[] presetStrings)
 {
     Debug.Log("Preset saved");
     presetHandler.setPreset(presetStrings[selectedPreset], properties);
     Repaint();
 }
Example #5
0
 private void saveProperties(PresetHandler presetHandler, string[] presetStrings)
 {
     Debug.Log(Locale.editor.Get("preset_saved"));
     presetHandler.setPreset(presetStrings[selectedPreset], properties);
     Repaint();
 }
 public void Init(PresetHandler presetHandler)
 {
     this.presetHandler = presetHandler;
 }
 public static void SetActiveShader(Shader shader, Material material = null, PresetHandler presetHandler = null)
 {
     activeShader              = shader;
     activeShaderMaterial      = material;
     activeShaderPresetHandler = presetHandler;
 }