Mesen-X/GUI.NET/Debugger/Controls/ctrlPaletteViewer.cs

110 lines
3.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
2016-12-11 14:25:29 -05:00
using Mesen.GUI.Controls;
using Mesen.GUI.Forms;
namespace Mesen.GUI.Debugger.Controls
{
2016-12-11 14:25:29 -05:00
public partial class ctrlPaletteViewer : BaseControl
{
private byte[] _paletteRam;
private int[] _palettePixelData;
2017-03-24 12:14:33 -04:00
private int _paletteIndex = -1;
public ctrlPaletteViewer()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
public void GetData()
{
this._paletteRam = InteropEmu.DebugGetMemoryState(DebugMemoryType.PaletteMemory);
this._palettePixelData = InteropEmu.DebugGetPalette();
}
public void RefreshViewer()
{
2017-03-24 12:14:33 -04:00
_paletteIndex = -1;
GCHandle handle = GCHandle.Alloc(this._palettePixelData, GCHandleType.Pinned);
try {
Bitmap source = new Bitmap(4, 8, 4*4, System.Drawing.Imaging.PixelFormat.Format32bppArgb, handle.AddrOfPinnedObject());
Bitmap target = new Bitmap(128, 256);
using(Graphics g = Graphics.FromImage(target)) {
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
g.ScaleTransform(32, 32);
g.DrawImageUnscaled(source, 0, 0);
g.ScaleTransform(1f/32, 1f/32);
2017-09-16 22:02:05 -04:00
Font font = new Font(BaseControl.MonospaceFontFamily, BaseControl.DefaultFontSize - 2, GraphicsUnit.Pixel);
using(Brush bg = new SolidBrush(Color.FromArgb(150, Color.LightGray))) {
for(int y = 0; y < 8; y++) {
for(int x = 0; x < 4; x++) {
g.DrawOutlinedString(_paletteRam[y*4+x].ToString("X2"), font, Brushes.Black, bg, 32*x+14, 32*y+18);
}
}
}
}
this.picPalette.Image = target;
} finally {
handle.Free();
}
}
private void picPalette_MouseMove(object sender, MouseEventArgs e)
{
2017-09-16 22:02:05 -04:00
int tileX = Math.Min(e.X * 128 / picPalette.Width / 32, 31);
int tileY = Math.Min(e.Y * 256 / picPalette.Height / 32, 31);
2017-03-24 12:14:33 -04:00
int tileIndex = tileY * 4 + tileX;
2017-03-24 12:14:33 -04:00
if(tileIndex != _paletteIndex) {
_paletteIndex = tileIndex;
2017-03-24 12:14:33 -04:00
this.txtColor.Text = _paletteRam[tileIndex].ToString("X2");
this.txtPaletteAddress.Text = (0x3F00 + tileIndex).ToString("X4");
Bitmap tile = new Bitmap(64, 64);
using(Graphics g = Graphics.FromImage(tile)) {
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
g.DrawImage(picPalette.Image, new Rectangle(0, 0, 64, 64), new Rectangle(tileX*32, tileY*32, 32, 32), GraphicsUnit.Pixel);
}
this.picColor.Image = tile;
}
}
private void picPalette_MouseDown(object sender, MouseEventArgs e)
{
using(frmSelectColor frm = new frmSelectColor()) {
if(frm.ShowDialog(this) == DialogResult.OK) {
2017-09-16 22:02:05 -04:00
int x = Math.Min(e.X * 128 / picPalette.Width / 32, 31);
int y = Math.Min(e.Y * 256 / picPalette.Height / 32, 31);
int colorAddress = y * 4 + x;
InteropEmu.DebugSetMemoryValue(DebugMemoryType.PaletteMemory, (uint)colorAddress, (byte)frm.ColorIndex);
this.GetData();
this.RefreshViewer();
}
}
}
}
}