From 09ccf0dbe89105a23e0150e47e76d98f6a42cd7a Mon Sep 17 00:00:00 2001 From: nattthebear Date: Mon, 18 Oct 2021 14:03:25 -0400 Subject: [PATCH] Unthread tastudio seek progress bar (#2774) Co-authored-by: feos --- .../tools/TAStudio/TAStudio.Designer.cs | 12 ++-- .../tools/TAStudio/TAStudio.IToolForm.cs | 37 +++++++++++ .../tools/TAStudio/TAStudio.ListView.cs | 6 +- .../tools/TAStudio/TAStudio.cs | 61 +------------------ 4 files changed, 47 insertions(+), 69 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.Designer.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.Designer.cs index 61fdd534bf..3a737092c3 100644 --- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.Designer.cs +++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.Designer.cs @@ -154,7 +154,7 @@ namespace BizHawk.Client.EmuHawk this.TasView = new BizHawk.Client.EmuHawk.InputRoll(); this.TasStatusStrip = new StatusStripEx(); this.MessageStatusLabel = new System.Windows.Forms.ToolStripStatusLabel(); - this.SavingProgressBar = new System.Windows.Forms.ToolStripProgressBar(); + this.ProgressBar = new System.Windows.Forms.ToolStripProgressBar(); this.toolStripStatusLabel2 = new System.Windows.Forms.ToolStripStatusLabel(); this.SplicerStatusLabel = new System.Windows.Forms.ToolStripStatusLabel(); this.TasPlaybackBox = new BizHawk.Client.EmuHawk.PlaybackBox(); @@ -925,7 +925,7 @@ namespace BizHawk.Client.EmuHawk // this.TasStatusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.MessageStatusLabel, - this.SavingProgressBar, + this.ProgressBar, this.toolStripStatusLabel2, this.SplicerStatusLabel}); this.TasStatusStrip.Location = new System.Drawing.Point(0, 554); @@ -938,10 +938,10 @@ namespace BizHawk.Client.EmuHawk this.MessageStatusLabel.Size = new System.Drawing.Size(95, 17); this.MessageStatusLabel.Text = "TAStudio engaged"; // - // SavingProgressBar + // ProgressBar // - this.SavingProgressBar.Name = "SavingProgressBar"; - this.SavingProgressBar.Size = new System.Drawing.Size(100, 16); + this.ProgressBar.Name = "ProgressBar"; + this.ProgressBar.Size = new System.Drawing.Size(100, 16); // // toolStripStatusLabel2 // @@ -1310,7 +1310,7 @@ namespace BizHawk.Client.EmuHawk private BizHawk.WinForms.Controls.ToolStripSeparatorEx StartFromNowSeparator; private BizHawk.WinForms.Controls.ToolStripMenuItemEx StartNewProjectFromNowMenuItem; private BizHawk.WinForms.Controls.ToolStripMenuItemEx RotateMenuItem; - private System.Windows.Forms.ToolStripProgressBar SavingProgressBar; + private System.Windows.Forms.ToolStripProgressBar ProgressBar; private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel2; private BizHawk.WinForms.Controls.ToolStripMenuItemEx HideLagFramesSubMenu; private BizHawk.WinForms.Controls.ToolStripMenuItemEx HideLagFrames3; diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IToolForm.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IToolForm.cs index a748d42c90..818a4e697f 100644 --- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IToolForm.cs +++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IToolForm.cs @@ -24,6 +24,37 @@ namespace BizHawk.Client.EmuHawk private int _lastRefresh; + private void UpdateProgressBar() + { + if (MainForm.PauseOnFrame.HasValue) + { + int diff = Emulator.Frame - _seekStartFrame.Value; + int unit = MainForm.PauseOnFrame.Value - _seekStartFrame.Value; + double progress = 0; + + if (diff != 0 && unit != 0) + { + progress = (double)100d / unit * diff; + } + + if (progress < 0) + { + progress = 0; + } + else if (progress > 100) + { + progress = 100; + } + + ProgressBar.Value = (int)progress; + } + else + { + ProgressBar.Visible = false; + MessageStatusLabel.Text = ""; + } + } + protected override void GeneralUpdate() { RefreshDialog(); @@ -61,6 +92,12 @@ namespace BizHawk.Client.EmuHawk } RefreshDialog(refreshNeeded, refreshBranches: false); + UpdateProgressBar(); + } + + protected override void FastUpdateAfter() + { + UpdateProgressBar(); } public override void Restart() diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs index bf058bfb01..bdee92c435 100644 --- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs +++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs @@ -98,15 +98,15 @@ namespace BizHawk.Client.EmuHawk TastudioPlayMode(); // suspend rec mode until seek ends, to allow mouse editing MainForm.UnpauseEmulator(); - if (!_seekBackgroundWorker.IsBusy && diff > TasView.VisibleRows) + if (diff > TasView.VisibleRows) { - _seekBackgroundWorker.RunWorkerAsync(); + MessageStatusLabel.Text = "Seeking..."; + ProgressBar.Visible = true; } } public void StopSeeking(bool skipRecModeCheck = false) { - _seekBackgroundWorker.CancelAsync(); if (WasRecording && !skipRecModeCheck) { TastudioRecordMode(); diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs index e308bcfa10..f6350b1685 100644 --- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs +++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs @@ -30,7 +30,6 @@ namespace BizHawk.Client.EmuHawk private readonly List _tasClipboard = new List(); private const string CursorColumnName = "CursorColumn"; private const string FrameColumnName = "FrameColumn"; - private BackgroundWorker _seekBackgroundWorker; private MovieEndAction _originalEndAction; // The movie end behavior selected by the user (that is overridden by TAStudio) private UndoHistoryForm _undoForm; private Timer _autosaveTimer; @@ -121,13 +120,11 @@ namespace BizHawk.Client.EmuHawk ForumThreadMenuItem.Image = Resources.TAStudio; Icon = Resources.TAStudioIcon; - InitializeSeekWorker(); - _defaultMainSplitDistance = MainVertialSplit.SplitterDistance; _defaultBranchMarkerSplitDistance = BranchesMarkersSplit.SplitterDistance; // TODO: show this at all times or hide it when saving is done? - SavingProgressBar.Visible = false; + ProgressBar.Visible = false; WantsToControlStopMovie = true; WantsToControlRestartMovie = true; @@ -323,62 +320,6 @@ namespace BizHawk.Client.EmuHawk } } - private void InitializeSeekWorker() - { - _seekBackgroundWorker?.Dispose(); - _seekBackgroundWorker = new BackgroundWorker - { - WorkerReportsProgress = true, - WorkerSupportsCancellation = true - }; - - _seekBackgroundWorker.DoWork += (s, e) => - { - this.Invoke(() => MessageStatusLabel.Text = "Seeking..."); - this.Invoke(() => SavingProgressBar.Visible = true); - for (;;) - { - if (_seekBackgroundWorker.CancellationPending || !IsHandleCreated || !MainForm.PauseOnFrame.HasValue) - { - e.Cancel = true; - break; - } - - int diff = Emulator.Frame - _seekStartFrame.Value; - int unit = MainForm.PauseOnFrame.Value - _seekStartFrame.Value; - double progress = 0; - - if (diff != 0 && unit != 0) - { - progress = (double)100d / unit * diff; - } - - if (progress < 0) - { - progress = 0; - } - else if (progress > 100) - { - progress = 100; - } - - _seekBackgroundWorker.ReportProgress((int)progress); - System.Threading.Thread.Sleep(1); - } - }; - - _seekBackgroundWorker.ProgressChanged += (s, e) => - { - this.Invoke(() => SavingProgressBar.Value = e.ProgressPercentage); - }; - - _seekBackgroundWorker.RunWorkerCompleted += (s, e) => - { - this.Invoke(() => SavingProgressBar.Visible = false); - this.Invoke(() => MessageStatusLabel.Text = ""); - }; - } - private void SetTasMovieCallbacks(ITasMovie movie) { movie.ClientSettingsForSave = () => TasView.UserSettingsSerialized();