SceneModalWindow.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. 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. this.okButton = new Button(sceneLoadCommit);
  54. this.okButton.ControlEvent += (s, a) => Commit();
  55. this.cancelButton = new Button("Cancel");
  56. this.cancelButton.ControlEvent += (s, a) => Cancel();
  57. this.deleteButton = new Button("Delete");
  58. this.deleteButton.ControlEvent += (s, a) =>
  59. {
  60. okButton.Label = sceneDeleteCommit;
  61. deleteScene = true;
  62. };
  63. this.overwriteButton = new Button("Overwrite");
  64. this.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) / (float)thumb.width, (WindowRect.height - 110f) / (float)thumb.height
  91. );
  92. float width = Mathf.Min(thumb.width, (float)thumb.width * scale);
  93. float height = Mathf.Min(thumb.height, (float)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. labelStyle.fontSize = Utility.GetPix(12);
  99. labelStyle.alignment = TextAnchor.MiddleCenter;
  100. labelStyle.normal.background = infoHighlight;
  101. Rect labelBox = GUILayoutUtility.GetLastRect();
  102. if (scene.NumberOfMaids != SceneManager.Scene.initialNumberOfMaids)
  103. {
  104. int numberOfMaids = scene.NumberOfMaids;
  105. string infoString = numberOfMaids == MeidoPhotoStudio.kankyoMagic
  106. ? infoKankyo
  107. : string.Format(numberOfMaids == 1 ? infoMaidSingular : infoMaidPlural, numberOfMaids);
  108. Vector2 labelSize = labelStyle.CalcSize(new GUIContent(infoString));
  109. labelBox = new Rect(
  110. labelBox.x + 10, labelBox.y + labelBox.height - (labelSize.y + 10),
  111. labelSize.x + 10, labelSize.y + 2
  112. );
  113. GUI.Label(labelBox, infoString, labelStyle);
  114. }
  115. GUILayout.FlexibleSpace();
  116. GUILayout.EndHorizontal();
  117. }
  118. // message
  119. string currentMessage = string.Empty;
  120. string context = string.Empty;
  121. if (directoryMode)
  122. {
  123. currentMessage = deleteDirectoryMessage;
  124. context = sceneManager.CurrentDirectoryName;
  125. }
  126. else
  127. {
  128. if (deleteScene)
  129. {
  130. currentMessage = deleteSceneMessage;
  131. context = sceneManager.CurrentScene.FileInfo.Name;
  132. }
  133. else
  134. {
  135. currentMessage = sceneManager.CurrentScene.FileInfo.Name;
  136. context = currentMessage;
  137. }
  138. }
  139. GUIStyle messageStyle = new GUIStyle(GUI.skin.label);
  140. messageStyle.alignment = TextAnchor.MiddleCenter;
  141. messageStyle.fontSize = Utility.GetPix(12);
  142. GUILayout.FlexibleSpace();
  143. GUILayout.Label(string.Format(currentMessage, context), messageStyle);
  144. GUILayout.FlexibleSpace();
  145. // Buttons
  146. GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
  147. buttonStyle.fontSize = Utility.GetPix(12);
  148. GUILayoutOption buttonHeight = GUILayout.Height(Utility.GetPix(20));
  149. GUILayout.BeginHorizontal();
  150. if (directoryMode || deleteScene)
  151. {
  152. GUILayout.FlexibleSpace();
  153. okButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  154. cancelButton.Draw(buttonStyle, buttonHeight, GUILayout.Width(100));
  155. }
  156. else
  157. {
  158. deleteButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  159. overwriteButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  160. GUILayout.FlexibleSpace();
  161. okButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  162. cancelButton.Draw(buttonStyle, buttonHeight, GUILayout.Width(100));
  163. }
  164. GUILayout.EndHorizontal();
  165. GUILayout.EndArea();
  166. }
  167. public void ShowDirectoryDialogue()
  168. {
  169. okButton.Label = directoryDeleteCommit;
  170. directoryMode = true;
  171. Modal.Show(this);
  172. }
  173. public void ShowSceneDialogue()
  174. {
  175. directoryMode = false;
  176. okButton.Label = sceneLoadCommit;
  177. Modal.Show(this);
  178. }
  179. private void Commit()
  180. {
  181. if (directoryMode)
  182. {
  183. sceneManager.DeleteDirectory();
  184. Modal.Close();
  185. }
  186. else
  187. {
  188. if (deleteScene)
  189. {
  190. sceneManager.DeleteScene();
  191. deleteScene = false;
  192. }
  193. else sceneManager.LoadScene();
  194. Modal.Close();
  195. }
  196. }
  197. private void Cancel()
  198. {
  199. if (directoryMode) Modal.Close();
  200. else
  201. {
  202. if (deleteScene) deleteScene = false;
  203. else Modal.Close();
  204. }
  205. okButton.Label = sceneLoadCommit;
  206. }
  207. }
  208. }