From a39cedb3661f8703c209e7358a6e882190b45163 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sun, 15 Dec 2019 16:06:33 -0600 Subject: [PATCH] GB config code cleanup --- BizHawk.Client.EmuHawk/config/GB/BmpView.cs | 11 +- .../config/GB/CGBColorChooserForm.cs | 25 ++- .../config/GB/ColorChooserForm.cs | 170 +++++++++--------- BizHawk.Client.EmuHawk/config/GB/DGBPrefs.cs | 34 ++-- .../config/GB/GBPrefControl.cs | 2 +- BizHawk.Client.EmuHawk/config/GB/GBPrefs.cs | 18 +- BizHawk.sln.DotSettings | 2 + 7 files changed, 124 insertions(+), 138 deletions(-) diff --git a/BizHawk.Client.EmuHawk/config/GB/BmpView.cs b/BizHawk.Client.EmuHawk/config/GB/BmpView.cs index 800cd1aa2d..dab612afa3 100644 --- a/BizHawk.Client.EmuHawk/config/GB/BmpView.cs +++ b/BizHawk.Client.EmuHawk/config/GB/BmpView.cs @@ -75,6 +75,7 @@ namespace BizHawk.Client.EmuHawk BMP.Dispose(); } + BMP = new Bitmap(w, h, PixelFormat.Format32bppArgb); BmpView_SizeChanged(null, null); Refresh(); @@ -82,17 +83,17 @@ namespace BizHawk.Client.EmuHawk public void Clear() { - var lockdata = BMP.LockBits(new Rectangle(0, 0, BMP.Width, BMP.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); - Win32.MemSet(lockdata.Scan0, 0xff, (uint)(lockdata.Height * lockdata.Stride)); - BMP.UnlockBits(lockdata); + var lockBits = BMP.LockBits(new Rectangle(0, 0, BMP.Width, BMP.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); + Win32.MemSet(lockBits.Scan0, 0xff, (uint)(lockBits.Height * lockBits.Stride)); + BMP.UnlockBits(lockBits); Refresh(); } public void SaveFile() { string path = PathManager.MakeAbsolutePath( - Global.Config.PathEntries[Global.Emulator.SystemId, "Screenshots"].Path, - Global.Emulator.SystemId); + Global.Config.PathEntries[Global.Emulator.SystemId, "Screenshots"].Path, + Global.Emulator.SystemId); var di = new DirectoryInfo(path); diff --git a/BizHawk.Client.EmuHawk/config/GB/CGBColorChooserForm.cs b/BizHawk.Client.EmuHawk/config/GB/CGBColorChooserForm.cs index b47bb2d969..96562fe64d 100644 --- a/BizHawk.Client.EmuHawk/config/GB/CGBColorChooserForm.cs +++ b/BizHawk.Client.EmuHawk/config/GB/CGBColorChooserForm.cs @@ -44,11 +44,11 @@ namespace BizHawk.Client.EmuHawk private unsafe void RefreshType() { - var lockdata = bmpView1.BMP.LockBits(new Rectangle(0, 0, 256, 128), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); + var lockBits = bmpView1.BMP.LockBits(new Rectangle(0, 0, 256, 128), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); int[] lut = GBColors.GetLut(_type); - int* dest = (int*)lockdata.Scan0; + int* dest = (int*)lockBits.Scan0; for (int j = 0; j < 128; j++) { @@ -62,10 +62,10 @@ namespace BizHawk.Client.EmuHawk } dest -= 256; - dest += lockdata.Stride / sizeof(int); + dest += lockBits.Stride / sizeof(int); } - bmpView1.BMP.UnlockBits(lockdata); + bmpView1.BMP.UnlockBits(lockBits); bmpView1.Refresh(); } @@ -101,23 +101,20 @@ namespace BizHawk.Client.EmuHawk _type = GBColors.ColorType.gba; } - var radioButton = sender as RadioButton; - if (radioButton != null && radioButton.Checked) + if (sender is RadioButton radioButton && radioButton.Checked) { RefreshType(); } } - public static void DoCGBColorChoserFormDialog(IWin32Window parent, Gameboy.GambatteSettings s) + public static void DoCGBColorChooserFormDialog(IWin32Window parent, Gameboy.GambatteSettings s) { - using (var dlg = new CGBColorChooserForm()) + using var dlg = new CGBColorChooserForm(); + dlg.LoadType(s); + var result = dlg.ShowDialog(parent); + if (result == DialogResult.OK) { - dlg.LoadType(s); - var result = dlg.ShowDialog(parent); - if (result == DialogResult.OK) - { - s.CGBColors = dlg._type; - } + s.CGBColors = dlg._type; } } } diff --git a/BizHawk.Client.EmuHawk/config/GB/ColorChooserForm.cs b/BizHawk.Client.EmuHawk/config/GB/ColorChooserForm.cs index 0cc0c3bdf2..644440b49f 100644 --- a/BizHawk.Client.EmuHawk/config/GB/ColorChooserForm.cs +++ b/BizHawk.Client.EmuHawk/config/GB/ColorChooserForm.cs @@ -50,7 +50,7 @@ namespace BizHawk.Client.EmuHawk panel12.BackColor = _colors[11]; } - private Color Betweencolor(Color left, Color right, double pos) + private Color BetweenColor(Color left, Color right, double pos) { int r = (int)(right.R * pos + left.R * (1.0 - pos) + 0.5); int g = (int)(right.G * pos + left.G * (1.0 - pos) + 0.5); @@ -60,12 +60,12 @@ namespace BizHawk.Client.EmuHawk return Color.FromArgb(a, r, g, b); } - private void InterpolateColors(int firstindex, int lastindex) + private void InterpolateColors(int firstIndex, int lastIndex) { - for (int i = firstindex + 1; i < lastindex; i++) + for (int i = firstIndex + 1; i < lastIndex; i++) { - double pos = (i - firstindex) / (double)(lastindex - firstindex); - _colors[i] = Betweencolor(_colors[firstindex], _colors[lastindex], pos); + double pos = (i - firstIndex) / (double)(lastIndex - firstIndex); + _colors[i] = BetweenColor(_colors[firstIndex], _colors[lastIndex], pos); } RefreshAllBackdrops(); @@ -118,39 +118,39 @@ namespace BizHawk.Client.EmuHawk else return; // i = -1; - using (var dlg = new ColorDialog()) + using var dlg = new ColorDialog { - dlg.AllowFullOpen = true; - dlg.AnyColor = true; - dlg.Color = _colors[i]; + AllowFullOpen = true, + AnyColor = true, + Color = _colors[i] + }; - // custom colors are ints, not Color structs? - // and they don't work right unless the alpha bits are set to 0 - // and the rgb order is switched - int[] customs = new int[12]; - for (int j = 0; j < customs.Length; j++) + // custom colors are ints, not Color structs? + // and they don't work right unless the alpha bits are set to 0 + // and the rgb order is switched + int[] customs = new int[12]; + for (int j = 0; j < customs.Length; j++) + { + customs[j] = _colors[j].R | _colors[j].G << 8 | _colors[j].B << 16; + } + + dlg.CustomColors = customs; + dlg.FullOpen = true; + + var result = dlg.ShowDialog(this); + + if (result == DialogResult.OK) + { + if (_colors[i] != dlg.Color) { - customs[j] = _colors[j].R | _colors[j].G << 8 | _colors[j].B << 16; - } - - dlg.CustomColors = customs; - dlg.FullOpen = true; - - var result = dlg.ShowDialog(this); - - if (result == DialogResult.OK) - { - if (_colors[i] != dlg.Color) - { - _colors[i] = dlg.Color; - psender.BackColor = _colors[i]; - } + _colors[i] = dlg.Color; + psender.BackColor = _colors[i]; } } } // ini keys for gambatte palette file - private static readonly string[] Paletteinikeys = + private static readonly string[] PaletteIniKeys = { "Background0", "Background1", @@ -197,7 +197,7 @@ namespace BizHawk.Client.EmuHawk { for (int i = 0; i < 12; i++) { - ret[i] = lines[Paletteinikeys[i]]; + ret[i] = lines[PaletteIniKeys[i]]; } } catch (KeyNotFoundException) @@ -214,7 +214,7 @@ namespace BizHawk.Client.EmuHawk f.WriteLine("[General]"); for (int i = 0; i < 12; i++) { - f.WriteLine($"{Paletteinikeys[i]}={colors[i]}"); + f.WriteLine($"{PaletteIniKeys[i]}={colors[i]}"); } } @@ -229,32 +229,30 @@ namespace BizHawk.Client.EmuHawk RefreshAllBackdrops(); } - private static void DoColorChooserFormDialog(IWin32Window parent, Gameboy.GambatteSettings s, bool fromemu) + private static void DoColorChooserFormDialog(IWin32Window parent, Gameboy.GambatteSettings s, bool fromMenu) { - using (var dlg = new ColorChooserForm()) + using var dlg = new ColorChooserForm(); + var gb = Global.Emulator as Gameboy; + if (fromMenu) { - var gb = Global.Emulator as Gameboy; - if (fromemu) + s = gb.GetSettings(); + } + + dlg.SetAllColors(s.GBPalette); + + var result = dlg.ShowDialog(parent); + if (result == DialogResult.OK) + { + int[] colors = new int[12]; + for (int i = 0; i < 12; i++) { - s = gb.GetSettings(); + colors[i] = dlg._colors[i].ToArgb(); } - dlg.SetAllColors(s.GBPalette); - - var result = dlg.ShowDialog(parent); - if (result == DialogResult.OK) + s.GBPalette = colors; + if (fromMenu) { - int[] colorints = new int[12]; - for (int i = 0; i < 12; i++) - { - colorints[i] = dlg._colors[i].ToArgb(); - } - - s.GBPalette = colorints; - if (fromemu) - { - gb.PutSettings(s); - } + gb.PutSettings(s); } } } @@ -268,16 +266,14 @@ namespace BizHawk.Client.EmuHawk { try { - using (var f = new StreamReader(filename)) + using var f = new StreamReader(filename); + int[] newColors = LoadPalFile(f); + if (newColors == null) { - int[] newcolors = LoadPalFile(f); - if (newcolors == null) - { - throw new Exception(); - } - - SetAllColors(newcolors); + throw new Exception(); } + + SetAllColors(newColors); } catch { @@ -292,17 +288,15 @@ namespace BizHawk.Client.EmuHawk { try { - using (var f = new StreamWriter(filename)) + using var f = new StreamWriter(filename); + int[] saveColors = new int[12]; + for (int i = 0; i < 12; i++) { - int[] savecolors = new int[12]; - for (int i = 0; i < 12; i++) - { - // clear alpha because gambatte color files don't usually contain it - savecolors[i] = _colors[i].ToArgb() & 0xffffff; - } - - SavePalFile(f, savecolors); + // clear alpha because gambatte color files don't usually contain it + saveColors[i] = _colors[i].ToArgb() & 0xffffff; } + + SavePalFile(f, saveColors); } catch { @@ -312,17 +306,17 @@ namespace BizHawk.Client.EmuHawk private void Button6_Click(object sender, EventArgs e) { - using (var ofd = new OpenFileDialog()) + using var ofd = new OpenFileDialog { - ofd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries["GB", "Palettes"].Path, "GB"); - ofd.Filter = "Gambatte Palettes (*.pal)|*.pal|All Files|*.*"; - ofd.RestoreDirectory = true; + InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries["GB", "Palettes"].Path, "GB"), + Filter = "Gambatte Palettes (*.pal)|*.pal|All Files|*.*", + RestoreDirectory = true + }; - var result = ofd.ShowDialog(this); - if (result == DialogResult.OK) - { - LoadColorFile(ofd.FileName, true); - } + var result = ofd.ShowDialog(this); + if (result == DialogResult.OK) + { + LoadColorFile(ofd.FileName, true); } } @@ -350,18 +344,18 @@ namespace BizHawk.Client.EmuHawk private void Button7_Click(object sender, EventArgs e) { - using (var sfd = new SaveFileDialog()) + using var sfd = new SaveFileDialog { - sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries["GB", "Palettes"].Path, "GB"); - sfd.FileName = $"{Global.Game.Name}.pal"; + InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries["GB", "Palettes"].Path, "GB"), + FileName = $"{Global.Game.Name}.pal", + Filter = "Gambatte Palettes (*.pal)|*.pal|All Files|*.*", + RestoreDirectory = true + }; - sfd.Filter = "Gambatte Palettes (*.pal)|*.pal|All Files|*.*"; - sfd.RestoreDirectory = true; - var result = sfd.ShowDialog(this); - if (result == DialogResult.OK) - { - SaveColorFile(sfd.FileName); - } + var result = sfd.ShowDialog(this); + if (result == DialogResult.OK) + { + SaveColorFile(sfd.FileName); } } diff --git a/BizHawk.Client.EmuHawk/config/GB/DGBPrefs.cs b/BizHawk.Client.EmuHawk/config/GB/DGBPrefs.cs index a63639f7cc..3279dd3e17 100644 --- a/BizHawk.Client.EmuHawk/config/GB/DGBPrefs.cs +++ b/BizHawk.Client.EmuHawk/config/GB/DGBPrefs.cs @@ -20,12 +20,8 @@ namespace BizHawk.Client.EmuHawk private void GetSettings(out GambatteLink.GambatteLinkSettings s, out GambatteLink.GambatteLinkSyncSettings ss) { - Gameboy.GambatteSettings sl; - Gameboy.GambatteSyncSettings ssl; - Gameboy.GambatteSettings sr; - Gameboy.GambatteSyncSettings ssr; - gbPrefControl1.GetSettings(out sl, out ssl); - gbPrefControl2.GetSettings(out sr, out ssr); + gbPrefControl1.GetSettings(out var sl, out var ssl); + gbPrefControl2.GetSettings(out var sr, out var ssr); s = new GambatteLink.GambatteLinkSettings(sl, sr); ss = new GambatteLink.GambatteLinkSyncSettings(ssl, ssr); @@ -39,22 +35,20 @@ namespace BizHawk.Client.EmuHawk var s = gambatte.GetSettings(); var ss = gambatte.GetSyncSettings(); - using (var dlg = new DGBPrefs()) + using var dlg = new DGBPrefs(); + dlg.PutSettings(s, ss); + + var emu = (GambatteLink)Global.Emulator; + dlg.gbPrefControl1.ColorGameBoy = emu.IsCGBMode(false); + dlg.gbPrefControl2.ColorGameBoy = emu.IsCGBMode(true); + + if (dlg.ShowDialog(owner) == DialogResult.OK) { - dlg.PutSettings(s, ss); - - var emu = (GambatteLink)Global.Emulator; - dlg.gbPrefControl1.ColorGameBoy = emu.IsCGBMode(false); - dlg.gbPrefControl2.ColorGameBoy = emu.IsCGBMode(true); - - if (dlg.ShowDialog(owner) == DialogResult.OK) + dlg.GetSettings(out s, out ss); + gambatte.PutSettings(s); + if (dlg.SyncSettingsChanged) { - dlg.GetSettings(out s, out ss); - gambatte.PutSettings(s); - if (dlg.SyncSettingsChanged) - { - GlobalWin.MainForm.PutCoreSyncSettings(ss); - } + GlobalWin.MainForm.PutCoreSyncSettings(ss); } } } diff --git a/BizHawk.Client.EmuHawk/config/GB/GBPrefControl.cs b/BizHawk.Client.EmuHawk/config/GB/GBPrefControl.cs index 6707787c5d..0e52e66db2 100644 --- a/BizHawk.Client.EmuHawk/config/GB/GBPrefControl.cs +++ b/BizHawk.Client.EmuHawk/config/GB/GBPrefControl.cs @@ -54,7 +54,7 @@ namespace BizHawk.Client.EmuHawk { if (ColorGameBoy) { - CGBColorChooserForm.DoCGBColorChoserFormDialog(ParentForm, _s); + CGBColorChooserForm.DoCGBColorChooserFormDialog(ParentForm, _s); } else { diff --git a/BizHawk.Client.EmuHawk/config/GB/GBPrefs.cs b/BizHawk.Client.EmuHawk/config/GB/GBPrefs.cs index e58489feb8..3f619900c6 100644 --- a/BizHawk.Client.EmuHawk/config/GB/GBPrefs.cs +++ b/BizHawk.Client.EmuHawk/config/GB/GBPrefs.cs @@ -18,18 +18,16 @@ namespace BizHawk.Client.EmuHawk var s = gb.GetSettings(); var ss = gb.GetSyncSettings(); - using (var dlg = new GBPrefs()) + using var dlg = new GBPrefs(); + dlg.gbPrefControl1.PutSettings(s, ss); + dlg.gbPrefControl1.ColorGameBoy = gb.IsCGBMode(); + if (dlg.ShowDialog(owner) == DialogResult.OK) { - dlg.gbPrefControl1.PutSettings(s, ss); - dlg.gbPrefControl1.ColorGameBoy = gb.IsCGBMode(); - if (dlg.ShowDialog(owner) == DialogResult.OK) + dlg.gbPrefControl1.GetSettings(out s, out ss); + gb.PutSettings(s); + if (dlg.gbPrefControl1.SyncSettingsChanged) { - dlg.gbPrefControl1.GetSettings(out s, out ss); - gb.PutSettings(s); - if (dlg.gbPrefControl1.SyncSettingsChanged) - { - GlobalWin.MainForm.PutCoreSyncSettings(ss); - } + GlobalWin.MainForm.PutCoreSyncSettings(ss); } } } diff --git a/BizHawk.sln.DotSettings b/BizHawk.sln.DotSettings index 71e9b88e70..5a2f1a4261 100644 --- a/BizHawk.sln.DotSettings +++ b/BizHawk.sln.DotSettings @@ -234,6 +234,7 @@ True True True + True True True True @@ -289,6 +290,7 @@ True True True + True True True True