WindowManager.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace COM3D2.MeidoPhotoStudio.Plugin
  5. {
  6. using Window = Constants.Window;
  7. public class WindowManager
  8. {
  9. private Dictionary<Window, BaseWindow> Windows;
  10. private static Window currentWindow = Window.Call;
  11. private static Window CurrentWindow
  12. {
  13. get => currentWindow;
  14. set
  15. {
  16. if (value > Window.BG2) currentWindow = Window.BG2;
  17. else if (value < Window.Call) currentWindow = Window.Call;
  18. else currentWindow = value;
  19. }
  20. }
  21. private Rect mainWindowRect;
  22. private Rect messageWindowRect;
  23. private MeidoManager meidoManager;
  24. private bool initializeWindows = false;
  25. public bool MainWindowVisible { get; set; }
  26. public bool MessageWindowVisible
  27. {
  28. get => Windows[Window.Message].Visible;
  29. set
  30. {
  31. Windows[Window.Message].Visible = value;
  32. }
  33. }
  34. public bool DropdownVisible
  35. {
  36. get => DropdownHelper.Visible;
  37. set
  38. {
  39. DropdownHelper.Visible = value;
  40. }
  41. }
  42. public WindowManager(
  43. MeidoManager meidoManager,
  44. EnvironmentManager environmentManager,
  45. MessageWindowManager messageWindowManager
  46. )
  47. {
  48. TabsPane.TabChange += ChangeTab;
  49. this.meidoManager = meidoManager;
  50. this.meidoManager.SelectMeido += MeidoSelect;
  51. mainWindowRect.y = Screen.height * 0.08f;
  52. mainWindowRect.x = Screen.width;
  53. Windows = new Dictionary<Window, BaseWindow>()
  54. {
  55. [Window.Call] = new MaidCallWindow(meidoManager),
  56. [Window.Pose] = new MaidPoseWindow(meidoManager),
  57. [Window.Face] = new MaidFaceWindow(meidoManager),
  58. [Window.BG] = new BackgroundWindow(environmentManager),
  59. [Window.BG2] = new Background2Window(environmentManager),
  60. [Window.Message] = new MessageWindow(messageWindowManager)
  61. };
  62. Windows[Window.Message].Visible = false;
  63. }
  64. ~WindowManager()
  65. {
  66. TabsPane.TabChange -= ChangeTab;
  67. }
  68. private void MeidoSelect(object sender, MeidoChangeEventArgs args)
  69. {
  70. if (args.fromMeido)
  71. TabsPane.SelectedTab = args.isBody ? Window.Pose : Window.Face;
  72. }
  73. private void ChangeTab(object sender, EventArgs args)
  74. {
  75. CurrentWindow = TabsPane.SelectedTab;
  76. }
  77. public void Update()
  78. {
  79. if (Input.GetKeyDown(KeyCode.M))
  80. {
  81. (Windows[Window.Message] as MessageWindow).ToggleVisibility();
  82. }
  83. if (Input.GetKeyDown(KeyCode.Tab))
  84. {
  85. MainWindowVisible = !MainWindowVisible;
  86. }
  87. HandleZoom();
  88. }
  89. private void HandleZoom()
  90. {
  91. bool mainWindowVisible = MainWindowVisible;
  92. bool dropdownVisible = DropdownVisible;
  93. bool messageWindowVisible = MessageWindowVisible;
  94. if (mainWindowVisible || dropdownVisible || messageWindowVisible)
  95. {
  96. if (Input.mouseScrollDelta.y != 0f)
  97. {
  98. Vector2 mousePos = Event.current.mousePosition;
  99. if (mainWindowVisible && mainWindowRect.Contains(mousePos)
  100. || dropdownVisible && DropdownHelper.dropdownWindow.Contains(mousePos)
  101. || messageWindowVisible && messageWindowRect.Contains(mousePos)
  102. )
  103. {
  104. GameMain.Instance.MainCamera.SetControl(false);
  105. Input.ResetInputAxes();
  106. }
  107. }
  108. }
  109. }
  110. public void OnGUI()
  111. {
  112. GUIStyle windowStyle = new GUIStyle(GUI.skin.box);
  113. GameMain.Instance.MainCamera.SetControl(true);
  114. if (MainWindowVisible)
  115. {
  116. mainWindowRect.width = 230;
  117. mainWindowRect.height = Screen.height * 0.8f;
  118. mainWindowRect.x = Mathf.Clamp(mainWindowRect.x, 0, Screen.width - mainWindowRect.width);
  119. mainWindowRect.y = Mathf.Clamp(mainWindowRect.y, -mainWindowRect.height + 30, Screen.height - 50);
  120. mainWindowRect = GUI.Window(
  121. Constants.mainWindowID, mainWindowRect, Windows[CurrentWindow].OnGUI, "", windowStyle
  122. );
  123. }
  124. if (MessageWindowVisible)
  125. {
  126. messageWindowRect.width = Mathf.Clamp(Screen.width * 0.4f, 440, Mathf.Infinity);
  127. messageWindowRect.height = Mathf.Clamp(Screen.height * 0.15f, 150, Mathf.Infinity);
  128. messageWindowRect.x = Mathf.Clamp(
  129. messageWindowRect.x,
  130. -messageWindowRect.width + Utility.GetPix(20),
  131. Screen.width - Utility.GetPix(20)
  132. );
  133. messageWindowRect.y = Mathf.Clamp(
  134. messageWindowRect.y,
  135. -messageWindowRect.height + Utility.GetPix(20),
  136. Screen.height - Utility.GetPix(20)
  137. );
  138. if (!initializeWindows)
  139. {
  140. messageWindowRect.x = Screen.width / 2 - messageWindowRect.width / 2;
  141. messageWindowRect.y = Screen.height - messageWindowRect.height;
  142. initializeWindows = true;
  143. }
  144. messageWindowRect = GUI.Window(
  145. Constants.messageWindowID, messageWindowRect, Windows[Window.Message].OnGUI, "", windowStyle
  146. );
  147. }
  148. if (DropdownVisible) DropdownHelper.HandleDropdown();
  149. }
  150. }
  151. }