ThreeKingdomsSlingshot/Assets/Scripts/OutGame/Arena/ArenaHubView.cs

107 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using UnityEngine.UI;
namespace TKS.OutGame
{
/// <summary>
/// 연무장 허브 화면 맞춤 — 화면(이 RectTransform) 크기가 바뀔 때마다 배율을 다시 정한다:
/// 기준 칸(표 box · 16:9 폰 1080×1920)이 화면에 다 담기는 가장 큰 배율 · 단 그림이 화면을 다 덮도록(그림 밖이 비지 않게) · 기준 칸 가운데 = 화면 가운데 →
/// 남는 쪽(세로 긴 폰 = 위아래 · 태블릿 = 좌우)은 그림이 더 보이고 좌우는 끌어서 스크롤(ScrollRect · 가로만).
/// </summary>
public class ArenaHubView : MonoBehaviour
{
public RectTransform content;
public ScrollRect scroll;
/// <summary>그림 좌표계 크기(캔버스 px).</summary>
public Vector2 canvasSize;
/// <summary>기준 칸(x · y = 왼쪽 위 · 폭 · 높이 · 캔버스 px).</summary>
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();
// 끌어서 볼 수 있는 방향 = 그림이 화면보다 큰 쪽만(가로 · 세로 따로) — 화면에 다 보이는 쪽은 움직이지 않는다
// (PD 2026-11-24 「상/하/좌/우 화면의 여백까지 스크롤 · 화면 전체가 보일 경우는 스크롤이 필요 없지만 스크롤 가능할 경우 스크롤」 · 그 전 = 가로만)
scroll.horizontal = mx > 0.5f;
scroll.vertical = my > 0.5f;
}
focusing = false;
content.anchoredPosition = new Vector2(Mathf.Clamp(ox, -mx, mx), Mathf.Clamp(oy, -my, my));
}
bool focusing;
Vector2 focusFrom, focusTo;
float focusT, focusSec;
/// <summary>그림 좌표(캔버스 px · 왼쪽 위 기준)의 한 점이 화면 가운데에 오게 한 자리(그림이 화면 밖으로 비지 않게 자름).</summary>
public Vector2 PositionFor(Vector2 canvasPoint)
{
var r = ((RectTransform)transform).rect;
float z = Zoom;
float mx = Mathf.Max(0f, (canvasSize.x * z - r.width) * 0.5f);
float my = Mathf.Max(0f, (canvasSize.y * z - r.height) * 0.5f);
float ox = (canvasSize.x * 0.5f - canvasPoint.x) * z;
float oy = (canvasPoint.y - canvasSize.y * 0.5f) * z;
return new Vector2(Mathf.Clamp(ox, -mx, mx), Mathf.Clamp(oy, -my, my));
}
/// <summary>그 점으로 화면을 옮긴다(sec 초 동안 부드럽게 · 0 = 바로) — 장소 소개(첫 실행 흐름의 군영 안내)의 포커스.</summary>
public void FocusOn(Vector2 canvasPoint, float sec)
{
if (content == null || Zoom <= 0f) return;
if (scroll != null) scroll.StopMovement();
focusFrom = content.anchoredPosition;
focusTo = PositionFor(canvasPoint);
focusT = 0f;
focusSec = sec;
focusing = sec > 0f && (focusTo - focusFrom).sqrMagnitude > 0.25f;
if (!focusing) content.anchoredPosition = focusTo;
}
/// <summary>포커스 이동이 끝났는가(테스트 · 안내가 자리 잡기를 기다림).</summary>
public bool Focusing
{
get { return focusing; }
}
void Update()
{
if (!focusing) return;
focusT += Time.unscaledDeltaTime;
float k = Mathf.Clamp01(focusT / focusSec);
k = k * k * (3f - 2f * k);
content.anchoredPosition = Vector2.Lerp(focusFrom, focusTo, k);
if (focusT >= focusSec) focusing = false;
}
}
}