SceneModalWindow.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. internal class SceneModalWindow : BaseWindow
  6. {
  7. private static Texture2D infoHighlight = Utility.MakeTex(2, 2, new Color(0f, 0f, 0f, 0.8f));
  8. private 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 Button okButton;
  34. private Button cancelButton;
  35. private Button deleteButton;
  36. private Button overwriteButton;
  37. private string deleteDirectoryMessage
  38. = "Are you sure you want to permanently delete '{0}' and all of its files?";
  39. private string deleteSceneMessage = "Are you sure you want to permanently delete '{0}'?";
  40. private string directoryDeleteCommit = "Delete Folder";
  41. private string sceneDeleteCommit = "Delete Scene";
  42. private string sceneLoadCommit = "Load Scene";
  43. private bool directoryMode = false;
  44. private bool deleteScene = false;
  45. public SceneModalWindow(SceneManager sceneManager)
  46. {
  47. this.sceneManager = sceneManager;
  48. windowRect.x = MiddlePosition.x;
  49. windowRect.y = MiddlePosition.y;
  50. this.okButton = new Button(sceneLoadCommit);
  51. this.okButton.ControlEvent += (s, a) => Commit();
  52. this.cancelButton = new Button("Cancel");
  53. this.cancelButton.ControlEvent += (s, a) => Cancel();
  54. this.deleteButton = new Button("Delete");
  55. this.deleteButton.ControlEvent += (s, a) =>
  56. {
  57. okButton.Label = sceneDeleteCommit;
  58. deleteScene = true;
  59. };
  60. this.overwriteButton = new Button("Overwrite");
  61. this.overwriteButton.ControlEvent += (s, a) =>
  62. {
  63. sceneManager.OverwriteScene();
  64. Visible = false;
  65. };
  66. }
  67. public override void Draw()
  68. {
  69. GUILayout.BeginArea(new Rect(10f, 10f, WindowRect.width - 20f, WindowRect.height - 20f));
  70. // thumbnail
  71. if (!directoryMode)
  72. {
  73. SceneManager.Scene scene = sceneManager.CurrentScene;
  74. Texture2D thumb = scene.Thumbnail;
  75. float scale = Mathf.Min(
  76. (WindowRect.width - 20f) / (float)thumb.width, (WindowRect.height - 110f) / (float)thumb.height
  77. );
  78. float width = Mathf.Min(thumb.width, (float)thumb.width * scale);
  79. float height = Mathf.Min(thumb.height, (float)thumb.height * scale);
  80. GUILayout.BeginHorizontal();
  81. GUILayout.FlexibleSpace();
  82. MiscGUI.DrawTexture(thumb, GUILayout.Width(width), GUILayout.Height(height));
  83. GUIStyle labelStyle = new GUIStyle(GUI.skin.label);
  84. labelStyle.fontSize = Utility.GetPix(12);
  85. labelStyle.alignment = TextAnchor.MiddleCenter;
  86. labelStyle.normal.background = infoHighlight;
  87. Rect labelBox = GUILayoutUtility.GetLastRect();
  88. if (scene.NumberOfMaids != SceneManager.Scene.initialNumberOfMaids)
  89. {
  90. string infoString = scene.NumberOfMaids == MeidoPhotoStudio.kankyoMagic
  91. ? "Kankyo"
  92. : $"{scene.NumberOfMaids} Maids";
  93. Vector2 labelSize = labelStyle.CalcSize(new GUIContent(infoString));
  94. labelBox = new Rect(
  95. labelBox.x + 10, labelBox.y + labelBox.height - (labelSize.y + 10),
  96. labelSize.x + 10, labelSize.y + 2
  97. );
  98. GUI.Label(labelBox, infoString, labelStyle);
  99. }
  100. GUILayout.FlexibleSpace();
  101. GUILayout.EndHorizontal();
  102. }
  103. // message
  104. string currentMessage = string.Empty;
  105. string context = string.Empty;
  106. if (directoryMode)
  107. {
  108. currentMessage = deleteDirectoryMessage;
  109. context = sceneManager.CurrentDirectoryName;
  110. }
  111. else
  112. {
  113. if (deleteScene)
  114. {
  115. currentMessage = deleteSceneMessage;
  116. context = sceneManager.CurrentScene.FileInfo.Name;
  117. }
  118. else
  119. {
  120. currentMessage = sceneManager.CurrentScene.FileInfo.Name;
  121. context = currentMessage;
  122. }
  123. }
  124. GUIStyle messageStyle = new GUIStyle(GUI.skin.label);
  125. messageStyle.alignment = TextAnchor.MiddleCenter;
  126. messageStyle.fontSize = Utility.GetPix(12);
  127. GUILayout.FlexibleSpace();
  128. GUILayout.Label(string.Format(currentMessage, context), messageStyle);
  129. GUILayout.FlexibleSpace();
  130. // Buttons
  131. GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
  132. buttonStyle.fontSize = Utility.GetPix(12);
  133. GUILayoutOption buttonHeight = GUILayout.Height(Utility.GetPix(20));
  134. GUILayout.BeginHorizontal();
  135. if (directoryMode || deleteScene)
  136. {
  137. GUILayout.FlexibleSpace();
  138. okButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  139. cancelButton.Draw(buttonStyle, buttonHeight, GUILayout.Width(100));
  140. }
  141. else
  142. {
  143. deleteButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  144. overwriteButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  145. GUILayout.FlexibleSpace();
  146. okButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  147. cancelButton.Draw(buttonStyle, buttonHeight, GUILayout.Width(100));
  148. }
  149. GUILayout.EndHorizontal();
  150. GUILayout.EndArea();
  151. }
  152. public void ShowDirectoryDialogue()
  153. {
  154. okButton.Label = directoryDeleteCommit;
  155. directoryMode = true;
  156. Modal.Show(this);
  157. }
  158. public void ShowSceneDialogue()
  159. {
  160. directoryMode = false;
  161. okButton.Label = sceneLoadCommit;
  162. Modal.Show(this);
  163. }
  164. private void Commit()
  165. {
  166. if (directoryMode)
  167. {
  168. sceneManager.DeleteDirectory();
  169. Modal.Close();
  170. }
  171. else
  172. {
  173. if (deleteScene)
  174. {
  175. sceneManager.DeleteScene();
  176. deleteScene = false;
  177. }
  178. else sceneManager.LoadScene();
  179. Modal.Close();
  180. }
  181. }
  182. private void Cancel()
  183. {
  184. if (directoryMode) Modal.Close();
  185. else
  186. {
  187. if (deleteScene) deleteScene = false;
  188. else Modal.Close();
  189. }
  190. okButton.Label = sceneLoadCommit;
  191. }
  192. }
  193. }