Refactor CheatList and Cheats to have changed event handlers, and have the client wire up an event handler to update all the relevant dialogs.

This commit is contained in:
adelikat 2013-11-04 15:52:59 +00:00
parent 3f88ece3db
commit 56cb2ba538
15 changed files with 87 additions and 90 deletions

View File

@ -6,12 +6,6 @@ namespace BizHawk.Client.Common
{
public class NESLuaLibrary : LuaLibraryBase
{
public NESLuaLibrary(Action updateCallback = null)
: base()
{
UpdateCallback = updateCallback;
}
public override string Name { get { return "nes"; } }
public override string[] Functions
{
@ -36,16 +30,6 @@ namespace BizHawk.Client.Common
}
}
private Action UpdateCallback;
private void Update()
{
if (UpdateCallback != null)
{
UpdateCallback();
}
}
public void nes_addgamegenie(string code)
{
if (Global.Emulator is NES)
@ -64,7 +48,6 @@ namespace BizHawk.Client.Common
decoder.Value,
decoder.Compare
));
Update();
}
}
@ -120,7 +103,6 @@ namespace BizHawk.Client.Common
Global.CheatList.RemoveRange(
Global.CheatList.Where(x => x.Address == decoder.Address)
);
Update();
}
}

View File

@ -51,6 +51,9 @@ namespace BizHawk.Client.Common
#region Properties
public delegate void CheatEventHandler(object sender);
public event CheatEventHandler Changed;
public bool IsSeparator
{
get { return _watch.IsSeparator; }
@ -155,7 +158,12 @@ namespace BizHawk.Client.Common
{
if (!IsSeparator)
{
bool wasEnabled = _enabled;
_enabled = true;
if (!wasEnabled)
{
Changes();
}
}
}
@ -163,7 +171,12 @@ namespace BizHawk.Client.Common
{
if (!IsSeparator)
{
bool wasEnabled = _enabled;
_enabled = false;
if (wasEnabled)
{
Changes();
}
}
}
@ -172,6 +185,7 @@ namespace BizHawk.Client.Common
if (!IsSeparator)
{
_enabled ^= true;
Changes();
}
}
@ -216,6 +230,7 @@ namespace BizHawk.Client.Common
{
_val++;
Pulse();
Changes();
}
}
@ -225,6 +240,7 @@ namespace BizHawk.Client.Common
{
_val--;
Pulse();
Changes();
}
}
@ -233,6 +249,7 @@ namespace BizHawk.Client.Common
if (Watch.AvailableTypes(_watch.Size).Contains(type))
{
_watch.Type = type;
Changes();
}
}
@ -245,6 +262,14 @@ namespace BizHawk.Client.Common
private int _val;
private bool _enabled;
private void Changes()
{
if (Changed != null)
{
Changed(this);
}
}
#endregion
}
}

View File

@ -11,9 +11,22 @@ namespace BizHawk.Client.Common
{
public class CheatList : IEnumerable<Cheat>
{
private bool _changes;
public bool Changes
{
get
{
return _changes;
}
set
{
_changes = value;
CheatChanged(Cheat.Separator); //Pass a dummy, no cheat invoked this change
}
}
private List<Cheat> _cheatList = new List<Cheat>();
private string _currentFileName = String.Empty;
private bool _changes;
private string _defaultFileName = String.Empty;
public IEnumerator<Cheat> GetEnumerator()
@ -57,11 +70,6 @@ namespace BizHawk.Client.Common
}
}
public void FlagChanges()
{
_changes = true;
}
public int Count
{
get { return _cheatList.Count; }
@ -82,7 +90,7 @@ namespace BizHawk.Client.Common
_defaultFileName = defaultFileName;
_cheatList.Clear();
_currentFileName = String.Empty;
_changes = false;
Changes = false;
}
public void Update()
@ -92,6 +100,7 @@ namespace BizHawk.Client.Common
public void Add(Cheat c)
{
c.Changed += CheatChanged;
if (_cheatList.Any(x => x.Domain == c.Domain && x.Address == c.Address))
{
_cheatList.FirstOrDefault(x => x.Domain == c.Domain && x.Address == c.Address).Enable();
@ -101,11 +110,12 @@ namespace BizHawk.Client.Common
_cheatList.Add(c);
}
_changes = true;
Changes = true;
}
public void Insert(int index, Cheat c)
{
c.Changed += CheatChanged;
if (_cheatList.Any(x => x.Domain == c.Domain && x.Address == c.Address))
{
_cheatList.FirstOrDefault(x => x.Domain == c.Domain && x.Address == c.Address).Enable();
@ -115,13 +125,13 @@ namespace BizHawk.Client.Common
_cheatList.Insert(index, c);
}
_changes = true;
Changes = true;
}
public void Remove(Cheat c)
{
_changes = true;
_cheatList.Remove(c);
Changes = true;
}
public void Remove(Watch w)
@ -130,40 +140,33 @@ namespace BizHawk.Client.Common
var cheat = _cheatList.FirstOrDefault(x => x.Domain == w.Domain && x.Address == w.Address);
if (cheat != null)
{
_changes = true;
_cheatList.Remove(cheat);
Changes = true;
}
}
public void RemoveRange(IEnumerable<Cheat> cheats)
{
_changes = true;
foreach (var cheat in cheats)
{
_cheatList.Remove(cheat);
}
}
public bool Changes
{
get { return _changes; }
Changes = true;
}
public void Clear()
{
_changes = true;
_cheatList.Clear();
Changes = true;
}
public void DisableAll()
{
_changes = true;
_cheatList.ForEach(x => x.Disable());
}
public void EnableAll()
{
_changes = true;
_cheatList.ForEach(x => x.Enable());
}
@ -188,7 +191,7 @@ namespace BizHawk.Client.Common
{
if (Global.Config.CheatsAutoSaveOnClose)
{
if (_changes && _cheatList.Any())
if (Changes && _cheatList.Any())
{
if (String.IsNullOrWhiteSpace(_currentFileName))
{
@ -256,9 +259,9 @@ namespace BizHawk.Client.Common
sw.WriteLine(sb.ToString());
}
_changes = false;
_currentFileName = path;
Global.Config.RecentCheats.Add(_currentFileName);
Changes = false;
return true;
}
catch
@ -282,14 +285,9 @@ namespace BizHawk.Client.Common
using (StreamReader sr = file.OpenText())
{
if (append)
{
_changes = true;
}
else
if (!append)
{
Clear();
_changes = false;
}
string s;
@ -310,7 +308,6 @@ namespace BizHawk.Client.Common
if (s.Length < 6) continue;
//NewCheat c = new NewCheat(
string[] vals = s.Split('\t');
int ADDR = Int32.Parse(vals[0], NumberStyles.HexNumber);
int value = Int32.Parse(vals[1], NumberStyles.HexNumber);
@ -355,6 +352,7 @@ namespace BizHawk.Client.Common
}
}
Changes = true;
return true;
}
@ -528,8 +526,22 @@ namespace BizHawk.Client.Common
}
}
public class CheatListEventArgs
{
public CheatListEventArgs(Cheat c)
{
Cheat = c;
}
public Cheat Cheat { get; private set; }
}
public delegate void CheatListEventHandler(object sender, CheatListEventArgs e);
public event CheatListEventHandler Changed;
#region Privates
//TODO: remove this and make MemoryDomains in IEmulator support this
private static MemoryDomain DomainByName(string name)
{
//Attempts to find the memory domain by name, if it fails, it defaults to index 0
@ -544,6 +556,15 @@ namespace BizHawk.Client.Common
return Global.Emulator.MainMemory;
}
private void CheatChanged(object sender)
{
if (Changed != null)
{
Changed(this, new CheatListEventArgs(sender as Cheat));
}
_changes = true;
}
#endregion
public const string NAME = "NamesColumn";

View File

@ -2181,7 +2181,6 @@ namespace BizHawk.Client.EmuHawk
{
Global.CheatList.Load(filePaths[0], false);
GlobalWin.Tools.Load<Cheats>();
ToolHelpers.UpdateCheatRelatedTools();
}
else if (ext.ToUpper() == ".WCH")
{

View File

@ -74,6 +74,8 @@ namespace BizHawk.Client.EmuHawk
FFMpeg.FFMpegPath = PathManager.MakeProgramRelativePath(Global.Config.FFMpegPath);
Global.CheatList = new CheatList();
Global.CheatList.Changed += ToolHelpers.UpdateCheatRelatedTools;
UpdateStatusSlots();
UpdateKeyPriorityIcon();
@ -1666,7 +1668,6 @@ namespace BizHawk.Client.EmuHawk
else
{
Global.CheatList.NewList(GenerateDefaultCheatFilename());
ToolHelpers.UpdateCheatRelatedTools();
}
}
@ -3586,7 +3587,6 @@ namespace BizHawk.Client.EmuHawk
{
if (Global.CheatList.AttemptToLoadCheatFile())
{
ToolHelpers.UpdateCheatRelatedTools();
GlobalWin.OSD.AddMessage("Cheats file loaded");
}
}

View File

@ -114,7 +114,6 @@ namespace BizHawk.Client.EmuHawk
Global.Config.RecentWatches.Add(path);
UpdateListView();
UpdateMessageLabel();
ToolHelpers.UpdateCheatRelatedTools();
}
}
}
@ -180,7 +179,6 @@ namespace BizHawk.Client.EmuHawk
UpdateListView();
UpdateMessageLabel();
Global.Config.RecentCheats.Add(Global.CheatList.CurrentFileName);
ToolHelpers.UpdateCheatRelatedTools();
}
}
}
@ -233,7 +231,6 @@ namespace BizHawk.Client.EmuHawk
Global.CheatList.Add(CheatEditor.Cheat);
UpdateListView();
UpdateMessageLabel();
ToolHelpers.UpdateCheatRelatedTools();
}
private void EditCheat()
@ -422,7 +419,6 @@ namespace BizHawk.Client.EmuHawk
}
UpdateListView();
ToolHelpers.UpdateCheatRelatedTools();
}
private void MoveDown()
@ -459,7 +455,6 @@ namespace BizHawk.Client.EmuHawk
}
UpdateListView();
ToolHelpers.UpdateCheatRelatedTools();
}
private void Remove()
@ -479,8 +474,6 @@ namespace BizHawk.Client.EmuHawk
private void Toggle()
{
SelectedCheats.ForEach(x => x.Toggle());
ToolHelpers.UpdateCheatRelatedTools();
Global.CheatList.FlagChanges();
}
private void SaveColumnInfo()
@ -575,7 +568,6 @@ namespace BizHawk.Client.EmuHawk
Global.CheatList.NewList(GlobalWin.MainForm.GenerateDefaultCheatFilename());
UpdateListView();
UpdateMessageLabel();
ToolHelpers.UpdateCheatRelatedTools();
}
}
@ -694,7 +686,6 @@ namespace BizHawk.Client.EmuHawk
UpdateListView();
UpdateMessageLabel();
ToolHelpers.UpdateCheatRelatedTools();
}
private void InsertSeparatorMenuItem_Click(object sender, EventArgs e)
@ -710,7 +701,6 @@ namespace BizHawk.Client.EmuHawk
UpdateListView();
UpdateMessageLabel();
ToolHelpers.UpdateCheatRelatedTools();
}
private void MoveUpMenuItem_Click(object sender, EventArgs e)
@ -739,7 +729,6 @@ namespace BizHawk.Client.EmuHawk
private void DisableAllCheatsMenuItem_Click(object sender, EventArgs e)
{
Global.CheatList.DisableAll();
ToolHelpers.UpdateCheatRelatedTools();
}
private void OpenGameGenieEncoderDecoderMenuItem_Click(object sender, EventArgs e)

View File

@ -430,8 +430,6 @@ namespace BizHawk.Client.EmuHawk
VALUE,
COMPARE,
enabled: true));
ToolHelpers.UpdateCheatRelatedTools();
}
}

View File

@ -295,8 +295,6 @@ namespace BizHawk.Client.EmuHawk
compare: null,
enabled: true
));
ToolHelpers.UpdateCheatRelatedTools();
}
}

View File

@ -866,7 +866,7 @@ namespace BizHawk.Client.EmuHawk
UpdateRelatedDialogs();
}
private void UnFreezeAddress(int address) //TODO: refactor to int?
private void UnFreezeAddress(int address)
{
if (address >= 0)
{
@ -874,7 +874,6 @@ namespace BizHawk.Client.EmuHawk
Global.CheatList.RemoveRange(cheats);
}
MemoryViewerBox.Refresh();
ToolHelpers.UpdateCheatRelatedTools();
}
private Watch.WatchSize WatchSize
@ -1463,7 +1462,7 @@ namespace BizHawk.Client.EmuHawk
case Keys.Delete:
if (e.Modifiers == Keys.Shift)
{
ToolHelpers.UnfreezeAll();
Global.CheatList.DisableAll();
}
else
{
@ -1667,12 +1666,12 @@ namespace BizHawk.Client.EmuHawk
private void unfreezeAllToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolHelpers.UnfreezeAll();
Global.CheatList.DisableAll();
}
private void unfreezeAllToolStripMenuItem1_Click(object sender, EventArgs e)
{
ToolHelpers.UnfreezeAll();
Global.CheatList.DisableAll();
}
private void HexEditor_MouseWheel(object sender, MouseEventArgs e)
@ -1698,7 +1697,7 @@ namespace BizHawk.Client.EmuHawk
if (Global.CheatList.IsActive(Domain, address))
{
Global.CheatList.FirstOrDefault(x => x.Domain == Domain && x.Address == address).Increment();
Global.CheatList.FlagChanges();
//Refactor TODO : this won't be necessary, the cheats themselves will raise change events: Global.CheatList.FlagChanges();
}
else
{
@ -1735,7 +1734,7 @@ namespace BizHawk.Client.EmuHawk
if (Global.CheatList.IsActive(Domain, address))
{
Global.CheatList.FirstOrDefault(x => x.Domain == Domain && x.Address == address).Decrement();
Global.CheatList.FlagChanges();
//Refactor TODO : this won't be necessary, the cheats themselves will raise change events:Global.CheatList.FlagChanges();
}
else
{

View File

@ -90,7 +90,7 @@ namespace BizHawk.Client.EmuHawk
new MemoryLuaLibrary().LuaRegister(lua, Docs);
new MainMemoryLuaLibrary(_lua).LuaRegister(lua, Docs);
new MovieLuaLibrary(_lua).LuaRegister(lua, Docs);
new NESLuaLibrary(ToolHelpers.UpdateCheatRelatedTools).LuaRegister(lua, Docs);
new NESLuaLibrary().LuaRegister(lua, Docs);
new SavestateLuaLibrary().LuaRegister(lua, Docs);
new SNESLuaLibrary().LuaRegister(lua, Docs);

View File

@ -168,8 +168,6 @@ namespace BizHawk.Client.EmuHawk
ValueBox.ToRawInt(),
compare
));
ToolHelpers.UpdateCheatRelatedTools();
}
}

View File

@ -364,8 +364,6 @@ namespace BizHawk.Client.EmuHawk
compare: null,
enabled: true
));
ToolHelpers.UpdateCheatRelatedTools();
}
}

View File

@ -205,7 +205,7 @@ namespace BizHawk.Client.EmuHawk
}
}
public static void UpdateCheatRelatedTools()
public static void UpdateCheatRelatedTools(object sender, CheatList.CheatListEventArgs e)
{
GlobalWin.Tools.UpdateValues<RamWatch>();
GlobalWin.Tools.UpdateValues<RamSearch>();
@ -214,12 +214,6 @@ namespace BizHawk.Client.EmuHawk
GlobalWin.MainForm.UpdateCheatStatus();
}
public static void UnfreezeAll()
{
Global.CheatList.DisableAll();
UpdateCheatRelatedTools();
}
public static void FreezeAddress(List<Watch> watches)
{
foreach(var watch in watches)
@ -231,8 +225,6 @@ namespace BizHawk.Client.EmuHawk
);
}
}
UpdateCheatRelatedTools();
}
public static void UnfreezeAddress(List<Watch> watches)
@ -244,8 +236,6 @@ namespace BizHawk.Client.EmuHawk
Global.CheatList.Remove(watch);
}
}
UpdateCheatRelatedTools();
}
public static void ViewInHexEditor(MemoryDomain domain, IEnumerable<int> addresses)

View File

@ -1413,7 +1413,7 @@ namespace BizHawk.Client.EmuHawk
private void UnfreezeAllContextMenuItem_Click(object sender, EventArgs e)
{
ToolHelpers.UnfreezeAll();
Global.CheatList.DisableAll();
}
private void ViewInHexEditorContextMenuItem_Click(object sender, EventArgs e)

View File

@ -1146,7 +1146,7 @@ namespace BizHawk.Client.EmuHawk
private void UnfreezeAllContextMenuItem_Click(object sender, EventArgs e)
{
ToolHelpers.UnfreezeAll();
Global.CheatList.DisableAll();
}
private void ViewInHexEditorContextMenuItem_Click(object sender, EventArgs e)