SceneModalWindow.cs 8.4 KB

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