From 9188baf7f19ec7f7f96e79fe10076ef49a1fc847 Mon Sep 17 00:00:00 2001 From: Sour Date: Sun, 10 Mar 2019 19:37:42 -0400 Subject: [PATCH] UI: Added recent files menu --- InteropDLL/EmuApiWrapper.cpp | 2 +- UI/Config/Configuration.cs | 41 +-------------------- UI/Config/RecentItems.cs | 71 ++++++++++++++++++++++++++++++++++++ UI/EmuRunner.cs | 28 ++++++++++++++ UI/Forms/frmMain.Designer.cs | 55 +++++++++++++++++++++++----- UI/Forms/frmMain.cs | 24 +++++++----- UI/UI.csproj | 2 + 7 files changed, 164 insertions(+), 59 deletions(-) create mode 100644 UI/Config/RecentItems.cs create mode 100644 UI/EmuRunner.cs diff --git a/InteropDLL/EmuApiWrapper.cpp b/InteropDLL/EmuApiWrapper.cpp index 5e09922..e27d122 100644 --- a/InteropDLL/EmuApiWrapper.cpp +++ b/InteropDLL/EmuApiWrapper.cpp @@ -87,7 +87,7 @@ extern "C" { } } - DllExport void __stdcall LoadRom(char* filename, char* patchFile) { _console->LoadRom((VirtualFile)filename, (VirtualFile)patchFile); } + DllExport void __stdcall LoadRom(char* filename, char* patchFile) { _console->LoadRom((VirtualFile)filename, patchFile ? (VirtualFile)patchFile : VirtualFile()); } //DllExport void __stdcall AddKnownGameFolder(char* folder) { FolderUtilities::AddKnownGameFolder(folder); } //DllExport void __stdcall SetFolderOverrides(char* saveFolder, char* saveStateFolder, char* screenshotFolder) { FolderUtilities::SetFolderOverrides(saveFolder, saveStateFolder, screenshotFolder); } diff --git a/UI/Config/Configuration.cs b/UI/Config/Configuration.cs index 5131583..4c79d90 100644 --- a/UI/Config/Configuration.cs +++ b/UI/Config/Configuration.cs @@ -12,11 +12,10 @@ namespace Mesen.GUI.Config { public class Configuration { - private const int MaxRecentFiles = 10; private bool _needToSave = false; public string Version = "0.1.0"; - public List RecentFiles; + public RecentItems RecentFiles; public VideoConfig Video; public AudioConfig Audio; public DebugInfo Debug; @@ -25,7 +24,7 @@ namespace Mesen.GUI.Config public Configuration() { - RecentFiles = new List(); + RecentFiles = new RecentItems(); Debug = new DebugInfo(); Video = new VideoConfig(); Audio = new AudioConfig(); @@ -61,21 +60,6 @@ namespace Mesen.GUI.Config public void InitializeDefaults() { } - - public void AddRecentFile(ResourcePath romFile, ResourcePath? patchFile) - { - RecentItem existingItem = RecentFiles.Where((item) => item.RomFile == romFile && item.PatchFile == patchFile).FirstOrDefault(); - if(existingItem != null) { - RecentFiles.Remove(existingItem); - } - RecentItem recentItem = new RecentItem { RomFile = romFile, PatchFile = patchFile }; - - RecentFiles.Insert(0, recentItem); - if(RecentFiles.Count > Configuration.MaxRecentFiles) { - RecentFiles.RemoveAt(Configuration.MaxRecentFiles); - } - ConfigManager.ApplyChanges(); - } public static Configuration Deserialize(string configFile) { @@ -122,27 +106,6 @@ namespace Mesen.GUI.Config } } - public class RecentItem - { - public ResourcePath RomFile; - public ResourcePath? PatchFile; - - public override string ToString() - { - string text; - /*if(ConfigManager.Config.PreferenceInfo.ShowFullPathInRecents) { - text = RomFile.ReadablePath.Replace("&", "&&"); - } else {*/ - text = Path.GetFileName(RomFile.FileName).Replace("&", "&&"); - //} - - if(PatchFile.HasValue) { - text += " [" + Path.GetFileName(PatchFile.Value) + "]"; - } - return text; - } - } - [Flags] public enum DefaultKeyMappingType { diff --git a/UI/Config/RecentItems.cs b/UI/Config/RecentItems.cs new file mode 100644 index 0000000..3ed6f90 --- /dev/null +++ b/UI/Config/RecentItems.cs @@ -0,0 +1,71 @@ +using Mesen.GUI.Forms; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Mesen.GUI.Config +{ + public class RecentItems + { + private const int MaxRecentFiles = 10; + public List Items; + + public void AddRecentFile(ResourcePath romFile, ResourcePath? patchFile) + { + if(patchFile.HasValue && string.IsNullOrWhiteSpace(patchFile)) { + patchFile = null; + } + + RecentItem existingItem = Items.Where((item) => item.RomFile == romFile && item.PatchFile == patchFile).FirstOrDefault(); + if(existingItem != null) { + Items.Remove(existingItem); + } + RecentItem recentItem = new RecentItem { RomFile = romFile, PatchFile = patchFile }; + + Items.Insert(0, recentItem); + if(Items.Count > RecentItems.MaxRecentFiles) { + Items.RemoveAt(RecentItems.MaxRecentFiles); + } + ConfigManager.ApplyChanges(); + } + + public List GetMenuItems() + { + List menuItems = new List(); + foreach(RecentItem recentItem in Items) { + ToolStripMenuItem tsmi = new ToolStripMenuItem(); + tsmi.Text = recentItem.ToString(); + tsmi.Click += (object sender, EventArgs args) => { + EmuRunner.LoadRom(recentItem.RomFile, recentItem.PatchFile); + }; + menuItems.Add(tsmi); + } + return menuItems; + } + } + + public class RecentItem + { + public ResourcePath RomFile; + public ResourcePath? PatchFile; + + public override string ToString() + { + string text; + /*if(ConfigManager.Config.PreferenceInfo.ShowFullPathInRecents) { + text = RomFile.ReadablePath.Replace("&", "&&"); + } else {*/ + text = Path.GetFileName(RomFile.FileName).Replace("&", "&&"); + //} + + if(PatchFile.HasValue) { + text += " [" + Path.GetFileName(PatchFile.Value) + "]"; + } + return text; + } + } +} diff --git a/UI/EmuRunner.cs b/UI/EmuRunner.cs new file mode 100644 index 0000000..76d9d94 --- /dev/null +++ b/UI/EmuRunner.cs @@ -0,0 +1,28 @@ +using Mesen.GUI.Config; +using Mesen.GUI.Forms; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Mesen.GUI +{ + public static class EmuRunner + { + private static Thread _emuThread = null; + + public static void LoadRom(ResourcePath romPath, ResourcePath? patchPath = null) + { + EmuApi.LoadRom(romPath, patchPath); + + ConfigManager.Config.RecentFiles.AddRecentFile(romPath, patchPath); + + _emuThread = new Thread(() => { + EmuApi.Run(); + }); + _emuThread.Start(); + } + } +} diff --git a/UI/Forms/frmMain.Designer.cs b/UI/Forms/frmMain.Designer.cs index a944e16..94bcbb8 100644 --- a/UI/Forms/frmMain.Designer.cs +++ b/UI/Forms/frmMain.Designer.cs @@ -32,6 +32,7 @@ this.mnuFile = new System.Windows.Forms.ToolStripMenuItem(); this.mnuOpen = new System.Windows.Forms.ToolStripMenuItem(); this.optionsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.mnuAudioConfig = new System.Windows.Forms.ToolStripMenuItem(); this.mnuVideoConfig = new System.Windows.Forms.ToolStripMenuItem(); this.debugToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.mnuRun = new System.Windows.Forms.ToolStripMenuItem(); @@ -44,7 +45,10 @@ this.mnuTilemapViewer = new System.Windows.Forms.ToolStripMenuItem(); this.mnuEventViewer = new System.Windows.Forms.ToolStripMenuItem(); this.pnlRenderer = new System.Windows.Forms.Panel(); - this.mnuAudioConfig = new System.Windows.Forms.ToolStripMenuItem(); + this.mnuRecentFiles = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItem6 = new System.Windows.Forms.ToolStripSeparator(); + this.mnuExit = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripSeparator(); this.mnuMain.SuspendLayout(); this.pnlRenderer.SuspendLayout(); this.SuspendLayout(); @@ -71,17 +75,22 @@ // mnuFile // this.mnuFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.mnuOpen}); + this.mnuOpen, + this.toolStripMenuItem2, + this.mnuRecentFiles, + this.toolStripMenuItem6, + this.mnuExit}); this.mnuFile.Name = "mnuFile"; this.mnuFile.Size = new System.Drawing.Size(37, 20); this.mnuFile.Text = "File"; + this.mnuFile.DropDownOpening += new System.EventHandler(this.mnuFile_DropDownOpening); // // mnuOpen // this.mnuOpen.Image = global::Mesen.GUI.Properties.Resources.Folder; this.mnuOpen.Name = "mnuOpen"; this.mnuOpen.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O))); - this.mnuOpen.Size = new System.Drawing.Size(146, 22); + this.mnuOpen.Size = new System.Drawing.Size(152, 22); this.mnuOpen.Text = "Open"; this.mnuOpen.Click += new System.EventHandler(this.mnuOpen_Click); // @@ -94,6 +103,14 @@ this.optionsToolStripMenuItem.Size = new System.Drawing.Size(61, 20); this.optionsToolStripMenuItem.Text = "Options"; // + // mnuAudioConfig + // + this.mnuAudioConfig.Image = global::Mesen.GUI.Properties.Resources.Audio; + this.mnuAudioConfig.Name = "mnuAudioConfig"; + this.mnuAudioConfig.Size = new System.Drawing.Size(152, 22); + this.mnuAudioConfig.Text = "Audio"; + this.mnuAudioConfig.Click += new System.EventHandler(this.mnuAudioConfig_Click); + // // mnuVideoConfig // this.mnuVideoConfig.Image = global::Mesen.GUI.Properties.Resources.VideoOptions; @@ -197,13 +214,29 @@ this.pnlRenderer.Size = new System.Drawing.Size(512, 448); this.pnlRenderer.TabIndex = 2; // - // mnuAudioConfig + // mnuRecentFiles // - this.mnuAudioConfig.Image = global::Mesen.GUI.Properties.Resources.Audio; - this.mnuAudioConfig.Name = "mnuAudioConfig"; - this.mnuAudioConfig.Size = new System.Drawing.Size(152, 22); - this.mnuAudioConfig.Text = "Audio"; - this.mnuAudioConfig.Click += new System.EventHandler(this.mnuAudioConfig_Click); + this.mnuRecentFiles.Name = "mnuRecentFiles"; + this.mnuRecentFiles.Size = new System.Drawing.Size(152, 22); + this.mnuRecentFiles.Text = "Recent Files"; + // + // toolStripMenuItem6 + // + this.toolStripMenuItem6.Name = "toolStripMenuItem6"; + this.toolStripMenuItem6.Size = new System.Drawing.Size(149, 6); + // + // mnuExit + // + this.mnuExit.Image = global::Mesen.GUI.Properties.Resources.Exit; + this.mnuExit.Name = "mnuExit"; + this.mnuExit.Size = new System.Drawing.Size(152, 22); + this.mnuExit.Text = "Exit"; + this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click); + // + // toolStripMenuItem2 + // + this.toolStripMenuItem2.Name = "toolStripMenuItem2"; + this.toolStripMenuItem2.Size = new System.Drawing.Size(149, 6); // // frmMain // @@ -244,5 +277,9 @@ private System.Windows.Forms.ToolStripMenuItem mnuVideoConfig; private System.Windows.Forms.Panel pnlRenderer; private System.Windows.Forms.ToolStripMenuItem mnuAudioConfig; + private System.Windows.Forms.ToolStripSeparator toolStripMenuItem2; + private System.Windows.Forms.ToolStripMenuItem mnuRecentFiles; + private System.Windows.Forms.ToolStripSeparator toolStripMenuItem6; + private System.Windows.Forms.ToolStripMenuItem mnuExit; } } \ No newline at end of file diff --git a/UI/Forms/frmMain.cs b/UI/Forms/frmMain.cs index 67c05ef..e085656 100644 --- a/UI/Forms/frmMain.cs +++ b/UI/Forms/frmMain.cs @@ -136,7 +136,7 @@ namespace Mesen.GUI.Forms using(OpenFileDialog ofd = new OpenFileDialog()) { ofd.Filter = ResourceHelper.GetMessage("FilterRom"); if(ofd.ShowDialog() == DialogResult.OK) { - LoadFile(ofd.FileName); + EmuRunner.LoadRom(ofd.FileName); } } } @@ -151,14 +151,6 @@ namespace Mesen.GUI.Forms DebugApi.Step(1000); } - private void LoadFile(string filepath) - { - EmuApi.LoadRom(filepath); - Task.Run(() => { - EmuApi.Run(); - }); - } - protected override void OnDragDrop(DragEventArgs e) { base.OnDragDrop(e); @@ -166,7 +158,7 @@ namespace Mesen.GUI.Forms try { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); if(File.Exists(files[0])) { - LoadFile(files[0]); + EmuRunner.LoadRom(files[0]); this.Activate(); } else { //InteropEmu.DisplayMessage("Error", "File not found: " + files[0]); @@ -190,5 +182,17 @@ namespace Mesen.GUI.Forms MesenMsgBox.Show("UnexpectedError", MessageBoxButtons.OK, MessageBoxIcon.Error, ex.ToString()); } } + + private void mnuExit_Click(object sender, EventArgs e) + { + this.Close(); + } + + private void mnuFile_DropDownOpening(object sender, EventArgs e) + { + mnuRecentFiles.DropDownItems.Clear(); + mnuRecentFiles.DropDownItems.AddRange(ConfigManager.Config.RecentFiles.GetMenuItems().ToArray()); + mnuRecentFiles.Enabled = ConfigManager.Config.RecentFiles.Items.Count > 0; + } } } diff --git a/UI/UI.csproj b/UI/UI.csproj index fa388fb..aa7c255 100644 --- a/UI/UI.csproj +++ b/UI/UI.csproj @@ -233,6 +233,7 @@ + @@ -502,6 +503,7 @@ +