Exemplo n.º 1
0
 public TilePalette(PaletteModel palette, int foregroundIndex, int backgroundIndex, int Glyph)
 {
     this.palette         = palette;
     this.foregroundIndex = foregroundIndex;
     this.backgroundIndex = backgroundIndex;
     this.Glyph           = Glyph;
 }
Exemplo n.º 2
0
    public static Palette MapToResource(this PaletteModel model, IColorFactory colorFactory)
    {
        var size    = colorFactory.CreateColor(model.ColorModel).Size;
        var sources = new List <IColorSource>();

        foreach (var source in model.ColorSources)
        {
            if (source is FileColorSourceModel fileColorSource)
            {
                var fileSources = Enumerable.Range(0, fileColorSource.Entries)
                                  .Select(x => fileColorSource.FileAddress + size * x)
                                  .Select(x => new FileColorSource(x, fileColorSource.Endian))
                                  .ToList();
                sources.AddRange(fileSources);
            }
            else if (source is ProjectNativeColorSourceModel nativeSource)
            {
                sources.Add(new ProjectNativeColorSource(nativeSource.Value));
            }
            else if (source is ProjectForeignColorSourceModel foreignSource)
            {
                sources.Add(new ProjectForeignColorSource(foreignSource.Value));
            }
            //else if (source is ScatteredColorSourceModel scatteredSource)
            //{

            //}
        }

        return(new Palette(model.Name, colorFactory, model.ColorModel, sources, model.ZeroIndexTransparent, model.StorageSource));
    }
Exemplo n.º 3
0
 public PaletteEditorWindow(PaletteModel model, int displayIndex)
 {
     InitializeComponent();
     dataModel     = model;
     backup        = (PaletteModel)dataModel.Clone();
     DataContext   = backup;
     Title        += " - Palette " + displayIndex;
     loadPalDialog = new System.Windows.Forms.OpenFileDialog();
     loadPalDialog.CheckFileExists = true;
     loadPalDialog.DefaultExt      = "pal";
     loadPalDialog.Filter          = "JASC-PAL RIFF palette files|*.pal";
     loadPalDialog.Multiselect     = false;
     loadPalDialog.ShowHelp        = false;
     loadPalDialog.Title           = "Import palette from file";
     loadImageDialog = new System.Windows.Forms.OpenFileDialog();
     loadImageDialog.CheckFileExists = true;
     loadImageDialog.DefaultExt      = "png";
     loadImageDialog.Filter          = "Images|*.png;*.jpg;*.jpeg;*.bmp";
     loadImageDialog.Multiselect     = false;
     loadImageDialog.ShowHelp        = false;
     loadImageDialog.Title           = "Import palette from image";
     savePalDialog = new System.Windows.Forms.SaveFileDialog();
     savePalDialog.OverwritePrompt = true;
     savePalDialog.DefaultExt      = "pal";
     savePalDialog.Filter          = "Palette files|*.pal";
     savePalDialog.ShowHelp        = false;
     savePalDialog.Title           = "Export palette";
 }
        // event handler pro filtraci palety podle vybrané kategorie
        private void CDElementsListbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (palette == null)
            {
                return;
            }
            String category = ((sender as ListBox).SelectedItem as ListBoxItem).Content as String;

            palette.Model.NodesSource = PaletteModel.Where(x => x.Category == category).OrderBy(x => x.Stereotype).ThenBy(x => x.Name).ToList();
        }
Exemplo n.º 5
0
        protected void UpdateVMColors(PaletteModel model)
        {
            Colors.UpdateAfter(() =>
            {
                for (int i = 0; i < model.Data.Length; i++)
                {
                    Colors[i] = model.Data[i];
                }
            });

            CurrentColorIndex = 0;
        }
Exemplo n.º 6
0
    public static PaletteModel MapToModel(this Palette pal, Dictionary <IProjectResource, string> resourceMap, IColorFactory colorFactory)
    {
        var size = colorFactory.CreateColor(pal.ColorModel).Size;

        var model = new PaletteModel()
        {
            Name                 = pal.Name,
            ColorModel           = pal.ColorModel,
            ZeroIndexTransparent = pal.ZeroIndexTransparent,
        };

        if (pal.DataSource is not null && resourceMap.TryGetValue(pal.DataSource, out var dataFileKey))
        {
            model.DataFileKey = dataFileKey;
        }

        int i = 0;

        while (i < pal.ColorSources.Length)
        {
            if (pal.ColorSources[i] is FileColorSource fileSource)
            {
                var sources = pal.ColorSources.Skip(i)
                              .TakeWhile((x, i) => x is FileColorSource && (x as FileColorSource).Offset == (fileSource.Offset + i * size))
                              .ToList();

                var sourceModel = new FileColorSourceModel(fileSource.Offset, sources.Count, fileSource.Endian);
                model.ColorSources.Add(sourceModel);

                i += sources.Count;
            }
            else if (pal.ColorSources[i] is ProjectNativeColorSource nativeSource)
            {
                var nativeModel = new ProjectNativeColorSourceModel(nativeSource.Value);
                model.ColorSources.Add(nativeModel);
                i++;
            }
            else if (pal.ColorSources[i] is ProjectForeignColorSource foreignSource)
            {
                var foreignModel = new ProjectForeignColorSourceModel(foreignSource.Value);
                model.ColorSources.Add(foreignModel);
                i++;
            }
            else if (pal.ColorSources[i] is ScatteredColorSource scatteredSource)
            {
            }
        }

        return(model);
    }
Exemplo n.º 7
0
        private void BuildPalettes()
        {
            SignalManager.Get <WriteBuildOutputSignal>().Dispatch("Building palettes", OutputMessageType.Information);

            ProjectModel projectModel = ModelManager.Get <ProjectModel>();

            string fullPath = Path.Combine(Path.GetFullPath(projectModel.Build.OutputFilePath), "palettes.s");

            using (StreamWriter outputFile = new StreamWriter(fullPath))
            {
                List <FileModelVO> paletteModelVOs = ProjectFiles.GetModels <PaletteModel>();

                outputFile.WriteLine("; This file is autogenerated!");

                SortedDictionary <string, StringWriter> pals = new SortedDictionary <string, StringWriter>();

                foreach (FileModelVO vo in paletteModelVOs)
                {
                    PaletteModel model = vo.Model as PaletteModel;

                    string name = "palette_" + vo.Name.Replace(' ', '_').ToLower();

                    Color color0 = Util.GetColorFromInt(model.Color0);
                    Color color1 = Util.GetColorFromInt(model.Color1);
                    Color color2 = Util.GetColorFromInt(model.Color2);
                    Color color3 = Util.GetColorFromInt(model.Color3);

                    StringWriter stringWriter = new StringWriter();

                    stringWriter.Write($"    .byte ");
                    stringWriter.Write($"${Util.ColorToColorHex(color0)},");
                    stringWriter.Write($"${Util.ColorToColorHex(color1)},");
                    stringWriter.Write($"${Util.ColorToColorHex(color2)},");
                    stringWriter.Write($"${Util.ColorToColorHex(color3)}");

                    pals.Add(name, stringWriter);
                }

                foreach (var item in pals)
                {
                    outputFile.WriteLine("");
                    outputFile.WriteLine($"{item.Key}:");

                    outputFile.Write(item.Value.ToString());
                    outputFile.Write(Environment.NewLine);
                }
            }

            SignalManager.Get <WriteBuildOutputSignal>().Dispatch("Finished building palettes", OutputMessageType.Information);
        }
Exemplo n.º 8
0
 private void ImportPaletteButton_Click(object sender, RoutedEventArgs e)
 {
     loadPalDialog.FileName = "";
     System.Windows.Forms.DialogResult result = loadPalDialog.ShowDialog();
     if (result == System.Windows.Forms.DialogResult.Abort || result == System.Windows.Forms.DialogResult.Cancel)
     {
         return;
     }
     try
     {
         PaletteModel pal = new PaletteModel(JASCPAL.Import(loadPalDialog.FileName));
         backup.Colors = pal.Colors;
         ColorListBox.SelectedIndex = 0;
     }
     catch (Exception ex)
     {
         MessageBox.Show("Error: " + ex.Message, "Import error", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
Exemplo n.º 9
0
    public MagitekResult AddPalette(PaletteModel paletteModel, string parentNodePath, string fileLocation)
    {
        var pal = paletteModel.MapToResource(_colorFactory);

        if (!Tree.TryGetItem <DataSource>(paletteModel.DataFileKey, out var df))
        {
            return(new MagitekResult.Failed($"Palette '{pal.Name}' could not locate DataFile with key '{paletteModel.DataFileKey}'"));
        }

        pal.DataSource = df;
        pal.LazyLoadPalette(pal.DataSource, pal.ColorModel, pal.ZeroIndexTransparent);

        var palNode = new PaletteNode(pal.Name, pal)
        {
            DiskLocation = fileLocation,
            Model        = paletteModel
        };

        Tree.TryGetNode(parentNodePath, out var parentNode);
        parentNode.AttachChildNode(palNode);

        return(MagitekResult.SuccessResult);
    }
Exemplo n.º 10
0
 private void SwitchPalette()
 {
     CurrentPalette = palettesDataProvider.GetPalette(CurrentPaletteRef);
     BitmapHelper.SetPaletteColors(model.Bitmap, CurrentPalette.Data);
 }
Exemplo n.º 11
0
    private bool TryDeserializePalette(XElement element, string resourceName, out PaletteModel paletteModel)
    {
        var model = new PaletteModel();

        model.Name                 = resourceName;
        model.DataFileKey          = element.Attribute("datafile").Value;
        model.ColorModel           = Palette.StringToColorModel(element.Attribute("color").Value);
        model.ZeroIndexTransparent = bool.Parse(element.Attribute("zeroindextransparent").Value);

        foreach (var item in element.Elements())
        {
            if (item.Name.LocalName == "filesource")
            {
                var source     = new FileColorSourceModel();
                var fileOffset = long.Parse(item.Attribute("fileoffset").Value, System.Globalization.NumberStyles.HexNumber);
                if (item.Attribute("bitoffset") is null)
                {
                    source.FileAddress = new BitAddress(fileOffset, 0);
                }
                else
                {
                    source.FileAddress = new BitAddress(fileOffset, int.Parse(element.Attribute("bitoffset").Value));
                }

                source.Entries = int.Parse(item.Attribute("entries").Value);

                if (item.Attribute("endian") is not null)
                {
                    if (item.Attribute("endian").Value == "big")
                    {
                        source.Endian = Endian.Big;
                    }
                    else if (item.Attribute("endian").Value == "little")
                    {
                        source.Endian = Endian.Little;
                    }
                    else
                    {
                        _errors.Add($"'endian' has unknown value '{item.Attribute("endian").Value}'");
                    }
                }

                model.ColorSources.Add(source);
            }
            else if (item.Name.LocalName == "nativecolor")
            {
                if (ColorParser.TryParse(item.Attribute("value").Value, ColorModel.Rgba32, out var nativeColor))
                {
                    model.ColorSources.Add(new ProjectNativeColorSourceModel((ColorRgba32)nativeColor));
                }
            }
            else if (item.Name.LocalName == "foreigncolor")
            {
                if (ColorParser.TryParse(item.Attribute("value").Value, model.ColorModel, out var foreignColor))
                {
                    model.ColorSources.Add(new ProjectForeignColorSourceModel(foreignColor));
                }
            }
            else if (item.Name.LocalName == "scatteredcolor")
            {
            }
            else if (item.Name.LocalName == "import")
            {
            }
            else if (item.Name.LocalName == "export")
            {
            }
        }

        paletteModel = model;
        return(true);
    }
Exemplo n.º 12
0
 private void OKButton_Click(object sender, RoutedEventArgs e)
 {
     DialogDataResult = backup;
     DialogResult     = true;
     Close();
 }
Exemplo n.º 13
0
        private static void CacheColorsFromBank(int group, AttributeTable attributeTable, MapModel mapModel, BankModel bankModel)
        {
            string paletteId = mapModel.PaletteIDs[(int)attributeTable.PaletteIndex];

            PaletteModel paletteModel = ProjectFiles.GetModel <PaletteModel>(paletteId);

            Color firstColor = paletteModel == null ? Util.NullColor : Util.GetColorFromInt(paletteModel.Color0);

            Tuple <int, PaletteIndex> tuple = Tuple.Create(group, (PaletteIndex)attributeTable.PaletteIndex);

            if (!MapViewModel.GroupedPalettes.TryGetValue(tuple, out Dictionary <Color, Color> colors))
            {
                colors = new Dictionary <Color, Color>
                {
                    // always add the first color of the palette as the background color
                    { Util.NullColor, firstColor }
                };

                MapViewModel.GroupedPalettes.Add(tuple, colors);
            }

            // only 4 color per tile
            if (colors.Count >= 4)
            {
                return;
            }

            foreach (PTTileModel tile in bankModel.PTTiles)
            {
                if (string.IsNullOrEmpty(tile.GUID))
                {
                    continue;
                }

                if (tile.Group != group)
                {
                    continue;
                }

                if (!TileSetModel.BitmapCache.TryGetValue(tile.TileSetID, out WriteableBitmap tileSetBitmap))
                {
                    continue;
                }

                WriteableBitmap cropped = tileSetBitmap.Crop((int)tile.Point.X, (int)tile.Point.Y, 8, 8);

                for (int y = 0; y < 8; ++y)
                {
                    for (int x = 0; x < 8; ++x)
                    {
                        Color color = cropped.GetPixel(x, y);
                        color.A = 255;

                        if (!colors.TryGetValue(color, out _))
                        {
                            int paletteColor;
                            switch (colors.Count)
                            {
                            case 0: paletteColor = paletteModel == null ? 0 : paletteModel.Color0; break;

                            case 1: paletteColor = paletteModel == null ? 0 : paletteModel.Color1; break;

                            case 2: paletteColor = paletteModel == null ? 0 : paletteModel.Color2; break;

                            case 3: paletteColor = paletteModel == null ? 0 : paletteModel.Color3; break;

                            default: paletteColor = paletteModel == null ? 0 : paletteModel.Color0; break;
                            }

                            Color newColor = Util.GetColorFromInt(paletteColor);
                            colors.Add(color, newColor);
                        }

                        if (colors.Count >= 4)
                        {
                            return;
                        }
                    }
                }
            }
        }
Exemplo n.º 14
0
        private static Dictionary <Color, Color> FillColorCacheByGroup(CharacterTile characterTile, int group, string paletteId)
        {
            Color nullColor = Util.NullColor;

            BankModel bankModel = ProjectFiles.GetModel <BankModel>(characterTile.BankID);

            PaletteModel paletteModel = ProjectFiles.GetModel <PaletteModel>(paletteId);

            Dictionary <Color, Color> colors = new Dictionary <Color, Color>()
            {
                { nullColor, nullColor }
            };

            foreach (PTTileModel tile in bankModel.PTTiles)
            {
                if (string.IsNullOrEmpty(tile.GUID))
                {
                    continue;
                }

                if (tile.Group != group)
                {
                    continue;
                }

                TileSetModel model = ProjectFiles.GetModel <TileSetModel>(tile.TileSetID);

                if (model == null)
                {
                    continue;
                }

                if (!TileSetModel.BitmapCache.TryGetValue(tile.TileSetID, out WriteableBitmap tileSetBitmap))
                {
                    continue;
                }

                WriteableBitmap cropped = tileSetBitmap.Crop((int)tile.Point.X, (int)tile.Point.Y, 8, 8);

                for (int y = 0; y < 8; ++y)
                {
                    for (int x = 0; x < 8; ++x)
                    {
                        Color color = cropped.GetPixel(x, y);
                        color.A = 255;

                        if (!colors.TryGetValue(color, out Color newColor))
                        {
                            if (paletteModel == null)
                            {
                                newColor = nullColor;
                            }
                            else
                            {
                                switch (colors.Count)
                                {
                                case 1: newColor = Util.GetColorFromInt(paletteModel.Color1); break;

                                case 2: newColor = Util.GetColorFromInt(paletteModel.Color2); break;

                                case 3: newColor = Util.GetColorFromInt(paletteModel.Color3); break;
                                }
                            }

                            colors.Add(color, newColor);
                        }
                    }
                }
            }

            return(colors);
        }