Browse Source

Rework WindowRect property for BaseWindow

habeebweeb 4 years ago
parent
commit
718b4f0978

+ 27 - 12
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Windows/BaseWindow.cs

@@ -1,3 +1,4 @@
+using System.Collections.Generic;
 using UnityEngine;
 
 namespace COM3D2.MeidoPhotoStudio.Plugin
@@ -7,29 +8,43 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         private static int id = 765;
         private static int ID => id++;
         public readonly int windowID;
-        protected Rect windowRect;
-        public abstract Rect WindowRect { get; set; }
-
-        public BaseWindow() : base()
+        protected Rect windowRect = new Rect(0f, 0f, 480f, 270f);
+        public virtual Rect WindowRect
         {
-            windowID = ID;
+            get => windowRect;
+            set
+            {
+                value.x = Mathf.Clamp(
+                    value.x, -value.width + Utility.GetPix(20), Screen.width - Utility.GetPix(20)
+                );
+                value.y = Mathf.Clamp(
+                    value.y, -value.height + Utility.GetPix(20), Screen.height - Utility.GetPix(20)
+                );
+                windowRect = value;
+            }
         }
+        protected Vector2 MiddlePosition => new Vector2(
+            Screen.width / 2 - windowRect.width / 2,
+            Screen.height / 2 - windowRect.height / 2
+        );
+
+        public BaseWindow ModalWindow { get; private set; }
 
-        protected virtual void HandleZoom()
+        public BaseWindow() : base() => windowID = ID;
+
+        public virtual void HandleZoom()
         {
             if (Input.mouseScrollDelta.y != 0f)
             {
-                if (Visible && WindowRect.Contains(Event.current.mousePosition))
+                if (Visible)
                 {
-                    Input.ResetInputAxes();
+                    Vector2 mousePos = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
+                    if (WindowRect.Contains(mousePos)) Input.ResetInputAxes();
                 }
             }
         }
 
-        public virtual void Update()
-        {
-            HandleZoom();
-        }
+        public virtual void Update() => HandleZoom();
 
         public virtual void Activate() { }
 

+ 8 - 7
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Windows/MainWindow.cs

@@ -12,15 +12,14 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         private BaseWindowPane currentWindowPane;
         public override Rect WindowRect
         {
-            get
+            set
             {
-                windowRect.width = 240f;
-                windowRect.height = Screen.height * 0.9f;
-                windowRect.x = Mathf.Clamp(windowRect.x, 0, Screen.width - windowRect.width);
-                windowRect.y = Mathf.Clamp(windowRect.y, -windowRect.height + 30, Screen.height - 50);
-                return windowRect;
+                value.width = 240f;
+                value.height = Screen.height * 0.9f;
+                value.x = Mathf.Clamp(value.x, 0, Screen.width - value.width);
+                value.y = Mathf.Clamp(value.y, -value.height + 30, Screen.height - 50);
+                windowRect = value;
             }
-            set => windowRect = value;
         }
         private Constants.Window selectedWindow = Constants.Window.Call;
 
@@ -46,6 +45,8 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             {
                 Translation.ReloadTranslation();
             };
+            windowRect.width = 240f;
+            windowRect.height = Screen.height * 0.9f;
         }
 
         public override void Activate()

+ 7 - 16
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Windows/MessageWindow.cs

@@ -12,30 +12,21 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         private Button okButton;
         public override Rect WindowRect
         {
-            get
+            set
             {
-                windowRect.width = Mathf.Clamp(Screen.width * 0.4f, 440, Mathf.Infinity);
-                windowRect.height = Mathf.Clamp(Screen.height * 0.15f, 150, Mathf.Infinity);
-                windowRect.x = Mathf.Clamp(
-                    windowRect.x,
-                    -windowRect.width + Utility.GetPix(20),
-                    Screen.width - Utility.GetPix(20)
-                );
-                windowRect.y = Mathf.Clamp(
-                    windowRect.y,
-                    -windowRect.height + Utility.GetPix(20),
-                    Screen.height - Utility.GetPix(20)
-                );
-                return windowRect;
+                value.width = Mathf.Clamp(Screen.width * 0.4f, 440, Mathf.Infinity);
+                value.height = Mathf.Clamp(Screen.height * 0.15f, 150, Mathf.Infinity);
+                base.WindowRect = value;
             }
-            set => windowRect = value;
         }
         private int fontSize = 25;
         private bool showingMessage = false;
 
         public MessageWindow(MessageWindowManager messageWindowManager) : base()
         {
-            windowRect = new Rect(Screen.width / 2f - 220f, Screen.height - 150f, 440f, 150f);
+            WindowRect = WindowRect;
+            windowRect.x = MiddlePosition.x;
+            windowRect.y = Screen.height - WindowRect.height;
             this.messageWindowManager = messageWindowManager;
             nameTextField = new TextField();
             Controls.Add(nameTextField);