using UnityEngine;

public class PanelPositions : MonoBehaviour {
    public Canvas canvas;
    public RectTransform mapPanelTransform;

    public Rect MapRect {
        get {
            Vector3[] corners = new Vector3[4];
            mapPanelTransform.GetWorldCorners(corners);
            // corners: 0=bottom-left, 1=top-left, 2=top-right, 3=bottom-right

            // Convert world corners to screen space if using ScreenSpaceCamera
            if (canvas.renderMode == RenderMode.ScreenSpaceCamera && canvas.worldCamera != null) {
                for (int i = 0; i < 4; i++) {
                    corners[i] = canvas.worldCamera.WorldToScreenPoint(corners[i]);
                }
            }

            Vector3 topLeft =
                    new Vector3(corners[1].x, canvas.pixelRect.height - corners[1].y, corners[1].z);

            // Calculate size from corners (includes any parent scale from zoom)
            Vector2 scaledSize =
                    new Vector2(corners[2].x - corners[0].x, corners[1].y - corners[0].y);
            return new Rect(topLeft, scaledSize);
        }
    }
}
