MainWindow.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace MeidoPhotoStudio.Plugin;
  4. public class MainWindow : BaseWindow
  5. {
  6. private readonly MeidoManager meidoManager;
  7. private readonly Dictionary<Constants.Window, BaseMainWindowPane> windowPanes;
  8. private readonly PropManager propManager;
  9. private readonly LightManager lightManager;
  10. private readonly TabsPane tabsPane;
  11. private readonly Button settingsButton;
  12. private BaseMainWindowPane currentWindowPane;
  13. private string settingsButtonLabel;
  14. private string closeButtonLabel;
  15. private Constants.Window selectedWindow;
  16. // TODO: Find a better way of doing this
  17. public MainWindow(MeidoManager meidoManager, PropManager propManager, LightManager lightManager)
  18. {
  19. this.meidoManager = meidoManager;
  20. this.meidoManager.UpdateMeido += UpdateMeido;
  21. this.propManager = propManager;
  22. this.propManager.FromPropSelect += (_, _) =>
  23. ChangeWindow(Constants.Window.BG2);
  24. this.lightManager = lightManager;
  25. this.lightManager.Select += (_, _) =>
  26. ChangeWindow(Constants.Window.BG);
  27. windowPanes = new();
  28. WindowRect = new(Screen.width, Screen.height * 0.08f, 240f, Screen.height * 0.9f);
  29. tabsPane = new();
  30. tabsPane.TabChange += (_, _) =>
  31. ChangeTab();
  32. settingsButtonLabel = Translation.Get("settingsLabels", "settingsButton");
  33. closeButtonLabel = Translation.Get("settingsLabels", "closeSettingsButton");
  34. settingsButton = new(settingsButtonLabel);
  35. settingsButton.ControlEvent += (_, _) =>
  36. {
  37. if (selectedWindow is Constants.Window.Settings)
  38. {
  39. ChangeTab();
  40. }
  41. else
  42. {
  43. settingsButton.Label = closeButtonLabel;
  44. SetCurrentWindow(Constants.Window.Settings);
  45. }
  46. };
  47. }
  48. public override Rect WindowRect
  49. {
  50. set
  51. {
  52. value.width = 240f;
  53. value.height = Screen.height * 0.9f;
  54. if (MeidoPhotoStudio.EditMode)
  55. value.height *= 0.85f;
  56. value.x = Mathf.Clamp(value.x, 0, Screen.width - value.width);
  57. value.y = Mathf.Clamp(value.y, -value.height + 30, Screen.height - 50);
  58. windowRect = value;
  59. }
  60. }
  61. public BaseMainWindowPane this[Constants.Window id]
  62. {
  63. get => windowPanes[id];
  64. set => AddWindow(id, value);
  65. }
  66. public void AddWindow(Constants.Window id, BaseMainWindowPane window)
  67. {
  68. if (windowPanes.ContainsKey(id))
  69. Panes.Remove(windowPanes[id]);
  70. windowPanes[id] = window;
  71. windowPanes[id].SetTabsPane(tabsPane);
  72. windowPanes[id].SetParent(this);
  73. Panes.Add(windowPanes[id]);
  74. }
  75. public override void Activate()
  76. {
  77. base.Activate();
  78. updating = true;
  79. tabsPane.SelectedTab = Constants.Window.Call;
  80. updating = false;
  81. Visible = true;
  82. }
  83. public override void Update()
  84. {
  85. base.Update();
  86. if (InputManager.GetKeyDown(MpsKey.ToggleUI))
  87. Visible = !Visible;
  88. }
  89. public override void Draw()
  90. {
  91. currentWindowPane?.Draw();
  92. GUI.enabled = true;
  93. GUILayout.FlexibleSpace();
  94. var labelStyle = new GUIStyle(GUI.skin.label)
  95. {
  96. fontSize = 10,
  97. alignment = TextAnchor.LowerLeft,
  98. };
  99. GUILayout.BeginHorizontal();
  100. GUILayout.Label(MeidoPhotoStudio.PluginString, labelStyle);
  101. GUILayout.FlexibleSpace();
  102. GUI.enabled = !InputManager.Listening;
  103. settingsButton.Draw(GUILayout.ExpandWidth(false));
  104. GUI.enabled = true;
  105. GUILayout.EndHorizontal();
  106. GUI.DragWindow();
  107. }
  108. protected override void ReloadTranslation()
  109. {
  110. settingsButtonLabel = Translation.Get("settingsLabels", "settingsButton");
  111. closeButtonLabel = Translation.Get("settingsLabels", "closeSettingsButton");
  112. settingsButton.Label = selectedWindow == Constants.Window.Settings ? closeButtonLabel : settingsButtonLabel;
  113. }
  114. private void ChangeTab()
  115. {
  116. settingsButton.Label = Translation.Get("settingsLabels", "settingsButton");
  117. SetCurrentWindow(tabsPane.SelectedTab);
  118. }
  119. private void SetCurrentWindow(Constants.Window window)
  120. {
  121. if (currentWindowPane is not null)
  122. currentWindowPane.ActiveWindow = false;
  123. selectedWindow = window;
  124. currentWindowPane = windowPanes[selectedWindow];
  125. currentWindowPane.ActiveWindow = true;
  126. currentWindowPane.UpdatePanes();
  127. }
  128. private void UpdateMeido(object sender, MeidoUpdateEventArgs args)
  129. {
  130. if (args.FromMeido)
  131. {
  132. var newWindow = args.IsBody ? Constants.Window.Pose : Constants.Window.Face;
  133. ChangeWindow(newWindow);
  134. }
  135. else
  136. {
  137. currentWindowPane.UpdatePanes();
  138. }
  139. }
  140. private void ChangeWindow(Constants.Window window)
  141. {
  142. if (selectedWindow == window)
  143. currentWindowPane.UpdatePanes();
  144. else
  145. tabsPane.SelectedTab = window;
  146. Visible = true;
  147. }
  148. }