SceneModalWindow.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. MPSScene 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. var infoString = scene.Environment
  104. ? infoKankyo
  105. : string.Format(scene.NumberOfMaids == 1 ? infoMaidSingular : infoMaidPlural, scene.NumberOfMaids);
  106. Vector2 labelSize = labelStyle.CalcSize(new GUIContent(infoString));
  107. labelBox = new Rect(
  108. labelBox.x + 10, labelBox.y + labelBox.height - (labelSize.y + 10),
  109. labelSize.x + 10, labelSize.y + 2
  110. );
  111. GUI.Label(labelBox, infoString, labelStyle);
  112. GUILayout.FlexibleSpace();
  113. GUILayout.EndHorizontal();
  114. }
  115. // message
  116. string currentMessage;
  117. string context;
  118. if (directoryMode)
  119. {
  120. currentMessage = deleteDirectoryMessage;
  121. context = sceneManager.CurrentDirectoryName;
  122. }
  123. else
  124. {
  125. if (deleteScene)
  126. {
  127. currentMessage = deleteSceneMessage;
  128. context = sceneManager.CurrentScene.FileInfo.Name;
  129. }
  130. else
  131. {
  132. currentMessage = sceneManager.CurrentScene.FileInfo.Name;
  133. context = currentMessage;
  134. }
  135. }
  136. GUIStyle messageStyle = new GUIStyle(GUI.skin.label)
  137. {
  138. alignment = TextAnchor.MiddleCenter,
  139. fontSize = Utility.GetPix(12)
  140. };
  141. GUILayout.FlexibleSpace();
  142. GUILayout.Label(string.Format(currentMessage, context), messageStyle);
  143. GUILayout.FlexibleSpace();
  144. // Buttons
  145. GUIStyle buttonStyle = new GUIStyle(GUI.skin.button)
  146. {
  147. fontSize = Utility.GetPix(12)
  148. };
  149. GUILayoutOption buttonHeight = GUILayout.Height(Utility.GetPix(20));
  150. GUILayout.BeginHorizontal();
  151. if (directoryMode || deleteScene)
  152. {
  153. GUILayout.FlexibleSpace();
  154. okButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  155. cancelButton.Draw(buttonStyle, buttonHeight, GUILayout.Width(100));
  156. }
  157. else
  158. {
  159. deleteButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  160. overwriteButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  161. GUILayout.FlexibleSpace();
  162. okButton.Draw(buttonStyle, buttonHeight, GUILayout.ExpandWidth(false));
  163. cancelButton.Draw(buttonStyle, buttonHeight, GUILayout.Width(100));
  164. }
  165. GUILayout.EndHorizontal();
  166. GUILayout.EndArea();
  167. }
  168. public void ShowDirectoryDialogue()
  169. {
  170. okButton.Label = directoryDeleteCommit;
  171. directoryMode = true;
  172. Modal.Show(this);
  173. }
  174. public void ShowSceneDialogue()
  175. {
  176. directoryMode = false;
  177. okButton.Label = sceneLoadCommit;
  178. Modal.Show(this);
  179. }
  180. private void Commit()
  181. {
  182. if (directoryMode)
  183. {
  184. sceneManager.DeleteDirectory();
  185. Modal.Close();
  186. }
  187. else
  188. {
  189. if (deleteScene)
  190. {
  191. sceneManager.DeleteScene();
  192. deleteScene = false;
  193. }
  194. else sceneManager.LoadScene(sceneManager.CurrentScene);
  195. Modal.Close();
  196. }
  197. }
  198. private void Cancel()
  199. {
  200. if (directoryMode) Modal.Close();
  201. else
  202. {
  203. if (deleteScene) deleteScene = false;
  204. else Modal.Close();
  205. }
  206. okButton.Label = sceneLoadCommit;
  207. }
  208. }
  209. }