using UnityEngine;
using UnityEngine.UI;
namespace TKS.OutGame
{
///
/// 연무장 허브 화면 맞춤 — 화면(이 RectTransform) 크기가 바뀔 때마다 배율을 다시 정한다:
/// 기준 칸(표 box · 16:9 폰 1080×1920)이 화면에 다 담기는 가장 큰 배율 · 단 그림이 화면을 다 덮도록(그림 밖이 비지 않게) · 기준 칸 가운데 = 화면 가운데 →
/// 남는 쪽(세로 긴 폰 = 위아래 · 태블릿 = 좌우)은 그림이 더 보이고 좌우는 끌어서 스크롤(ScrollRect · 가로만).
///
public class ArenaHubView : MonoBehaviour
{
public RectTransform content;
public ScrollRect scroll;
/// 그림 좌표계 크기(캔버스 px).
public Vector2 canvasSize;
/// 기준 칸(x · y = 왼쪽 위 · 폭 · 높이 · 캔버스 px).
public Rect box;
Vector2 lastSize;
public float Zoom { get; private set; }
void OnEnable()
{
Fit(true);
}
void OnRectTransformDimensionsChange()
{
Fit(false);
}
public void Fit(bool force)
{
if (content == null || canvasSize.x < 1f || canvasSize.y < 1f || box.width < 1f || box.height < 1f) return;
var r = ((RectTransform)transform).rect;
if (r.width < 1f || r.height < 1f) return;
if (!force && (r.size - lastSize).sqrMagnitude < 0.01f) return;
lastSize = r.size;
float z = Mathf.Max(Mathf.Min(r.width / box.width, r.height / box.height), Mathf.Max(r.width / canvasSize.x, r.height / canvasSize.y));
Zoom = z;
content.localScale = new Vector3(z, z, 1f);
// 기준 칸 가운데를 화면 가운데로 — 그림이 화면 밖으로 비지 않게 자름
float ox = (canvasSize.x * 0.5f - box.center.x) * z;
float oy = (box.center.y - canvasSize.y * 0.5f) * z;
float mx = Mathf.Max(0f, (canvasSize.x * z - r.width) * 0.5f);
float my = Mathf.Max(0f, (canvasSize.y * z - r.height) * 0.5f);
if (scroll != null) scroll.StopMovement();
content.anchoredPosition = new Vector2(Mathf.Clamp(ox, -mx, mx), Mathf.Clamp(oy, -my, my));
}
}
}