From 42a7404d4fd6db127d43ccecc0465b36205e8cb2 Mon Sep 17 00:00:00 2001 From: Sour Date: Sun, 29 Dec 2019 10:39:14 -0500 Subject: [PATCH] NSF: Fixed UI crash when switching between different NSF files --- GUI.NET/Controls/ctrlNsfPlayer.Designer.cs | 8 ---- GUI.NET/Controls/ctrlNsfPlayer.cs | 46 ++++++++++++++++++---- GUI.NET/Controls/ctrlNsfPlayer.resx | 3 -- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/GUI.NET/Controls/ctrlNsfPlayer.Designer.cs b/GUI.NET/Controls/ctrlNsfPlayer.Designer.cs index 1cedf7e4..7499b153 100644 --- a/GUI.NET/Controls/ctrlNsfPlayer.Designer.cs +++ b/GUI.NET/Controls/ctrlNsfPlayer.Designer.cs @@ -72,7 +72,6 @@ this.tlpRepeatShuffle = new System.Windows.Forms.TableLayoutPanel(); this.picRepeat = new System.Windows.Forms.PictureBox(); this.picShuffle = new System.Windows.Forms.PictureBox(); - this.tmrUpdate = new System.Windows.Forms.Timer(this.components); this.tlpMain.SuspendLayout(); this.tlpNsfInfo.SuspendLayout(); this.tableLayoutPanel3.SuspendLayout(); @@ -671,12 +670,6 @@ this.picShuffle.TabStop = false; this.picShuffle.Click += new System.EventHandler(this.picShuffle_Click); // - // tmrUpdate - // - this.tmrUpdate.Enabled = false; - this.tmrUpdate.Interval = 250; - this.tmrUpdate.Tick += new System.EventHandler(this.tmrUpdate_Tick); - // // ctrlNsfPlayer // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -759,7 +752,6 @@ private System.Windows.Forms.PictureBox picShuffle; private System.Windows.Forms.PictureBox picRepeat; private System.Windows.Forms.Panel pnlBackground; - private System.Windows.Forms.Timer tmrUpdate; private System.Windows.Forms.Panel panel4; private System.Windows.Forms.Label lblRewinding; private System.Windows.Forms.Label lblRewindIcon; diff --git a/GUI.NET/Controls/ctrlNsfPlayer.cs b/GUI.NET/Controls/ctrlNsfPlayer.cs index 0899a5f3..1c6f7432 100644 --- a/GUI.NET/Controls/ctrlNsfPlayer.cs +++ b/GUI.NET/Controls/ctrlNsfPlayer.cs @@ -14,12 +14,14 @@ namespace Mesen.GUI.Controls { public partial class ctrlNsfPlayer : BaseControl { + private InteropEmu.NotificationListener _notifListener; private List _trackList = new List(); private bool _fastForwarding = false; private UInt32 _originalSpeed = 100; private bool _disableShortcutKeys = false; private float _xFactor = 1; private float _yFactor = 1; + private bool _isNsf = false; public ctrlNsfPlayer() { @@ -27,12 +29,40 @@ namespace Mesen.GUI.Controls ThemeHelper.ExcludeFromTheme(this); } + private void notifListener_OnNotification(InteropEmu.NotificationEventArgs e) + { + switch(e.NotificationType) { + case InteropEmu.ConsoleNotificationType.GameLoaded: + _isNsf = InteropEmu.IsNsf(); + break; + + case InteropEmu.ConsoleNotificationType.PpuFrameDone: + if(_isNsf) { + UInt32 elapsedFrames = InteropEmu.NsfGetFrameCount(); + if((elapsedFrames % 15) == 0) { + this.BeginInvoke((Action)(() => this.UpdateTimeDisplay())); + } + } + break; + + } + } + + protected override void OnHandleDestroyed(EventArgs e) + { + base.OnHandleDestroyed(e); + _notifListener?.Dispose(); + } + protected override void OnLoad(EventArgs e) { base.OnLoad(e); if(!IsDesignMode) { - this.tmrUpdate.Enabled = true; + _isNsf = InteropEmu.IsNsf(); + _notifListener = new InteropEmu.NotificationListener(InteropEmu.ConsoleId.Master); + _notifListener.OnNotification += notifListener_OnNotification; + this.btnNext.KeyUp += Child_KeyUp; this.btnPause.KeyUp += Child_KeyUp; this.btnPrevious.KeyUp += Child_KeyUp; @@ -122,7 +152,7 @@ namespace Mesen.GUI.Controls private void UpdateTimeDisplay() { - if(!InteropEmu.IsNsf()) { + if(!_isNsf) { return; } @@ -182,6 +212,10 @@ namespace Mesen.GUI.Controls private void UpdateTrackDisplay() { + if(!_isNsf) { + return; + } + NsfHeader header = InteropEmu.NsfGetHeader(); int currentTrack = InteropEmu.NsfGetCurrentTrack(); @@ -212,7 +246,8 @@ namespace Mesen.GUI.Controls if(this.InvokeRequired) { this.BeginInvoke((MethodInvoker)(() => UpdateText())); } else { - if(InteropEmu.IsNsf()) { + _isNsf = InteropEmu.IsNsf(); + if(_isNsf) { UpdateTrackDisplay(); UpdateTimeDisplay(); @@ -439,11 +474,6 @@ namespace Mesen.GUI.Controls this.tlpNsfInfo.MouseMove -= value; } } - - private void tmrUpdate_Tick(object sender, EventArgs e) - { - UpdateTimeDisplay(); - } } public class ComboboxItem diff --git a/GUI.NET/Controls/ctrlNsfPlayer.resx b/GUI.NET/Controls/ctrlNsfPlayer.resx index 56288894..e794cb43 100644 --- a/GUI.NET/Controls/ctrlNsfPlayer.resx +++ b/GUI.NET/Controls/ctrlNsfPlayer.resx @@ -123,7 +123,4 @@ 154, 17 - - 244, 17 - \ No newline at end of file