Files
SAMKWANG/Assets/Scripts/BranchSelection/UI/Panel_BranchSelectionMap.cs
2025-11-13 18:11:49 +09:00

188 lines
6.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using WI;
namespace Samkwang
{
public class Panel_BranchSelectionMap : MonoBehaviour
{
public CanvasGroup SelectionMap;
public RectTransform Title;
public RectTransform Image_Map;
public RectTransform LocationsParent;
public Button Button_Exit;
public float fadeOutDuration = 0.8f;
public List<UI_BranchLocation> locations = new List<UI_BranchLocation>();
public UI_BranchLocation selectedLocation;
public Action<List<UI_BranchLocation>> onSetlocations;
public Action<UI_BranchLocation> onLoadBranchScene;
public Action onClickExitButton;
private static bool hasPlayedOnce = false;
private Coroutine zoomCoroutine;
public float targetZoom;
public float zoomDuration;
public float moveDuration;
public float zoomScale;
public void Init()
{
SelectionMap = transform.GetComponentInChildren<CanvasGroup>();
var rectDict = transform.GetChildComponentsByName<RectTransform>();
Title = rectDict.GetOrNull(nameof(Title));
Image_Map = rectDict.GetOrNull(nameof(Image_Map));
LocationsParent = rectDict.GetOrNull(nameof(LocationsParent));
var buttonDict = transform.GetChildComponentsByName<Button>();
Button_Exit = buttonDict.GetOrNull(nameof(Button_Exit));
locations = LocationsParent.GetComponentsInChildren<UI_BranchLocation>().ToList();
Button_Exit.onClick.AddListener(OnClickExitButton);
SetLocationButtons();
SelectionMap.alpha = hasPlayedOnce ? 1f : 0f;
}
public void FadeInSelectionMap()
{
hasPlayedOnce = true;
StopAllCoroutines();
StartCoroutine(FadeIn());
}
IEnumerator FadeIn()
{
float t = 0f;
float start = SelectionMap.alpha;
while (t < fadeOutDuration)
{
t += Time.deltaTime;
SelectionMap.alpha = Mathf.Lerp(start, 1f, t / fadeOutDuration);
yield return null;
}
SelectionMap.alpha = 1f;
}
public void Start()
{
onSetlocations?.Invoke(locations);
}
private void OnClickExitButton()
{
onClickExitButton?.Invoke();
}
private void SetLocationButtons()
{
foreach (var location in locations)
{
location.onClickBranchLocation += OnClickLocationButton;
}
}
private void OnClickLocationButton(UI_BranchLocation targetIcon)
{
if (selectedLocation != targetIcon)
{
selectedLocation = targetIcon;
selectedLocation.clickCount = 0;
if (zoomCoroutine != null)
{
StopCoroutine(zoomCoroutine);
}
zoomCoroutine = StartCoroutine(ZoomAndMove(selectedLocation.rectTransform));
selectedLocation.clickCount++;
}
else
{
if (selectedLocation.clickCount == 1)
{
if (!selectedLocation.isConversionStatus)
return;
Title.gameObject.SetActive(false);
LocationsParent.gameObject.SetActive(false);
Button_Exit.gameObject.SetActive(false);
OnDoubleClickLocationButton(selectedLocation);
onLoadBranchScene?.Invoke(selectedLocation);
selectedLocation.clickCount = 0;
}
}
}
private void OnDoubleClickLocationButton(UI_BranchLocation targetIcon)
{
if (zoomCoroutine != null)
{
StopCoroutine(zoomCoroutine);
}
var endScale = Vector3.one * targetZoom;
var worldPos = targetIcon.rectTransform.position;
RectTransformUtility.ScreenPointToLocalPointInRectangle(Image_Map, worldPos, null, out Vector2 localIconPosInParent);
Vector2 endPos = -localIconPosInParent * targetZoom;
Image_Map.localScale = endScale;
Image_Map.localPosition = endPos;
zoomCoroutine = null;
var newScale = Vector3.one * zoomScale;
foreach (var loaction in locations)
{
loaction.StopUpAndDownRoutine();
loaction.BranchNameIcon.transform.localScale = newScale;
}
}
private IEnumerator ZoomAndMove(RectTransform targetIcon)
{
var startScale = Image_Map.localScale;
var endScale = Vector3.one * targetZoom;
var startPos = Image_Map.localPosition;
var worldPos = targetIcon.position;
RectTransformUtility.ScreenPointToLocalPointInRectangle(Image_Map, worldPos, null, out Vector2 localIconPosInParent);
Vector2 endPos = -localIconPosInParent * targetZoom;
float time = 0f;
while (time < Mathf.Max(zoomDuration, moveDuration))
{
float tZoom = Mathf.Clamp01(time / zoomDuration);
float tMove = Mathf.Clamp01(time / moveDuration);
Image_Map.localScale = Vector3.Lerp(startScale, endScale, tZoom);
Image_Map.localPosition = Vector2.Lerp(startPos, endPos, tMove);
SetIconSize(tZoom);
time += Time.deltaTime;
yield return null;
}
Image_Map.localScale = endScale;
Image_Map.localPosition = endPos;
zoomCoroutine = null;
}
private void SetIconSize(float lerpScale)
{
var newScale = Vector3.one * zoomScale;
foreach (var loaction in locations)
{
loaction.BranchNameIcon.transform.localScale = Vector3.Lerp(loaction.BranchNameIcon.transform.localScale, newScale, lerpScale);
}
}
}
}