142 lines
4.0 KiB
C#
142 lines
4.0 KiB
C#
using UnityEngine;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class CombatSelectForm : UGuiForm
|
|
{
|
|
[SerializeField] private RectTransform _content;
|
|
|
|
[SerializeField] private CombatSelectBuildArea _buildArea;
|
|
|
|
[SerializeField] private CombatSelectFuncArea _funcArea;
|
|
|
|
private CombatSelectFormContext _context;
|
|
|
|
public void RefreshUI(CombatSelectFormContext context)
|
|
{
|
|
_context = context;
|
|
EnsureContentReference();
|
|
|
|
if (_content == null)
|
|
{
|
|
Log.Warning("CombatSelectForm content is missing.");
|
|
return;
|
|
}
|
|
|
|
bool isVisible = context != null && context.IsVisible;
|
|
_content.gameObject.SetActive(isVisible);
|
|
if (!isVisible)
|
|
{
|
|
_buildArea?.OnReset();
|
|
_funcArea?.OnReset();
|
|
return;
|
|
}
|
|
|
|
_content.anchoredPosition = ResolveContentAnchoredPosition(context.ScreenPosition);
|
|
|
|
if (_buildArea != null)
|
|
{
|
|
bool showBuildArea = context.ShowBuildArea;
|
|
_buildArea.gameObject.SetActive(showBuildArea);
|
|
if (showBuildArea)
|
|
{
|
|
_buildArea.OnInit(context.BuildItems);
|
|
}
|
|
else
|
|
{
|
|
_buildArea.OnReset();
|
|
}
|
|
}
|
|
|
|
if (_funcArea != null)
|
|
{
|
|
bool showFuncArea = context.ShowFuncArea;
|
|
_funcArea.gameObject.SetActive(showFuncArea);
|
|
if (showFuncArea)
|
|
{
|
|
_funcArea.OnInit(context.UpgradeItem, context.DestroyItem);
|
|
}
|
|
else
|
|
{
|
|
_funcArea.OnReset();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void OnOpen(object userData)
|
|
{
|
|
base.OnOpen(userData);
|
|
|
|
if (userData is CombatSelectFormContext context)
|
|
{
|
|
RefreshUI(context);
|
|
return;
|
|
}
|
|
|
|
Log.Warning("CombatSelectForm requires CombatSelectFormContext as userData.");
|
|
RefreshUI(null);
|
|
}
|
|
|
|
protected override void OnClose(bool isShutdown, object userData)
|
|
{
|
|
_context = null;
|
|
_buildArea?.OnReset();
|
|
_funcArea?.OnReset();
|
|
|
|
if (_content != null)
|
|
{
|
|
_content.gameObject.SetActive(false);
|
|
}
|
|
|
|
base.OnClose(isShutdown, userData);
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
EnsureContentReference();
|
|
}
|
|
|
|
private void EnsureContentReference()
|
|
{
|
|
if (_content != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Transform contentTransform = transform.Find("Content");
|
|
_content = contentTransform as RectTransform;
|
|
}
|
|
|
|
private Vector2 ResolveContentAnchoredPosition(Vector2 screenPosition)
|
|
{
|
|
if (_content == null)
|
|
{
|
|
return Vector2.zero;
|
|
}
|
|
|
|
RectTransform parentRect = _content.parent as RectTransform;
|
|
if (parentRect == null)
|
|
{
|
|
return screenPosition;
|
|
}
|
|
|
|
Canvas canvas = _content.GetComponentInParent<Canvas>();
|
|
Canvas rootCanvas = canvas != null ? canvas.rootCanvas : null;
|
|
Camera uiCamera = null;
|
|
if (rootCanvas != null && rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay)
|
|
{
|
|
uiCamera = rootCanvas.worldCamera != null ? rootCanvas.worldCamera : Camera.main;
|
|
}
|
|
|
|
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRect, screenPosition, uiCamera,
|
|
out Vector2 localPoint))
|
|
{
|
|
return Vector2.zero;
|
|
}
|
|
|
|
return localPoint;
|
|
}
|
|
}
|
|
}
|