From c316fba5ff95c6d26839b65198b76c817630edcd Mon Sep 17 00:00:00 2001 From: adelikat Date: Mon, 25 Feb 2013 01:23:03 +0000 Subject: [PATCH] RamSearch - infinite undo/redo levels --- .../BizHawk.MultiClient.csproj | 1 + .../DisplayManager/DisplayManager.cs | 2 +- BizHawk.MultiClient/HistoryCollection.cs | 74 ++++++++ BizHawk.MultiClient/tools/RamSearch.cs | 158 +++++++++--------- 4 files changed, 159 insertions(+), 76 deletions(-) create mode 100644 BizHawk.MultiClient/HistoryCollection.cs diff --git a/BizHawk.MultiClient/BizHawk.MultiClient.csproj b/BizHawk.MultiClient/BizHawk.MultiClient.csproj index 0a418c0afd..f5611b3816 100644 --- a/BizHawk.MultiClient/BizHawk.MultiClient.csproj +++ b/BizHawk.MultiClient/BizHawk.MultiClient.csproj @@ -255,6 +255,7 @@ + diff --git a/BizHawk.MultiClient/DisplayManager/DisplayManager.cs b/BizHawk.MultiClient/DisplayManager/DisplayManager.cs index 131bb72b30..0487c5ce8a 100644 --- a/BizHawk.MultiClient/DisplayManager/DisplayManager.cs +++ b/BizHawk.MultiClient/DisplayManager/DisplayManager.cs @@ -788,6 +788,6 @@ namespace BizHawk.MultiClient SwappableDisplaySurfaceSet nativeDisplaySurfaceSet = new SwappableDisplaySurfaceSet(); - Thread displayThread; + //Thread displayThread; } } \ No newline at end of file diff --git a/BizHawk.MultiClient/HistoryCollection.cs b/BizHawk.MultiClient/HistoryCollection.cs new file mode 100644 index 0000000000..eeb8b938c9 --- /dev/null +++ b/BizHawk.MultiClient/HistoryCollection.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BizHawk.MultiClient +{ + public class HistoryCollection + { + public List> History = new List>(); + private int curPos = 0; //1-based + + public HistoryCollection(List newState) + { + AddState(newState); + } + + public bool CanUndo + { + get + { + return curPos > 1; + } + } + + public bool CanRedo + { + get + { + return curPos < History.Count; + } + } + + public void AddState(List newState) + { + if (curPos < History.Count) + { + for (int i = curPos + 1; i <= History.Count; i++) + { + History.Remove(History[i - 1]); + } + } + + History.Add(newState); + curPos = History.Count; + } + + public List Undo() + { + if (CanUndo) + { + curPos--; + return History[curPos - 1]; + } + else + { + return null; + } + } + + public List Redo() + { + if (CanRedo) + { + curPos++; + return History[curPos - 1]; + } + else + { + return null; + } + } + } +} diff --git a/BizHawk.MultiClient/tools/RamSearch.cs b/BizHawk.MultiClient/tools/RamSearch.cs index c94629de5d..89f6e57302 100644 --- a/BizHawk.MultiClient/tools/RamSearch.cs +++ b/BizHawk.MultiClient/tools/RamSearch.cs @@ -11,38 +11,37 @@ using System.Globalization; namespace BizHawk.MultiClient { - //TODO: - //Go To Address (Ctrl+G) feature - //Multiple undo levels (List> UndoLists) - /// /// A winform designed to search through ram values /// public partial class RamSearch : Form { - string systemID = "NULL"; - List Searches = new List(); - List undoList = new List(); - List redoList = new List(); + //TODO: + //Go To Address (Ctrl+G) feature + //Multiple undo levels (List> UndoLists) + + private string systemID = "NULL"; + private List Searches = new List(); + private HistoryCollection SearchHistory; private bool IsAWeededList = false; //For deciding whether the weeded list is relevant (0 size could mean all were removed in a legit preview - List domainMenuItems = new List(); - MemoryDomain Domain = new MemoryDomain("NULL", 1, Endian.Little, addr => 0, (a, v) => { }); + private List domainMenuItems = new List(); + private MemoryDomain Domain = new MemoryDomain("NULL", 1, Endian.Little, addr => 0, (a, v) => { }); public enum SCompareTo { PREV, VALUE, ADDRESS, CHANGES }; public enum SOperator { LESS, GREATER, LESSEQUAL, GREATEREQUAL, EQUAL, NOTEQUAL, DIFFBY }; public enum SSigned { SIGNED, UNSIGNED, HEX }; //Reset window position item - int defaultWidth; //For saving the default size of the dialog, so the user can restore if desired - int defaultHeight; - int defaultAddressWidth; - int defaultValueWidth; - int defaultPrevWidth; - int defaultChangesWidth; - string currentFile = ""; - string addressFormatStr = "{0:X4} "; - bool sortReverse; - string sortedCol; + private int defaultWidth; //For saving the default size of the dialog, so the user can restore if desired + private int defaultHeight; + private int defaultAddressWidth; + private int defaultValueWidth; + private int defaultPrevWidth; + private int defaultChangesWidth; + private string currentFile = ""; + private string addressFormatStr = "{0:X4} "; + private bool sortReverse; + private string sortedCol; public void SaveConfigSettings() { @@ -98,11 +97,10 @@ namespace BizHawk.MultiClient private void RamSearch_Load(object sender, EventArgs e) { - ClearUndo(); - ClearRedo(); LoadConfigSettings(); StartNewSearch(); SetMemoryDomainMenu(); + SearchHistory = new HistoryCollection(Searches); } private void SetEndian() @@ -393,8 +391,6 @@ namespace BizHawk.MultiClient private void StartNewSearch() { - ClearUndo(); - ClearRedo(); IsAWeededList = false; Searches.Clear(); SetPlatformAndMemoryDomainLabel(); @@ -454,6 +450,8 @@ namespace BizHawk.MultiClient sortReverse = false; sortedCol = ""; DisplaySearchList(); + SearchHistory = new HistoryCollection(Searches); + UpdateUndoRedoToolItems(); } private void DisplaySearchList() @@ -521,7 +519,6 @@ namespace BizHawk.MultiClient ListView.SelectedIndexCollection indexes = SearchListView.SelectedIndices; if (indexes.Count > 0) { - SaveUndo(); MessageLabel.Text = MakeAddressString(indexes.Count) + " removed"; for (int x = 0; x < indexes.Count; x++) { @@ -529,6 +526,9 @@ namespace BizHawk.MultiClient } indexes.Clear(); DisplaySearchList(); + + SearchHistory.AddState(Searches); + UpdateUndoRedoToolItems(); } } @@ -537,54 +537,35 @@ namespace BizHawk.MultiClient RemoveAddresses(); } - /// - /// Saves the current search list to the undo list - /// This function should be called before any destructive operation to the list! - /// - private void SaveUndo() + private void UpdateUndoRedoToolItems() { - undoList.Clear(); - undoList.AddRange(Searches); - UndotoolStripButton.Enabled = true; + UndotoolStripButton.Enabled = SearchHistory.CanUndo; + RedotoolStripButton2.Enabled = SearchHistory.CanRedo; } private void DoUndo() { - if (undoList.Count > 0) + if (SearchHistory.CanUndo) { - MessageLabel.Text = MakeAddressString(undoList.Count - Searches.Count) + " restored"; - redoList = new List(Searches); - Searches = new List(undoList); - ClearUndo(); - RedotoolStripButton2.Enabled = true; + int oldVal = Searches.Count; + Searches = SearchHistory.Undo(); + int newVal = Searches.Count; + MessageLabel.Text = MakeAddressString(newVal - oldVal) + " restored"; + UpdateUndoRedoToolItems(); DisplaySearchList(); } } - private void ClearUndo() - { - undoList.Clear(); - UndotoolStripButton.Enabled = false; - } - - private void ClearRedo() - { - redoList.Clear(); - RedotoolStripButton2.Enabled = false; - } - private void DoRedo() { - if (redoList.Count > 0) + if (SearchHistory.CanRedo) { - MessageLabel.Text = MakeAddressString(Searches.Count - redoList.Count) + " removed"; - undoList = new List(Searches); - Searches = new List(redoList); - ClearRedo(); - UndotoolStripButton.Enabled = true; + int oldVal = Searches.Count; + Searches = SearchHistory.Redo(); + int newVal = Searches.Count; + MessageLabel.Text = MakeAddressString(newVal - oldVal) + " removed"; + UpdateUndoRedoToolItems(); DisplaySearchList(); - //OutputLabel.Text = "Redo: s" + searchList.Count.ToString() + " u" + - // undoList.Count.ToString() + " r" + redoList.Count.ToString(); } } @@ -689,13 +670,15 @@ namespace BizHawk.MultiClient private void ClearChangeCounts() { - SaveUndo(); for (int x = 0; x < Searches.Count; x++) { Searches[x].Changecount = 0; } DisplaySearchList(); MessageLabel.Text = "Change counts cleared"; + + SearchHistory.AddState(Searches); + UpdateUndoRedoToolItems(); } private void ClearChangeCountstoolStripButton_Click(object sender, EventArgs e) @@ -725,11 +708,12 @@ namespace BizHawk.MultiClient { if (GenerateWeedOutList()) { - SaveUndo(); MessageLabel.Text = MakeAddressString(Searches.Where(x => x.Deleted == true).Count()) + " removed"; TrimWeededList(); UpdateLastSearch(); DisplaySearchList(); + SearchHistory.AddState(Searches); + UpdateUndoRedoToolItems(); } else { @@ -1524,11 +1508,18 @@ namespace BizHawk.MultiClient private void ConvertListsDataType(Watch.DISPTYPE s) { for (int x = 0; x < Searches.Count; x++) + { Searches[x].Signed = s; - for (int x = 0; x < undoList.Count; x++) - undoList[x].Signed = s; - for (int x = 0; x < redoList.Count; x++) - redoList[x].Signed = s; + } + + foreach (List state in SearchHistory.History) + { + foreach (Watch watch in state) + { + watch.Signed = s; + } + } + SetSpecificValueBoxMaxLength(); sortReverse = false; sortedCol = ""; @@ -1538,8 +1529,13 @@ namespace BizHawk.MultiClient private void ConvertListsDataSize(Watch.TYPE s, bool bigendian) { ConvertDataSize(s, bigendian, ref Searches); - ConvertDataSize(s, bigendian, ref undoList); - ConvertDataSize(s, bigendian, ref redoList); + + //TODO + //for (int i = 0; i < SearchHistory.History.Count; i++) + //{ + // ConvertDataSize(s, bigendian, ref SearchHistory.History[i]); + //} + SetSpecificValueBoxMaxLength(); sortReverse = false; sortedCol = ""; @@ -1959,19 +1955,31 @@ namespace BizHawk.MultiClient private void searchToolStripMenuItem_DropDownOpened(object sender, EventArgs e) { if (Searches.Count == 0) + { searchToolStripMenuItem.Enabled = false; + } else + { searchToolStripMenuItem.Enabled = true; + } - if (undoList.Count == 0) - undoToolStripMenuItem.Enabled = false; - else + if (SearchHistory.CanUndo) + { undoToolStripMenuItem.Enabled = true; - - if (redoList.Count == 0) - redoToolStripMenuItem.Enabled = false; + } else + { + undoToolStripMenuItem.Enabled = false; + } + + if (SearchHistory.CanRedo) + { redoToolStripMenuItem.Enabled = true; + } + else + { + redoToolStripMenuItem.Enabled = false; + } ListView.SelectedIndexCollection indexes = SearchListView.SelectedIndices; @@ -2133,12 +2141,12 @@ namespace BizHawk.MultiClient private void DoTruncate() { - - SaveUndo(); MessageLabel.Text = MakeAddressString(Searches.Where(x => x.Deleted == true).Count()) + " removed"; TrimWeededList(); UpdateLastSearch(); DisplaySearchList(); + SearchHistory.AddState(Searches); + UpdateUndoRedoToolItems(); } private void TruncateFromFile()