Example #1
0
 private void onClickImportPalette(object sender, EventArgs e)
 {
     if (FileType != 0)
     {
         using (OpenFileDialog dialog = new OpenFileDialog())
         {
             dialog.Multiselect     = false;
             dialog.Title           = "Выбирите файл палитры";
             dialog.CheckFileExists = true;
             dialog.Filter          = "Текстовый файл (*.txt)|*.txt";
             if (dialog.ShowDialog() == DialogResult.OK)
             {
                 AnimIdx edit = Ultima.AnimationEdit.GetAnimation(FileType, CurrBody, CurrAction, CurrDir);
                 if (edit != null)
                 {
                     using (StreamReader sr = new StreamReader(dialog.FileName))
                     {
                         string   line;
                         ushort[] Palette = new ushort[0x100];
                         int      i       = 0;
                         while ((line = sr.ReadLine()) != null)
                         {
                             if ((line = line.Trim()).Length == 0 || line.StartsWith("#"))
                             {
                                 continue;
                             }
                             Palette[i++] = ushort.Parse(line);
                             if (i >= 0x100)
                             {
                                 break;
                             }
                         }
                         edit.ReplacePalette(Palette);
                     }
                     SetPaletteBox();
                     listView1.Invalidate();
                     Options.ChangedUltimaClass["Animations"] = true;
                 }
             }
         }
     }
 }
Example #2
0
        private unsafe void OnClickAddGen(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Multiselect     = true;
                dialog.Title           = "Выбирите файлы изображений для добавления. Напоминание: для выбранных изображений будет сгенерирована палитра, которая заменит старую.";
                dialog.CheckFileExists = true;
                dialog.Filter          = "файлы изображений (*.tiff;*.bmp)|*.tiff;*.bmp";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    // Получаем палитру
                    ushort[] Palette = new ushort[0x100];
                    int      count   = 0;
                    foreach (string filename in dialog.FileNames)
                    {
                        Bitmap bmp = new Bitmap(filename);
                        if (dialog.FileName.Contains(".bmp"))
                        {
                            if (useCKeyFilter)
                            {
                                bmp = Utils.CKeyFilter(bmp);
                            }
                            if (useBColFilter)
                            {
                                bmp = Utils.BColFilter(bmp, false);
                            }

                            bmp = Utils.ConvertBmp(bmp);
                        }

                        BitmapData bd    = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format16bppArgb1555);
                        ushort *   line  = (ushort *)bd.Scan0;
                        int        delta = bd.Stride >> 1;
                        ushort *   cur   = line;
                        for (int y = 0; y < bmp.Height; ++y, line += delta)
                        {
                            cur = line;
                            for (int x = 0; x < bmp.Width; ++x)
                            {
                                ushort c = cur[x];
                                if (c != 0)
                                {
                                    bool found = false;
                                    for (int i = 0; i < Palette.Length; ++i)
                                    {
                                        if (Palette[i] == c)
                                        {
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        Palette[count++] = c;
                                    }
                                    if (count >= 0x100)
                                    {
                                        MessageBox.Show(
                                            "Используется больше чем 0x100 (256) цветов!",
                                            "Генерация палитры",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Error,
                                            MessageBoxDefaultButton.Button1);
                                        return;
                                    }
                                }
                            }
                        }
                    }

                    // Устанавливаем палитру
                    AnimIdx edit = Ultima.AnimationEdit.GetAnimation(FileType, CurrBody, CurrAction, CurrDir);
                    if (edit != null)
                    {
                        edit.ReplacePalette(Palette);
                        SetPaletteBox();
                        listView1.Invalidate();
                        Options.ChangedUltimaClass["Animations"] = true;
                    }
                    else
                    {
                        MessageBox.Show(
                            String.Format("Палитра не может быть применена в данный момент, из-за выполняемых действий."),
                            "Генерация палитры",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information,
                            MessageBoxDefaultButton.Button1);
                        return;
                    }

                    // Добавляем кадры
                    listView1.BeginUpdate();
                    foreach (string filename in dialog.FileNames)
                    {
                        Bitmap bmp = new Bitmap(filename);
                        if (filename.Contains(".bmp"))
                        {
                            if (useCKeyFilter)
                            {
                                bmp = Utils.CKeyFilter(bmp);
                            }
                            if (useBColFilter)
                            {
                                bmp = Utils.BColFilter(bmp, false);
                            }

                            bmp = Utils.ConvertBmp(bmp);
                        }
                        AnimIdx edit2 = Ultima.AnimationEdit.GetAnimation(FileType, CurrBody, CurrAction, CurrDir);
                        if (edit2 != null)
                        {
                            edit2.AddFrame(bmp);
                            TreeNode node = GetNode(CurrBody);
                            if (node != null)
                            {
                                node.ForeColor = Color.Black;
                                node.Nodes[CurrAction].ForeColor = Color.Black;
                            }
                            ListViewItem item;
                            int          i = edit2.Frames.Count - 1;
                            item     = new ListViewItem(i.ToString(), 0);
                            item.Tag = i;
                            listView1.Items.Add(item);
                            int width = listView1.TileSize.Width - 5;
                            if (bmp.Width > listView1.TileSize.Width)
                            {
                                width = bmp.Width;
                            }
                            int height = listView1.TileSize.Height - 5;
                            if (bmp.Height > listView1.TileSize.Height)
                            {
                                height = bmp.Height;
                            }

                            listView1.TileSize = new Size(width + 5, height + 5);
                            trackBar2.Maximum  = i;
                            Options.ChangedUltimaClass["Animations"] = true;
                        }
                    }
                    listView1.EndUpdate();
                    listView1.Invalidate();
                }
            }
        }