using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using DG.Tweening;
public class CltUGUI : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerDownHandler
{
RectTransform rectTrandform;
bool isMoving = true;
bool Drag = false;
Vector2 DragDelta = Vector2.zero;
Vector3 lastMouPos;
Vector3 CurMouPos;
List<RectTransform> allRect = new List<RectTransform>();
[SerializeField] int CurIndex = 1;
[SerializeField] float value;
Vector2 maxScal = Vector2.zero;
Vector2 mixScal = Vector2.zero;
Vector2 endRectPos;
[SerializeField] RectTransform CurSelet;
public void OnBeginDrag(PointerEventData eventData)
{
Drag = false;
}
public void OnDrag(PointerEventData eventData)
{
if (eventData.dragging)
{
Drag = true;
DragDelta = eventData.delta;
}
}
public void OnEndDrag(PointerEventData eventData)
{
DragDelta = Vector2.zero;
endRectPos = rectTrandform.anchoredPosition;
for (int i = 0; i < allRect.Count - 2; i++)
{
if ((endRectPos.x - (-i * (maxScal.x + 106.7f - 253))) < (maxScal.x + 106.7f) / 2)
{
value = Mathf.Clamp(-(i) * (maxScal.x + 106.7f - 253), -(allRect.Count - 3) * (maxScal.x + 600 - 253), 0f);
rectTrandform.anchoredPosition = new Vector2(value, rectTrandform.anchoredPosition.y);
CurIndex = i + 1;
}
}
CtrlSele(CurIndex);
CtrlScal();
CtrlRot(CurIndex);
}
void Start()
{
CurIndex = 1;
rectTrandform = this.transform.GetChild(0).GetComponent<RectTransform>();
foreach (RectTransform item in this.transform.GetChild(0).GetComponent<RectTransform>())
{
if (item.GetSiblingIndex() == 1)
{
item.sizeDelta = new Vector2(1858, 1254);
maxScal = item.sizeDelta;
}
else
{
item.sizeDelta = new Vector2(1605f, 1254);
mixScal = item.sizeDelta;
}
allRect.Add(item);
}
CtrlRot(CurIndex);
GlobalEntity.GetInstance().AddListener<int>(MenuEvent.ChangeMenu, ChangeMenu);
GlobalEntity.GetInstance().AddListener<bool>(MenuEvent.MainMenu, CtrlMainMenuActive);
}
// Update is called once per frame
void Update()
{
if (Drag && isMoving)
{
float Offect = rectTrandform.anchoredPosition.x;
Offect += DragDelta.x;
value = Mathf.Clamp(Offect, -(allRect.Count - 3) * (maxScal.x + 106.7f - 253), 0f);
rectTrandform.anchoredPosition = new Vector2(value, rectTrandform.anchoredPosition.y);
SliderCtrlRot();
}
MouseMoving();
}
void MouseMoving()
{
if (Input.GetMouseButtonDown(0))
{
lastMouPos = Input.mousePosition;
}
if (Input.GetMouseButton(0))
{
CurMouPos = Input.mousePosition;
if (Vector3.Distance(lastMouPos, CurMouPos) > 0)
{
isMoving = true;
}
else
{
isMoving = false;
}
lastMouPos = CurMouPos;
}
if (Input.GetMouseButtonUp(0))
{
isMoving = false;
}
}
//控制当前选择
void CtrlSele(int index)
{
index = ((index - 1) * 200) + 30;
index = Mathf.Clamp(index, 30, 1030);
Tween t = CurSelet.DOAnchorPosX(index, 0.2f);
t.OnComplete(() =>
{
t.Kill();
});
}
//大小转变
void CtrlScal()
{
for (int i = 0; i < allRect.Count; i++)
{
if (i == CurIndex)
{
Tween t = allRect[i].DOSizeDelta(maxScal, 0.2f);
t.OnComplete(() =>
{
t.Kill();
});
}
else
{
Tween t = allRect[i].DOSizeDelta(mixScal, 0.2f);
t.OnComplete(() =>
{
t.Kill();
});
}
}
}
//数字控制转换
void ChangeMenu(int index)
{
float Offect = -(index - 1) * (maxScal.x + 106.7f - 253);
CurIndex = index;
rectTrandform.anchoredPosition = new Vector2(Offect, rectTrandform.anchoredPosition.y);
CtrlScal();
CtrlSele(index);
CtrlRot(CurIndex);
}
private void OnDestroy()
{
GlobalEntity.GetInstance().RemoveListener<int>(MenuEvent.ChangeMenu, ChangeMenu);
GlobalEntity.GetInstance().RemoveListener<bool>(MenuEvent.MainMenu, CtrlMainMenuActive);
}
public void OnPointerDown(PointerEventData eventData)
{
GlobalEntity.GetInstance().Dispatch(MenuEvent.HideSecondaryNavigation);
}
void CtrlMainMenuActive(bool ison)
{
this.gameObject.SetActive(ison);
}
void CtrlRot(int index)
{
allRect[index].DORotate(new Vector3(0, 0, 0), 0.2f);
for (int i = 0; i < allRect.Count; i++)
{
if (i < CurIndex)
{
allRect[i].DORotate(new Vector3(0, -45, 0), 0.2f);
}
if (i > CurIndex)
{
allRect[i].DORotate(new Vector3(0, 45, 0), 0.2f);
}
}
}
void SliderCtrlRot()
{
Vector2 pos = rectTrandform.anchoredPosition;
float Space = maxScal.x + 106.7f - 253;
allRect[CurIndex + 1].localEulerAngles = new Vector3(0, (((CurIndex * Space - Mathf.Abs(pos.x)) / Space)) * 45, 0);
//左小右大
if (Mathf.Abs(rectTrandform.anchoredPosition.x) > (CurIndex-1) * Space)
{
//左滑
allRect[CurIndex].sizeDelta = new Vector3((((CurIndex * Space - Mathf.Abs(pos.x)) / Space) * 253) + 1605, 1254, 0);
//逐步减少
allRect[CurIndex + 1].sizeDelta = new Vector3(((1 - ((CurIndex * Space - Mathf.Abs(pos.x)) / Space)) * 253) + 1605, 1254, 0);
}
else if (Mathf.Abs(rectTrandform.anchoredPosition.x) < (CurIndex-1) * Space)
{
Debug.Log(((CurIndex * Space - Mathf.Abs(pos.x)) / Space)+"往右滑动");
allRect[CurIndex].sizeDelta = new Vector3(((2 - ((CurIndex * Space - Mathf.Abs(pos.x)) / Space)) * 253) + 1605, 1254, 0);
allRect[CurIndex-1].sizeDelta = new Vector3(((((CurIndex * Space - Mathf.Abs(pos.x)) / Space)-1) * 253) + 1605, 1254, 0);
}
allRect[CurIndex].localEulerAngles = new Vector3(0, -(1 - ((CurIndex * Space - Mathf.Abs(pos.x)) / Space)) * 45, 0);
allRect[CurIndex - 1].localEulerAngles = new Vector3(0, -(1 + (1 - ((CurIndex * Space - Mathf.Abs(pos.x)) / Space))) * 45, 0);
}
}