SceneModalWindow.cs 8.5 KB

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