BaseWindow.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace MeidoPhotoStudio.Plugin;
  4. public abstract class BaseWindow : BasePane
  5. {
  6. public readonly int WindowID = ID;
  7. protected readonly List<BasePane> Panes = new();
  8. protected Vector2 scrollPos;
  9. protected Rect windowRect = new(0f, 0f, 480f, 270f);
  10. private static int id = 765;
  11. public bool ActiveWindow { get; set; }
  12. public virtual Rect WindowRect
  13. {
  14. get => windowRect;
  15. set
  16. {
  17. value.x =
  18. Mathf.Clamp(value.x, -value.width + Utility.GetPix(20), Screen.width - Utility.GetPix(20));
  19. value.y =
  20. Mathf.Clamp(value.y, -value.height + Utility.GetPix(20), Screen.height - Utility.GetPix(20));
  21. windowRect = value;
  22. }
  23. }
  24. protected Vector2 MiddlePosition =>
  25. new((float)Screen.width / 2 - windowRect.width / 2, (float)Screen.height / 2 - windowRect.height / 2);
  26. private static int ID =>
  27. id++;
  28. public virtual void Update() =>
  29. HandleZoom();
  30. public virtual void GUIFunc(int id)
  31. {
  32. Draw();
  33. GUI.DragWindow();
  34. }
  35. public virtual void UpdatePanes()
  36. {
  37. foreach (var pane in Panes)
  38. pane.UpdatePane();
  39. }
  40. public override void SetParent(BaseWindow window)
  41. {
  42. foreach (var pane in Panes)
  43. pane.SetParent(window);
  44. }
  45. public override void Activate()
  46. {
  47. base.Activate();
  48. foreach (var pane in Panes)
  49. pane.Activate();
  50. }
  51. public override void Deactivate()
  52. {
  53. base.Deactivate();
  54. foreach (var pane in Panes)
  55. pane.Deactivate();
  56. }
  57. protected T AddPane<T>(T pane)
  58. where T : BasePane
  59. {
  60. Panes.Add(pane);
  61. pane.SetParent(this);
  62. return pane;
  63. }
  64. private void HandleZoom()
  65. {
  66. if (Input.mouseScrollDelta.y is 0f || !Visible)
  67. return;
  68. var mousePos = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
  69. if (WindowRect.Contains(mousePos))
  70. Input.ResetInputAxes();
  71. }
  72. }