MainWindow.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. internal class MainWindow : BaseWindow
  6. {
  7. private MeidoManager meidoManager;
  8. private Dictionary<Constants.Window, BaseWindowPane> windowPanes;
  9. private TabsPane tabsPane;
  10. private Button ReloadTranslationButton;
  11. private BaseWindowPane currentWindowPane;
  12. public override Rect WindowRect
  13. {
  14. get
  15. {
  16. windowRect.width = 240f;
  17. windowRect.height = Screen.height * 0.9f;
  18. windowRect.x = Mathf.Clamp(windowRect.x, 0, Screen.width - windowRect.width);
  19. windowRect.y = Mathf.Clamp(windowRect.y, -windowRect.height + 30, Screen.height - 50);
  20. return windowRect;
  21. }
  22. set => windowRect = value;
  23. }
  24. private Constants.Window selectedWindow = Constants.Window.Call;
  25. public BaseWindowPane this[Constants.Window id]
  26. {
  27. get => windowPanes[id];
  28. set => AddWindow(id, value);
  29. }
  30. public MainWindow(MeidoManager meidoManager) : base()
  31. {
  32. this.meidoManager = meidoManager;
  33. this.meidoManager.UpdateMeido += UpdateMeido;
  34. windowPanes = new Dictionary<Constants.Window, BaseWindowPane>();
  35. windowRect = new Rect(Screen.width, Screen.height * 0.08f, 230f, Screen.height * 0.9f);
  36. tabsPane = new TabsPane();
  37. tabsPane.TabChange += (s, a) => ChangeTab();
  38. ReloadTranslationButton = new Button("Reload Translation");
  39. ReloadTranslationButton.ControlEvent += (s, a) =>
  40. {
  41. Translation.ReloadTranslation();
  42. };
  43. }
  44. public override void Activate()
  45. {
  46. this.updating = true;
  47. tabsPane.SelectedTab = Constants.Window.Call;
  48. this.updating = false;
  49. this.Visible = true;
  50. }
  51. public void AddWindow(Constants.Window id, BaseWindowPane window)
  52. {
  53. if (windowPanes.ContainsKey(id))
  54. {
  55. Panes.Remove(windowPanes[id]);
  56. }
  57. windowPanes[id] = window;
  58. Panes.Add(windowPanes[id]);
  59. }
  60. private void ChangeTab()
  61. {
  62. this.selectedWindow = (Constants.Window)tabsPane.SelectedTab;
  63. SetCurrentWindow();
  64. }
  65. private void SetCurrentWindow()
  66. {
  67. if (currentWindowPane != null) currentWindowPane.ActiveWindow = false;
  68. currentWindowPane = windowPanes[selectedWindow];
  69. currentWindowPane.ActiveWindow = true;
  70. currentWindowPane.UpdatePanes();
  71. }
  72. public override void Update()
  73. {
  74. base.Update();
  75. if (Input.GetKeyDown(KeyCode.Tab))
  76. {
  77. this.Visible = !this.Visible;
  78. }
  79. }
  80. public override void Draw()
  81. {
  82. tabsPane.Draw();
  83. currentWindowPane?.Draw();
  84. GUI.enabled = true;
  85. GUILayout.FlexibleSpace();
  86. ReloadTranslationButton.Draw();
  87. GUIStyle labelStyle = new GUIStyle(GUI.skin.label);
  88. labelStyle.fontSize = 10;
  89. labelStyle.alignment = TextAnchor.LowerLeft;
  90. GUILayout.Label(MeidoPhotoStudio.pluginString, labelStyle);
  91. GUI.DragWindow();
  92. }
  93. private void UpdateMeido(object sender, MeidoUpdateEventArgs args)
  94. {
  95. if (args.FromMeido)
  96. {
  97. Constants.Window newWindow = args.IsBody ? Constants.Window.Pose : Constants.Window.Face;
  98. if (this.selectedWindow == newWindow) currentWindowPane.UpdatePanes();
  99. else tabsPane.SelectedTab = newWindow;
  100. }
  101. else currentWindowPane.UpdatePanes();
  102. }
  103. }
  104. }