AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/UI/ThirdMenuRoot.cs

159 lines
5.2 KiB
C#
Raw Normal View History

2024-08-30 16:23:27 +08:00
using AxibugEmuOnline.Client.UI;
using DG.Tweening;
using System.Collections.Generic;
2024-08-22 15:16:58 +08:00
using UnityEngine;
2024-08-30 16:23:27 +08:00
using UnityEngine.UI;
2024-08-22 15:16:58 +08:00
namespace AxibugEmuOnline.Client
{
2024-08-30 16:23:27 +08:00
public class ThirdMenuRoot : SubMenuItemGroup
2024-08-22 15:16:58 +08:00
{
private RectTransform m_rect;
private RectTransform m_parent;
2024-08-30 16:23:27 +08:00
[SerializeField]
private RectTransform m_selectArrow;
2024-08-30 16:23:27 +08:00
[SerializeField]
float ArrowOffset = 50;
2024-08-22 15:16:58 +08:00
[SerializeField]
float WidthFix = 50;
2024-08-30 16:23:27 +08:00
[SerializeField]
public ItemPresent itemGroup;
[SerializeField]
ScrollRect srollRect;
public override int SelectIndex
{
get => m_selectIndex;
set
{
if (itemGroup.DataList == null) return;
value = Mathf.Clamp(value, 0, itemGroup.DataList.Count - 1);
if (m_selectIndex == value) return;
bool useAnim = m_selectIndex != -1;
m_selectIndex = value;
RollToIndex(m_selectIndex, useAnim);
OnSelectMenuChanged();
}
}
2024-08-22 15:16:58 +08:00
2024-09-12 11:02:30 +08:00
public void ResetToFirst()
{
m_selectIndex = -1;
SelectIndex = 0;
}
2024-09-11 16:33:48 +08:00
protected override MenuItem GetItemUIByIndex(int index)
{
2024-11-07 19:18:50 +08:00
return itemGroup.GetItemUIByDataIndex(index)?.GetComponent<MenuItem>();
2024-09-11 16:33:48 +08:00
}
protected override void Awake()
2024-08-22 15:16:58 +08:00
{
m_rect = transform as RectTransform;
m_parent = transform.parent as RectTransform;
2024-09-11 16:33:48 +08:00
base.Awake();
}
public override void Init(List<MenuData> menuDataList) { }
2024-09-12 15:08:48 +08:00
protected override bool OnCmdEnter()
2024-09-11 16:33:48 +08:00
{
2024-09-12 15:08:48 +08:00
var item = GetItemUIByIndex(SelectIndex);
2024-11-07 19:18:50 +08:00
if (item != null)
return item.OnEnterItem();
else
return true;
2024-09-11 16:33:48 +08:00
}
2024-09-12 15:08:48 +08:00
protected override void OnCmdBack()
2024-09-11 16:33:48 +08:00
{
2024-09-12 15:08:48 +08:00
var item = GetItemUIByIndex(SelectIndex);
2024-11-07 19:18:50 +08:00
item?.OnExitItem();
2024-08-22 15:16:58 +08:00
}
private void LateUpdate()
{
SyncRectToLaunchUI();
}
2024-08-30 16:23:27 +08:00
protected override void OnSelectMenuChanged()
{
itemGroup.UpdateDependencyProperty(this);
}
void RollToIndex(int index, bool useAnim = false)
{
SyncRectToLaunchUI();
2024-08-30 16:23:27 +08:00
Vector2 itemPos = itemGroup.GetItemAnchorePos(index);
Vector2 targetPos = itemGroup.transform.InverseTransformPoint(m_selectArrow.position);
2024-09-18 15:53:58 +08:00
Vector3[] corners = new Vector3[4];
2024-08-30 16:23:27 +08:00
itemGroup.RectTransform.GetLocalCorners(corners);
targetPos = targetPos - (Vector2)corners[1];
float gap = targetPos.y - itemPos.y;
srollRect.velocity = Vector2.zero;
if (!useAnim)
srollRect.content.anchoredPosition += new Vector2(0, gap);
else
2024-09-12 11:02:30 +08:00
{
var srcPos = m_selectArrow.position;
2024-09-12 11:02:30 +08:00
var endValue = srollRect.content.anchoredPosition + new Vector2(0, gap);
var tween = DOTween.To(
() => srollRect.content.anchoredPosition,
(x) => srollRect.content.anchoredPosition = x,
endValue, 0.125f);
2024-09-12 11:02:30 +08:00
}
2024-08-30 16:23:27 +08:00
}
2024-08-22 15:16:58 +08:00
Vector3[] corner = new Vector3[4];
private void SyncRectToLaunchUI()
{
if (LaunchUI.Instance == null) return;
var launchUIRect = LaunchUI.Instance.transform as RectTransform;
m_rect.pivot = new Vector2(1, 0.5f);
m_rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, launchUIRect.rect.width);
m_rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, launchUIRect.rect.height);
m_rect.position = launchUIRect.position;
var temp = m_rect.localPosition;
var offsetX = (m_rect.pivot.x - 0.5f) * m_rect.rect.size.x;
temp.x += offsetX;
var offsetY = (m_rect.pivot.y - 0.5f) * m_rect.rect.size.y;
temp.y += offsetY;
m_rect.localPosition = temp;
m_rect.localScale = launchUIRect.localScale;
m_parent.GetWorldCorners(corner);
var parentPosition = corner[0];
parentPosition = launchUIRect.InverseTransformPoint(parentPosition);
launchUIRect.GetWorldCorners(corner);
var rootPosition = corner[0];
rootPosition = launchUIRect.InverseTransformPoint(rootPosition);
var widthGap = parentPosition.x - rootPosition.x;
m_rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, launchUIRect.rect.width - widthGap - WidthFix);
2024-08-30 16:23:27 +08:00
m_selectArrow.position = m_parent.transform.position;
temp = m_selectArrow.anchoredPosition;
temp.x += ArrowOffset;
m_selectArrow.anchoredPosition = temp;
}
protected override void OnCmdSelectItemDown()
{
base.OnCmdSelectItemDown();
}
protected override void OnCmdSelectItemUp()
{
base.OnCmdSelectItemUp();
2024-08-22 15:16:58 +08:00
}
}
}