RollList.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using UnityEngine.EventSystems;
  4. using SC.XR.Unity.Module_InputSystem;
  5. public class RollList : BaseList, IDragHandler, IPointerEnterHandler, IPointerExitHandler, IPointerUpHandler, IPointerDownHandler
  6. {
  7. private float moveTime=0.2f;
  8. public List<GameObject> controlPoints;
  9. // public LineRenderer lineRenderer;
  10. public bool isFixed;
  11. Vector3 v1;
  12. Vector3 v2;
  13. Vector3 v3;
  14. [HideInInspector]
  15. private int startItem = 0;
  16. [HideInInspector]
  17. private int endItem = 0;
  18. //private j
  19. private float speedTime = 0;
  20. private float dragTime = 0.2f;
  21. private float clickTime = 0.2f;
  22. private float lastClickTime;
  23. private float lastDragTime;
  24. public virtual void Update()
  25. {
  26. if (isJiasudu)
  27. {
  28. speedTime = speedTime + Time.deltaTime;
  29. if(speedTime>3)
  30. {
  31. speedTime = 0;
  32. isJiasudu = false;
  33. fixedItems();
  34. }
  35. jiasudu = jiasudu / 1.4f;
  36. Move(jiasudu);
  37. }
  38. DrawCurve();
  39. if(eventEnterData!=null)
  40. {
  41. RollView rv = OnCheckView(eventEnterData);
  42. if(rv==null&& chooseView!=null)
  43. {
  44. chooseView.OnPointerExit(eventEnterData);
  45. // chooseView = null;
  46. }
  47. if (chooseView != rv&& chooseView!=null)
  48. {
  49. if (rv != null)
  50. {
  51. chooseView.OnPointerExit(eventEnterData);
  52. chooseView = rv;
  53. chooseView.OnPointerEnter(eventEnterData);
  54. }
  55. };
  56. }
  57. }
  58. public override void initList(List<BaseConfig> c)
  59. {
  60. base.initList(c);
  61. DrawCurve();
  62. Column = 1;
  63. startItem = 0;
  64. endItem = Row * Column;
  65. }
  66. float time = 1f;
  67. void Move(float f)
  68. {
  69. if (Mathf.Abs(f) >= 1)
  70. {
  71. if (f > 0)
  72. {
  73. f = 1;
  74. }
  75. else
  76. {
  77. f = -1;
  78. }
  79. }
  80. if (Mathf.Abs(f) <= 1)
  81. {
  82. for (int i = 0; i < Views.Count; i++)
  83. {
  84. if (f > 0)
  85. {
  86. if (i + 1 < Views.Count)
  87. {
  88. // LeanTween.moveLocal(itemView[i], pos[i+1], time);
  89. // Views[i].transform.localPosition = Vector3.Lerp(Views[i].transform.localPosition, pos[i + 1], Mathf.Abs(f));
  90. Views[i].transform.localPosition = Views[i].transform.localPosition + (pos[i + 1] - Views[i].transform.localPosition) * f;
  91. // Views[i].gameObject.SetActive(true);
  92. }
  93. else
  94. {
  95. Views[i].transform.localPosition = Views[i].transform.localPosition + (pos[i + 1] - Views[i].transform.localPosition) * f;
  96. // Views[i].gameObject.SetActive(false);
  97. }
  98. }
  99. else
  100. {
  101. if ((i - 1) >= 0)
  102. {
  103. Views[i].transform.localPosition = Views[i].transform.localPosition - (pos[i - 1] - Views[i].transform.localPosition) * f;
  104. // Views[i].gameObject.SetActive(true);
  105. }
  106. else
  107. {
  108. Views[i].transform.localPosition = Views[i].transform.localPosition - (pos[i - 1] - Views[i].transform.localPosition) * f;
  109. // Views[i].gameObject.SetActive(false);
  110. }
  111. }
  112. }
  113. }
  114. if (f > 0)
  115. {
  116. if (Vector3.Distance(Views[0].transform.localPosition, pos[1]) < (Vector3.Distance(pos[0], pos[1]) / 10))
  117. {
  118. moveRightEnd();
  119. }
  120. }
  121. else if (f < 0)
  122. {
  123. if (Vector3.Distance(Views[1].transform.localPosition, pos[0]) < (Vector3.Distance(pos[0], pos[1]) / 10))
  124. {
  125. moveleftEnd();
  126. }
  127. }
  128. // Invoke("moveEnd", time);
  129. }
  130. void getShowItem()
  131. {
  132. List<int> showItemS = new List<int>();
  133. int ct=0;
  134. int vCt = 0;
  135. if (Views.Count > configs.Count)
  136. {
  137. vCt = Views.Count;
  138. }
  139. else
  140. {
  141. vCt = configs.Count;
  142. }
  143. // Debug.Log("滚动次数===》"+ rCount);
  144. for (int i = rCount; i < Views.Count+ rCount; i++)
  145. {
  146. ct = i;
  147. if (i<0)
  148. {
  149. ct = vCt - Mathf.Abs(i);
  150. }
  151. else if(i >= vCt)
  152. {
  153. ct = i - vCt;
  154. }
  155. // Debug.Log(ct);
  156. showItemS.Add(ct);
  157. }
  158. showItems(showItemS);
  159. }
  160. void moveleftEnd(bool Tween=false)
  161. {
  162. rCount++;
  163. if(Mathf.Abs(rCount)>=configs.Count)
  164. {
  165. rCount = 0;
  166. }
  167. List<BaseView> controlPoints2 = new List<BaseView>();
  168. Views[0].transform.localPosition = pos[Views.Count - 1];
  169. for (int i = 0; i < Views.Count; i++)
  170. {
  171. if (i == (Views.Count - 1))
  172. {
  173. if(Tween)
  174. {
  175. LeanTween.moveLocal(Views[0].gameObject, pos[i], moveTime);
  176. }
  177. else
  178. {
  179. Views[0].transform.localPosition = pos[i];
  180. }
  181. controlPoints2.Add(Views[0]);
  182. }
  183. else
  184. {
  185. if (Tween)
  186. {
  187. LeanTween.moveLocal(Views[i + 1].gameObject, pos[i], moveTime);
  188. }
  189. else
  190. {
  191. Views[i + 1].transform.localPosition = pos[i];
  192. }
  193. controlPoints2.Add(Views[i + 1]);
  194. }
  195. }
  196. Views = controlPoints2;
  197. controlPoints2[Views.Count - 1].gameObject.SetActive(true);
  198. getShowItem();
  199. // Move();
  200. }
  201. void moveRightEnd(bool Tween = false)
  202. {
  203. rCount--;
  204. if (Mathf.Abs(rCount) >= configs.Count)
  205. {
  206. rCount = 0;
  207. }
  208. List<BaseView> controlPoints2 = new List<BaseView>();
  209. Views[Views.Count - 1].transform.localPosition = pos[0];
  210. for (int i = 0; i < Views.Count; i++)
  211. {
  212. if (i == 0)
  213. {
  214. if (Tween)
  215. {
  216. LeanTween.moveLocal(Views[Views.Count - 1].gameObject, pos[i], moveTime);
  217. }else
  218. {
  219. Views[Views.Count - 1].transform.localPosition = pos[i];
  220. }
  221. controlPoints2.Add(Views[Views.Count - 1]);
  222. }
  223. else
  224. {
  225. if (Tween)
  226. {
  227. LeanTween.moveLocal(Views[i - 1].gameObject, pos[i], moveTime);
  228. }
  229. else
  230. {
  231. Views[i - 1].transform.localPosition = pos[i];
  232. }
  233. controlPoints2.Add(Views[i - 1]);
  234. }
  235. }
  236. Views = controlPoints2;
  237. controlPoints2[0].gameObject.SetActive(true);
  238. getShowItem();
  239. }
  240. bool isStart=false;
  241. public Dictionary<int, Vector3> pos = new Dictionary<int, Vector3>();
  242. // List<Vector3> pos = new List<Vector3>();
  243. void DrawCurve()
  244. {
  245. if (controlPoints.Count > 0)
  246. {
  247. v1 = controlPoints[0].transform.localPosition;
  248. v2 = controlPoints[1].transform.localPosition;
  249. v3 = controlPoints[2].transform.localPosition;
  250. pos = new Dictionary<int, Vector3>();
  251. int count = -1;
  252. for (int i = 0; i < Row+2; i++)
  253. {
  254. float t = i / ((float)Row+2);
  255. Vector3 pixel = CalculateCubicBezierPoint(t, v1, v2, v3);
  256. pos.Add(count, pixel);
  257. if (!isStart&& Views.Count>(i-1)&&i>=1)
  258. Views[i-1].transform.localPosition = pos[count];
  259. count++;
  260. // itemView[i].transform.localPosition = pixel;
  261. }
  262. }
  263. isStart = true;
  264. }
  265. Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
  266. {
  267. float u = 1 - t;
  268. float tt = t * t;
  269. float uu = u * u;
  270. Vector3 p = uu * p0;
  271. p += 2 * u * t * p1;
  272. p += tt * p2;
  273. return p;
  274. }
  275. bool isMove;
  276. private Vector3 startPos;
  277. private float oldNum=0;
  278. public virtual void OnDrag(PointerEventData eventData)
  279. {
  280. if ((Time.time - lastClickTime) < clickTime)
  281. {
  282. return;
  283. }
  284. isMove = true;
  285. SCPointEventData sCPointEventData = eventData as SCPointEventData;
  286. float num = 0;
  287. Vector3 pos3d = sCPointEventData.Position3D / this.transform.localScale.x;
  288. num = (startPos.x - pos3d.x);
  289. num = Vector3.Cross(this.transform.forward, pos3d).y- oldNum;
  290. oldNum = Vector3.Cross(this.transform.forward, pos3d).y;
  291. Vector3 a = GSXRManager.Instance.leftCamera.WorldToScreenPoint(startScreenV3);
  292. Vector3 b = GSXRManager.Instance.leftCamera.WorldToScreenPoint(sCPointEventData.Position3D);
  293. num = (a - b).x;
  294. startPos = pos3d;
  295. startScreenV3 = sCPointEventData.Position3D;
  296. if(Mathf.Abs(num)>10)
  297. {
  298. if(num>0)
  299. num = 10;
  300. else
  301. {
  302. num = -10;
  303. }
  304. }
  305. else if(Mathf.Abs(num) < 2f)
  306. {
  307. num = 0;
  308. }
  309. Move(-num / 50);
  310. jTime = jTime + Time.deltaTime;
  311. if(jTime>0.3f)
  312. {
  313. jTime = 0;
  314. jiasudu = 0;
  315. }
  316. else
  317. {
  318. jiasudu = jiasudu - num/50;
  319. }
  320. }
  321. PointerEventData eventEnterData;
  322. public virtual void OnPointerEnter(PointerEventData eventData)
  323. {
  324. eventEnterData = eventData;
  325. chooseView = OnCheckView(eventData);
  326. if(chooseView!=null)
  327. chooseView.OnPointerEnter(eventData);
  328. }
  329. public virtual void OnPointerExit(PointerEventData eventData)
  330. {
  331. eventEnterData = null;
  332. if (chooseView!=null)
  333. {
  334. chooseView.OnPointerExit(eventData);
  335. chooseView = null;
  336. }
  337. }
  338. public RollView OnCheckView(PointerEventData eventData)
  339. {
  340. if(isMove)
  341. {
  342. return null;
  343. }
  344. SCPointEventData sCPointEventData = eventData as SCPointEventData;
  345. Vector3 v3event = sCPointEventData.inputDevicePartBase.detectorBase.pointerBase.cursorBase.transform.position;
  346. RollView baseView = null;
  347. float distanceMin=10;
  348. for (int i = 0; i < Views.Count; i++)
  349. {
  350. Vector3 v3 = this.transform.InverseTransformPoint(Views[i].transform.position);
  351. v3 = new Vector3(v3.x, v3.y, 0);
  352. v3 = this.transform.TransformPoint(v3);
  353. float disView = Vector3.Distance(v3event, v3);
  354. if(distanceMin> disView)
  355. {
  356. distanceMin = disView;
  357. baseView =(RollView)Views[i];
  358. }
  359. }
  360. return baseView;
  361. }
  362. public virtual void OnPointerUp(PointerEventData eventData)
  363. {
  364. if (jiasudu!=0)
  365. isJiasudu = true;
  366. else
  367. {
  368. fixedItems();
  369. }
  370. if (Time.time - lastDragTime < dragTime)
  371. {
  372. if (eventData.pointerEnter)
  373. {
  374. var tmpbutton = eventData.pointerEnter.GetComponentInParent<IPointerClickHandler>();
  375. if (tmpbutton != null)
  376. {
  377. tmpbutton.OnPointerClick(eventData);
  378. }
  379. }
  380. }
  381. if (chooseView != null)
  382. {
  383. if ((Time.time - lastClickTime)< clickTime)
  384. {
  385. chooseView.OnClick(eventData);
  386. }
  387. chooseView.OnPointerUp(eventData);
  388. // chooseView = null;
  389. }
  390. isMove = false;
  391. }
  392. private void fixedItems()
  393. {
  394. if(isFixed&&!isJiasudu)
  395. {
  396. if (Vector3.Distance(Views[1].transform.localPosition, pos[1]) > Vector3.Distance(pos[1], pos[2])/2f)
  397. {
  398. moveleftEnd(true);
  399. }
  400. if (Vector3.Distance(Views[1].transform.localPosition, pos[1]) > Vector3.Distance(pos[0], pos[1]) / 2f)
  401. {
  402. moveRightEnd(true);
  403. }
  404. else
  405. {
  406. // Debug.Log("滚动次数===》"+ rCount);
  407. for (int i = 0; i < Views.Count; i++)
  408. {
  409. LeanTween.moveLocal(Views[i].gameObject, pos[i], 0.3f);
  410. //Views[i].transform.localPosition = pos[i];
  411. }
  412. }
  413. }
  414. }
  415. private float jiasudu;
  416. private float jTime;
  417. private bool isJiasudu;
  418. private Vector3 startScreenV3;
  419. private RollView chooseView;
  420. public virtual void OnPointerDown(PointerEventData eventData)
  421. {
  422. SCPointEventData sCPointEventData = eventData as SCPointEventData;
  423. Vector3 pos3d = sCPointEventData.Position3D / this.transform.localScale.x;
  424. startScreenV3 = sCPointEventData.Position3D;
  425. // num = Vector3.Cross(this.transform.forward, pos3d).y;
  426. startPos = pos3d;
  427. isJiasudu = false;
  428. speedTime = 0;
  429. lastDragTime = Time.time;
  430. lastClickTime = Time.time;
  431. chooseView = OnCheckView(eventData);
  432. if(chooseView!=null)
  433. chooseView.OnPointerDown(eventData);
  434. }
  435. public override void updateConfig(List<BaseConfig> c)
  436. {
  437. base.updateConfig(c);
  438. if (this.configs.Count < c.Count)
  439. {
  440. configs = new List<BaseConfig>();
  441. c.ForEach(i => configs.Add(i));
  442. if(c.Count> Views.Count)
  443. {
  444. rCount = c.Count - Views.Count;
  445. }
  446. getShowItem();
  447. }
  448. else if(rCount>(c.Count - 1))
  449. {
  450. configs = new List<BaseConfig>();
  451. c.ForEach(i => configs.Add(i));
  452. if (c.Count > Views.Count)
  453. {
  454. rCount = c.Count - Views.Count;
  455. }
  456. getShowItem();
  457. }
  458. else
  459. {
  460. configs = new List<BaseConfig>();
  461. c.ForEach(i => configs.Add(i));
  462. getShowItem();
  463. }
  464. }
  465. }
  466. public class BezierUtils
  467. {
  468. /// <summary>
  469. /// 根据T值,计算贝塞尔曲线上面相对应的点
  470. /// </summary>
  471. /// <param name="t"></param>T值
  472. /// <param name="p0"></param>起始点
  473. /// <param name="p1"></param>控制点
  474. /// <param name="p2"></param>目标点
  475. /// <returns></returns>根据T值计算出来的贝赛尔曲线点
  476. private static Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
  477. {
  478. float u = 1 - t;
  479. float tt = t * t;
  480. float uu = u * u;
  481. Vector3 p = uu * p0;
  482. p += 2 * u * t * p1;
  483. p += tt * p2;
  484. return p;
  485. }
  486. /// <summary>
  487. /// 获取存储贝塞尔曲线点的数组
  488. /// </summary>
  489. /// <param name="startPoint"></param>起始点
  490. /// <param name="controlPoint"></param>控制点
  491. /// <param name="endPoint"></param>目标点
  492. /// <param name="segmentNum"></param>采样点的数量
  493. /// <returns></returns>存储贝塞尔曲线点的数组
  494. public static Vector3[] GetBeizerList(Vector3 startPoint, Vector3 controlPoint, Vector3 endPoint, int segmentNum)
  495. {
  496. Vector3[] path = new Vector3[segmentNum];
  497. for (int i = 1; i <= segmentNum; i++)
  498. {
  499. float t = i / (float)segmentNum;
  500. Vector3 pixel = CalculateCubicBezierPoint(t, startPoint,
  501. controlPoint, endPoint);
  502. path[i - 1] = pixel;
  503. Debug.Log(path[i - 1]);
  504. }
  505. return path;
  506. }
  507. }