From d81cf50f997695975b0d3687a1141cfc0715387e Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 28 Dec 2016 14:35:14 -0800 Subject: [PATCH] Simplify hex seed editing Undo is nifty, no need to have 3 separate methods when the underlying function is the same. --- PKHeX/MainWindow/Main.Designer.cs | 8 +-- PKHeX/MainWindow/Main.cs | 105 +++++++----------------------- 2 files changed, 28 insertions(+), 85 deletions(-) diff --git a/PKHeX/MainWindow/Main.Designer.cs b/PKHeX/MainWindow/Main.Designer.cs index d5effeae1..5ff9263d9 100644 --- a/PKHeX/MainWindow/Main.Designer.cs +++ b/PKHeX/MainWindow/Main.Designer.cs @@ -5134,7 +5134,7 @@ this.TB_RNGSeed.Size = new System.Drawing.Size(120, 20); this.TB_RNGSeed.TabIndex = 8; this.TB_RNGSeed.Text = "0123456789ABCDEF"; - this.TB_RNGSeed.TextChanged += new System.EventHandler(this.updateEggRNGSeed); + this.TB_RNGSeed.Validated += new System.EventHandler(this.updateStringSeed); // // dcpkx2 // @@ -5349,7 +5349,7 @@ this.TB_Secure2.Size = new System.Drawing.Size(120, 20); this.TB_Secure2.TabIndex = 17; this.TB_Secure2.Text = "0000000000000000"; - this.TB_Secure2.TextChanged += new System.EventHandler(this.updateU64); + this.TB_Secure2.Validated += new System.EventHandler(this.updateStringSeed); // // L_Secure1 // @@ -5370,7 +5370,7 @@ this.TB_Secure1.Size = new System.Drawing.Size(120, 20); this.TB_Secure1.TabIndex = 15; this.TB_Secure1.Text = "0000000000000000"; - this.TB_Secure1.TextChanged += new System.EventHandler(this.updateU64); + this.TB_Secure1.Validated += new System.EventHandler(this.updateStringSeed); // // B_JPEG // @@ -5401,7 +5401,7 @@ this.TB_GameSync.Size = new System.Drawing.Size(120, 20); this.TB_GameSync.TabIndex = 10; this.TB_GameSync.Text = "0000000000000000"; - this.TB_GameSync.TextChanged += new System.EventHandler(this.updateGameSyncID); + this.TB_GameSync.Validated += new System.EventHandler(this.updateStringSeed); // // B_SaveBoxBin // diff --git a/PKHeX/MainWindow/Main.cs b/PKHeX/MainWindow/Main.cs index ea3354733..2a1212dbc 100644 --- a/PKHeX/MainWindow/Main.cs +++ b/PKHeX/MainWindow/Main.cs @@ -3466,106 +3466,49 @@ namespace PKHeX ((SAV4BR)SAV).CurrentSlot = Util.getIndex(CB_SaveSlot); setPKXBoxes(); } - private void updateEggRNGSeed(object sender, EventArgs e) - { - if (TB_RNGSeed.Text.Length == 0) - { - // Reset to 0 - TB_RNGSeed.Text = 0.ToString("X"+SAV.DaycareSeedSize); - return; // recursively triggers this method, no need to continue - } - - string filterText = Util.getOnlyHex(TB_RNGSeed.Text); - if (filterText.Length != TB_RNGSeed.Text.Length) - { - Util.Alert("Expected HEX (0-9, A-F).", "Received: " + Environment.NewLine + TB_RNGSeed.Text); - // Reset to Stored Value - var seed = SAV.getDaycareRNGSeed(SAV.DaycareIndex); - if (seed != null) - TB_RNGSeed.Text = seed; - return; // recursively triggers this method, no need to continue - } - - // Write final value back to the save - var value = filterText.PadLeft(SAV.DaycareSeedSize, '0'); - if (value != SAV.getDaycareRNGSeed(SAV.DaycareIndex)) - { - SAV.setDaycareRNGSeed(SAV.DaycareIndex, value); - SAV.Edited = true; - } - } - private void updateGameSyncID(object sender, EventArgs e) - { - TextBox tb = TB_GameSync; - if (tb.Text.Length == 0) - { - // Reset to 0 - tb.Text = 0.ToString("X" + SAV.GameSyncIDSize); - return; // recursively triggers this method, no need to continue - } - - string filterText = Util.getOnlyHex(tb.Text); - if (filterText.Length != tb.Text.Length) - { - Util.Alert("Expected HEX (0-9, A-F).", "Received: " + Environment.NewLine + tb.Text); - // Reset to Stored Value - var seed = SAV.GameSyncID; - if (seed != null) - tb.Text = seed; - return; // recursively triggers this method, no need to continue - } - - // Write final value back to the save - var value = filterText.PadLeft(SAV.GameSyncIDSize, '0'); - if (value != SAV.GameSyncID) - { - SAV.GameSyncID = value; - SAV.Edited = true; - } - } - private void updateU64(object sender, EventArgs e) + private void updateStringSeed(object sender, EventArgs e) { if (!fieldsLoaded) return; TextBox tb = sender as TextBox; - if (tb?.Text.Length == 0) - { - // Reset to 0 - tb.Text = 0.ToString("X16"); - return; // recursively triggers this method, no need to continue - } + if (tb == null) + return; - // Currently saved Value - ulong? oldval = 0; - if (SAV.Generation == 6) + if (tb.Text.Length == 0) { - if (tb == TB_Secure1) - oldval = SAV.Secure1; - else if (tb == TB_Secure2) - oldval = SAV.Secure2; + tb.Undo(); + return; } string filterText = Util.getOnlyHex(tb.Text); - if (filterText.Length != tb.Text.Length) { Util.Alert("Expected HEX (0-9, A-F).", "Received: " + Environment.NewLine + tb.Text); - // Reset to Stored Value - tb.Text = (oldval ?? 0).ToString("X16"); - return; // recursively triggers this method, no need to continue + tb.Undo(); + return; } // Write final value back to the save - ulong? newval = Convert.ToUInt64(filterText, 16); - if (newval == oldval) return; - - if (SAV.Generation >= 6) + if (tb == TB_RNGSeed) { + var value = filterText.PadLeft(SAV.DaycareSeedSize, '0'); + SAV.setDaycareRNGSeed(SAV.DaycareIndex, value); + SAV.Edited = true; + } + else if (tb == TB_GameSync) + { + var value = filterText.PadLeft(SAV.GameSyncIDSize, '0'); + SAV.GameSyncID = value; + SAV.Edited = true; + } + else if (SAV.Generation >= 6) + { + var value = Convert.ToUInt64(filterText, 16); if (tb == TB_Secure1) - SAV.Secure1 = newval; + SAV.Secure1 = value; else if (tb == TB_Secure2) - SAV.Secure2 = newval; + SAV.Secure2 = value; SAV.Edited = true; } }