Exemple #1
0
    public static RBPaletteGroup CreateInstance(string groupPaletteName = DefaultGroupName, RBPalette basePalette = null)
    {
        RBPaletteGroup paletteGroup = ScriptableObject.CreateInstance <RBPaletteGroup> ();

        paletteGroup.Initialize(groupPaletteName, basePalette);
        return(paletteGroup);
    }
Exemple #2
0
    public static RBPaletteGroup CreatePaletteGroupFromPaletteTexture(string path, string filename, Texture2D sourceTexture, bool overwriteExisting)
    {
        ValidateSaveLocation(path + filename, overwriteExisting);
        RBPaletteGroup paletteGroup = RBPaletteGroup.CreateInstanceFromPaletteTexture(sourceTexture, sourceTexture.name + suffix);

        return((RBPaletteGroup)AssetDatabaseUtility.SaveObject(paletteGroup, path, filename));
    }
    static RBPaletteGroup CreatePaletteGroup()
    {
        string         unformattedFilename = "RBPaletteGroup{0}.asset";
        string         formattedFilename   = string.Empty;
        string         path         = AssetDatabaseUtility.GetDirectoryOfSelection();
        RBPaletteGroup createdGroup = null;

        for (int i = 0; i < 100; i++)
        {
            formattedFilename = string.Format(unformattedFilename, i);
            try {
                createdGroup = CreatePaletteGroup(path, formattedFilename, false);
                break;
            } catch (IOException) {
                // Do nothing, move on to next index
            }
        }

        if (createdGroup == null)
        {
            throw new IOException("Failed to create file. Too many generic RBPaletteGroups exist in save location.");
        }

        // Unlock standalone palette groups by default.
        createdGroup.Locked = false;

        AssetDatabaseUtility.SelectObject(createdGroup);

        return(createdGroup);
    }
Exemple #4
0
    public static RBPaletteGroup CreatePaletteGroup(string path, string filename, bool overwriteExisting)
    {
        ValidateSaveLocation(path + filename, overwriteExisting);
        RBPaletteGroup paletteAsset = RBPaletteGroup.CreateInstance();

        return((RBPaletteGroup)AssetDatabaseUtility.SaveObject(paletteAsset, path, filename));
    }
        public static bool CreatePaletteMapAndKey(string outputPath, Texture2D sourceTexture, RBPaletteGroup suppliedPaletteGroup,
                                                  bool overwriteExistingFiles, string paletteKeyFileName, string paletteMapFilename)
        {
            // If no palette key texture is provided, create a new one from the source image
            RBPalette basePalette = null;

            if (suppliedPaletteGroup == null)
            {
                basePalette = RBPalette.CreatePaletteFromTexture(sourceTexture);
            }
            else
            {
                RBPaletteDiff diff = suppliedPaletteGroup.DiffWithTexture(sourceTexture);
                if (diff.NumChanges > 0)
                {
                    var result = EditorUtility.DisplayDialogComplex("Palettes Differ",
                                                                    $"Current BasePalette is different from the one that would generate from this texture. {diff.Insertions.Count} colors added  and {diff.Deletions.Count} removed." +
                                                                    "Are you sure you want to sync this PaletteGroup to this texture?", "Yes", "No", "Cancel");
                    if (result == 0)
                    {
                        suppliedPaletteGroup.ApplyDiff(diff);
                    }
                    else if (result == 2)
                    {
                        return(false);
                    }
                    //suppliedPaletteGroup.ApplyDiff (diff);
                }
                basePalette = suppliedPaletteGroup.BasePalette;
            }

            // Create the palette map from the palette key
            PaletteMap palettemap;

            palettemap = new PaletteMap(sourceTexture, basePalette);

            if (suppliedPaletteGroup == null)
            {
                // Write PaletteGroup to disk
                try {
                    string         paletteGroupFilename = paletteKeyFileName + ".asset";
                    RBPaletteGroup createdGroup         = RBPaletteCreator.CreatePaletteGroup(outputPath, paletteGroupFilename, overwriteExistingFiles);
                    createdGroup.GroupName = paletteKeyFileName;
                    createdGroup.SetBasePalette(basePalette);
                    createdGroup.Locked = true;
                } catch (IOException) {
                    throw new System.IO.IOException("Failed to write PaletteMap. A PaletteGroup already exists by the name: " + paletteKeyFileName);
                }
            }

            // Write PaletteMap to disk
            string paletteMapFilenameWithExtension = paletteMapFilename + ".png";
            string fullPathToPaletteMapFile        = outputPath + paletteMapFilenameWithExtension;

            palettemap.WriteToFile(fullPathToPaletteMapFile, overwriteExistingFiles);

            return(true);
        }
Exemple #6
0
    public static RBPaletteGroup CreateInstanceFromTexture(Texture2D sourceTexture, string groupPaletteName = DefaultGroupName)
    {
        // Create a base palette from the Texture
        RBPalette paletteFromTexture = RBPalette.CreatePaletteFromTexture(sourceTexture);

        paletteFromTexture.PaletteName = "Base Palette";
        // Create the paletteGroup with the base Palette
        RBPaletteGroup paletteGroup = RBPaletteGroup.CreateInstance(groupPaletteName, paletteFromTexture);

        return(paletteGroup);
    }
Exemple #7
0
    public static RBPaletteGroup ExtractPaletteFromSelection()
    {
        Texture2D selectedTexture = (Texture2D)Selection.activeObject;
        string    selectionPath   = AssetDatabaseUtility.GetDirectoryOfSelection();

        RBPaletteGroup extractedPaletteGroup = ExtractPaletteFromTexture(selectedTexture, selectionPath);

        AssetDatabaseUtility.SelectObject(extractedPaletteGroup);

        return(extractedPaletteGroup);
    }
Exemple #8
0
    public void CreatePaletteGroupForTexture()
    {
        RBPaletteGroup paletteGroup = RBPaletteCreator.ExtractPaletteFromTexture(SourceTexture, AssetDatabaseUtility.GetAssetDirectory(this),
                                                                                 name + "_PaletteGroup.asset");

        if (paletteGroup != null)
        {
            paletteGroup.Locked = true;
            PaletteGroup        = paletteGroup;
        }
    }
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        RBPaletteGroup targetRBPaletteGroup = (RBPaletteGroup)target;

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Colors", EditorStyles.boldLabel);

        if (GUILayout.Button("Add Color", GUILayout.ExpandWidth(false)))
        {
            targetRBPaletteGroup.AddColor();
        }

        colorIndex = EditorGUILayout.IntSlider("Color index: ", colorIndex, 0, targetRBPaletteGroup.NumColorsInPalette - 1);
        if (GUILayout.Button("Remove Color At Index", GUILayout.ExpandWidth(false)))
        {
            targetRBPaletteGroup.RemoveColorAtIndex(colorIndex);
        }

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Palettes", EditorStyles.boldLabel);

        if (GUILayout.Button("Add Palette", GUILayout.ExpandWidth(false)))
        {
            targetRBPaletteGroup.AddPalette();
        }

        paletteIndex = EditorGUILayout.IntSlider("Palette index: ", paletteIndex, 0, targetRBPaletteGroup.Count - 1);
        if (GUILayout.Button("Remove Palette At Index", GUILayout.ExpandWidth(false)))
        {
            targetRBPaletteGroup.RemovePaletteAtIndex(paletteIndex);
        }
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Utilities", EditorStyles.boldLabel);

        if (GUILayout.Button("Export As Texture", GUILayout.ExpandWidth(false)))
        {
            string outputPath = GetPathToAsset(targetRBPaletteGroup);
            string extension  = ".png";
            string filename   = targetRBPaletteGroup.GroupName + extension;
            try {
                targetRBPaletteGroup.WriteToFile(outputPath + filename, false);
            } catch (System.AccessViolationException) {
                if (EditorUtility.DisplayDialog("Warning!",
                                                "This will overwrite the existing file, " + filename +
                                                ". Are you sure you want to export the texture?", "Yes", "No"))
                {
                    targetRBPaletteGroup.WriteToFile(outputPath + filename, true);
                }
            }
        }
    }
Exemple #10
0
    public static RBPaletteGroup CreateInstanceFromPaletteTexture(Texture2D paletteTexture, string groupPaletteName = DefaultGroupName)
    {
        RBPalette[] palettesInTexture = ExtractRBPalettesFromPaletteTexture(paletteTexture);
        // Create the paletteGroup with the base Palette
        RBPaletteGroup paletteGroup = CreateInstance(groupPaletteName, palettesInTexture [0]);

        for (int i = 1; i < palettesInTexture.Length; i++)
        {
            paletteGroup.AddPalette(palettesInTexture [i]);
        }
        return(paletteGroup);
    }
Exemple #11
0
    public override void OnInspectorGUI()
    {
        RBPaletteGroup targetRBPaletteGroup = (RBPaletteGroup)target;

        //DrawDefaultInspector ();
        serializedObject.Update();

        list.elementHeight = GetElementHeight();

        list.DoLayoutList();

        SerializedProperty lockedProperty = serializedObject.FindProperty("Locked");

        lockedProperty.boolValue = EditorGUILayout.Toggle("Locked", lockedProperty.boolValue);
        SetGroupLocked(lockedProperty.boolValue);

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Colors", EditorStyles.boldLabel);

        if (GUILayout.Button("Add Color", GUILayout.ExpandWidth(false)))
        {
            targetRBPaletteGroup.AddColor(RBPaletteGroup.DefaultNewColor);
        }

        colorIndex = EditorGUILayout.IntSlider("Color index: ", colorIndex, 0, targetRBPaletteGroup.NumColorsInPalette - 1);
        if (GUILayout.Button("Remove Color At Index", GUILayout.ExpandWidth(false)))
        {
            targetRBPaletteGroup.RemoveColorAtIndex(colorIndex);
        }

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Utilities", EditorStyles.boldLabel);

        if (GUILayout.Button("Export As Texture", GUILayout.ExpandWidth(false)))
        {
            string outputPath = AssetDatabaseUtility.GetAssetDirectory(targetRBPaletteGroup);
            string extension  = ".png";
            string filename   = targetRBPaletteGroup.GroupName + extension;
            try {
                targetRBPaletteGroup.WriteToFile(outputPath + filename, false);
            } catch (System.AccessViolationException) {
                if (EditorUtility.DisplayDialog("Warning!",
                                                "This will overwrite the existing file, " + filename +
                                                ". Are you sure you want to export the texture?", "Yes", "No"))
                {
                    targetRBPaletteGroup.WriteToFile(outputPath + filename, true);
                }
            }
        }

        serializedObject.ApplyModifiedProperties();
    }
    public static RBPaletteGroup CreatePaletteGroup(string path, string filename, Texture2D sourceTexture, bool overwriteExisting)
    {
        ValidateSaveLocation(path + filename, overwriteExisting);

        // Create a base palette from the Texture
        RBPalette paletteFromTexture = RBPalette.CreatePaletteFromTexture(sourceTexture);

        paletteFromTexture.PaletteName = "Base Palette";

        // Create the paletteGroup with the base Palette
        RBPaletteGroup paletteGroup = RBPaletteGroup.CreateInstance(sourceTexture.name + suffix, paletteFromTexture);

        return((RBPaletteGroup)AssetDatabaseUtility.SaveObject(paletteGroup, path, filename));
    }
Exemple #13
0
    float GetElementHeight()
    {
        // Get number of lines we need to show all colors given the width
        RBPaletteGroup targetRBPaletteGroup = (RBPaletteGroup)target;
        int            numColorsInPalette   = targetRBPaletteGroup.NumColorsInPalette;
        int            numColorLines        = Mathf.CeilToInt(numColorsInPalette / (float)GetMaxNumColorsPerLine());

        // Get size we will need to show all lines
        float sizePerLine  = EditorGUIUtility.singleLineHeight + palettePadding;
        float expectedSize = numColorLines * sizePerLine;

        // At minimum show a line per palette
        expectedSize = Mathf.Max(expectedSize, sizePerLine) + palettePadding;

        return(expectedSize);
    }
        public static void CreatePaletteMapAndKey(string outputPath, Texture2D sourceTexture, RBPaletteGroup suppliedPaletteGroup,
                                                  bool sortPaletteKey, bool overwriteExistingFiles, string paletteKeyFileName, string paletteMapFilename)
        {
            // If no palette key texture is provided, create a new one from the source image
            RBPalette basePalette = null;

            if (suppliedPaletteGroup == null)
            {
                basePalette = RBPalette.CreatePaletteFromTexture(sourceTexture);
                if (sortPaletteKey)
                {
                    basePalette.SortByGrayscale();
                }
            }
            else
            {
                // Sync the palette group up with the texture
                suppliedPaletteGroup.SyncWithTexture(sourceTexture);
                basePalette = suppliedPaletteGroup.BasePalette;
            }

            // Create the palette map from the palette key
            PaletteMap palettemap;

            palettemap = new PaletteMap(sourceTexture, basePalette);

            if (suppliedPaletteGroup == null)
            {
                // Write PaletteGroup to disk
                try {
                    string         paletteGroupFilename = paletteKeyFileName + ".asset";
                    RBPaletteGroup createdGroup         = RBPaletteCreator.CreatePaletteGroup(outputPath, paletteGroupFilename, overwriteExistingFiles);
                    createdGroup.GroupName = paletteKeyFileName;
                    createdGroup.SetBasePalette(basePalette);
                    createdGroup.BasePalette.Locked = true;
                    createdGroup.Locked             = true;
                } catch (IOException) {
                    throw new System.IO.IOException("Failed to write PaletteMap. A PaletteGroup already exists by the name: " + paletteKeyFileName);
                }
            }

            // Write PaletteMap to disk
            string paletteMapFilenameWithExtension = paletteMapFilename + ".png";
            string fullPathToPaletteMapFile        = outputPath + paletteMapFilenameWithExtension;

            palettemap.WriteToFile(fullPathToPaletteMapFile, overwriteExistingFiles);
        }
Exemple #15
0
    static RBPaletteGroup CreatePaletteGroup()
    {
        string         path            = AssetDatabaseUtility.GetDirectoryOfSelection();
        string         filename        = "RBPaletteGroup.asset";
        string         uniqueAssetPath = AssetDatabase.GenerateUniqueAssetPath(path + filename);
        RBPaletteGroup createdGroup    = null;

        try {
            createdGroup = CreatePaletteGroup(path, Path.GetFileName(uniqueAssetPath), false);
        } catch (IOException e) {
            throw new IOException("Failed to create file. Encountered exception: " + e);
        }

        // Unlock standalone palette groups by default.
        createdGroup.Locked = false;

        AssetDatabaseUtility.SelectObject(createdGroup);

        return(createdGroup);
    }
Exemple #16
0
    public static RBPaletteGroup ExtractPaletteFromTexture(Texture2D extractTexture, string savePath, string filename = "")
    {
        if (string.IsNullOrEmpty(filename))
        {
            filename = extractTexture.name + suffix + ".asset";
        }

        RBPaletteGroup createdGroup = null;

        try {
            createdGroup = CreatePaletteGroupFromTexture(savePath, filename, extractTexture, false);
        } catch (IOException) {
            if (EditorUtility.DisplayDialog("Warning!",
                                            "This will overwrite the existing file, " + filename +
                                            ". Are you sure you want to extract the palette?", "Yes", "No"))
            {
                createdGroup = CreatePaletteGroupFromTexture(savePath, filename, extractTexture, true);
            }
        }

        return(createdGroup);
    }
 public static void ValidatePaletteGroup(RBPaletteGroup paletteGroup)
 {
     // Nothing to validate as of right now.
 }
        void OnGUI()
        {
            GUILayout.Label("Palette Map", EditorStyles.boldLabel);
            sourceTexture = EditorGUILayout.ObjectField("Source Texture", sourceTexture, typeof(Texture2D), false);

            bool sourceTextureChanged = lastSourceTexture != sourceTexture;

            if (sourceTextureChanged)
            {
                lastSourceTexture = sourceTexture;

                paletteMapFilename = sourceTexture.name + "_PaletteMap";
                paletteKeyFilename = sourceTexture.name + "_PaletteGroup";
            }

            if (sourceTexture != null)
            {
                paletteMapFilename = EditorGUILayout.TextField("Output Filename", paletteMapFilename);
            }

            GUILayout.Label("Palette Key", EditorStyles.boldLabel);
            paletteKeyOption = (PaletteKeyOption)EditorGUILayout.EnumPopup("Palette Key Creation: ", paletteKeyOption);
            switch (paletteKeyOption)
            {
            case PaletteKeyOption.GenerateNewPaletteKey:
                suppliedPalleteKey = null;
                if (sourceTexture != null)
                {
                    paletteKeyFilename = EditorGUILayout.TextField("Output Filename", paletteKeyFilename);
                }
                sortPalette = EditorGUILayout.Toggle("Sort PaletteKey", sortPalette);
                break;

            case PaletteKeyOption.SupplyCustomPaletteKey:
                suppliedPalleteKey = EditorGUILayout.ObjectField("Linked Palette Group", suppliedPalleteKey, typeof(RBPaletteGroup), false);
                sortPalette        = false;
                break;
            }

            GUILayout.Label("Options", EditorStyles.boldLabel);
            overwriteExistingFiles = EditorGUILayout.Toggle("Overwite Existing files", overwriteExistingFiles);

            if (GUILayout.Button("Build"))
            {
                if (sourceTexture == null)
                {
                    Debug.LogError("RBPaletteMapper Error: No source texture specified");
                    return;
                }

                // Validate source texture
                Texture2D inTexture = (Texture2D)sourceTexture;
                try {
                    RBPaletteMapper.ValidateSourceTexture(inTexture);
                } catch (System.BadImageFormatException e) {
                    Debug.LogError("RBPaletteMapper Error: " + e.Message);
                    return;
                }

                // Validate or skip Palette Key
                RBPaletteGroup inPaletteKey = null;
                if (paletteKeyOption == PaletteKeyOption.SupplyCustomPaletteKey)
                {
                    if (suppliedPalleteKey == null)
                    {
                        Debug.LogError("RBPaletteMapper Error: Trying to use custom palette key but no palette key specified." +
                                       "\nPlease select a RBPaletteGroup to use as the Palette Key.");
                        return;
                    }
                    inPaletteKey = (RBPaletteGroup)suppliedPalleteKey;
                    try {
                        RBPaletteMapper.ValidatePaletteGroup(inPaletteKey);
                    } catch {
                        Debug.LogError("Unknown PaletteMap Error encountered while Validating supplied PaletteGroup.");
                        return;
                    }
                }

                string path = AssetDatabaseUtility.GetAssetDirectory(inTexture);
                try {
                    RBPaletteMapper.CreatePaletteMapAndKey(path, inTexture, inPaletteKey, sortPalette, overwriteExistingFiles, paletteKeyFilename, paletteMapFilename);

                    Debug.Log("<color=green>Palette Map and Key for file " + inTexture.name + " created successfully</color>");
                    // TODO: Better error handling messages
                } catch (System.NotSupportedException e) {
                    LogError(e.Message, e);
                } catch (System.ArgumentException e) {
                    LogError(e.Message, e);
                } catch (System.AccessViolationException e) {
                    LogError(e.Message, e);
                } catch (System.IO.IOException e) {
                    LogError("Encountered IO Exception: " + e.Message, e);
                } catch (System.Exception e) {
                    LogError("Encountered unknown error: " + e.Message, e);
                }
            }
        }