From 44ba1c25946ab0b4813b70b5db763969f21a8007 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sat, 17 Mar 2012 21:31:48 +0000 Subject: [PATCH] Lua Console - implement Save Session and Save Session As --- .../tools/LuaConsole.Designer.cs | 107 +++++++++--------- BizHawk.MultiClient/tools/LuaConsole.cs | 98 ++++++++++++++-- 2 files changed, 144 insertions(+), 61 deletions(-) diff --git a/BizHawk.MultiClient/tools/LuaConsole.Designer.cs b/BizHawk.MultiClient/tools/LuaConsole.Designer.cs index 81bb9bba00..7ef91617ab 100644 --- a/BizHawk.MultiClient/tools/LuaConsole.Designer.cs +++ b/BizHawk.MultiClient/tools/LuaConsole.Designer.cs @@ -30,9 +30,6 @@ { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LuaConsole)); - this.LuaListView = new BizHawk.VirtualListView(); - this.Script = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); - this.PathName = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.toggleScriptToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.editScriptToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -79,6 +76,8 @@ this.OutputBox = new System.Windows.Forms.RichTextBox(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.NumberOfScripts = new System.Windows.Forms.Label(); + this.contextMenuStrip2 = new System.Windows.Forms.ContextMenuStrip(this.components); + this.clearToolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStrip1 = new ToolStripEx(); this.openToolStripButton = new System.Windows.Forms.ToolStripButton(); this.copyToolStripButton = new System.Windows.Forms.ToolStripButton(); @@ -88,48 +87,16 @@ this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator(); this.toolStripButtonMoveUp = new System.Windows.Forms.ToolStripButton(); this.toolStripButtonMoveDown = new System.Windows.Forms.ToolStripButton(); - this.contextMenuStrip2 = new System.Windows.Forms.ContextMenuStrip(this.components); - this.clearToolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem(); + this.LuaListView = new BizHawk.VirtualListView(); + this.Script = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.PathName = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.contextMenuStrip1.SuspendLayout(); this.menuStrip1.SuspendLayout(); this.groupBox1.SuspendLayout(); - this.toolStrip1.SuspendLayout(); this.contextMenuStrip2.SuspendLayout(); + this.toolStrip1.SuspendLayout(); this.SuspendLayout(); // - // LuaListView - // - this.LuaListView.CheckBoxes = true; - this.LuaListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { - this.Script, - this.PathName}); - this.LuaListView.ContextMenuStrip = this.contextMenuStrip1; - this.LuaListView.FullRowSelect = true; - this.LuaListView.GridLines = true; - this.LuaListView.HideSelection = false; - this.LuaListView.ItemCount = 0; - this.LuaListView.Location = new System.Drawing.Point(13, 71); - this.LuaListView.Name = "LuaListView"; - this.LuaListView.selectedItem = -1; - this.LuaListView.Size = new System.Drawing.Size(291, 280); - this.LuaListView.TabIndex = 0; - this.LuaListView.UseCompatibleStateImageBehavior = false; - this.LuaListView.View = System.Windows.Forms.View.Details; - this.LuaListView.ItemActivate += new System.EventHandler(this.LuaListView_ItemActivate); - this.LuaListView.SelectedIndexChanged += new System.EventHandler(this.LuaListView_SelectedIndexChanged); - this.LuaListView.DoubleClick += new System.EventHandler(this.LuaListView_DoubleClick); - this.LuaListView.KeyDown += new System.Windows.Forms.KeyEventHandler(this.LuaListView_KeyDown); - // - // Script - // - this.Script.Text = "Script"; - this.Script.Width = 92; - // - // PathName - // - this.PathName.Text = "Path"; - this.PathName.Width = 195; - // // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { @@ -215,6 +182,7 @@ this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20); this.fileToolStripMenuItem.Text = "&File"; + this.fileToolStripMenuItem.DropDownOpened += new System.EventHandler(this.fileToolStripMenuItem_DropDownOpened); // // newToolStripMenuItem // @@ -271,18 +239,18 @@ // noneToolStripMenuItem1 // this.noneToolStripMenuItem1.Name = "noneToolStripMenuItem1"; - this.noneToolStripMenuItem1.Size = new System.Drawing.Size(103, 22); + this.noneToolStripMenuItem1.Size = new System.Drawing.Size(152, 22); this.noneToolStripMenuItem1.Text = "None"; // // toolStripSeparator8 // this.toolStripSeparator8.Name = "toolStripSeparator8"; - this.toolStripSeparator8.Size = new System.Drawing.Size(100, 6); + this.toolStripSeparator8.Size = new System.Drawing.Size(149, 6); // // clearToolStripMenuItem1 // this.clearToolStripMenuItem1.Name = "clearToolStripMenuItem1"; - this.clearToolStripMenuItem1.Size = new System.Drawing.Size(103, 22); + this.clearToolStripMenuItem1.Size = new System.Drawing.Size(152, 22); this.clearToolStripMenuItem1.Text = "Clear"; // // recentToolStripMenuItem @@ -515,6 +483,20 @@ this.NumberOfScripts.TabIndex = 4; this.NumberOfScripts.Text = " 0 Scripts "; // + // contextMenuStrip2 + // + this.contextMenuStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.clearToolStripMenuItem2}); + this.contextMenuStrip2.Name = "contextMenuStrip2"; + this.contextMenuStrip2.Size = new System.Drawing.Size(102, 26); + // + // clearToolStripMenuItem2 + // + this.clearToolStripMenuItem2.Name = "clearToolStripMenuItem2"; + this.clearToolStripMenuItem2.Size = new System.Drawing.Size(101, 22); + this.clearToolStripMenuItem2.Text = "&Clear"; + this.clearToolStripMenuItem2.Click += new System.EventHandler(this.clearToolStripMenuItem2_Click); + // // toolStrip1 // this.toolStrip1.ClickThrough = true; @@ -608,19 +590,38 @@ this.toolStripButtonMoveDown.Text = "Move Down"; this.toolStripButtonMoveDown.Click += new System.EventHandler(this.toolStripButtonMoveDown_Click); // - // contextMenuStrip2 + // LuaListView // - this.contextMenuStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.clearToolStripMenuItem2}); - this.contextMenuStrip2.Name = "contextMenuStrip2"; - this.contextMenuStrip2.Size = new System.Drawing.Size(102, 26); + this.LuaListView.CheckBoxes = true; + this.LuaListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { + this.Script, + this.PathName}); + this.LuaListView.ContextMenuStrip = this.contextMenuStrip1; + this.LuaListView.FullRowSelect = true; + this.LuaListView.GridLines = true; + this.LuaListView.HideSelection = false; + this.LuaListView.ItemCount = 0; + this.LuaListView.Location = new System.Drawing.Point(13, 71); + this.LuaListView.Name = "LuaListView"; + this.LuaListView.selectedItem = -1; + this.LuaListView.Size = new System.Drawing.Size(291, 280); + this.LuaListView.TabIndex = 0; + this.LuaListView.UseCompatibleStateImageBehavior = false; + this.LuaListView.View = System.Windows.Forms.View.Details; + this.LuaListView.ItemActivate += new System.EventHandler(this.LuaListView_ItemActivate); + this.LuaListView.SelectedIndexChanged += new System.EventHandler(this.LuaListView_SelectedIndexChanged); + this.LuaListView.DoubleClick += new System.EventHandler(this.LuaListView_DoubleClick); + this.LuaListView.KeyDown += new System.Windows.Forms.KeyEventHandler(this.LuaListView_KeyDown); // - // clearToolStripMenuItem2 + // Script // - this.clearToolStripMenuItem2.Name = "clearToolStripMenuItem2"; - this.clearToolStripMenuItem2.Size = new System.Drawing.Size(152, 22); - this.clearToolStripMenuItem2.Text = "&Clear"; - this.clearToolStripMenuItem2.Click += new System.EventHandler(this.clearToolStripMenuItem2_Click); + this.Script.Text = "Script"; + this.Script.Width = 92; + // + // PathName + // + this.PathName.Text = "Path"; + this.PathName.Width = 195; // // LuaConsole // @@ -645,9 +646,9 @@ this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); this.groupBox1.ResumeLayout(false); + this.contextMenuStrip2.ResumeLayout(false); this.toolStrip1.ResumeLayout(false); this.toolStrip1.PerformLayout(); - this.contextMenuStrip2.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); diff --git a/BizHawk.MultiClient/tools/LuaConsole.cs b/BizHawk.MultiClient/tools/LuaConsole.cs index 9f190cb7fb..15282ce531 100644 --- a/BizHawk.MultiClient/tools/LuaConsole.cs +++ b/BizHawk.MultiClient/tools/LuaConsole.cs @@ -13,27 +13,22 @@ namespace BizHawk.MultiClient { public partial class LuaConsole : Form { - //session file saving //track changes - //new session - //open session //recent session - //save/save as session //options - autoload session //options - disable scripts on load //TODO: remember column widths //TODO: restore column width on restore default settings //TODO: load scripts from recent scripts menu //TODO: context menu & main menu - Edit is grayed out if seperator is highlighted - //Free lua object when toggling a lua script off? - //Fix up lua functions list display int defaultWidth; //For saving the default size of the dialog, so the user can restore if desired int defaultHeight; - + string currentSessionFile = ""; List luaList = new List(); public LuaImplementation LuaImp; string lastLuaFile = ""; + bool changes = true; //TODO private List GetLuaFileList() { @@ -282,12 +277,18 @@ namespace BizHawk.MultiClient private void saveToolStripMenuItem_Click(object sender, EventArgs e) { + if (string.Compare(currentSessionFile, "") == 0) return; + if (changes) + { + SaveSession(currentSessionFile); + AddText('\n' + Path.GetFileName(currentSessionFile) + " saved."); + } } private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { - + SaveAs(); } private void newToolStripMenuItem_Click(object sender, EventArgs e) @@ -654,6 +655,7 @@ namespace BizHawk.MultiClient RunLuaScripts(); DisplayLuaList(); UpdateNumberOfScripts(); + ClearOutput(); } } @@ -704,5 +706,85 @@ namespace BizHawk.MultiClient { OutputBox.Text = ""; } + + private FileInfo GetSaveFileFromUser() + { + var sfd = new SaveFileDialog(); + if (currentSessionFile.Length > 0) + { + sfd.FileName = Path.GetFileNameWithoutExtension(currentSessionFile); + sfd.InitialDirectory = Path.GetDirectoryName(currentSessionFile); + } + else if (!(Global.Emulator is NullEmulator)) + { + sfd.FileName = PathManager.FilesystemSafeName(Global.Game); + sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.LuaPath, ""); + + } + else + { + sfd.FileName = "NULL"; + sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.LuaPath, ""); + } + sfd.Filter = "Lua Session Files (*.luases)|*.luases|All Files|*.*"; + sfd.RestoreDirectory = true; + Global.Sound.StopSound(); + var result = sfd.ShowDialog(); + Global.Sound.StartSound(); + if (result != DialogResult.OK) + return null; + var file = new FileInfo(sfd.FileName); + return file; + } + + private void SaveAs() + { + var file = GetSaveFileFromUser(); + if (file != null) + { + SaveSession(file.FullName); + currentSessionFile = file.FullName; + AddText('\n' + Path.GetFileName(currentSessionFile) + " saved."); + //Global.Config.Recent.RecentWatches.Add(file.FullName); + } + } + + private bool SaveSession(string path) + { + var file = new FileInfo(path); + + using (StreamWriter sw = new StreamWriter(path)) + { + string str = ""; + for (int i = 0; i < luaList.Count; i++) + { + if (!luaList[i].IsSeparator) + { + if (luaList[i].Enabled) + str += "1 "; + else + str += "0 "; + + str += luaList[i].Path; + } + } + sw.WriteLine(str); + } + + changes = false; + return true; + } + + private void fileToolStripMenuItem_DropDownOpened(object sender, EventArgs e) + { + if (string.Compare(currentSessionFile, "") == 0) // || !changes) //TODO: changes + { + saveToolStripMenuItem.Enabled = false; + } + else + { + saveToolStripMenuItem.Enabled = true; + } + } } }