add loading of gambatte-style palette files to the palette selector (includes drag and drop)

This commit is contained in:
goyuken 2012-09-14 00:02:37 +00:00
parent f5d37cf73b
commit c05c9ebda7
3 changed files with 133 additions and 6 deletions

View File

@ -49,6 +49,7 @@
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// panel1
@ -245,11 +246,23 @@
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(42, 197);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 23);
this.button6.TabIndex = 28;
this.button6.Text = "Load...";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// ColorChooserForm
//
this.AllowDrop = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(315, 259);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
@ -273,6 +286,8 @@
this.Controls.Add(this.panel1);
this.Name = "ColorChooserForm";
this.Text = "ColorChooserForm";
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.ColorChooserForm_DragDrop);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.ColorChooserForm_DragEnter);
this.ResumeLayout(false);
this.PerformLayout();
@ -301,5 +316,6 @@
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
}
}

View File

@ -6,6 +6,7 @@ using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace BizHawk.Emulation.Consoles.Nintendo.Gameboy
{
@ -126,13 +127,66 @@ namespace BizHawk.Emulation.Consoles.Nintendo.Gameboy
}
}
/// <summary>
/// load gambatte-style .pal file
/// </summary>
/// <param name="f"></param>
/// <returns>null on failure</returns>
public static int[] LoadPalFile(TextReader f)
{
Dictionary<string, int> lines = new Dictionary<string, int>();
string line;
while ((line = f.ReadLine()) != null)
{
int i = line.IndexOf('=');
if (i < 0)
continue;
try
{
lines.Add(line.Substring(0, i), int.Parse(line.Substring(i + 1)));
}
catch (FormatException)
{
}
}
int[] ret = new int[12];
try
{
ret[0] = lines["Background0"];
ret[1] = lines["Background1"];
ret[2] = lines["Background2"];
ret[3] = lines["Background3"];
ret[4] = lines["Sprite%2010"];
ret[5] = lines["Sprite%2011"];
ret[6] = lines["Sprite%2012"];
ret[7] = lines["Sprite%2013"];
ret[8] = lines["Sprite%2020"];
ret[9] = lines["Sprite%2021"];
ret[10] = lines["Sprite%2022"];
ret[11] = lines["Sprite%2023"];
}
catch (KeyNotFoundException)
{
return null;
}
return ret;
}
void SetAllColors(int[] colors)
{
// fix alpha to 255 in created color objects, else problems
for (int i = 0; i < this.colors.Length; i++)
this.colors[i] = Color.FromArgb(255, Color.FromArgb(colors[i]));
RefreshAllBackdrops();
}
public static bool DoColorChooserFormDialog(int[] colors)
{
using (var dlg = new ColorChooserForm())
{
for (int i = 0; i < dlg.colors.Length; i++)
dlg.colors[i] = Color.FromArgb(255, Color.FromArgb(colors[i]));
dlg.RefreshAllBackdrops();
dlg.SetAllColors(colors);
var result = dlg.ShowDialog();
if (result != DialogResult.OK)
@ -147,5 +201,61 @@ namespace BizHawk.Emulation.Consoles.Nintendo.Gameboy
}
}
}
void LoadColorFile(string filename)
{
try
{
using (StreamReader f = new StreamReader(filename))
{
int[] newcolors = LoadPalFile(f);
if (newcolors == null)
throw new Exception();
SetAllColors(newcolors);
}
}
catch
{
MessageBox.Show(this, "Error loading .pal file!");
}
}
private void button6_Click(object sender, EventArgs e)
{
using (var ofd = new OpenFileDialog())
{
//ofd.InitialDirectory =
ofd.Filter = "Gambatte Palettes (*.pal)|*.pal|All Files|*.*";
ofd.RestoreDirectory = true;
var result = ofd.ShowDialog(this);
if (result != System.Windows.Forms.DialogResult.OK)
return;
LoadColorFile(ofd.FileName);
}
}
private void ColorChooserForm_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length > 1)
return;
LoadColorFile(files[0]);
}
}
private void ColorChooserForm_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
}
}

View File

@ -430,6 +430,10 @@ namespace BizHawk.Emulation.Consoles.GB
get { return 0; }
}
#endregion
#region palette
/// <summary>
/// palette colors to display in dmg mode
/// </summary>
@ -464,9 +468,6 @@ namespace BizHawk.Emulation.Consoles.GB
LibGambatte.gambatte_setdmgpalettecolor(GambatteState, (LibGambatte.PalType)(i / 4), (uint)i % 4, (uint)dmgcolors[i]);
}
#endregion
#region ISoundProvider