MainWindow.cs 4.9 KB

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