Some fixes to the ToolManager and make cheats not redraw every frame

This commit is contained in:
adelikat 2013-11-07 20:33:29 +00:00
parent 0adffa65c1
commit 0b71e4d08c
3 changed files with 84 additions and 30 deletions

View File

@ -74,14 +74,7 @@ namespace BizHawk.Client.EmuHawk
public void UpdateValues() public void UpdateValues()
{ {
if (!IsHandleCreated || IsDisposed) //Do nothing;
{
return;
}
else
{
UpdateListView();
}
} }
public void Restart() public void Restart()
@ -89,7 +82,10 @@ namespace BizHawk.Client.EmuHawk
StartNewList(); StartNewList();
} }
private void UpdateListView() /// <summary>
/// Tools that want to refresh the cheats list should call this, not UpdateValues
/// </summary>
public void UpdateDialog()
{ {
CheatListView.ItemCount = Global.CheatList.Count; CheatListView.ItemCount = Global.CheatList.Count;
TotalLabel.Text = Global.CheatList.CheatCount.ToString() TotalLabel.Text = Global.CheatList.CheatCount.ToString()
@ -115,7 +111,7 @@ namespace BizHawk.Client.EmuHawk
else else
{ {
Global.Config.RecentWatches.Add(path); Global.Config.RecentWatches.Add(path);
UpdateListView(); UpdateDialog();
UpdateMessageLabel(); UpdateMessageLabel();
} }
} }
@ -180,7 +176,7 @@ namespace BizHawk.Client.EmuHawk
if (result) if (result)
{ {
Global.CheatList.Load(file.FullName, append); Global.CheatList.Load(file.FullName, append);
UpdateListView(); UpdateDialog();
UpdateMessageLabel(); UpdateMessageLabel();
Global.Config.RecentCheats.Add(Global.CheatList.CurrentFileName); Global.Config.RecentCheats.Add(Global.CheatList.CurrentFileName);
} }
@ -223,7 +219,7 @@ namespace BizHawk.Client.EmuHawk
private void AddCheat() private void AddCheat()
{ {
Global.CheatList.Add(CheatEditor.Cheat); Global.CheatList.Add(CheatEditor.Cheat);
UpdateListView(); UpdateDialog();
UpdateMessageLabel(); UpdateMessageLabel();
} }
@ -407,7 +403,7 @@ namespace BizHawk.Client.EmuHawk
CheatListView.SelectItem(newi, true); CheatListView.SelectItem(newi, true);
} }
UpdateListView(); UpdateDialog();
} }
private void MoveDown() private void MoveDown()
@ -443,7 +439,7 @@ namespace BizHawk.Client.EmuHawk
CheatListView.SelectItem(newi, true); CheatListView.SelectItem(newi, true);
} }
UpdateListView(); UpdateDialog();
} }
private void Remove() private void Remove()
@ -457,7 +453,7 @@ namespace BizHawk.Client.EmuHawk
CheatListView.SelectedIndices.Clear(); CheatListView.SelectedIndices.Clear();
} }
UpdateListView(); UpdateDialog();
} }
private void Toggle() private void Toggle()
@ -547,7 +543,7 @@ namespace BizHawk.Client.EmuHawk
private void StartNewList() private void StartNewList()
{ {
Global.CheatList.NewList(GlobalWin.MainForm.GenerateDefaultCheatFilename()); Global.CheatList.NewList(GlobalWin.MainForm.GenerateDefaultCheatFilename());
UpdateListView(); UpdateDialog();
UpdateMessageLabel(); UpdateMessageLabel();
ToggleGameGenieButton(); ToggleGameGenieButton();
} }
@ -679,7 +675,7 @@ namespace BizHawk.Client.EmuHawk
} }
} }
UpdateListView(); UpdateDialog();
UpdateMessageLabel(); UpdateMessageLabel();
} }
@ -694,7 +690,7 @@ namespace BizHawk.Client.EmuHawk
Global.CheatList.Add(Cheat.Separator); Global.CheatList.Add(Cheat.Separator);
} }
UpdateListView(); UpdateDialog();
UpdateMessageLabel(); UpdateMessageLabel();
} }
@ -944,7 +940,7 @@ namespace BizHawk.Client.EmuHawk
_sortedColumn = column.Name; _sortedColumn = column.Name;
_sortReverse ^= true; _sortReverse ^= true;
UpdateListView(); UpdateDialog();
} }
private void NewCheatForm_DragDrop(object sender, DragEventArgs e) private void NewCheatForm_DragDrop(object sender, DragEventArgs e)
@ -953,7 +949,7 @@ namespace BizHawk.Client.EmuHawk
if (Path.GetExtension(filePaths[0]) == (".cht")) if (Path.GetExtension(filePaths[0]) == (".cht"))
{ {
LoadFile(new FileInfo(filePaths[0]), append: false); LoadFile(new FileInfo(filePaths[0]), append: false);
UpdateListView(); UpdateDialog();
UpdateMessageLabel(); UpdateMessageLabel();
} }
} }

View File

@ -210,7 +210,12 @@ namespace BizHawk.Client.EmuHawk
GlobalWin.Tools.UpdateValues<RamWatch>(); GlobalWin.Tools.UpdateValues<RamWatch>();
GlobalWin.Tools.UpdateValues<RamSearch>(); GlobalWin.Tools.UpdateValues<RamSearch>();
GlobalWin.Tools.UpdateValues<HexEditor>(); GlobalWin.Tools.UpdateValues<HexEditor>();
GlobalWin.Tools.UpdateValues<Cheats>();
if (GlobalWin.Tools.Has<Cheats>())
{
GlobalWin.Tools.Cheats.UpdateDialog();
}
GlobalWin.MainForm.UpdateCheatStatus(); GlobalWin.MainForm.UpdateCheatStatus();
} }

View File

@ -60,17 +60,20 @@ namespace BizHawk.Client.EmuHawk
var existingTool = _tools.FirstOrDefault(x => x is T); var existingTool = _tools.FirstOrDefault(x => x is T);
if (existingTool != null) if (existingTool != null)
{ {
return existingTool; if (existingTool.IsDisposed)
{
Close<T>();
return CreateInstance<T>();
}
else
{
return existingTool;
}
} }
else else
{ {
var tool = Activator.CreateInstance(typeof(T)); return CreateInstance<T>();
//Add to the list and extract it, so it will be strongly typed as T
_tools.Add(tool as IToolForm);
return _tools.FirstOrDefault(x => x is T);
} }
} }
public void UpdateBefore() public void UpdateBefore()
@ -78,7 +81,10 @@ namespace BizHawk.Client.EmuHawk
var beforeList = _tools.Where(x => x.UpdateBefore); var beforeList = _tools.Where(x => x.UpdateBefore);
foreach (var tool in beforeList) foreach (var tool in beforeList)
{ {
tool.UpdateValues(); if (!tool.IsDisposed)
{
tool.UpdateValues();
}
} }
} }
@ -87,7 +93,10 @@ namespace BizHawk.Client.EmuHawk
var afterList = _tools.Where(x => !x.UpdateBefore); var afterList = _tools.Where(x => !x.UpdateBefore);
foreach (var tool in afterList) foreach (var tool in afterList)
{ {
tool.UpdateValues(); if (!tool.IsDisposed)
{
tool.UpdateValues();
}
} }
} }
@ -96,9 +105,11 @@ namespace BizHawk.Client.EmuHawk
/// </summary> /// </summary>
public void UpdateValues<T>() where T : IToolForm public void UpdateValues<T>() where T : IToolForm
{ {
CloseIfDisposed<T>();
var tool = _tools.FirstOrDefault(x => x is T); var tool = _tools.FirstOrDefault(x => x is T);
if (tool != null) if (tool != null)
{ {
tool.UpdateValues(); tool.UpdateValues();
} }
} }
@ -114,6 +125,7 @@ namespace BizHawk.Client.EmuHawk
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
public void Restart<T>() where T : IToolForm public void Restart<T>() where T : IToolForm
{ {
CloseIfDisposed<T>();
var tool = _tools.FirstOrDefault(x => x is T); var tool = _tools.FirstOrDefault(x => x is T);
if (tool != null) if (tool != null)
{ {
@ -178,6 +190,24 @@ namespace BizHawk.Client.EmuHawk
_tools.Clear(); _tools.Clear();
} }
private IToolForm CreateInstance<T>()
{
var tool = Activator.CreateInstance(typeof(T));
//Add to the list and extract it, so it will be strongly typed as T
_tools.Add(tool as IToolForm);
return _tools.FirstOrDefault(x => x is T);
}
private void CloseIfDisposed<T>() where T : IToolForm
{
var existingTool = _tools.FirstOrDefault(x => x is T);
if (existingTool != null && existingTool.IsDisposed)
{
Close<T>();
}
}
//Note: Referencing these properties creates an instance of the tool and persists it. They should be referenced by type if this is not desired //Note: Referencing these properties creates an instance of the tool and persists it. They should be referenced by type if this is not desired
#region Tools #region Tools
@ -227,6 +257,29 @@ namespace BizHawk.Client.EmuHawk
} }
} }
public Cheats Cheats
{
get
{
var tool = _tools.FirstOrDefault(x => x is Cheats);
if (tool != null)
{
if (tool.IsDisposed)
{
_tools.Remove(tool);
}
else
{
return tool as Cheats;
}
}
var newTool = new Cheats();
_tools.Add(newTool);
return newTool;
}
}
public HexEditor HexEditor public HexEditor HexEditor
{ {
get get