using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using DG.Tweening;
public class RotationChart2D : MonoBehaviour,IDragHandler, IEndDragHandler
{
public RadarMap radarMap;
public int num;//个数
public Image prefab;
public float spacing;//间距
public float max = 1;//最大缩放比
public float min = 0.5f;//最小缩放比
public float cutSpeed ; // 减速度
float c;//周长
float r;//半径
float ang;//角弧度
float distance;//拖动距离
List<GameObject> list = new List<GameObject>();
List<Transform> sorts = new List<Transform>();
// Start is called before the first frame update
void Start()
{
//周长
c = (spacing + prefab.rectTransform.rect.width) * num;
//半径
r = c / (2 * Mathf.PI);
ang = 2 * Mathf.PI / num;
Move();
}
public void Move()
{
float moveAng = distance / r;
for (int i = 0; i < num; i++)
{
if (list.Count<=i )
{
GameObject g = Instantiate(prefab.gameObject, transform);
g.transform.Find("Text").GetComponent<Text>().text = i.ToString();
list.Add(g);
sorts.Add(list[i].transform);
}
float x = Mathf.Sin(i * ang + moveAng) * r;
float z = Mathf.Cos(i * ang + moveAng) * r;
//位置
list[i].transform.localPosition = Vector3.right * x;
//计算缩放比
float p = (z + r) / (r + r);
float scale = (max - min) *p + min;
list[i].transform.localScale = Vector3.one * scale;
}
sorts.Sort((a, b) =>
{
if (a.localScale.x > b.localScale.x)
{
return 1;
}
else if (a.localScale.x == b.localScale.x)
{
return 0;
}
else
{
return -1;
}
});
//按层级排序
for (int i = 0; i < sorts.Count; i++)
{
sorts[i].SetSiblingIndex(i);
}
}
// Update is called once per frame
void Update()
{
}
public void OnDrag(PointerEventData eventData)
{
//eventData.delta.x每帧移动的距离
distance += eventData.delta.x;
Move();
}
public void OnEndDrag(PointerEventData eventData)
{
float speed = eventData.delta.x; //初始速度
float time = Mathf.Abs(speed / cutSpeed );//惯性时间
print("我的惯性时间是"+time);
DOTween.To((a) =>
{
distance += a;
Move();
}, speed, 0, time).OnComplete(() =>
{
float aliAng = Mathf.Asin(sorts[num - 1].localPosition.x / r);
float aliDis = aliAng * r;
float aliTime = Mathf.Abs(aliDis) / cutSpeed;
DOTween.To((a) =>
{
distance = a;
Move();
}, distance, distance - aliDis, aliTime);
//惯性结束之后调用对其
//Alignment(list.IndexOf(sorts[sorts.Count-1].gameObject));
});
}
//对齐
public void Alignment(int n)
{
//弧度
float moveAng= Mathf.Asin(sorts[sorts.Count - 1].localPosition.x / r);
//最近的下标
int index = list.IndexOf(sorts[sorts.Count - 1].gameObject);
//两个下标的差值
float a = n - index;
//要旋转的弧度
float b = num - Mathf.Abs(a);
//ab反方向 a正b负 a负b正
b = a > 0 ? -b : b;
//找较短的移动距离
float moveDis = Mathf.Abs(a) < Mathf.Abs(b) ? a : b;
//求弧长
moveAng += moveDis * ang+Mathf.Asin(sorts[num-1].transform.localPosition.x/r);
//弧长
float dis = moveAng * r;//+ c * 3 ;
//转动时间
float time = Mathf.Abs(dis / cutSpeed);
print("转动时间是" + time);
DOTween.To((aa) =>
{
distance = aa;
Move();
}, distance, distance - dis, time).OnComplete(() =>
{
//对其之后执行对应函数
//修改雷达图
for (int i = 0; i < radarMap.arr.Length; i++)
{
radarMap.arr[i] = Random.Range(0, 100);
}
});
//清理脏数据
radarMap.SetVerticesDirty();
}
}