Esempio n. 1
0
    public void SyncWithTexture(Texture2D sourceTexture)
    {
        Color[] sourcePixels = sourceTexture.GetPixels();

        // Unlock the PaletteGroup so that we can edit it.
        bool wasLocked = Locked;

        Locked = false;
        bool wasPaletteLocked = BasePalette.Locked;

        BasePalette.Locked = false;

        // Add new colors from the texture into the Palette
        List <Color> seenColors = new List <Color>();

        for (int i = 0; i < sourcePixels.Length; i++)
        {
            Color colorAtSource = RBPalette.ClearRGBIfNoAlpha(sourcePixels [i]);
            int   index         = BasePalette.IndexOf(colorAtSource);
            bool  colorNotFound = index < 0;
            if (colorNotFound)
            {
                AddColor();
                BasePalette [BasePalette.Count - 1] = colorAtSource;                 // Note this assumes color is added to the end...
            }
            else
            {
                // Add unique seen colors to list of seen colors
                if (!seenColors.Contains(colorAtSource))
                {
                    seenColors.Add(colorAtSource);
                }
            }
        }

        // Remove unused colors, back to front to avoid shifting indeces
        for (int i = BasePalette.Count - 1; i >= 0; i--)
        {
            bool colorWasSeen = seenColors.Contains(BasePalette[i]);
            if (!colorWasSeen)
            {
                RemoveColorAtIndex(i);
            }
        }

        // Relock the palette group
        Locked             = wasLocked;
        BasePalette.Locked = wasPaletteLocked;
    }
Esempio n. 2
0
    public void ApplyDiff(RBPaletteDiff diff)
    {
        // Unlock the PaletteGroup so that we can edit it.
        bool wasLocked = Locked;

        Locked = false;

        // Add new colors to the palette
        for (int i = 0; i < diff.Insertions.Count; i++)
        {
            AddColor(diff.Insertions [i]);
        }

        // Remove unused colors
        for (int i = 0; i < diff.Deletions.Count; i++)
        {
            int unusedColorIndex = BasePalette.IndexOf(diff.Deletions [i]);
            RemoveColorAtIndex(unusedColorIndex);
        }

        // Relock the palette group
        Locked = wasLocked;
    }
        private async void bOK_Click(object sender, System.EventArgs e)
        {
            Enabled = false;
            bool exportAll = cbSelectAll.Checked;

            List <int> selected = new List <int>(lboxItems.SelectedIndices.Count);

            if (!exportAll)
            {
                foreach (int s in lboxItems.SelectedIndices)
                {
                    selected.Add(s);
                }
            }

            try
            {
                string        rpath             = tbFilepath.Text;
                string        rootDir           = Path.GetDirectoryName(rpath);
                string        relImagePathFmt   = @"images\img_{0:0000}.png";
                string        relPalettePathFmt = @"palettes\pal_{0:0000}.bin";
                Palette_18Bit pal = BasePalette.Clone();
                Directory.CreateDirectory(rootDir);
                Directory.CreateDirectory(Path.Combine(rootDir, "images"));
                Directory.CreateDirectory(Path.Combine(rootDir, "palettes"));

                ResourceExportINIFile rfile = new ResourceExportINIFile();
                rfile.EntryCount = _resource.Resources.Count;

                Progress <int> p = new Progress <int>();
                pbarProgress.Maximum = rfile.EntryCount;
                pbarProgress.Value   = 0;
                lblProgress.Text     = "Setting up...";
                p.ProgressChanged   += (s, id) =>
                {
                    pbarProgress.Value = id;
                    lblProgress.Text   = "{0} / {1}".F(id, rfile.EntryCount);
                };

                await Task.Factory.StartNew(() =>
                {
                    if (exportAll)
                    {
                        rfile.Export(pal, _resource.Resources, rootDir, relImagePathFmt, relPalettePathFmt, p);
                    }
                    else
                    {
                        foreach (int index in selected.ToArray())
                        {
                            rfile.Export(pal, index, _resource.Resources[index], rootDir, relImagePathFmt.F(index), relPalettePathFmt.F(index), p);
                        }
                    }
                    rfile.WriteToFile(rpath);
                });

                pbarProgress.Value = pbarProgress.Maximum;
                lblProgress.Text   = "Finished!";
                MessageBox.Show("Export completed!");
            }
            catch (AggregateException ex)
            {
                MessageBox.Show("Export failed.\n\nReason: {0}".F(ex.InnerException?.Message ?? ex.Message));
            }
            finally
            {
                pbarProgress.Value = 0;
                lblProgress.Text   = "";
                Enabled            = true;
            }
        }