From 6fd8aad86b98f67aa1d82652bd91afbae374f5cc Mon Sep 17 00:00:00 2001 From: SuuperW Date: Mon, 9 Mar 2015 22:30:54 +0000 Subject: [PATCH] Lots of work on the Macro Tool. --- .../BizHawk.Client.EmuHawk.csproj | 4 + .../tools/Macros/MacroInput.ButtonSelect.cs | 89 +++++++ .../tools/Macros/MacroInput.Designer.cs | 243 +++++++++++++++++- .../tools/Macros/MacroInput.cs | 220 +++++++++++++++- .../tools/Macros/MovieZone.cs | 135 ++++++++++ 5 files changed, 678 insertions(+), 13 deletions(-) create mode 100644 BizHawk.Client.EmuHawk/tools/Macros/MacroInput.ButtonSelect.cs create mode 100644 BizHawk.Client.EmuHawk/tools/Macros/MovieZone.cs diff --git a/BizHawk.Client.EmuHawk/BizHawk.Client.EmuHawk.csproj b/BizHawk.Client.EmuHawk/BizHawk.Client.EmuHawk.csproj index 7de3d48746..678fcc9649 100644 --- a/BizHawk.Client.EmuHawk/BizHawk.Client.EmuHawk.csproj +++ b/BizHawk.Client.EmuHawk/BizHawk.Client.EmuHawk.csproj @@ -810,12 +810,16 @@ Component + + Form + Form MacroInput.cs + Form diff --git a/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.ButtonSelect.cs b/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.ButtonSelect.cs new file mode 100644 index 0000000000..9915faffc7 --- /dev/null +++ b/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.ButtonSelect.cs @@ -0,0 +1,89 @@ +using System; +using System.Drawing; +using System.Windows.Forms; + +using BizHawk.Emulation.Common; +using BizHawk.Client.Common; + +namespace BizHawk.Client.EmuHawk +{ + public partial class MacroInputTool + { + private CheckBox[] _buttonBoxes; + private void SetUpButtonBoxes() + { + ControllerDefinition def = Global.Emulator.ControllerDefinition; + int count = def.BoolButtons.Count + def.FloatControls.Count; + _buttonBoxes = new CheckBox[count]; + + for (int i = 0; i < def.FloatControls.Count; i++) + { + CheckBox box = new CheckBox(); + box.Text = def.FloatControls[i]; + _buttonBoxes[i] = box; + } + for (int i = 0; i < def.BoolButtons.Count; i++) + { + CheckBox box = new CheckBox(); + box.Text = def.BoolButtons[i]; + _buttonBoxes[i + def.FloatControls.Count] = box; + } + + for (int i = 0; i < _buttonBoxes.Length; i++) + { + _buttonBoxes[i].Parent = this; + _buttonBoxes[i].AutoSize = true; + _buttonBoxes[i].CheckedChanged += ButtonBox_CheckedChanged; + } + + PositionBoxes(); + } + + private void ButtonBox_CheckedChanged(object sender, EventArgs e) + { + if (selectedZone == null) + return; + + CheckBox s = sender as CheckBox; + s.ForeColor = s.Checked ? SystemColors.ControlText : SystemColors.ControlLight; + + // Update the selected zone's key + var lg = Global.MovieSession.LogGeneratorInstance() as Bk2LogEntryGenerator; + lg.SetSource(Global.MovieSession.MovieControllerAdapter); + string key = lg.GenerateLogKey(); + key = key.Replace("LogKey:", "").Replace("#", ""); + key = key.Substring(0, key.Length - 1); + + for (int i = 0; i < _buttonBoxes.Length; i++) + key = key.Replace(_buttonBoxes[i].Text + "|", ""); + + selectedZone.InputKey = key; + } + + private void PositionBoxes() + { + int X = this.ClientSize.Width - 3; + int Y = this.ClientSize.Height - _buttonBoxes[0].Height - 3; + + for (int i = _buttonBoxes.Length - 1; i >= 0; i--) + { + X -= _buttonBoxes[i].Width; + if (X <= 3) + { + X = this.ClientSize.Width - 3 - _buttonBoxes[i].Width; + Y -= (_buttonBoxes[0].Height + 6); + } + + _buttonBoxes[i].Location = new Point(X, Y); + } + } + private void MacroInputTool_Resize(object sender, EventArgs e) + { + if (_initializing) + return; + + PositionBoxes(); + } + + } +} \ No newline at end of file diff --git a/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.Designer.cs b/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.Designer.cs index 491c766312..60e64f5499 100644 --- a/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.Designer.cs +++ b/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.Designer.cs @@ -30,8 +30,29 @@ { this.MacroMenu = new System.Windows.Forms.MenuStrip(); this.FileSubMenu = new System.Windows.Forms.ToolStripMenuItem(); + this.saveAsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.loadMacroToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.RecentToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); + this.sepToolStripMenuItem = new System.Windows.Forms.ToolStripSeparator(); this.ExitMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.NameTextbox = new System.Windows.Forms.TextBox(); + this.ReplaceBox = new System.Windows.Forms.CheckBox(); + this.label2 = new System.Windows.Forms.Label(); + this.PlaceNum = new System.Windows.Forms.NumericUpDown(); + this.EndNum = new System.Windows.Forms.NumericUpDown(); + this.PlaceZoneButton = new System.Windows.Forms.Button(); + this.StartNum = new System.Windows.Forms.NumericUpDown(); + this.SetZoneButton = new System.Windows.Forms.Button(); + this.ZonesList = new System.Windows.Forms.ListBox(); + this.label3 = new System.Windows.Forms.Label(); + this.label1 = new System.Windows.Forms.Label(); + this.CurrentButton = new System.Windows.Forms.Button(); + this.OverlayBox = new System.Windows.Forms.CheckBox(); this.MacroMenu.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.PlaceNum)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.EndNum)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.StartNum)).BeginInit(); this.SuspendLayout(); // // MacroMenu @@ -40,39 +61,237 @@ this.FileSubMenu}); this.MacroMenu.Location = new System.Drawing.Point(0, 0); this.MacroMenu.Name = "MacroMenu"; - this.MacroMenu.Size = new System.Drawing.Size(352, 24); + this.MacroMenu.Size = new System.Drawing.Size(289, 24); this.MacroMenu.TabIndex = 0; this.MacroMenu.Text = "menuStrip1"; // // FileSubMenu // this.FileSubMenu.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.saveAsToolStripMenuItem, + this.loadMacroToolStripMenuItem, + this.RecentToolStripMenuItem, + this.sepToolStripMenuItem, this.ExitMenuItem}); this.FileSubMenu.Name = "FileSubMenu"; this.FileSubMenu.Size = new System.Drawing.Size(37, 20); this.FileSubMenu.Text = "&File"; // + // saveAsToolStripMenuItem + // + this.saveAsToolStripMenuItem.Name = "saveAsToolStripMenuItem"; + this.saveAsToolStripMenuItem.Size = new System.Drawing.Size(170, 22); + this.saveAsToolStripMenuItem.Text = "Save Selected As..."; + this.saveAsToolStripMenuItem.Click += new System.EventHandler(this.saveAsToolStripMenuItem_Click); + // + // loadMacroToolStripMenuItem + // + this.loadMacroToolStripMenuItem.Name = "loadMacroToolStripMenuItem"; + this.loadMacroToolStripMenuItem.Size = new System.Drawing.Size(170, 22); + this.loadMacroToolStripMenuItem.Text = "Load Macro..."; + this.loadMacroToolStripMenuItem.Click += new System.EventHandler(this.loadMacroToolStripMenuItem_Click); + // + // RecentToolStripMenuItem + // + this.RecentToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripSeparator1}); + this.RecentToolStripMenuItem.Name = "RecentToolStripMenuItem"; + this.RecentToolStripMenuItem.Size = new System.Drawing.Size(170, 22); + this.RecentToolStripMenuItem.Text = "Recent"; + this.RecentToolStripMenuItem.DropDownOpened += new System.EventHandler(this.RecentToolStripMenuItem_DropDownOpened); + // + // toolStripSeparator1 + // + this.toolStripSeparator1.Name = "toolStripSeparator1"; + this.toolStripSeparator1.Size = new System.Drawing.Size(57, 6); + // + // sepToolStripMenuItem + // + this.sepToolStripMenuItem.Name = "sepToolStripMenuItem"; + this.sepToolStripMenuItem.Size = new System.Drawing.Size(167, 6); + // // ExitMenuItem // this.ExitMenuItem.Name = "ExitMenuItem"; this.ExitMenuItem.ShortcutKeyDisplayString = "Alt+F4"; - this.ExitMenuItem.Size = new System.Drawing.Size(152, 22); + this.ExitMenuItem.Size = new System.Drawing.Size(170, 22); this.ExitMenuItem.Text = "E&xit"; this.ExitMenuItem.Click += new System.EventHandler(this.ExitMenuItem_Click); // + // NameTextbox + // + this.NameTextbox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.NameTextbox.Location = new System.Drawing.Point(77, 181); + this.NameTextbox.Name = "NameTextbox"; + this.NameTextbox.Size = new System.Drawing.Size(99, 20); + this.NameTextbox.TabIndex = 4; + this.NameTextbox.Text = "Zone 0"; + this.NameTextbox.TextChanged += new System.EventHandler(this.NameTextbox_TextChanged); + // + // ReplaceBox + // + this.ReplaceBox.AutoSize = true; + this.ReplaceBox.Checked = true; + this.ReplaceBox.CheckState = System.Windows.Forms.CheckState.Checked; + this.ReplaceBox.Location = new System.Drawing.Point(88, 207); + this.ReplaceBox.Name = "ReplaceBox"; + this.ReplaceBox.Size = new System.Drawing.Size(66, 17); + this.ReplaceBox.TabIndex = 5; + this.ReplaceBox.Text = "Replace"; + this.ReplaceBox.UseVisualStyleBackColor = true; + this.ReplaceBox.CheckedChanged += new System.EventHandler(this.ReplaceBox_CheckedChanged); + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(12, 24); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(127, 13); + this.label2.TabIndex = 16; + this.label2.Text = "macro start macro end"; + // + // PlaceNum + // + this.PlaceNum.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.PlaceNum.Location = new System.Drawing.Point(220, 182); + this.PlaceNum.Maximum = new decimal(new int[] { + 10000000, + 0, + 0, + 0}); + this.PlaceNum.Name = "PlaceNum"; + this.PlaceNum.Size = new System.Drawing.Size(63, 20); + this.PlaceNum.TabIndex = 6; + this.PlaceNum.ValueChanged += new System.EventHandler(this.PlaceNum_ValueChanged); + // + // EndNum + // + this.EndNum.Location = new System.Drawing.Point(77, 40); + this.EndNum.Maximum = new decimal(new int[] { + 10000000, + 0, + 0, + 0}); + this.EndNum.Name = "EndNum"; + this.EndNum.Size = new System.Drawing.Size(65, 20); + this.EndNum.TabIndex = 1; + // + // PlaceZoneButton + // + this.PlaceZoneButton.Location = new System.Drawing.Point(7, 203); + this.PlaceZoneButton.Name = "PlaceZoneButton"; + this.PlaceZoneButton.Size = new System.Drawing.Size(75, 23); + this.PlaceZoneButton.TabIndex = 8; + this.PlaceZoneButton.Text = "Place Zone"; + this.PlaceZoneButton.UseVisualStyleBackColor = true; + this.PlaceZoneButton.Click += new System.EventHandler(this.PlaceZoneButton_Click); + // + // StartNum + // + this.StartNum.Location = new System.Drawing.Point(7, 40); + this.StartNum.Maximum = new decimal(new int[] { + 10000000, + 0, + 0, + 0}); + this.StartNum.Name = "StartNum"; + this.StartNum.Size = new System.Drawing.Size(65, 20); + this.StartNum.TabIndex = 0; + // + // SetZoneButton + // + this.SetZoneButton.Location = new System.Drawing.Point(148, 37); + this.SetZoneButton.Name = "SetZoneButton"; + this.SetZoneButton.Size = new System.Drawing.Size(75, 23); + this.SetZoneButton.TabIndex = 2; + this.SetZoneButton.Text = "Set Macro"; + this.SetZoneButton.UseVisualStyleBackColor = true; + this.SetZoneButton.Click += new System.EventHandler(this.SetZoneButton_Click); + // + // ZonesList + // + this.ZonesList.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.ZonesList.FormattingEnabled = true; + this.ZonesList.Location = new System.Drawing.Point(7, 66); + this.ZonesList.Name = "ZonesList"; + this.ZonesList.Size = new System.Drawing.Size(276, 108); + this.ZonesList.TabIndex = 3; + this.ZonesList.SelectedIndexChanged += new System.EventHandler(this.ZonesList_SelectedIndexChanged); + // + // label3 + // + this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(178, 184); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(39, 13); + this.label3.TabIndex = 17; + this.label3.Text = "Frame:"; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(4, 184); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(71, 13); + this.label1.TabIndex = 22; + this.label1.Text = "Macro Name:"; + // + // CurrentButton + // + this.CurrentButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.CurrentButton.Location = new System.Drawing.Point(227, 203); + this.CurrentButton.Name = "CurrentButton"; + this.CurrentButton.Size = new System.Drawing.Size(56, 23); + this.CurrentButton.TabIndex = 7; + this.CurrentButton.Text = "Current"; + this.CurrentButton.UseVisualStyleBackColor = true; + this.CurrentButton.Click += new System.EventHandler(this.CurrentButton_Click); + // + // OverlayBox + // + this.OverlayBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.OverlayBox.AutoSize = true; + this.OverlayBox.Location = new System.Drawing.Point(160, 207); + this.OverlayBox.Name = "OverlayBox"; + this.OverlayBox.Size = new System.Drawing.Size(62, 17); + this.OverlayBox.TabIndex = 24; + this.OverlayBox.Text = "Overlay"; + this.OverlayBox.UseVisualStyleBackColor = true; + this.OverlayBox.CheckedChanged += new System.EventHandler(this.OverlayBox_CheckedChanged); + // // MacroInputTool // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(352, 332); + this.ClientSize = new System.Drawing.Size(289, 333); + this.Controls.Add(this.OverlayBox); + this.Controls.Add(this.CurrentButton); + this.Controls.Add(this.label1); + this.Controls.Add(this.NameTextbox); + this.Controls.Add(this.ReplaceBox); + this.Controls.Add(this.label2); + this.Controls.Add(this.PlaceNum); + this.Controls.Add(this.EndNum); + this.Controls.Add(this.PlaceZoneButton); + this.Controls.Add(this.StartNum); + this.Controls.Add(this.SetZoneButton); + this.Controls.Add(this.ZonesList); + this.Controls.Add(this.label3); this.Controls.Add(this.MacroMenu); this.MainMenuStrip = this.MacroMenu; this.Name = "MacroInputTool"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Macro Input"; this.Load += new System.EventHandler(this.MacroInputTool_Load); + this.Resize += new System.EventHandler(this.MacroInputTool_Resize); this.MacroMenu.ResumeLayout(false); this.MacroMenu.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.PlaceNum)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.EndNum)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.StartNum)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -83,6 +302,24 @@ private System.Windows.Forms.MenuStrip MacroMenu; private System.Windows.Forms.ToolStripMenuItem FileSubMenu; private System.Windows.Forms.ToolStripMenuItem ExitMenuItem; + private System.Windows.Forms.TextBox NameTextbox; + private System.Windows.Forms.CheckBox ReplaceBox; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.NumericUpDown PlaceNum; + private System.Windows.Forms.NumericUpDown EndNum; + private System.Windows.Forms.Button PlaceZoneButton; + private System.Windows.Forms.NumericUpDown StartNum; + private System.Windows.Forms.Button SetZoneButton; + private System.Windows.Forms.ListBox ZonesList; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.ToolStripMenuItem saveAsToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem loadMacroToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem RecentToolStripMenuItem; + private System.Windows.Forms.ToolStripSeparator sepToolStripMenuItem; + private System.Windows.Forms.Button CurrentButton; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; + private System.Windows.Forms.CheckBox OverlayBox; } } \ No newline at end of file diff --git a/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.cs b/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.cs index 7b4453d867..c8df550e93 100644 --- a/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.cs +++ b/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.cs @@ -1,5 +1,4 @@ -using BizHawk.Emulation.Common; -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -7,6 +6,12 @@ using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; +using System.IO; + +using BizHawk.Emulation.Common; +using BizHawk.Client.Common; +using BizHawk.Client.EmuHawk.WinFormExtensions; +using BizHawk.Client.EmuHawk.ToolExtensions; namespace BizHawk.Client.EmuHawk { @@ -14,46 +19,241 @@ namespace BizHawk.Client.EmuHawk { [RequiredService] private IEmulator Emulator { get; set; } + // Zones + List zones = new List(); + private TasMovie CurrentTasMovie + { + get { return Global.MovieSession.Movie as TasMovie; } + } + [ConfigPersist] + private MacroSettings Settings { get; set; } + + class MacroSettings + { + public MacroSettings() + { + RecentMacro = new RecentFiles(8); + } + + public RecentFiles RecentMacro { get; set; } + } + + private bool _initializing = false; public MacroInputTool() { + _initializing = true; InitializeComponent(); } - private void MacroInputTool_Load(object sender, EventArgs e) { + // Movie recording must be active + if (!Global.MovieSession.Movie.IsActive) + { + MessageBox.Show("In order to use this tool you must be recording a movie."); + this.Close(); + this.DialogResult = System.Windows.Forms.DialogResult.Cancel; + return; + } + ReplaceBox.Enabled = CurrentTasMovie is TasMovie; + PlaceNum.Enabled = CurrentTasMovie is TasMovie; + + Settings = new MacroSettings(); + + MovieZone main = new MovieZone(CurrentTasMovie, 0, CurrentTasMovie.InputLogLength); + main.Name = "Entire Movie"; + + zones.Add(main); + ZonesList.Items.Add(main.Name + " - length: " + main.Length); + ZonesList.Items[0] += " [Zones don't change!]"; + + SetUpButtonBoxes(); + + _initializing = false; } public void Restart() { + if (_initializing) + return; + zones.Clear(); + ZonesList.Items.Clear(); + + MovieZone main = new MovieZone(CurrentTasMovie, 0, CurrentTasMovie.InputLogLength); + main.Name = "Entire Movie"; + + zones.Add(main); + ZonesList.Items.Add(main.Name + " - length: " + main.Length); + ZonesList.Items[0] += " [Zones don't change!]"; } + // These do absolutely nothing. public void UpdateValues() { - - } + } public void FastUpdate() { } + public bool UpdateBefore + { + get { return true; } + } public bool AskSaveChanges() { return true; } - public bool UpdateBefore - { - get { return true; } // TODO: think about this - } - private void ExitMenuItem_Click(object sender, EventArgs e) { Close(); } + + private void SetZoneButton_Click(object sender, EventArgs e) + { + if (StartNum.Value >= CurrentTasMovie.InputLogLength || EndNum.Value > CurrentTasMovie.InputLogLength) + { + MessageBox.Show("Start and end frames must be inside the movie."); + return; + } + + MovieZone newZone = new MovieZone(CurrentTasMovie, (int)StartNum.Value, (int)(EndNum.Value - StartNum.Value)); + newZone.Name = "Zone " + zones.Count; + zones.Add(newZone); + ZonesList.Items.Add(newZone.Name + " - length: " + newZone.Length); + } + + private MovieZone selectedZone + { + get + { + if (ZonesList.SelectedIndex == -1) + return null; + return zones[ZonesList.SelectedIndex]; + } + } + private bool _selecting = false; + private void ZonesList_SelectedIndexChanged(object sender, EventArgs e) + { + if (selectedZone == null) + return; + + _selecting = true; + PlaceNum.Value = selectedZone.Start; + ReplaceBox.Checked = selectedZone.Replace; + NameTextbox.Text = selectedZone.Name; + OverlayBox.Checked = selectedZone.Overlay; + _selecting = false; + } + + private void NameTextbox_TextChanged(object sender, EventArgs e) + { + if (selectedZone == null || _selecting) + return; + + selectedZone.Name = NameTextbox.Text; + ZonesList.Items[ZonesList.SelectedIndex] = selectedZone.Name + " - length: " + selectedZone.Length; + } + private void PlaceNum_ValueChanged(object sender, EventArgs e) + { + if (selectedZone == null || _selecting) + return; + + selectedZone.Start = (int)PlaceNum.Value; + } + private void ReplaceBox_CheckedChanged(object sender, EventArgs e) + { + if (selectedZone == null || _selecting) + return; + + selectedZone.Replace = ReplaceBox.Checked; + } + private void OverlayBox_CheckedChanged(object sender, EventArgs e) + { + if (selectedZone == null || _selecting) + return; + + selectedZone.Overlay = OverlayBox.Checked; + } + private void CurrentButton_Click(object sender, EventArgs e) + { + PlaceNum.Value = Global.Emulator.Frame; + } + + private void PlaceZoneButton_Click(object sender, EventArgs e) + { + if (selectedZone == null) + return; + + if (!(CurrentTasMovie is TasMovie)) + { + selectedZone.Start = Global.Emulator.Frame; + } + selectedZone.PlaceZone(CurrentTasMovie); + } + + #region "Menu Items" + private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) + { + if (selectedZone == null) + { + MessageBox.Show("Please select a zone first."); + return; + } + + SaveFileDialog dialog = new SaveFileDialog(); + dialog.InitialDirectory = SuggestedFolder(); + dialog.Filter = "Movie Macros (*.bk2m)|*.bk2m|All Files|*.*"; + + DialogResult result = dialog.ShowHawkDialog(); + if (result != DialogResult.OK) + return; + + selectedZone.Save(dialog.FileName); + Settings.RecentMacro.Add(dialog.FileName); + } + + private void loadMacroToolStripMenuItem_Click(object sender, EventArgs e) + { + OpenFileDialog dialog = new OpenFileDialog(); + dialog.InitialDirectory = SuggestedFolder(); + dialog.Filter = "Movie Macros (*.bk2m)|*.bk2m|All Files|*.*"; + + DialogResult result = dialog.ShowHawkDialog(); + if (result != DialogResult.OK) + return; + + MovieZone loadZone = new MovieZone(dialog.FileName); + zones.Add(loadZone); + ZonesList.Items.Add(loadZone.Name + " - length: " + loadZone.Length); + + Settings.RecentMacro.Add(dialog.FileName); + } + + private void RecentToolStripMenuItem_DropDownOpened(object sender, EventArgs e) + { + RecentToolStripMenuItem.DropDownItems.Clear(); + RecentToolStripMenuItem.DropDownItems.AddRange( + Settings.RecentMacro.RecentMenu(DummyLoadProject, true)); + } + private void DummyLoadProject(string path) + { + MovieZone loadZone = new MovieZone(path); + zones.Add(loadZone); + ZonesList.Items.Add(loadZone.Name + " - length: " + loadZone.Length); + } + + private string SuggestedFolder() + { + return PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null) + + "\\Macros"; + } + #endregion + } } diff --git a/BizHawk.Client.EmuHawk/tools/Macros/MovieZone.cs b/BizHawk.Client.EmuHawk/tools/Macros/MovieZone.cs new file mode 100644 index 0000000000..3fd821912a --- /dev/null +++ b/BizHawk.Client.EmuHawk/tools/Macros/MovieZone.cs @@ -0,0 +1,135 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; + +using BizHawk.Client.Common; +using BizHawk.Emulation.Common; + +namespace BizHawk.Client.EmuHawk +{ + class MovieZone + { + public string Name; + + public int Start; + public int Length; + public string InputKey; + private string[] _log; + + public bool Replace = true; + public bool Overlay = false; + public float defaultFloat = 0f; + + private Bk2ControllerAdapter controller; + private Bk2ControllerAdapter targetController; + + public MovieZone() + { + } + public MovieZone(TasMovie movie, int start, int length, string key = "") + { + var lg = Global.MovieSession.LogGeneratorInstance() as Bk2LogEntryGenerator; + lg.SetSource(Global.MovieSession.MovieControllerAdapter); + targetController = new Bk2ControllerAdapter(); + targetController.Type = Global.Emulator.ControllerDefinition; + + if (key == "") + key = lg.GenerateLogKey(); + key = key.Replace("LogKey:", "").Replace("#", ""); + key = key.Substring(0, key.Length - 1); + + InputKey = key; + Length = length; + _log = new string[length]; + + // Get a IController that only contains buttons in key. + string[] keys = key.Split('|'); + ControllerDefinition d = new ControllerDefinition(); + for (int i = 0; i < keys.Length; i++) + { + if (Global.Emulator.ControllerDefinition.BoolButtons.Contains(keys[i])) + d.BoolButtons.Add(keys[i]); + else + d.FloatControls.Add(keys[i]); + } + + controller = new Bk2ControllerAdapter() { Type = d }; + var logGenerator = new Bk2LogEntryGenerator(""); + logGenerator.SetSource(controller); + for (int i = 0; i < length; i++) + { + // Set controller with only the buttons in it's Type. + _log[i] = logGenerator.GenerateLogEntry(); + } + } + + public void PlaceZone(TasMovie movie) + { + // TODO: This should probably do something with undo history batches/naming. + if (!Replace) + movie.InsertEmptyFrame(Start, Length); + + if (Overlay) + { + for (int i = 0; i < Length; i++) + { + // Overlay the frame. Float controls should use the defaultFloat value. + } + } + else + { + for (int i = 0; i < Length; i++) + { + // Copy over the frame. (Only using the buttons in controller.Type) + } + } + + if (movie is TasMovie) // Assume TAStudio is open? + { + if (Global.Emulator.Frame < Start) + { + // TODO: Go to start of macro? Ask TAStudio to do that? + + // TasMovie.InvalidateAfter(Start) [this is private] + // Load last state, Emulate to Start + + // Or do this, if we can accept that TAStudio has to be open. + (GlobalWin.Tools.Get() as TAStudio).GoToFrame(Start); + + GlobalWin.Tools.UpdateBefore(); + GlobalWin.Tools.UpdateAfter(); + } + else if (GlobalWin.Tools.IsLoaded()) + { + GlobalWin.Tools.Get().UpdateValues(); + } + } + } + + public void Save(string fileName) + { + // Save the controller definition/LogKey + // Save the controller name and player count. (Only for the user.) + // Save whether or not the macro should use overlay input, and/or replace + + File.WriteAllText(fileName, Global.Emulator.ControllerDefinition.Name); + File.AppendAllText(fileName, "\n" + Global.Emulator.ControllerDefinition.PlayerCount); + File.AppendAllLines(fileName, _log); + } + public MovieZone(string fileName) + { + // If the LogKey contains buttons/controls not accepted by the emulator, tell the user and display the macro's controller name and player count. + + if (!File.Exists(fileName)) + return; + + _log = File.ReadAllLines(fileName); + Length = _log.Length; + Start = 0; + + Name = Path.GetFileNameWithoutExtension(fileName); + } + } +}