MainWindow.cs 4.7 KB

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