SceneModalWindow.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using UnityEngine;
  2. namespace MeidoPhotoStudio.Plugin;
  3. public class SceneModalWindow : BaseWindow
  4. {
  5. private static readonly Texture2D InfoHighlight = Utility.MakeTex(2, 2, new(0f, 0f, 0f, 0.8f));
  6. private readonly SceneManager sceneManager;
  7. private readonly Button okButton;
  8. private readonly Button cancelButton;
  9. private readonly Button deleteButton;
  10. private readonly Button overwriteButton;
  11. private bool visible;
  12. private string deleteDirectoryMessage;
  13. private string deleteSceneMessage;
  14. private string directoryDeleteCommit;
  15. private string sceneDeleteCommit;
  16. private string sceneLoadCommit;
  17. private string infoKankyo;
  18. private string infoMaidSingular;
  19. private string infoMaidPlural;
  20. private bool directoryMode;
  21. private bool deleteScene;
  22. public SceneModalWindow(SceneManager sceneManager)
  23. {
  24. ReloadTranslation();
  25. this.sceneManager = sceneManager;
  26. windowRect.x = MiddlePosition.x;
  27. windowRect.y = MiddlePosition.y;
  28. okButton = new(sceneLoadCommit);
  29. okButton.ControlEvent += (_, _) =>
  30. Commit();
  31. cancelButton = new("Cancel");
  32. cancelButton.ControlEvent += (_, _) =>
  33. Cancel();
  34. deleteButton = new("Delete");
  35. deleteButton.ControlEvent += (_, _) =>
  36. {
  37. okButton.Label = sceneDeleteCommit;
  38. deleteScene = true;
  39. };
  40. overwriteButton = new("Overwrite");
  41. overwriteButton.ControlEvent += (_, _) =>
  42. {
  43. sceneManager.OverwriteScene();
  44. Visible = false;
  45. };
  46. }
  47. public override Rect WindowRect
  48. {
  49. set
  50. {
  51. value.width = Mathf.Clamp(Screen.width * 0.3f, 360f, 500f);
  52. value.height = directoryMode ? 150f : Mathf.Clamp(Screen.height * 0.4f, 240f, 380f);
  53. base.WindowRect = value;
  54. }
  55. }
  56. public override bool Visible
  57. {
  58. get => visible;
  59. set
  60. {
  61. visible = value;
  62. if (!value)
  63. return;
  64. WindowRect = WindowRect;
  65. windowRect.x = MiddlePosition.x;
  66. windowRect.y = MiddlePosition.y;
  67. }
  68. }
  69. public override void Draw()
  70. {
  71. GUILayout.BeginArea(new(10f, 10f, WindowRect.width - 20f, WindowRect.height - 20f));
  72. // thumbnail
  73. if (!directoryMode)
  74. {
  75. var scene = sceneManager.CurrentScene;
  76. var thumb = scene.Thumbnail;
  77. var scale = Mathf.Min((WindowRect.width - 20f) / thumb.width, (WindowRect.height - 110f) / thumb.height);
  78. var width = Mathf.Min(thumb.width, thumb.width * scale);
  79. var height = Mathf.Min(thumb.height, thumb.height * scale);
  80. GUILayout.BeginHorizontal();
  81. GUILayout.FlexibleSpace();
  82. MpsGui.DrawTexture(thumb, GUILayout.Width(width), GUILayout.Height(height));
  83. var labelStyle = new GUIStyle(GUI.skin.label)
  84. {
  85. fontSize = Utility.GetPix(12),
  86. alignment = TextAnchor.MiddleCenter,
  87. };
  88. labelStyle.normal.background = InfoHighlight;
  89. var labelBox = GUILayoutUtility.GetLastRect();
  90. var infoString = scene.Environment
  91. ? infoKankyo
  92. : string.Format(scene.NumberOfMaids is 1 ? infoMaidSingular : infoMaidPlural, scene.NumberOfMaids);
  93. var labelSize = labelStyle.CalcSize(new GUIContent(infoString));
  94. labelBox = new(
  95. labelBox.x + 10, labelBox.y + labelBox.height - (labelSize.y + 10), labelSize.x + 10, labelSize.y + 2);
  96. GUI.Label(labelBox, infoString, labelStyle);
  97. GUILayout.FlexibleSpace();
  98. GUILayout.EndHorizontal();
  99. }
  100. // message
  101. string currentMessage;
  102. string context;
  103. if (directoryMode)
  104. {
  105. currentMessage = deleteDirectoryMessage;
  106. context = sceneManager.CurrentDirectoryName;
  107. }
  108. else
  109. {
  110. if (deleteScene)
  111. {
  112. currentMessage = deleteSceneMessage;
  113. context = sceneManager.CurrentScene.FileInfo.Name;
  114. }
  115. else
  116. {
  117. currentMessage = sceneManager.CurrentScene.FileInfo.Name;
  118. context = currentMessage;
  119. }
  120. }
  121. var messageStyle = new GUIStyle(GUI.skin.label)
  122. {
  123. alignment = TextAnchor.MiddleCenter,
  124. fontSize = Utility.GetPix(12),
  125. };
  126. GUILayout.FlexibleSpace();
  127. GUILayout.Label(string.Format(currentMessage, context), messageStyle);
  128. GUILayout.FlexibleSpace();
  129. // Buttons
  130. var buttonStyle = new GUIStyle(GUI.skin.button)
  131. {
  132. fontSize = Utility.GetPix(12),
  133. };
  134. var buttonHeight = GUILayout.Height(Utility.GetPix(20));
  135. GUILayout.BeginHorizontal();
  136. if (directoryMode || deleteScene)
  137. {
  138. GUILayout.FlexibleSpace();
  139. okButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  140. cancelButton.Draw(buttonStyle, buttonHeight, GUILayout.Width(100));
  141. }
  142. else
  143. {
  144. deleteButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  145. overwriteButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  146. GUILayout.FlexibleSpace();
  147. okButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  148. cancelButton.Draw(buttonStyle, buttonHeight, GUILayout.Width(100));
  149. }
  150. GUILayout.EndHorizontal();
  151. GUILayout.EndArea();
  152. }
  153. public void ShowDirectoryDialogue()
  154. {
  155. okButton.Label = directoryDeleteCommit;
  156. directoryMode = true;
  157. Modal.Show(this);
  158. }
  159. public void ShowSceneDialogue()
  160. {
  161. directoryMode = false;
  162. okButton.Label = sceneLoadCommit;
  163. Modal.Show(this);
  164. }
  165. protected override void ReloadTranslation()
  166. {
  167. deleteDirectoryMessage = Translation.Get("sceneManagerModal", "deleteDirectoryConfirm");
  168. deleteSceneMessage = Translation.Get("sceneManagerModal", "deleteFileConfirm");
  169. directoryDeleteCommit = Translation.Get("sceneManagerModal", "deleteDirectoryButton");
  170. sceneDeleteCommit = Translation.Get("sceneManagerModal", "deleteFileCommit");
  171. sceneLoadCommit = Translation.Get("sceneManagerModal", "fileLoadCommit");
  172. infoKankyo = Translation.Get("sceneManagerModal", "infoKankyo");
  173. infoMaidSingular = Translation.Get("sceneManagerModal", "infoMaidSingular");
  174. infoMaidPlural = Translation.Get("sceneManagerModal", "infoMaidPlural");
  175. }
  176. private void Commit()
  177. {
  178. if (directoryMode)
  179. {
  180. sceneManager.DeleteDirectory();
  181. Modal.Close();
  182. }
  183. else
  184. {
  185. if (deleteScene)
  186. {
  187. sceneManager.DeleteScene();
  188. deleteScene = false;
  189. }
  190. else
  191. {
  192. sceneManager.LoadScene(sceneManager.CurrentScene);
  193. }
  194. Modal.Close();
  195. }
  196. }
  197. private void Cancel()
  198. {
  199. if (directoryMode)
  200. {
  201. Modal.Close();
  202. }
  203. else
  204. {
  205. if (deleteScene)
  206. deleteScene = false;
  207. else
  208. Modal.Close();
  209. }
  210. okButton.Label = sceneLoadCommit;
  211. }
  212. }