コード例 #1
0
ファイル: SwatchPresetExporter.cs プロジェクト: bmjoy/swatchr
        /*
         * - m_Name:
         *      m_Color: {r: 0.18616219, g: 0.05374134, b: 0.52205884, a: 1}
         * - m_Name:
         *      m_Color: {r: 0.31163496, g: 0.45094258, b: 0.9632353, a: 1}
         */

        public static void ExportToColorPresetLibrary(Swatch swatch)
        {
            var swatchProjectPath = AssetDatabase.GetAssetPath(swatch.GetInstanceID());
            var swatchDirectory   = Path.GetDirectoryName(swatchProjectPath);
            var libraryDirectory  = swatchDirectory + "/Editor";

            if (!AssetDatabase.IsValidFolder(libraryDirectory))
            {
                AssetDatabase.CreateFolder(swatchDirectory, "Editor");
            }

            var    libraryProjectpath = libraryDirectory + "/" + swatch.name + ".colors";
            var    fullFileName       = libraryProjectpath.Replace("Assets", Application.dataPath);
            string fileText           = COLORS_TEMPLATE;

            for (int i = 0; i < swatch.colors.Length; i++)
            {
                fileText += GetYAMLForColor(swatch.colors[i]);
            }
            Debug.Log("[SwatchPresetExporter] writing to " + fullFileName);
            File.WriteAllText(fullFileName, fileText);
            AssetDatabase.ImportAsset(libraryProjectpath);
        }
コード例 #2
0
        public static bool DrawColorPallete(Swatch swatch, ref int colorKey, bool drawNewColorButton)
        {
            if (swatch == null)
            {
                return(false);
            }

            var lastRect = GUILayoutUtility.GetLastRect();

            if (swatch.colors != null && swatch.colors.Length > 0)
            {
                int swatchHash = swatch.cachedTexture.GetHashCode();
                if (palleteTexture == null || palleteTextureCachedHashCode != swatchHash)
                {
                    if (palleteTexture == null)
                    {
                                                #if SWATCHR_VERBOSE
                        Debug.LogWarning("[SwatchrPalleteDrawer] creating pallete texture because there is none");
                                                #endif
                    }
                    else
                    {
                                                #if SWATCHR_VERBOSE
                        Debug.LogWarningFormat("[SwatchrPalleteDrawer] creating pallete texture because cache miss. {0} != {1}", palleteTextureCachedHashCode, swatchHash);
                                                #endif
                    }
                    palleteTexture = textureWithColors(swatch.colors);
                    palleteTextureCachedHashCode = swatchHash;
                }
            }
            else
            {
                palleteTexture = null;
            }

            if (blackTexture == null)
            {
                                #if SWATCHR_VERBOSE
                Debug.LogWarning("[SwatchrPalleteDrawer] creating black texture");
                                #endif
                blackTexture = textureWithColor(Color.black);
            }
            if (whiteTexture == null)
            {
                                #if SWATCHR_VERBOSE
                Debug.LogWarning("[SwatchrPalleteDrawer] creating white texture");
                                #endif
                whiteTexture = textureWithColor(Color.white);
            }

            int numColors      = swatch.colors != null ? swatch.colors.Length : 0;
            int numPerRow      = itemsPerRow;
            int numInBottomRow = numColors % numPerRow;

            float heightOfPallete = 0;
            var   textureRect     = new Rect(lastRect.x, lastRect.y + lastRect.height, 0.0f, 0.0f);
            if (palleteTexture != null)
            {
                textureRect     = new Rect(lastRect.x, lastRect.y + lastRect.height, palleteTexture.width * EditorGUIUtility.singleLineHeight, palleteTexture.height * EditorGUIUtility.singleLineHeight);
                heightOfPallete = textureRect.height;
            }

            if (numInBottomRow == 0)
            {
                heightOfPallete += EditorGUIUtility.singleLineHeight;
            }

            Rect clickRect = textureRect;
            if (swatch.colors == null || swatch.colors.Length == 0)
            {
                clickRect.width = EditorGUIUtility.singleLineHeight;
            }
            clickRect.height = heightOfPallete;

            GUILayoutUtility.GetRect(clickRect.width, clickRect.height);
            if (palleteTexture != null)
            {
                DrawTexture(palleteTexture, textureRect);
                DrawBlackGrid(textureRect.x, textureRect.y, swatch.colors.Length, palleteTexture.width, palleteTexture.height, (int)EditorGUIUtility.singleLineHeight, blackTexture);
            }

            if (drawNewColorButton)
            {
                DrawNewColorButton(numColors, textureRect);
            }

            bool somethingHasChanged = false;
            if (IsClick())
            {
                if (IsClickInRect(clickRect))
                {
                    var     e = Event.current;
                    Vector2 rectClickPosition = e.mousePosition - textureRect.position;
                    int     cellXIndex        = (int)(rectClickPosition.x / EditorGUIUtility.singleLineHeight);
                    int     cellYIndex        = (int)(rectClickPosition.y / EditorGUIUtility.singleLineHeight);
                    int     textureWidth      = palleteTexture != null ? palleteTexture.width : 0;
                    int     clickedOnKey      = cellYIndex * textureWidth + cellXIndex;
                    if (numColors > 0 && clickedOnKey < numColors)
                    {
                        colorKey            = clickedOnKey;
                        somethingHasChanged = true;
                    }
                    else if (clickedOnKey == numColors)
                    {
                        colorKey = clickedOnKey;
                        System.Array.Resize(ref swatch.colors, numColors + 1);
                        swatch.colors[colorKey] = Color.white;
                        swatch.SignalChange();
                        somethingHasChanged = true;
                    }
                    else
                    {
                    }
                }
            }

            if (swatch.colors != null && swatch.colors.Length > 0)
            {
                DrawOnSelectedCell(colorKey, textureRect);
                int   selectedColorRow = colorKey / SwatchrPaletteDrawer.itemsPerRow;
                float selectedColorY   = selectedColorRow * EditorGUIUtility.singleLineHeight + EditorGUIUtility.singleLineHeight;
                var   colorKeyRect     = new Rect(lastRect.x + SwatchrPaletteDrawer.itemsPerRow * EditorGUIUtility.singleLineHeight, lastRect.y + selectedColorY, 64, EditorGUIUtility.singleLineHeight);
                EditorGUI.LabelField(colorKeyRect, colorKey.ToString());
            }

            return(somethingHasChanged);
        }
コード例 #3
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            SwatchrColor swatchrColor = (SwatchrColor)fieldInfo.GetValue(property.serializedObject.targetObject);
            Swatch       swatch       = swatchrColor.swatch;
            Color        color        = swatchrColor.color;

            if (swatchTexture == null)
            {
                                #if SWATCHR_VERBOSE
                Debug.LogWarning("[swatchrColorDrawer] creating swatch texture");
                                #endif
                swatchTexture = textureWithColor(color);
            }

            var swatchProperty        = property.FindPropertyRelative("_swatch");
            var colorIndexProperty    = property.FindPropertyRelative("_colorIndex");
            var overrideColorProperty = property.FindPropertyRelative("_overrideColor");

            position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

            var swatchSize = EditorGUIUtility.singleLineHeight;
            var keySize    = EditorGUIUtility.singleLineHeight * 1.25f;
            var spacing    = EditorGUIUtility.singleLineHeight * 0.5f;
            //var toggleSize				= EditorGUIUtility.singleLineHeight;
            var toggleSize            = 0;
            var swatchObjectPositionX = swatch == null ? position.x : position.x + swatchSize + keySize + toggleSize + spacing * 2;

            //var swatchObjectWidth = swatch == null ? position.width : position.width - swatchSize - keySize - spacing * 2;
            var   fullWidth         = position.width - swatchObjectPositionX + position.x;
            float swatchObjectWidth = fullWidth;
            float colorWidth        = 0.25f * fullWidth;
            if (swatch == null)
            {
                swatchObjectWidth *= 0.75f;
            }
            var swatchObjectRect = new Rect(swatchObjectPositionX, position.y, swatchObjectWidth, EditorGUIUtility.singleLineHeight);
            var swatchRect       = new Rect(position.x, position.y, swatchSize, EditorGUIUtility.singleLineHeight);
            var colorIndexRect   = new Rect(swatchRect.position.x + swatchRect.width + spacing, position.y, keySize, EditorGUIUtility.singleLineHeight);
            var colorField       = new Rect(position.x + position.width - colorWidth, position.y, colorWidth, EditorGUIUtility.singleLineHeight);

            EditorGUI.BeginProperty(position, label, property);


            var indent = EditorGUI.indentLevel;
            EditorGUI.indentLevel = 0;



            // Draw Swatch object
            EditorGUI.BeginChangeCheck();
            EditorGUI.PropertyField(swatchObjectRect, swatchProperty, GUIContent.none);
            if (EditorGUI.EndChangeCheck())
            {
                property.serializedObject.ApplyModifiedProperties();
                swatchrColor.swatch = swatchrColor._swatch;                 // hack which calls observer pattern
                UpdateActiveSwatch(swatchrColor.color);
            }

            if (swatch != null)
            {
                // Draw Color Field
                if (DrawTextureButton(swatchTexture, swatchRect))
                {
                    paletteOpen = !paletteOpen && swatch != null && swatch.colors != null && swatch.colors.Length > 0;
                }
                DrawBlackGrid(swatchRect.x, swatchRect.y, 1, 1, (int)EditorGUIUtility.singleLineHeight);

                // Draw Color index text field
                EditorGUI.BeginChangeCheck();
                EditorGUI.PropertyField(colorIndexRect, colorIndexProperty, GUIContent.none);
                if (EditorGUI.EndChangeCheck())
                {
                    property.serializedObject.ApplyModifiedProperties();
                    swatchrColor.colorIndex = colorIndexProperty.intValue;                     // hack which calls observer pattern
                    UpdateActiveSwatch(swatchrColor.color);
                }
                // Draw Toggle
                //EditorGUI.PropertyField(usingSwatchGroupToggleR, usingSwatchGroupProperty, GUIContent.none);
                //usingSwatchGroupProperty.boolValue = EditorGUI.Toggle(usingSwatchGroupToggleR, usingSwatchGroupProperty.boolValue);

                if (paletteOpen)
                {
                    int swatchHash = swatch.cachedTexture.GetHashCode();
                    if (palleteTexture == null || palleteTextureCachedHash != swatchHash)
                    {
                                                #if SWATCHR_VERBOSE
                        Debug.LogWarning("[swatchrColorDrawer] creating pallete texture");
                                                #endif
                        palleteTexture           = textureWithColors(swatch.colors);
                        palleteTextureCachedHash = swatchHash;
                    }
                    var textureRect = new Rect(swatchRect.x, swatchRect.y + EditorGUIUtility.singleLineHeight + 3, palleteTexture.width * EditorGUIUtility.singleLineHeight, palleteTexture.height * EditorGUIUtility.singleLineHeight);
                    DrawTexture(palleteTexture, textureRect);
                    DrawBlackGrid(textureRect.x, textureRect.y, palleteTexture.width, palleteTexture.height, (int)EditorGUIUtility.singleLineHeight);

                    // listen to click
                    Event e = Event.current;
                    if (IsClickInRect(textureRect))
                    {
                        Vector2 rectClickPosition = e.mousePosition - textureRect.position;
                        int     cellXIndex        = (int)(rectClickPosition.x / EditorGUIUtility.singleLineHeight);
                        int     cellYIndex        = (int)(rectClickPosition.y / EditorGUIUtility.singleLineHeight);
                        int     colorIndex        = cellYIndex * palleteTexture.width + cellXIndex;
                        colorIndexProperty.intValue = colorIndex;
                        property.serializedObject.ApplyModifiedProperties();
                        swatchrColor.colorIndex = colorIndex;                         //  calls observer pattern
                        UpdateActiveSwatch(swatchrColor.color);
                    }
                    else if (IsClick())
                    {
                        paletteOpen = false;
                        EditorUtility.SetDirty(property.serializedObject.targetObject);                         // Repaint
                    }
                }
            }
            else
            {
                EditorGUI.BeginChangeCheck();
                EditorGUI.PropertyField(colorField, overrideColorProperty, GUIContent.none);
                if (EditorGUI.EndChangeCheck())
                {
                    property.serializedObject.ApplyModifiedProperties();
                    swatchrColor.colorIndex = colorIndexProperty.intValue;                     // hack which calls observer pattern
                }
            }
            // Set indent back to what it was
            EditorGUI.indentLevel = indent;
            EditorGUI.EndProperty();
        }
コード例 #4
0
        public override void OnInspectorGUI()
        {
            Swatch swatch = (Swatch)target;

            // Swatch
            {
                EditorGUILayout.LabelField("Swatch", EditorStyles.boldLabel);
                // if (swatch.colors != null && swatch.colors.Length > 0) {
                var startingRect = GUILayoutUtility.GetLastRect();
                if (SwatchrPaletteDrawer.DrawColorPallete(swatch, ref colorRef, true))
                {
                    Repaint();
                }

                if (swatch.numColors > 0)
                {
                    var   selectedColor    = swatch.GetColor(colorRef);
                    int   selectedColorRow = colorRef / SwatchrPaletteDrawer.itemsPerRow;
                    float selectedColorY   = selectedColorRow * EditorGUIUtility.singleLineHeight + EditorGUIUtility.singleLineHeight;
                    // EditorGUI.LabelField(colorKeyRect, ""+colorRef);
                    var changeColorRect = new Rect(startingRect.x + SwatchrPaletteDrawer.itemsPerRow * EditorGUIUtility.singleLineHeight + 30, startingRect.y + selectedColorY, 64, EditorGUIUtility.singleLineHeight);

                    EditorGUI.BeginChangeCheck();
                    var newColor = EditorGUI.ColorField(changeColorRect, selectedColor);
                    if (EditorGUI.EndChangeCheck())
                    {
                        swatch.colors[colorRef] = newColor;
                        swatch.SignalChange();
                        GameViewRepaint();
                    }
                    int x = (int)(changeColorRect.x + changeColorRect.width + 2);
                    int y = (int)(changeColorRect.y + changeColorRect.height - EditorGUIUtility.singleLineHeight);
                    if (SwatchrPaletteDrawer.DrawDeleteButton(x, y))
                    {
                        if (colorRef + 1 < swatch.colors.Length)
                        {
                            Array.Copy(swatch.colors, colorRef + 1, swatch.colors, colorRef, swatch.colors.Length - colorRef - 1);
                        }
                        Array.Resize <Color>(ref swatch.colors, swatch.colors.Length - 1);
                        if (colorRef >= swatch.colors.Length)
                        {
                            colorRef = swatch.colors.Length - 1;
                            if (colorRef < 0)
                            {
                                colorRef = 0;
                            }
                        }
                        swatch.SignalChange();
                        GameViewRepaint();
                    }
                }
                //}
            }

            // Add
            {
                EditorGUILayout.LabelField("Add", EditorStyles.boldLabel);
                if (GUILayout.Button("Add .ASE"))
                {
                    var path = EditorUtility.OpenFilePanel("Swatchr Import", "", "ase");
                    if (path != null && path != string.Empty)
                    {
                        Debug.Log("[SwatchEditorGUI] path " + path);
                        SwatchASEFile aseFile = new SwatchASEFile(path);
                        swatch.AddColorsFromASEFile(aseFile);
                        GameViewRepaint();
                    }
                }

                if (GUILayout.Button("Add .ASE Folder"))
                {
                    var path = EditorUtility.OpenFolderPanel("Swatchr Folder Import", "", "");
                    //var path = EditorUtility.OpenFilePanel("Import", "", "ase");
                    if (path != null && path != string.Empty)
                    {
                        var files = Directory.GetFiles(path);
                        for (int i = 0; i < files.Length; i++)
                        {
                            string file = files[i];
                            if (file.EndsWith(".ase"))
                            {
                                SwatchASEFile aseFile = new SwatchASEFile(file);
                                swatch.AddColorsFromASEFile(aseFile);
                                GameViewRepaint();
                            }
                        }
                    }
                }

                if (GUILayout.Button("Add Texture"))
                {
                    var path = EditorUtility.OpenFilePanel("Swatchr Import Texture", "", "png");
                    if (path != null && path != string.Empty)
                    {
                        Debug.Log("[SwatchEditorGUI] importing texture at path " + path);

                        var bytes = File.ReadAllBytes(path);
                        var tex   = new Texture2D(1, 1);
                        tex.LoadImage(bytes);
                        var pixels = tex.GetPixels();
                        if (pixels != null && pixels.Length > 0)
                        {
                            //int i = swatch.colors.Length;
                            int i = 0;
                            Array.Resize <Color>(ref swatch.colors, pixels.Length);
                            for (int j = 0; j < pixels.Length; j++)
                            {
                                swatch.colors[i++] = pixels[j];
                            }
                            swatch.SignalChange();
                            GameViewRepaint();
                        }
                    }
                }
            }

            // Replace
            {
                EditorGUILayout.LabelField("Replace", EditorStyles.boldLabel);
                if (replace)
                {
                    // Object Field
                    replaceObject = (Swatch)EditorGUILayout.ObjectField(replaceObject, typeof(Swatch), false);
                    // Confirm
                    EditorGUI.BeginDisabledGroup(replaceObject == null);
                    if (GUILayout.Button("Replace"))
                    {
                        swatch.ReplaceSelfWithOtherSwatch(replaceObject);
                        GameViewRepaint();
                        replaceObject = null;
                        //replace = false;
                    }
                    EditorGUI.EndDisabledGroup();
                }
                // Start & Cancel
                if (GUILayout.Button(replace ? "Cancel" : "Replace"))
                {
                    replace       = !replace;
                    replaceObject = null;
                }
            }

            // Merge
            {
                EditorGUILayout.LabelField("Merge", EditorStyles.boldLabel);
                if (merge)
                {
                    // Object Field
                    mergeObject = (Swatch)EditorGUILayout.ObjectField(mergeObject, typeof(Swatch), false);
                    // Confirm
                    EditorGUI.BeginDisabledGroup(mergeObject == null);
                    if (GUILayout.Button("Merge"))
                    {
                        swatch.AddColorsFromOtherSwatch(mergeObject);
                        GameViewRepaint();
                        mergeObject = null;
                        merge       = false;
                    }
                    EditorGUI.EndDisabledGroup();
                }
                // Start & Cancel
                if (GUILayout.Button(merge ? "Cancel" : "Merge"))
                {
                    mergeObject = null;
                    merge       = !merge;
                }
            }

            // Export
            {
                EditorGUILayout.LabelField("Export", EditorStyles.boldLabel);
                if (GUILayout.Button("Export To Color Picker Presets"))
                {
                    SwatchPresetExporter.ExportToColorPresetLibrary(swatch);
                }
                if (GUILayout.Button("Export To Texture"))
                {
                    SwatchCreator.ExportSwatchToTexture();
                }
                EditorGUILayout.Space();
            }

            // Save
            if (GUILayout.Button("Save"))
            {
                EditorUtility.SetDirty(swatch);
                AssetDatabase.SaveAssets();
            }
        }