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); }
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(); } }