Advertisement
jarppaaja

ScreenCapture

Apr 4th, 2019
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. // ScreenCapture.cs "$Revision: 1583 $" "$Date: 2019-04-04 20:04:51 +0300 (to, 04 huhti 2019) $"
  2. using System;
  3. using System.Globalization;
  4. using Turbo.Plugins.Default;
  5.  
  6. namespace Turbo.Plugins.JarJar.DefaultUI
  7. {
  8.     public class ScreenCapture : BasePlugin, IBeforeRenderHandler
  9.     {
  10.         public bool StopRenderingWhenCapturing { get; set; } = false;
  11.         public string SubFolderName { get; set; } = "capture_screen";
  12.         public int DelayBetweenFrames { get; set; } = 500;
  13.  
  14.         public Func<IController, bool> IsCaptureOn = (hud) => { return false; };
  15.  
  16.         public Func<IController, string> CaptureFilename = (hud) => { return hud.Game.Me.HeroName + "_" + hud.Time.Now.ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture); };
  17.  
  18.         IWatch _lastCaptureDelay;
  19.         int captureCount = 0;
  20.  
  21.         public ScreenCapture() { Enabled = true; }
  22.  
  23.         public void BeforeRender()
  24.         {
  25.             if (!Enabled) return;
  26.             if (!Hud.Game.IsInGame) return;
  27.             if (Hud.Game.IsInTown) return;
  28.  
  29.             var isCapturing = IsCaptureOn(Hud);
  30.             if (!isCapturing)
  31.             {
  32.                 if (StopRenderingWhenCapturing && !Hud.Render.IsRenderEnabled)
  33.                 {
  34.                     // turn back
  35.                     Hud.Render.IsRenderEnabled = true;
  36.                 }
  37.                 if (captureCount > 0)
  38.                 {
  39.                     Hud.Debug(string.Format("Captured {0} screenshots", captureCount));
  40.                     _lastCaptureDelay = null;
  41.                     captureCount = 0;
  42.                 }
  43.                 return;
  44.             }
  45.  
  46.             if (StopRenderingWhenCapturing && Hud.Render.IsRenderEnabled)
  47.             {
  48.                 // turn off and wait a cycle before first capture so HUD can disappear properly
  49.                 Hud.Render.IsRenderEnabled = false;
  50.                 return;
  51.             }
  52.  
  53.             if (_lastCaptureDelay == null || _lastCaptureDelay.TimerTest(DelayBetweenFrames))
  54.             {
  55.                 if (_lastCaptureDelay == null)
  56.                 {
  57.                     _lastCaptureDelay = Hud.Time.CreateWatch();
  58.                 }
  59.                 _lastCaptureDelay.Restart();
  60.                 try
  61.                 {
  62.                     var fileName = CaptureFilename(Hud) + ".jpg";
  63.                     Hud.Render.CaptureScreenToFile(SubFolderName, fileName);
  64.                     captureCount += 1;
  65.                 }
  66.                 catch (Exception x)
  67.                 {
  68.                     Enabled = false;
  69.                     Hud.Debug("Exception in ScreenCapture: " + x.Message);
  70.                     throw x;
  71.                 }
  72.             }
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement