From df75543f2a60165c0135aae44f5b988d9b9f123d Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 21 Apr 2018 16:31:41 -0700 Subject: [PATCH] Implement pkm sort option control right click the box tab * no longer silly key-combos to initiate a clear/sort * provides multiple sorting options Advanced sorting includes: * Recieved Date * Friendship usage (using it raises friendship, more used pkm will be sorted to the top) * Ownership (current SAV's first, then by other OTs) --- .../Controls/SAV Editor/SAVEditor.Designer.cs | 2 +- .../Controls/SAV Editor/SAVEditor.cs | 90 ++++++++++--------- PKHeX.WinForms/Controls/SAV Editor/Sorter.cs | 61 +++++++++++++ PKHeX.WinForms/PKHeX.WinForms.csproj | 1 + PKHeX.WinForms/Resources/text/shortcuts.txt | 5 +- 5 files changed, 114 insertions(+), 45 deletions(-) create mode 100644 PKHeX.WinForms/Controls/SAV Editor/Sorter.cs diff --git a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.Designer.cs b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.Designer.cs index bf6a2b262..b2800cbfd 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.Designer.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.Designer.cs @@ -148,7 +148,7 @@ this.tabBoxMulti.SelectedIndex = 0; this.tabBoxMulti.Size = new System.Drawing.Size(310, 225); this.tabBoxMulti.TabIndex = 101; - this.tabBoxMulti.Click += new System.EventHandler(this.ClickBoxSort); + this.tabBoxMulti.MouseClick += new System.Windows.Forms.MouseEventHandler(this.ClickBoxSort); this.tabBoxMulti.DragOver += new System.Windows.Forms.DragEventHandler(this.MultiDragOver); this.tabBoxMulti.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.ClickBoxDouble); // diff --git a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs index dc37d0569..8a84d4a27 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs @@ -13,7 +13,7 @@ using static PKHeX.Core.MessageStrings; namespace PKHeX.WinForms.Controls { - public partial class SAVEditor : UserControl + public partial class SAVEditor : UserControl, ISortableDisplay { public SaveFile SAV; public readonly PictureBox[] SlotPictureBoxes; @@ -21,6 +21,7 @@ namespace PKHeX.WinForms.Controls public readonly Stack UndoStack = new Stack(); public readonly Stack RedoStack = new Stack(); public readonly ContextMenuSAV menu = new ContextMenuSAV(); + public readonly ContextMenuStrip SortMenu; public bool HaX; public bool ModifyPKM; @@ -73,6 +74,7 @@ namespace PKHeX.WinForms.Controls GB_Daycare.Click += SwitchDaycare; FLP_SAVtools.Scroll += WinFormsUtil.PanelScroll; + SortMenu = this.GetSortStrip(); } private void InitializeDragDrop(Control pb) { @@ -361,52 +363,60 @@ namespace PKHeX.WinForms.Controls pb.BackColor = Color.Transparent; } - private void ClickBoxSort(object sender, EventArgs e) + #region Box Manipulation + private void ClickBoxSort(object sender, MouseEventArgs e) { - if (tabBoxMulti.SelectedTab != Tab_Box) + if (tabBoxMulti.SelectedTab != Tab_Box || !e.Button.HasFlag(MouseButtons.Right)) return; - if (!SAV.HasBox) + if (!tabBoxMulti.GetTabRect(tabBoxMulti.SelectedIndex).Contains(PointToClient(MousePosition))) return; - - string modified; - bool all = false; - if (ModifierKeys == (Keys.Alt | Keys.Shift) && DialogResult.Yes == WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgSaveBoxClearAll)) - { - if (SAV.IsAnySlotLockedInBox(0, SAV.BoxCount - 1)) - { WinFormsUtil.Alert(MsgSaveBoxClearAllFailBattle); return; } - SAV.ClearBoxes(); - modified = MsgSaveBoxClearAllSuccess; - all = true; - } - else if (ModifierKeys == Keys.Alt && DialogResult.Yes == WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgSaveBoxClearCurrent)) - { - if (SAV.IsAnySlotLockedInBox(Box.CurrentBox, Box.CurrentBox)) - { WinFormsUtil.Alert(MsgSaveBoxClearCurrentFailBattle); return; } - SAV.ClearBoxes(Box.CurrentBox, Box.CurrentBox + 1); - modified = MsgSaveBoxClearCurrentSuccess; - } - else if (ModifierKeys == (Keys.Control | Keys.Shift) && DialogResult.Yes == WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgSaveBoxSortAll)) - { - if (SAV.IsAnySlotLockedInBox(0, SAV.BoxCount - 1)) - { WinFormsUtil.Alert(MsgSaveBoxSortAllFailBattle); return; } - SAV.SortBoxes(); - modified = MsgSaveBoxSortAllSuccess; - all = true; - } - else if (ModifierKeys == Keys.Control && DialogResult.Yes == WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgSaveBoxSortCurrent)) - { - if (SAV.IsAnySlotLockedInBox(Box.CurrentBox, Box.CurrentBox)) - { WinFormsUtil.Alert(MsgSaveBoxSortCurrentFailBattle); return; } - SAV.SortBoxes(Box.CurrentBox, Box.CurrentBox + 1); - modified = MsgSaveBoxSortCurrentSuccess; - } - else + var pt = Tab_Box.PointToScreen(new Point(0, 0)); + SortMenu.Show(pt); + } + public void ClearAll() + { + if (!CanManipulateRegion(0, SAV.BoxCount - 1, MsgSaveBoxClearAll, MsgSaveBoxClearAllFailBattle)) return; - + SAV.ClearBoxes(); + FinishBoxManipulation(MsgSaveBoxClearAllSuccess, true); + } + public void ClearCurrent() + { + if (!CanManipulateRegion(Box.CurrentBox, Box.CurrentBox, MsgSaveBoxClearCurrent, MsgSaveBoxClearCurrentFailBattle)) + return; + SAV.ClearBoxes(Box.CurrentBox, Box.CurrentBox + 1); + FinishBoxManipulation(MsgSaveBoxClearCurrentSuccess, false); + } + public void SortAll(Func, IEnumerable> sorter) + { + if (!CanManipulateRegion(0, SAV.BoxCount - 1, MsgSaveBoxSortAll, MsgSaveBoxSortAllFailBattle)) + return; + SAV.SortBoxes(0, SAV.BoxCount - 1, sorter); + FinishBoxManipulation(MsgSaveBoxSortAllSuccess, true); + } + public void SortCurrent(Func, IEnumerable> sorter) + { + if (!CanManipulateRegion(Box.CurrentBox, Box.CurrentBox, MsgSaveBoxSortCurrent, MsgSaveBoxSortCurrentFailBattle)) + return; + SAV.SortBoxes(Box.CurrentBox, Box.CurrentBox + 1, sorter); + FinishBoxManipulation(MsgSaveBoxSortCurrentSuccess, false); + } + private void FinishBoxManipulation(string message, bool all) + { SetPKMBoxes(); UpdateBoxViewers(all); - WinFormsUtil.Alert(modified); + WinFormsUtil.Alert(message); } + private bool CanManipulateRegion(int start, int end, string prompt, string fail) + { + if (WinFormsUtil.Prompt(MessageBoxButtons.YesNo, prompt) != DialogResult.Yes) + return false; + if (!SAV.IsAnySlotLockedInBox(start, end)) + return true; + WinFormsUtil.Alert(fail); + return false; + } + #endregion private void ClickBoxDouble(object sender, MouseEventArgs e) { if (tabBoxMulti.SelectedTab == Tab_SAV) diff --git a/PKHeX.WinForms/Controls/SAV Editor/Sorter.cs b/PKHeX.WinForms/Controls/SAV Editor/Sorter.cs new file mode 100644 index 000000000..d35d81697 --- /dev/null +++ b/PKHeX.WinForms/Controls/SAV Editor/Sorter.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Windows.Forms; +using PKHeX.Core; + +namespace PKHeX.WinForms.Controls +{ + public static class Sorter + { + public static ContextMenuStrip GetSortStrip(this SAVEditor sav) + { + var sortMenu = new ContextMenuStrip(); + var options = new[] + { + GetItem("mnu_ClearBox", "Clear", Clear), + GetItem("mnu_SortBoxSpecies", "Sort: SpeciesID", () => Sort(PKMSorting.OrderBySpecies)), + GetItem("mnu_SortBoxSpecies", "Sort: SpeciesIDRev", () => Sort(PKMSorting.OrderByDescendingSpecies)), + GetItem("mnu_SortBoxSpecies", "Sort: Level Low->High", () => Sort(PKMSorting.OrderByLevel)), + GetItem("mnu_SortBoxSpecies", "Sort: Level High->Low", () => Sort(PKMSorting.OrderByDescendingLevel)), + GetItem("mnu_SortBoxSpecies", "Sort: Date", () => Sort(PKMSorting.OrderByDateObtained)), + GetItem("mnu_SortBoxSpecies", "Sort: Usage", () => Sort(PKMSorting.OrderByDescendingLevel)), + GetItem("mnu_SortBoxSpecies", "Sort: SpeciesName", () => Sort(list => list.OrderBySpeciesName(GameInfo.Strings.Species))), + GetItem("mnu_SortBoxSpecies", "Sort: Ownership", () => Sort(list => list.OrderByOwnership(sav.SAV))), + }; + sortMenu.Items.AddRange(options); + + void Clear() + { + if (Control.ModifierKeys.HasFlag(Keys.Shift)) + sav.ClearAll(); + else + sav.ClearCurrent(); + } + + void Sort(Func, IEnumerable> sorter) + { + if (Control.ModifierKeys.HasFlag(Keys.Shift)) + sav.SortAll(sorter); + else + sav.SortCurrent(sorter); + } + + return sortMenu; + } + + private static ToolStripItem GetItem(string name, string text, Action action) + { + var tsi = new ToolStripMenuItem {Name = name, Text = text}; + tsi.Click += (s, e) => action(); + return tsi; + } + } + + public interface ISortableDisplay + { + void ClearAll(); + void ClearCurrent(); + void SortAll(Func, IEnumerable> sorter); + void SortCurrent(Func, IEnumerable> sorter); + } +} diff --git a/PKHeX.WinForms/PKHeX.WinForms.csproj b/PKHeX.WinForms/PKHeX.WinForms.csproj index a0d7c02f7..17246ca20 100644 --- a/PKHeX.WinForms/PKHeX.WinForms.csproj +++ b/PKHeX.WinForms/PKHeX.WinForms.csproj @@ -203,6 +203,7 @@ SlotList.cs + Form diff --git a/PKHeX.WinForms/Resources/text/shortcuts.txt b/PKHeX.WinForms/Resources/text/shortcuts.txt index f3ac214ab..7b475f50f 100644 --- a/PKHeX.WinForms/Resources/text/shortcuts.txt +++ b/PKHeX.WinForms/Resources/text/shortcuts.txt @@ -53,10 +53,7 @@ Doubleclick on the SAV Tab: Auto-detect/Reload latest save. // Boxes -Control-Click Box Tab: Sort Current Box contents. -Control-Shift-Click Box Tab: Sort All Boxes. -Alt-Click Box Tab: Delete Current Box contents. -Alt-Shift-Click Box Tab: Delete All Boxes. +Right click on Box Tab: Clear/Sort Box contents. Hold shift when selecting the action to apply to ALL boxes. Control-RightClick a Box Slot to show extra options (ie Legality check) Control-Drag a Box Slot to Copy-Overwrite