Browse Source

Make sixel output stop on dispose/close Images scenario

tznind 10 months ago
parent
commit
3566ac2c93
1 changed files with 51 additions and 28 deletions
  1. 51 28
      UICatalog/Scenarios/Images.cs

+ 51 - 28
UICatalog/Scenarios/Images.cs

@@ -52,6 +52,12 @@ public class Images : Scenario
     /// </summary>
     /// </summary>
     private View _sixelView;
     private View _sixelView;
 
 
+    private DoomFire _fire;
+    private SixelEncoder _encoder;
+    private int _fireFrameCounter;
+    private bool _isDisposed;
+    private SixelToRender _fireSixel;
+
     public override void Main ()
     public override void Main ()
     {
     {
         Application.Init ();
         Application.Init ();
@@ -143,42 +149,56 @@ public class Images : Scenario
 
 
     private void BtnStartFireOnAccept (object sender, HandledEventArgs e)
     private void BtnStartFireOnAccept (object sender, HandledEventArgs e)
     {
     {
-        var fire = new DoomFire (_win.Frame.Width * _pxX.Value, _win.Frame.Height * _pxY.Value);
-        var encoder = new SixelEncoder ();
-        encoder.Quantizer.PaletteBuildingAlgorithm = new ConstPalette (fire.Palette);
+        if (_fire != null)
+        {
+            return;
+        }
 
 
-        var counter = 0;
+        _fire = new DoomFire (_win.Frame.Width * _pxX.Value, _win.Frame.Height * _pxY.Value);
+        _encoder = new SixelEncoder ();
+        _encoder.Quantizer.PaletteBuildingAlgorithm = new ConstPalette (_fire.Palette);
 
 
-        Application.AddTimeout (
-                                TimeSpan.FromMilliseconds (30),
-                                () =>
-                                {
-                                    fire.AdvanceFrame ();
-                                    counter++;
+        _fireFrameCounter = 0;
+
+        Application.AddTimeout (TimeSpan.FromMilliseconds (30), AdvanceFireTimerCallback);
+    }
+
+    private void StopFire ()
+    {
+
+    }
 
 
-                                    // Control frame rate by adjusting this
-                                    // Lower number means more FPS
-                                    if (counter % 2 != 0)
-                                    {
-                                        return true;
-                                    }
+    private bool AdvanceFireTimerCallback ()
+    {
+        _fire.AdvanceFrame ();
+        _fireFrameCounter++;
+
+        // Control frame rate by adjusting this
+        // Lower number means more FPS
+        if (_fireFrameCounter % 2 != 0 || _isDisposed)
+        {
+            return !_isDisposed;
+        }
+
+        Color [,] bmp = _fire.GetFirePixels ();
 
 
-                                    Color [,] bmp = fire.GetFirePixels ();
+        // TODO: Static way of doing this, suboptimal
+        if (_fireSixel != null)
+        {
+            Application.Sixel.Remove (_fireSixel);
+        }
 
 
-                                    // TODO: Static way of doing this, suboptimal
-                                    Application.Sixel.Clear ();
+        _fireSixel = new ()
+        {
+            SixelData = _encoder.EncodeSixel (bmp),
+            ScreenPosition = new (0, 0)
+        };
 
 
-                                    Application.Sixel.Add (
-                                                           new()
-                                                           {
-                                                               SixelData = encoder.EncodeSixel (bmp),
-                                                               ScreenPosition = new (0, 0)
-                                                           });
+        Application.Sixel.Add (_fireSixel);
 
 
-                                    _win.SetNeedsDisplay ();
+        _win.SetNeedsDisplay ();
 
 
-                                    return true;
-                                });
+        return !_isDisposed;
     }
     }
 
 
     /// <inheritdoc/>
     /// <inheritdoc/>
@@ -188,6 +208,9 @@ public class Images : Scenario
         _imageView.Dispose ();
         _imageView.Dispose ();
         _sixelNotSupported.Dispose ();
         _sixelNotSupported.Dispose ();
         _sixelSupported.Dispose ();
         _sixelSupported.Dispose ();
+        _isDisposed = true;
+
+        Application.Sixel.Clear ();
     }
     }
 
 
     private void OpenImage (object sender, HandledEventArgs e)
     private void OpenImage (object sender, HandledEventArgs e)