TransferCell.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. using DG.Tweening;
  2. using SC.XR.Unity.Module_InputSystem;
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using XRTool.Util;
  8. using XRTool.WorldUI;
  9. using System.Collections;
  10. using UnityEditor;
  11. using ShadowStudio.Model;
  12. using UnityEngine.UIElements;
  13. using static BoundingBox;
  14. namespace ShadowStudio.Tool
  15. {
  16. /// <summary>
  17. /// 编辑状态
  18. /// </summary>
  19. public enum TransferState
  20. {
  21. /// <summary>
  22. /// 克隆状态,此状态不同,仅克隆物体时使用此作为参数
  23. /// 克隆完成后,状态自动切换
  24. /// </summary>
  25. Clone = -1,
  26. /// <summary>
  27. /// 同步时不存在此状态,仅做初始化处理
  28. /// </summary>
  29. Init = 0,
  30. /// <summary>
  31. /// 状态同步开始
  32. /// </summary>
  33. Start = 1,
  34. /// <summary>
  35. /// 状态同步执行中
  36. /// </summary>
  37. Doing = 2,
  38. /// <summary>
  39. /// 状态同步结束,但是同步并没有完成
  40. /// </summary>
  41. Stop = 3,
  42. /// <summary>
  43. /// 状态同步结束,不再对此数据进行同步
  44. /// </summary>
  45. Over = 4,
  46. }
  47. [RequireComponent(typeof(XBoundingBox))]
  48. [RequireComponent(typeof(XDragComponent))]
  49. /// <summary>
  50. /// 物体姿态控制器
  51. /// 选中状态,非选中状态
  52. /// 默认所有物体都是非选中状态
  53. /// 当处于选中状态时,此物体只有选中人可编辑,同时将此物体的状态进行同步,其他人能看到选中的状态
  54. /// </summary>
  55. public class TransferCell : MonoBehaviour, IPointerDownHandler
  56. {
  57. [SerializeField]
  58. /// <summary>
  59. /// 是否垂直水平面
  60. /// </summary>
  61. private bool isLookHead;
  62. /// <summary>
  63. /// 拖拽事件
  64. /// </summary>
  65. public event Action<TransferState> DragTransfer;
  66. /// <summary>
  67. /// 缩放事件
  68. /// </summary>
  69. public event Action<TransferState> ZoomTransfer;
  70. /// <summary>
  71. /// 旋转事件
  72. /// </summary>
  73. public event Action<TransferState> RoteTransfer;
  74. /// <summary>
  75. /// 编辑事件
  76. /// 当物体进行拖拽,缩放,旋转时都会触发
  77. /// </summary>
  78. public event Action<TransferState> EditTransfer;
  79. public event Action<TransferState> TransferStateChange;
  80. public HandleType normalHandType = HandleType.Rotation | HandleType.Scale;
  81. public HandleType editorHandType = HandleType.Rotation | HandleType.Scale | HandleType.AxisScale;
  82. /// <summary>
  83. /// 选中和非选中的事件
  84. /// </summary>
  85. //public event Action<TransferCell, bool> TransferSelect;
  86. public static TransferCell curActiveCell;
  87. private XDragComponent dragCell;
  88. private XBoundingBox boundBox;
  89. private Coroutine editCoroutine;
  90. private bool isLock;
  91. private TransferState transferState = TransferState.Init;
  92. private bool isCanEditor = true;
  93. private bool isInitBound = false;
  94. /// <summary>
  95. /// 编辑模式
  96. /// -1代表未编辑
  97. /// 0代表拖拽
  98. /// 1代表旋转
  99. /// 2代表缩放
  100. /// 开始前/结束后要设置为-1
  101. /// 开始后到执行中设置对应的状态
  102. /// </summary>
  103. private int editType = -1;
  104. private Timer dragTimer;
  105. public float autoDisDrag = 5f;
  106. private Stack<Posture> postureHistroy;
  107. private Stack<Posture> postureFuture;
  108. private GameObject boundCell;
  109. private bool isSelect;
  110. [SerializeField]
  111. private bool isDragEnable;
  112. private bool isInit = false;
  113. public event Action TransferDisEnable;
  114. /// <summary>
  115. /// 后退的栈
  116. /// </summary>
  117. public Stack<Posture> PostureBack
  118. {
  119. get
  120. {
  121. if (postureHistroy == null)
  122. {
  123. postureHistroy = new Stack<Posture>();
  124. }
  125. return postureHistroy;
  126. }
  127. }
  128. /// <summary>
  129. /// 前进的栈
  130. /// </summary>
  131. public Stack<Posture> PostureAhead
  132. {
  133. get
  134. {
  135. if (postureFuture == null)
  136. {
  137. postureFuture = new Stack<Posture>();
  138. }
  139. return postureFuture;
  140. }
  141. }
  142. /// <summary>
  143. /// 锁定
  144. /// </summary>
  145. public bool IsLock
  146. {
  147. get
  148. {
  149. return isLock;
  150. }
  151. set
  152. {
  153. isLock = value;
  154. if (isLock)
  155. {
  156. PostureAhead.Clear();
  157. PostureBack.Clear();
  158. IsDragEnable = false;
  159. SetBoundActive(true);
  160. SetBoundColor(value, Color.yellow);
  161. }
  162. else
  163. {
  164. SetBoundColor(value, Color.white);
  165. if (curActiveCell != this)
  166. {
  167. SetBoundActive(false);
  168. }
  169. }
  170. }
  171. }
  172. /// <summary>
  173. /// 拖拽组件
  174. /// </summary>
  175. public XDragComponent DragCell
  176. {
  177. get
  178. {
  179. if (!dragCell)
  180. {
  181. dragCell = GetComponent<XDragComponent>();
  182. dragCell.PointerDrag.AddListener(OnPointerDrag);
  183. //dragCell.PointerDown.AddListener(OnPointerDown);
  184. dragCell.PointerUp.AddListener(OnPointerUp);
  185. }
  186. return dragCell;
  187. }
  188. }
  189. /// <summary>
  190. /// 包围盒显示对象
  191. /// </summary>
  192. public GameObject BoundCell
  193. {
  194. get
  195. {
  196. if (!boundCell)
  197. {
  198. boundCell = UnityUtil.GetBreadthChild(transform, "XRTool.Util.XBoundingBox");
  199. }
  200. return boundCell;
  201. }
  202. }
  203. /// <summary>
  204. /// 包围盒算法组件
  205. /// </summary>
  206. public XBoundingBox BoundBox
  207. {
  208. get
  209. {
  210. if (!boundBox)
  211. {
  212. boundBox = GetComponent<XBoundingBox>();
  213. if (!boundBox)
  214. {
  215. UnityLog.LogError(gameObject.name + "this is no XBoundingBox");
  216. }
  217. else
  218. {
  219. DragTransfer += state => { EditTransfer?.Invoke(state); };
  220. ZoomTransfer += state => { EditTransfer?.Invoke(state); };
  221. RoteTransfer += state => { EditTransfer?.Invoke(state); };
  222. EditTransfer += OnEditTransfer;
  223. boundBox.ScaleStarted.AddListener(OnScaleStarted);
  224. boundBox.Scaling.AddListener(OnScaling);
  225. boundBox.ScaleStopped.AddListener(OnScaleStopped);
  226. boundBox.RotateStarted.AddListener(OnRotateStarted);
  227. boundBox.Rotating.AddListener(OnRotating);
  228. boundBox.RotateStopped.AddListener(OnRotateStopped);
  229. }
  230. }
  231. return boundBox;
  232. }
  233. }
  234. private void OnRotating()
  235. {
  236. RoteTransfer?.Invoke(TransferState.Doing);
  237. }
  238. private void OnScaling()
  239. {
  240. ZoomTransfer?.Invoke(TransferState.Doing);
  241. }
  242. public void SetBoundActive(bool isShow)
  243. {
  244. if (BoundCell)
  245. {
  246. BoundCell.SetActive(isShow);
  247. }
  248. }
  249. /// <summary>
  250. /// 初始化
  251. /// </summary>
  252. private void Start()
  253. {
  254. if (BoundBox)
  255. {
  256. TimerMgr.Instance.CreateTimer(() =>
  257. {
  258. IsDragEnable = false;
  259. IsInit = true;
  260. }, 0);
  261. }
  262. }
  263. /// <summary>
  264. /// 当编辑物体的时候
  265. /// </summary>
  266. /// <param name="state"></param>
  267. public void OnEditTransfer(TransferState state)
  268. {
  269. if (state == TransferState.Start && editType != -1)
  270. {
  271. ClearTime();
  272. AddBackNode(UnityUtil.GetPosture(transform));
  273. }
  274. if (state == TransferState.Stop)
  275. {
  276. ClearTime();
  277. if (autoDisDrag > 0)
  278. {
  279. dragTimer = TimerMgr.Instance.CreateTimer(() => { IsDragEnable = false; }, autoDisDrag);
  280. }
  281. }
  282. TransferState = state;
  283. }
  284. /// <summary>
  285. /// 清除协程
  286. /// </summary>
  287. private void ClearEditor()
  288. {
  289. if (editCoroutine != null)
  290. {
  291. StopCoroutine(editCoroutine);
  292. editCoroutine = null;
  293. }
  294. }
  295. /// <summary>
  296. /// 旋转结束
  297. /// </summary>
  298. public void OnRotateStopped()
  299. {
  300. ClearEditor();
  301. editType = 1;
  302. RoteTransfer?.Invoke(TransferState.Stop);
  303. editType = -1;
  304. }
  305. /// <summary>
  306. /// 旋转开始
  307. /// </summary>
  308. private void OnRotateStarted()
  309. {
  310. ClearEditor();
  311. editType = 1;
  312. RoteTransfer?.Invoke(TransferState.Start);
  313. //editCoroutine = StartCoroutine(EditorCoroutine());
  314. }
  315. /// <summary>
  316. /// 缩放结束
  317. /// </summary>
  318. public void OnScaleStopped()
  319. {
  320. ClearEditor();
  321. editType = 2;
  322. ZoomTransfer?.Invoke(TransferState.Stop);
  323. editType = -1;
  324. TimerMgr.Instance.CreateTimer(RemoveBound, 0.01f);
  325. }
  326. /// <summary>
  327. /// 缩放开始
  328. /// </summary>
  329. private void OnScaleStarted()
  330. {
  331. ClearEditor();
  332. editType = 2;
  333. ZoomTransfer?.Invoke(TransferState.Start);
  334. //editCoroutine = StartCoroutine(EditorCoroutine());
  335. }
  336. /// <summary>
  337. /// 编辑的协程
  338. /// </summary>
  339. /// <returns></returns>
  340. //private IEnumerator EditorCoroutine()
  341. //{
  342. // while (editType != -1)
  343. // {
  344. // yield return new WaitForFixedUpdate();
  345. // yield return null;
  346. // if (editType == 1)
  347. // {
  348. // RoteTransfer?.Invoke(TransferState.Doing);
  349. // }
  350. // else if (editType == 2)
  351. // {
  352. // ZoomTransfer?.Invoke(TransferState.Doing);
  353. // }
  354. // }
  355. //}
  356. /// <summary>
  357. /// 当按下的时候
  358. /// 如果物体没有被锁定,处于可操作的状态时,显示操作框
  359. /// </summary>
  360. /// <param name="eventData"></param>
  361. public void OnPointerDown(PointerEventData eventData)
  362. {
  363. if (IsLock || (SCInputModule.Instance && !SCInputModule.Instance.CanDrag) || !IsCanEditor)
  364. {
  365. return;
  366. }
  367. transform.DOKill();
  368. IsDragEnable = true;
  369. if (!isInitBound)
  370. {
  371. RemoveBound();
  372. }
  373. if (BoundBox)
  374. {
  375. BoundBox.OnPointerDown(eventData);
  376. }
  377. if (DragCell)
  378. {
  379. DragCell.OnPointerDown(eventData);
  380. }
  381. editType = -1;
  382. EditTransfer?.Invoke(TransferState.Start);
  383. }
  384. /// <summary>
  385. /// 当抬起的时候
  386. /// </summary>
  387. /// <param name="eventData"></param>
  388. public virtual void OnPointerUp(PointerEventData eventData)
  389. {
  390. if (SCInputModule.Instance && !SCInputModule.Instance.CanDrag || !IsCanEditor)
  391. {
  392. return;
  393. }
  394. if (editType != -1)
  395. {
  396. DragTransfer?.Invoke(TransferState.Stop);
  397. editType = -1;
  398. }
  399. else
  400. {
  401. EditTransfer?.Invoke(TransferState.Stop);
  402. }
  403. ClearTime();
  404. if (autoDisDrag > 0)
  405. {
  406. dragTimer = TimerMgr.Instance.CreateTimer(() => { IsDragEnable = false; }, autoDisDrag);
  407. }
  408. }
  409. /// <summary>
  410. /// 拖拽事件
  411. /// </summary>
  412. /// <param name="eventData"></param>
  413. private void OnPointerDrag(PointerEventData eventData)
  414. {
  415. if (editType == -1)
  416. {
  417. editType = 0;
  418. DragTransfer?.Invoke(TransferState.Start);
  419. //EditorTransfer?.Invoke(state, false);
  420. }
  421. if (isLookHead)
  422. {
  423. editType = 1;
  424. }
  425. else
  426. {
  427. editType = 0;
  428. }
  429. //if (isLookHead && GameSession.Instance && GameSession.Instance.gameHead)
  430. //{
  431. // Vector3 pos = GameSession.Instance.gameHead.position;
  432. // pos.y = transform.position.y;
  433. // Vector3 forward = (transform.position - pos).normalized;
  434. // pos.y = 0;
  435. // if (forward != Vector3.zero)
  436. // {
  437. // transform.forward = forward;
  438. // }
  439. //}
  440. DragTransfer?.Invoke(TransferState.Doing);
  441. }
  442. /// <summary>
  443. /// 添加一个后退的节点
  444. /// 当退出一个前进的节点或者编辑物体的时候添加
  445. /// </summary>
  446. /// <param name="posture"></param>
  447. public void AddBackNode(Posture posture)
  448. {
  449. if (PostureBack != null)
  450. {
  451. PostureBack.Push(posture);
  452. }
  453. }
  454. /// <summary>
  455. /// 退出一个后退的节点
  456. /// </summary>
  457. public bool PeekBackNode()
  458. {
  459. if (PostureBack != null && PostureBack.Count > 0)
  460. {
  461. AddAheadNode(UnityUtil.GetPosture(transform));
  462. Posture posture = PostureBack.Pop();
  463. DelaySyn(posture, 0.2f);
  464. return true;
  465. }
  466. return false;
  467. }
  468. /// <summary>
  469. /// 延时控制
  470. /// </summary>
  471. /// <param name="posture"></param>
  472. /// <param name="time"></param>
  473. public void DelaySyn(Posture posture, float time)
  474. {
  475. ClearTime();
  476. editType = -1;
  477. //EditTransfer?.Invoke(TransferState.Start);
  478. editType = 2;
  479. UnityUtil.SetPosture(transform, posture, time);
  480. ///同步3次位置,保持位移的实时性
  481. TimerMgr.Instance.CreateTimer(() =>
  482. {
  483. EditTransfer?.Invoke(TransferState.Doing);
  484. }, time / 4f, 3);
  485. TimerMgr.Instance.CreateTimer(() =>
  486. {
  487. EditTransfer?.Invoke(TransferState.Stop);
  488. editType = -1;
  489. }, time + 0.01f);
  490. }
  491. /// <summary>
  492. /// 添加一个前进的节点,仅当回退的时候添加
  493. /// </summary>
  494. /// <param name="posture"></param>
  495. public void AddAheadNode(Posture posture)
  496. {
  497. if (PostureAhead != null)
  498. {
  499. PostureAhead.Push(posture);
  500. }
  501. }
  502. /// <summary>
  503. /// 退出一个前进的节点
  504. /// </summary>
  505. public bool PeekAheadNode()
  506. {
  507. if (PostureAhead != null && PostureAhead.Count > 0)
  508. {
  509. AddBackNode(UnityUtil.GetPosture(transform));
  510. Posture posture = PostureAhead.Pop();
  511. DelaySyn(posture, 0.2f);
  512. return true;
  513. }
  514. return false;
  515. }
  516. /// <summary>
  517. /// 清理此计时器
  518. /// </summary>
  519. public void ClearTime()
  520. {
  521. if (dragTimer != null && TimerMgr.Instance)
  522. {
  523. TimerMgr.Instance.DestroyTimer(dragTimer);
  524. }
  525. dragTimer = null;
  526. }
  527. /// <summary>
  528. /// 重构包围盒
  529. /// </summary>
  530. /// <param name="isReStart"></param>
  531. public void RemoveBound()
  532. {
  533. if (BoundCell)
  534. {
  535. Destroy(BoundCell);
  536. }
  537. BoundBox.Redraw();
  538. isInitBound = true;
  539. }
  540. /// <summary>
  541. /// 设置编辑框的颜色
  542. /// 用来判定颜色无法被选中的效果
  543. /// </summary>
  544. /// <param name="color"></param>
  545. public void SetBoundColor(bool isLock, Color color)
  546. {
  547. if (BoundBox)
  548. {
  549. BoundBox.SetZoomColor(isLock, color);
  550. }
  551. }
  552. /// <summary>
  553. /// 获得距离最近的低啊
  554. /// </summary>
  555. /// <param name="point"></param>
  556. /// <returns></returns>
  557. public Vector3 GetCastPoint(Vector3 point)
  558. {
  559. if (BoundBox)
  560. {
  561. return BoundBox.GetCastPoint(point);
  562. }
  563. return transform.position;
  564. }
  565. /// <summary>
  566. /// 设置位移编辑激活和失活
  567. /// </summary>
  568. /// <param name="isActive"></param>
  569. public void SetTransferActive(bool isActive)
  570. {
  571. if (BoundBox && BoundBox.isActiveAndEnabled != isActive)
  572. {
  573. BoundBox.enabled = isActive;
  574. }
  575. if (DragCell && DragCell.isActiveAndEnabled != isActive)
  576. {
  577. DragCell.enabled = isActive;
  578. }
  579. SetBoundActive(isActive);
  580. if (!isActive)
  581. {
  582. DisActiveCell();
  583. }
  584. }
  585. private void OnDisable()
  586. {
  587. ClearTime();
  588. TransferDisEnable?.Invoke();
  589. //IsDragEnable = false;
  590. }
  591. /// <summary>
  592. /// 设置可操作性
  593. /// </summary>
  594. public bool IsDragEnable
  595. {
  596. get => isDragEnable;
  597. set
  598. {
  599. isDragEnable = value;
  600. SetTransferActive(isDragEnable);
  601. if (!IsLock)
  602. {
  603. if (isDragEnable)
  604. {
  605. ChangeActive();
  606. }
  607. IsSelect = value;
  608. }
  609. }
  610. }
  611. public bool IsSelect { get => isSelect; set => isSelect = value; }
  612. public TransferState TransferState
  613. {
  614. get => transferState; set
  615. {
  616. if (transferState != value)
  617. {
  618. transferState = value;
  619. TransferStateChange?.Invoke(transferState);
  620. }
  621. }
  622. }
  623. public bool IsInit { get => isInit; set => isInit = value; }
  624. public void ChangeActive()
  625. {
  626. if (curActiveCell && curActiveCell != this)
  627. {
  628. if (!curActiveCell.IsLock)
  629. {
  630. curActiveCell.IsDragEnable = false;
  631. }
  632. }
  633. if (!IsLock)
  634. {
  635. if (curActiveCell != this)
  636. {
  637. curActiveCell = this;
  638. //TransferSelect?.Invoke(this, true);
  639. }
  640. if (curActiveCell)
  641. {
  642. curActiveCell.SetBoundActive(true);
  643. }
  644. }
  645. }
  646. public void DisActiveCell()
  647. {
  648. if (curActiveCell == this)
  649. {
  650. EditTransfer?.Invoke(TransferState.Over);
  651. //TransferSelect?.Invoke(this, false);
  652. curActiveCell = null;
  653. }
  654. }
  655. public static void CancelCell()
  656. {
  657. if (curActiveCell)
  658. {
  659. curActiveCell.DisActiveCell();
  660. }
  661. }
  662. /// <summary>
  663. /// 切换畸形缩放和非畸形缩放
  664. /// </summary>
  665. /// <param name="isJiXing"></param>
  666. public void SetScaleModel(bool isJiXing = true)
  667. {
  668. if (BoundBox)
  669. {
  670. BoundBox.SetScaleModel(isJiXing);
  671. }
  672. }
  673. /// <summary>
  674. /// 畸形设置面板
  675. /// </summary>
  676. public bool IsJiXingScale
  677. {
  678. get => BoundBox.IsJiXingScale;
  679. set
  680. {
  681. BoundBox.IsJiXingScale = value;
  682. if (value)
  683. {
  684. BoundBox.ActiveHandle = editorHandType;
  685. }
  686. else
  687. {
  688. BoundBox.ActiveHandle = normalHandType;
  689. }
  690. }
  691. }
  692. public bool IsCanEditor { get => isCanEditor; set => isCanEditor = value; }
  693. }
  694. }