From b23658dee1b45c0d023461629c8d92407558edbb Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 1 Oct 2017 21:25:23 -0700 Subject: [PATCH] Misc tweaks allow cute charm static encounters ignore user-entry control types --- PKHeX.Core/Legality/RNG/MethodFinder.cs | 14 ++-- PKHeX.WinForms/Util/ImageUtil.cs | 102 ++++++++++++------------ PKHeX.WinForms/Util/WinFormsUtil.cs | 29 ++++--- 3 files changed, 77 insertions(+), 68 deletions(-) diff --git a/PKHeX.Core/Legality/RNG/MethodFinder.cs b/PKHeX.Core/Legality/RNG/MethodFinder.cs index 472a8470d..d4b385178 100644 --- a/PKHeX.Core/Legality/RNG/MethodFinder.cs +++ b/PKHeX.Core/Legality/RNG/MethodFinder.cs @@ -682,16 +682,18 @@ public static bool IsCompatible4(this PIDType val, IEncounterable encounter, PKM switch (encounter) { case EncounterStatic s: - if (s == Encounters4.SpikyEaredPichu // nonshiny forced nature - || s.Location == 233 && s.Gift) // Pokewalker + if (s == Encounters4.SpikyEaredPichu || s.Location == 233 && s.Gift) // Pokewalker return val == PIDType.Pokewalker; - return s.Shiny == true ? val == PIDType.ChainShiny : val == PIDType.Method_1; + if (s.Shiny == true) + return val == PIDType.ChainShiny; + if (val == PIDType.CuteCharm) + return true; + return val == PIDType.Method_1; case EncounterSlot sl: if (val == PIDType.Method_1) return true; if (val == PIDType.CuteCharm) - // Cute charm does not work with swarms pokemon - return sl.Type != SlotType.Swarm; + return sl.Type != SlotType.Swarm; // Cute Charm does not work with Swarm if (val != PIDType.ChainShiny) return false; // Chain shiny with poke radar is only possible in DPPt in tall grass, safari zone do not allow pokeradar @@ -700,7 +702,7 @@ public static bool IsCompatible4(this PIDType val, IEncounterable encounter, PKM return pkm.IsShiny && IsDPPt && sl.TypeEncounter == EncounterType.TallGrass && !Encounters4.SafariZoneLocation_4.Contains(sl.Location); case PGT _: // manaphy return IsG4ManaphyPIDValid(val, pkm); - default: + default: // eggs return val == PIDType.None; } } diff --git a/PKHeX.WinForms/Util/ImageUtil.cs b/PKHeX.WinForms/Util/ImageUtil.cs index 6220baaa4..15d3e7aaf 100644 --- a/PKHeX.WinForms/Util/ImageUtil.cs +++ b/PKHeX.WinForms/Util/ImageUtil.cs @@ -28,19 +28,12 @@ public static Bitmap ChangeOpacity(Image img, double trans) if (img.PixelFormat.HasFlag(PixelFormat.Indexed)) return (Bitmap)img; - Bitmap bmp = (Bitmap)img.Clone(); - BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); - IntPtr ptr = bmpData.Scan0; + var bmp = (Bitmap)img.Clone(); + GetBitmapData(bmp, out BitmapData bmpData, out IntPtr ptr, out byte[] data); - int len = bmp.Width * bmp.Height * 4; - byte[] data = new byte[len]; - - Marshal.Copy(ptr, data, 0, len); - - for (int i = 0; i < data.Length; i += 4) - data[i + 3] = (byte)(data[i + 3] * trans); - - Marshal.Copy(data, 0, ptr, len); + Marshal.Copy(ptr, data, 0, data.Length); + SetAllTransparencyTo(data, trans); + Marshal.Copy(data, 0, ptr, data.Length); bmp.UnlockBits(bmpData); return bmp; @@ -52,27 +45,12 @@ public static Bitmap ChangeAllColorTo(Image img, Color c) if (img.PixelFormat.HasFlag(PixelFormat.Indexed)) return (Bitmap)img; - Bitmap bmp = (Bitmap)img.Clone(); - BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); - IntPtr ptr = bmpData.Scan0; + var bmp = (Bitmap)img.Clone(); + GetBitmapData(bmp, out BitmapData bmpData, out IntPtr ptr, out byte[] data); - int len = bmp.Width * bmp.Height * 4; - byte[] data = new byte[len]; - - Marshal.Copy(ptr, data, 0, len); - - byte R = c.R; - byte G = c.G; - byte B = c.B; - for (int i = 0; i < data.Length; i += 4) - if (data[i + 3] != 0) - { - data[i + 0] = B; - data[i + 1] = G; - data[i + 2] = R; - } - - Marshal.Copy(data, 0, ptr, len); + Marshal.Copy(ptr, data, 0, data.Length); + SetAllColorTo(data, c); + Marshal.Copy(data, 0, ptr, data.Length); bmp.UnlockBits(bmpData); return bmp; @@ -84,28 +62,52 @@ public static Bitmap ToGrayscale(Image img) if (img.PixelFormat.HasFlag(PixelFormat.Indexed)) return (Bitmap)img; - Bitmap bmp = (Bitmap)img.Clone(); - BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); - IntPtr ptr = bmpData.Scan0; + var bmp = (Bitmap)img.Clone(); + GetBitmapData(bmp, out BitmapData bmpData, out IntPtr ptr, out byte[] data); - int len = bmp.Width * bmp.Height * 4; - byte[] data = new byte[len]; - - Marshal.Copy(ptr, data, 0, len); - - for (int i = 0; i < data.Length; i += 4) - if (data[i + 3] != 0) - { - byte greyS = (byte)((0.3 * data[i + 2] + 0.59 * data[i + 1] + 0.11 * data[i + 0]) / 3); - data[i + 0] = greyS; - data[i + 1] = greyS; - data[i + 2] = greyS; - } - - Marshal.Copy(data, 0, ptr, len); + Marshal.Copy(ptr, data, 0, data.Length); + SetAllColorToGrayScale(data); + Marshal.Copy(data, 0, ptr, data.Length); bmp.UnlockBits(bmpData); return bmp; } + private static void GetBitmapData(Bitmap bmp, out BitmapData bmpData, out IntPtr ptr, out byte[] data) + { + bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); + ptr = bmpData.Scan0; + data = new byte[bmp.Width * bmp.Height * 4]; + } + private static void SetAllTransparencyTo(byte[] data, double trans) + { + for (int i = 0; i < data.Length; i += 4) + data[i + 3] = (byte)(data[i + 3] * trans); + } + private static void SetAllColorTo(byte[] data, Color c) + { + byte R = c.R; + byte G = c.G; + byte B = c.B; + for (int i = 0; i < data.Length; i += 4) + { + if (data[i + 3] == 0) + continue; + data[i + 0] = B; + data[i + 1] = G; + data[i + 2] = R; + } + } + private static void SetAllColorToGrayScale(byte[] data) + { + for (int i = 0; i < data.Length; i += 4) + { + if (data[i + 3] == 0) + continue; + byte greyS = (byte)((0.3 * data[i + 2] + 0.59 * data[i + 1] + 0.11 * data[i + 0]) / 3); + data[i + 0] = greyS; + data[i + 1] = greyS; + data[i + 2] = greyS; + } + } } } diff --git a/PKHeX.WinForms/Util/WinFormsUtil.cs b/PKHeX.WinForms/Util/WinFormsUtil.cs index 062a2f51f..799d8b76e 100644 --- a/PKHeX.WinForms/Util/WinFormsUtil.cs +++ b/PKHeX.WinForms/Util/WinFormsUtil.cs @@ -122,12 +122,16 @@ private static void TranslateForm(Control form, IEnumerable stringdata) default: if (string.IsNullOrWhiteSpace(z.Name)) break; - - yield return new KeyValuePair(z.Name, z); - + if (z.ContextMenuStrip != null) // control has attached menustrip foreach (var pair in GetToolStripMenuItems(z.ContextMenuStrip)) yield return pair; + + if (z is ComboBox || z is TextBox || z is MaskedTextBox || z is LinkLabel) + break; // undesirable to modify, ignore + + if (!string.IsNullOrWhiteSpace(z.Text)) + yield return new KeyValuePair(z.Name, z); break; } } @@ -149,8 +153,9 @@ private static void TranslateForm(Control form, IEnumerable stringdata) { foreach (var i in menu.Items.OfType()) { - yield return new KeyValuePair(i.Name, i); - foreach (var sub in GetToolsStripDropDownItems(i)) + if (!string.IsNullOrWhiteSpace(i.Text)) + yield return new KeyValuePair(i.Name, i); + foreach (var sub in GetToolsStripDropDownItems(i).Where(z => !string.IsNullOrWhiteSpace(z.Text))) yield return new KeyValuePair(sub.Name, sub); } } @@ -284,10 +289,13 @@ public static bool OpenSAVPKMDialog(string[] Extensions, out string path) if (Directory.Exists(pathCache)) cgse = Path.Combine(pathCache); if (!PathUtilWindows.DetectSaveFile(out path, cgse) && !string.IsNullOrEmpty(path)) + { Error(path); // `path` contains the error message + path = null; + } if (path != null) - { ofd.FileName = path; } + ofd.FileName = path; if (ofd.ShowDialog() != DialogResult.OK) return false; @@ -322,7 +330,7 @@ public static bool SavePKMDialog(PKM pk) if (File.Exists(path)) { // File already exists, save a .bak - string bakpath = path + ".bak"; + string bakpath = $"{path}.bak"; if (!File.Exists(bakpath)) { byte[] backupfile = File.ReadAllBytes(path); @@ -407,12 +415,9 @@ public static bool SaveMGDialog(MysteryGift gift) if (File.Exists(path)) { // File already exists, save a .bak - string bakpath = path + ".bak"; + string bakpath = $"{path}.bak"; if (!File.Exists(bakpath)) - { - byte[] backupfile = File.ReadAllBytes(path); - File.WriteAllBytes(bakpath, backupfile); - } + File.Move(path, bakpath); } File.WriteAllBytes(path, gift.Data);