ScreenshotManager.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using alphaShot;
  2. using BepInEx;
  3. using BepInEx.Common;
  4. using Illusion.Game;
  5. using System;
  6. using System.Collections;
  7. using System.IO;
  8. using UnityEngine;
  9. namespace Screencap
  10. {
  11. public class ScreenshotManager : BaseUnityPlugin
  12. {
  13. public override string ID => "com.bepis.bepinex.screenshotmanager";
  14. public override string Name => "Screenshot Manager";
  15. public override Version Version => new Version("2.0");
  16. Event ScreenKeyEvent = Event.KeyboardEvent("f9");
  17. Event CharacterKeyEvent = Event.KeyboardEvent("f11");
  18. Event CharacterSettingsKeyEvent = Event.KeyboardEvent("#f11");
  19. private string screenshotDir = Utility.CombinePaths(Utility.ExecutingDirectory, "UserData", "cap");
  20. #region Config properties
  21. private int ResolutionX
  22. {
  23. get => int.Parse(this.GetEntry("resolution-x", "1024"));
  24. set => this.SetEntry("resolution-x", value.ToString());
  25. }
  26. private int ResolutionY
  27. {
  28. get => int.Parse(this.GetEntry("resolution-y", "1024"));
  29. set => this.SetEntry("resolution-y", value.ToString());
  30. }
  31. private int AntiAliasing
  32. {
  33. get => int.Parse(this.GetEntry("antialiasing", "4"));
  34. set => this.SetEntry("antialiasing", value.ToString());
  35. }
  36. private int DownscalingRate
  37. {
  38. get => int.Parse(this.GetEntry("downscalerate", "1"));
  39. set => this.SetEntry("downscalerate", value.ToString());
  40. }
  41. private int RenderMethod
  42. {
  43. get => int.Parse(this.GetEntry("rendermethod", "1"));
  44. set => this.SetEntry("rendermethod", value.ToString());
  45. }
  46. #endregion
  47. void Awake()
  48. {
  49. if (!Directory.Exists(screenshotDir))
  50. Directory.CreateDirectory(screenshotDir);
  51. }
  52. void Update()
  53. {
  54. if (UnityEngine.Event.current.Equals(CharacterSettingsKeyEvent))
  55. {
  56. showingUI = !showingUI;
  57. }
  58. else if (UnityEngine.Event.current.Equals(ScreenKeyEvent))
  59. {
  60. string filename = Path.Combine(screenshotDir, $"Koikatsu -{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.png");
  61. StartCoroutine(TakeScreenshot(filename));
  62. }
  63. else if (UnityEngine.Event.current.Equals(CharacterKeyEvent))
  64. {
  65. string filename = Path.Combine(screenshotDir, $"Koikatsu Char-{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.png");
  66. TakeCharScreenshot(filename);
  67. }
  68. }
  69. IEnumerator TakeScreenshot(string filename)
  70. {
  71. Application.CaptureScreenshot(filename);
  72. Illusion.Game.Utils.Sound.Play(SystemSE.photo);
  73. while (!File.Exists(filename))
  74. yield return new WaitForSeconds(0.01f);
  75. BepInLogger.Log($"Screenshot saved to {filename}", true);
  76. }
  77. void TakeCharScreenshot(string filename)
  78. {
  79. Camera.main.backgroundColor = Color.clear;
  80. switch (RenderMethod)
  81. {
  82. case 0: //legacy
  83. default:
  84. File.WriteAllBytes(filename, LegacyRenderer.RenderCamera(ResolutionX, ResolutionY, DownscalingRate, AntiAliasing));
  85. break;
  86. //case 1: //alphashot
  87. // File.WriteAllBytes(filename, AlphaShot.Capture(ResolutionX, ResolutionY, DownscalingRate, AntiAliasing));
  88. // break;
  89. }
  90. Illusion.Game.Utils.Sound.Play(SystemSE.photo);
  91. BepInLogger.Log($"Character screenshot saved to {filename}", true);
  92. }
  93. #region UI
  94. private Rect UI = new Rect(20, 20, 160, 250);
  95. private bool showingUI = false;
  96. void OnGUI()
  97. {
  98. if (showingUI)
  99. UI = GUI.Window(Name.GetHashCode() + 0, UI, WindowFunction, "Rendering settings");
  100. }
  101. void WindowFunction(int windowID)
  102. {
  103. GUI.Label(new Rect(0, 20, 160, 20), "Output resolution", new GUIStyle
  104. {
  105. alignment = TextAnchor.MiddleCenter,
  106. normal = new GUIStyleState
  107. {
  108. textColor = Color.white
  109. }
  110. });
  111. GUI.Label(new Rect(0, 40, 160, 20), "x", new GUIStyle
  112. {
  113. alignment = TextAnchor.MiddleCenter,
  114. normal = new GUIStyleState
  115. {
  116. textColor = Color.white
  117. }
  118. });
  119. string resX = GUI.TextField(new Rect(10, 40, 60, 20), ResolutionX.ToString());
  120. string resY = GUI.TextField(new Rect(90, 40, 60, 20), ResolutionY.ToString());
  121. bool screenSize = GUI.Button(new Rect(10, 65, 140, 20), "Set to screen size");
  122. GUI.Label(new Rect(0, 90, 160, 20), "Downscaling rate", new GUIStyle
  123. {
  124. alignment = TextAnchor.MiddleCenter,
  125. normal = new GUIStyleState
  126. {
  127. textColor = Color.white
  128. }
  129. });
  130. int downscale = (int)Math.Round(GUI.HorizontalSlider(new Rect(10, 113, 120, 20), DownscalingRate, 1, 4));
  131. GUI.Label(new Rect(0, 110, 150, 20), $"{downscale}x", new GUIStyle
  132. {
  133. alignment = TextAnchor.UpperRight,
  134. normal = new GUIStyleState
  135. {
  136. textColor = Color.white
  137. }
  138. });
  139. GUI.Label(new Rect(0, 130, 160, 20), "Antialiasing", new GUIStyle
  140. {
  141. alignment = TextAnchor.MiddleCenter,
  142. normal = new GUIStyleState
  143. {
  144. textColor = Color.white
  145. }
  146. });
  147. int antia = (int)Math.Round(GUI.HorizontalSlider(new Rect(10, 153, 120, 20), AntiAliasing, 1, 16));
  148. GUI.Label(new Rect(0, 150, 154, 20), $"{antia}x", new GUIStyle
  149. {
  150. alignment = TextAnchor.UpperRight,
  151. normal = new GUIStyleState
  152. {
  153. textColor = Color.white
  154. }
  155. });
  156. GUI.Label(new Rect(0, 170, 160, 20), "Render method", new GUIStyle
  157. {
  158. alignment = TextAnchor.MiddleCenter,
  159. normal = new GUIStyleState
  160. {
  161. textColor = Color.white
  162. }
  163. });
  164. int renderMethod = GUI.SelectionGrid(new Rect(10, 190, 140, 50), RenderMethod, new[] { "Legacy", "AlphaShot" }, 1);
  165. if (GUI.changed)
  166. {
  167. BepInEx.Config.SaveOnConfigSet = false;
  168. if (int.TryParse(resX, out int x))
  169. ResolutionX = Mathf.Clamp(x, 2, 4096);
  170. if (int.TryParse(resY, out int y))
  171. ResolutionY = Mathf.Clamp(y, 2, 4096);
  172. if (screenSize)
  173. {
  174. ResolutionX = Screen.width;
  175. ResolutionY = Screen.height;
  176. }
  177. DownscalingRate = downscale;
  178. AntiAliasing = antia;
  179. RenderMethod = renderMethod;
  180. BepInEx.Config.SaveOnConfigSet = true;
  181. BepInEx.Config.SaveConfig();
  182. }
  183. GUI.DragWindow();
  184. }
  185. #endregion
  186. }
  187. }