From 2535207a403b6af282d438f842209a5e6d0f021a Mon Sep 17 00:00:00 2001 From: feos Date: Sat, 12 Dec 2015 23:59:55 +0300 Subject: [PATCH] tastudio: actually implement seeking progressbar. --- BizHawk.Client.EmuHawk/MainForm.cs | 13 ++-- .../tools/TAStudio/TAStudio.ListView.cs | 26 ++++++- .../tools/TAStudio/TAStudio.Navigation.cs | 4 +- .../tools/TAStudio/TAStudio.cs | 67 +++++++++++++------ 4 files changed, 78 insertions(+), 32 deletions(-) diff --git a/BizHawk.Client.EmuHawk/MainForm.cs b/BizHawk.Client.EmuHawk/MainForm.cs index 943128d917..037e451dca 100644 --- a/BizHawk.Client.EmuHawk/MainForm.cs +++ b/BizHawk.Client.EmuHawk/MainForm.cs @@ -2839,16 +2839,13 @@ namespace BizHawk.Client.EmuHawk UpdateToolsAfter(); } - if (IsSeeking) + if (IsSeeking && Global.Emulator.Frame == PauseOnFrame.Value) { - if (Global.Emulator.Frame == PauseOnFrame.Value) + PauseEmulator(); + PauseOnFrame = null; + if (GlobalWin.Tools.IsLoaded()) { - PauseEmulator(); - PauseOnFrame = null; - } - else if (GlobalWin.Tools.IsLoaded()) - { - GlobalWin.Tools.TAStudio.ReportSeekingProgress(); + GlobalWin.Tools.TAStudio.StopSeeking(); } } } diff --git a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs index dae45074f8..8f9e89dd16 100644 --- a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs +++ b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs @@ -46,6 +46,7 @@ namespace BizHawk.Client.EmuHawk private int? _autoRestoreFrame; // The frame auto-restore will restore to, if set private bool? _autoRestorePaused = null; private int? _seekStartFrame = null; + private void JumpToGreenzone() { if (Global.Emulator.Frame > CurrentTasMovie.LastValidFrame) @@ -58,11 +59,32 @@ namespace BizHawk.Client.EmuHawk } GoToLastEmulatedFrameIfNecessary(CurrentTasMovie.LastValidFrame); - GlobalWin.MainForm.PauseOnFrame = _autoRestoreFrame; - GlobalWin.MainForm.PauseEmulator(); + StartSeeking(_autoRestoreFrame, true); } } + private void StartSeeking(int? frame, bool pause = false) + { + if (!frame.HasValue) + return; + + _seekStartFrame = Emulator.Frame; + GlobalWin.MainForm.PauseOnFrame = frame.Value; + + if (pause) + GlobalWin.MainForm.PauseEmulator(); + else + GlobalWin.MainForm.UnpauseEmulator(); + + if (!_seekBackgroundWorker.IsBusy) + _seekBackgroundWorker.RunWorkerAsync(); + } + + public void StopSeeking() + { + _seekBackgroundWorker.CancelAsync(); + } + // public static Color CurrentFrame_FrameCol = Color.FromArgb(0xCFEDFC); Why? public static Color CurrentFrame_InputLog = Color.FromArgb(0xB5E7F7); diff --git a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.Navigation.cs b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.Navigation.cs index 1a51108ae8..43fcf6d34f 100644 --- a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.Navigation.cs +++ b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.Navigation.cs @@ -58,9 +58,7 @@ namespace BizHawk.Client.EmuHawk if (lastState > Emulator.Frame) LoadState(CurrentTasMovie.TasStateManager[lastState]); // STATE ACCESS - _seekStartFrame = Emulator.Frame; - GlobalWin.MainForm.UnpauseEmulator(); - GlobalWin.MainForm.PauseOnFrame = frame; + StartSeeking(frame); } } diff --git a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs index e4d5ebbed5..c42d350a1c 100644 --- a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs +++ b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs @@ -26,6 +26,7 @@ namespace BizHawk.Client.EmuHawk private readonly List _tasClipboard = new List(); private BackgroundWorker _saveBackgroundWorker; + private BackgroundWorker _seekBackgroundWorker; private MovieEndAction _originalEndAction; // The movie end behavior selected by the user (that is overridden by TAStudio) private Dictionary GenerateColumnNames() @@ -127,6 +128,7 @@ namespace BizHawk.Client.EmuHawk this.SavingProgressBar.Visible = false; InitializeSaveWorker(); + InitializeSeekWorker(); WantsToControlStopMovie = true; TasPlaybackBox.Tastudio = this; @@ -178,6 +180,50 @@ namespace BizHawk.Client.EmuHawk CurrentTasMovie.NewBGWorker(_saveBackgroundWorker); } + private void InitializeSeekWorker() + { + if (_seekBackgroundWorker != null) + { + _seekBackgroundWorker.Dispose(); + _seekBackgroundWorker = null; // Idk if this line is even useful. + } + + _seekBackgroundWorker = new BackgroundWorker(); + _seekBackgroundWorker.WorkerReportsProgress = true; + _seekBackgroundWorker.WorkerSupportsCancellation = true; + + _seekBackgroundWorker.DoWork += (s, e) => + { + this.Invoke(() => this.MessageStatusLabel.Text = "Seeking..."); + this.Invoke(() => this.SavingProgressBar.Visible = true); + for ( ; ; ) + { + if (_seekBackgroundWorker.CancellationPending) + { + e.Cancel = true; + break; + } + double progress = (double)100d / + (GlobalWin.MainForm.PauseOnFrame.Value - _seekStartFrame.Value) * + (Global.Emulator.Frame - _seekStartFrame.Value); + _seekBackgroundWorker.ReportProgress((int)progress); + System.Threading.Thread.Sleep(1); + } + }; + + _seekBackgroundWorker.ProgressChanged += (s, e) => + { + SavingProgressBar.Value = e.ProgressPercentage; + }; + + _seekBackgroundWorker.RunWorkerCompleted += (s, e) => + { + this.Invoke(() => this.SavingProgressBar.Visible = false); + this.Invoke(() => this.MessageStatusLabel.Text = ""); + InitializeSeekWorker(); // Required, or it will error when trying to report progress again. + }; + } + private bool _initialized = false; private void Tastudio_Load(object sender, EventArgs e) { @@ -641,9 +687,7 @@ namespace BizHawk.Client.EmuHawk { if (_autoRestoreFrame > Emulator.Frame) // Don't unpause if we are already on the desired frame, else runaway seek { - _seekStartFrame = Emulator.Frame; - GlobalWin.MainForm.PauseOnFrame = _autoRestoreFrame; - GlobalWin.MainForm.UnpauseEmulator(); + StartSeeking(_autoRestoreFrame); } } else @@ -653,8 +697,6 @@ namespace BizHawk.Client.EmuHawk _autoRestorePaused = null; GlobalWin.MainForm.PauseOnFrame = null; // Cancel seek to autorestore point } - - _autoRestoreFrame = null; } @@ -675,9 +717,7 @@ namespace BizHawk.Client.EmuHawk { if (GlobalWin.MainForm.EmulatorPaused || GlobalWin.MainForm.IsSeeking) // make seek frame keep up with emulation on fast scrolls { - _seekStartFrame = Emulator.Frame; - GlobalWin.MainForm.PauseOnFrame = frame; - GlobalWin.MainForm.UnpauseEmulator(); + StartSeeking(frame); } } } @@ -707,17 +747,6 @@ namespace BizHawk.Client.EmuHawk BookMarkControl.RemoveBranchExtrenal(); } - public void ReportSeekingProgress() - { - if (_seekStartFrame.HasValue) - { - CurrentTasMovie.ReportProgress((double)100d / - (GlobalWin.MainForm.PauseOnFrame.Value - _seekStartFrame.Value) * - (Global.Emulator.Frame - _seekStartFrame.Value)); - RefreshDialog(); - } - } - private void UpdateOtherTools() // a hack probably, surely there is a better way to do this { _hackyDontUpdate = true;