BizHawk/BizHawk.Client.EmuHawk/tools/NES/PaletteViewer.cs

127 lines
3.1 KiB
C#
Raw Normal View History

2013-04-15 02:14:14 +00:00
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using BizHawk.Client.Common;
using BizHawk.Client.EmuHawk.WinFormExtensions;
namespace BizHawk.Client.EmuHawk
{
2013-04-15 02:14:14 +00:00
public sealed class PaletteViewer : Control
2011-08-27 21:31:47 +00:00
{
public class Palette
{
2011-09-04 15:20:52 +00:00
public int Address { get; private set; }
public int Value { get; set; }
2013-04-15 02:14:14 +00:00
public Color Color
{
get { return Color.FromArgb(Value); }
}
2011-09-04 15:20:52 +00:00
public Palette(int address)
2011-08-27 21:31:47 +00:00
{
2011-09-04 15:20:52 +00:00
Address = address;
Value = -1;
2011-08-27 21:31:47 +00:00
}
}
2013-04-15 02:14:14 +00:00
public Palette[] BgPalettes = new Palette[16];
public Palette[] SpritePalettes = new Palette[16];
2011-08-27 21:31:47 +00:00
2013-04-15 02:14:14 +00:00
public Palette[] BgPalettesPrev = new Palette[16];
public Palette[] SpritePalettesPrev = new Palette[16];
2011-08-27 21:31:47 +00:00
public PaletteViewer()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
2013-04-15 02:14:14 +00:00
Size = new Size(128, 32);
BackColor = Color.Transparent;
Paint += PaletteViewer_Paint;
2011-08-27 21:31:47 +00:00
for (int x = 0; x < 16; x++)
{
2013-04-15 02:14:14 +00:00
BgPalettes[x] = new Palette(x);
SpritePalettes[x] = new Palette(x + 16);
BgPalettesPrev[x] = new Palette(x);
SpritePalettesPrev[x] = new Palette(x + 16);
2011-08-27 21:31:47 +00:00
}
2011-08-27 21:31:47 +00:00
}
2011-08-27 21:31:47 +00:00
private void PaletteViewer_Paint(object sender, PaintEventArgs e)
{
2011-09-04 15:20:52 +00:00
for (int x = 0; x < 16; x++)
2011-08-27 21:31:47 +00:00
{
2013-04-15 02:14:14 +00:00
e.Graphics.FillRectangle(new SolidBrush(BgPalettes[x].Color), new Rectangle(x * 16, 0, 16, 16));
e.Graphics.FillRectangle(new SolidBrush(SpritePalettes[x].Color), new Rectangle(x * 16, 16, 16, 16));
2011-08-27 21:31:47 +00:00
}
}
2011-08-27 21:31:47 +00:00
public bool HasChanged()
{
for (int x = 0; x < 16; x++)
{
2013-04-15 02:14:14 +00:00
if (BgPalettes[x].Value != BgPalettesPrev[x].Value)
2011-08-27 21:31:47 +00:00
return true;
2013-04-15 02:14:14 +00:00
if (SpritePalettes[x].Value != SpritePalettesPrev[x].Value)
2011-08-27 21:31:47 +00:00
return true;
}
return false;
}
public void Screenshot()
{
2013-04-15 02:14:14 +00:00
var sfd = new SaveFileDialog
{
FileName = PathManager.FilesystemSafeName(Global.Game) + "-Palettes",
InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries["NES", "Screenshots"].Path, "NES"),
2013-04-15 02:14:14 +00:00
Filter = "PNG (*.png)|*.png|Bitmap (*.bmp)|*.bmp|All Files|*.*",
RestoreDirectory = true
};
var result = sfd.ShowHawkDialog();
if (result != DialogResult.OK)
{
return;
}
var file = new FileInfo(sfd.FileName);
Bitmap b = new Bitmap(Width, Height);
Rectangle rect = new Rectangle(new Point(0, 0), Size);
DrawToBitmap(b, rect);
ImageFormat i;
string extension = file.Extension.ToUpper();
switch (extension)
{
default:
case ".PNG":
i = ImageFormat.Png;
break;
case ".BMP":
i = ImageFormat.Bmp;
break;
}
b.Save(file.FullName, i);
}
public void ScreenshotToClipboard()
{
Bitmap b = new Bitmap(Width, Height);
Rectangle rect = new Rectangle(new Point(0, 0), Size);
DrawToBitmap(b, rect);
using (var img = b)
{
2013-04-15 02:14:14 +00:00
Clipboard.SetImage(img);
}
}
2011-08-27 21:31:47 +00:00
}
}