TransferCell.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. ChangeLayerRecursively(boundCell.transform);
  200. }
  201. return boundCell;
  202. }
  203. }
  204. /// <summary>
  205. /// 更改层级
  206. /// </summary>
  207. /// <param name="parentTransform"></param>
  208. public void ChangeLayerRecursively(Transform parentTransform)
  209. {
  210. // 修改父物体的层级
  211. parentTransform.gameObject.layer = LayerMask.NameToLayer("Arrow");
  212. // 遍历所有子物体,并递归调用ChangeLayerRecursively方法
  213. for (int i = 0; i < parentTransform.childCount; i++)
  214. {
  215. Transform childTransform = parentTransform.GetChild(i);
  216. ChangeLayerRecursively(childTransform);
  217. }
  218. }
  219. /// <summary>
  220. /// 包围盒算法组件
  221. /// </summary>
  222. public XBoundingBox BoundBox
  223. {
  224. get
  225. {
  226. if (!boundBox)
  227. {
  228. boundBox = GetComponent<XBoundingBox>();
  229. if (!boundBox)
  230. {
  231. UnityLog.LogError(gameObject.name + "this is no XBoundingBox");
  232. }
  233. else
  234. {
  235. DragTransfer += state => { EditTransfer?.Invoke(state); };
  236. ZoomTransfer += state => { EditTransfer?.Invoke(state); };
  237. RoteTransfer += state => { EditTransfer?.Invoke(state); };
  238. EditTransfer += OnEditTransfer;
  239. boundBox.ScaleStarted.AddListener(OnScaleStarted);
  240. boundBox.Scaling.AddListener(OnScaling);
  241. boundBox.ScaleStopped.AddListener(OnScaleStopped);
  242. boundBox.RotateStarted.AddListener(OnRotateStarted);
  243. boundBox.Rotating.AddListener(OnRotating);
  244. boundBox.RotateStopped.AddListener(OnRotateStopped);
  245. }
  246. }
  247. return boundBox;
  248. }
  249. }
  250. private void OnRotating()
  251. {
  252. RoteTransfer?.Invoke(TransferState.Doing);
  253. }
  254. private void OnScaling()
  255. {
  256. ZoomTransfer?.Invoke(TransferState.Doing);
  257. }
  258. public void SetBoundActive(bool isShow)
  259. {
  260. if (BoundCell)
  261. {
  262. BoundCell.SetActive(isShow);
  263. }
  264. }
  265. /// <summary>
  266. /// 初始化
  267. /// </summary>
  268. private void Start()
  269. {
  270. if (BoundBox)
  271. {
  272. TimerMgr.Instance.CreateTimer(() =>
  273. {
  274. IsDragEnable = false;
  275. IsInit = true;
  276. }, 0);
  277. }
  278. }
  279. /// <summary>
  280. /// 当编辑物体的时候
  281. /// </summary>
  282. /// <param name="state"></param>
  283. public void OnEditTransfer(TransferState state)
  284. {
  285. if (state == TransferState.Start && editType != -1)
  286. {
  287. ClearTime();
  288. AddBackNode(UnityUtil.GetPosture(transform));
  289. }
  290. if (state == TransferState.Stop)
  291. {
  292. ClearTime();
  293. if (autoDisDrag > 0)
  294. {
  295. dragTimer = TimerMgr.Instance.CreateTimer(() => { IsDragEnable = false; }, autoDisDrag);
  296. }
  297. }
  298. TransferState = state;
  299. }
  300. /// <summary>
  301. /// 清除协程
  302. /// </summary>
  303. private void ClearEditor()
  304. {
  305. if (editCoroutine != null)
  306. {
  307. StopCoroutine(editCoroutine);
  308. editCoroutine = null;
  309. }
  310. }
  311. /// <summary>
  312. /// 旋转结束
  313. /// </summary>
  314. public void OnRotateStopped()
  315. {
  316. ClearEditor();
  317. editType = 1;
  318. RoteTransfer?.Invoke(TransferState.Stop);
  319. editType = -1;
  320. }
  321. /// <summary>
  322. /// 旋转开始
  323. /// </summary>
  324. private void OnRotateStarted()
  325. {
  326. ClearEditor();
  327. editType = 1;
  328. RoteTransfer?.Invoke(TransferState.Start);
  329. //editCoroutine = StartCoroutine(EditorCoroutine());
  330. }
  331. /// <summary>
  332. /// 缩放结束
  333. /// </summary>
  334. public void OnScaleStopped()
  335. {
  336. ClearEditor();
  337. editType = 2;
  338. ZoomTransfer?.Invoke(TransferState.Stop);
  339. editType = -1;
  340. TimerMgr.Instance.CreateTimer(RemoveBound, 0.01f);
  341. }
  342. /// <summary>
  343. /// 缩放开始
  344. /// </summary>
  345. private void OnScaleStarted()
  346. {
  347. ClearEditor();
  348. editType = 2;
  349. ZoomTransfer?.Invoke(TransferState.Start);
  350. //editCoroutine = StartCoroutine(EditorCoroutine());
  351. }
  352. /// <summary>
  353. /// 编辑的协程
  354. /// </summary>
  355. /// <returns></returns>
  356. //private IEnumerator EditorCoroutine()
  357. //{
  358. // while (editType != -1)
  359. // {
  360. // yield return new WaitForFixedUpdate();
  361. // yield return null;
  362. // if (editType == 1)
  363. // {
  364. // RoteTransfer?.Invoke(TransferState.Doing);
  365. // }
  366. // else if (editType == 2)
  367. // {
  368. // ZoomTransfer?.Invoke(TransferState.Doing);
  369. // }
  370. // }
  371. //}
  372. /// <summary>
  373. /// 当按下的时候
  374. /// 如果物体没有被锁定,处于可操作的状态时,显示操作框
  375. /// </summary>
  376. /// <param name="eventData"></param>
  377. public void OnPointerDown(PointerEventData eventData)
  378. {
  379. if (IsLock || (SCInputModule.Instance && !SCInputModule.Instance.CanDrag) || !IsCanEditor)
  380. {
  381. return;
  382. }
  383. transform.DOKill();
  384. IsDragEnable = true;
  385. if (!isInitBound)
  386. {
  387. RemoveBound();
  388. }
  389. if (BoundBox)
  390. {
  391. BoundBox.OnPointerDown(eventData);
  392. }
  393. if (DragCell)
  394. {
  395. DragCell.OnPointerDown(eventData);
  396. }
  397. editType = -1;
  398. EditTransfer?.Invoke(TransferState.Start);
  399. }
  400. /// <summary>
  401. /// 当抬起的时候
  402. /// </summary>
  403. /// <param name="eventData"></param>
  404. public virtual void OnPointerUp(PointerEventData eventData)
  405. {
  406. if (SCInputModule.Instance && !SCInputModule.Instance.CanDrag || !IsCanEditor)
  407. {
  408. return;
  409. }
  410. if (editType != -1)
  411. {
  412. DragTransfer?.Invoke(TransferState.Stop);
  413. editType = -1;
  414. }
  415. else
  416. {
  417. EditTransfer?.Invoke(TransferState.Stop);
  418. }
  419. ClearTime();
  420. if (autoDisDrag > 0)
  421. {
  422. dragTimer = TimerMgr.Instance.CreateTimer(() => { IsDragEnable = false; }, autoDisDrag);
  423. }
  424. }
  425. /// <summary>
  426. /// 拖拽事件
  427. /// </summary>
  428. /// <param name="eventData"></param>
  429. private void OnPointerDrag(PointerEventData eventData)
  430. {
  431. if (editType == -1)
  432. {
  433. editType = 0;
  434. DragTransfer?.Invoke(TransferState.Start);
  435. //EditorTransfer?.Invoke(state, false);
  436. }
  437. if (isLookHead)
  438. {
  439. editType = 1;
  440. }
  441. else
  442. {
  443. editType = 0;
  444. }
  445. //if (isLookHead && GameSession.Instance && GameSession.Instance.gameHead)
  446. //{
  447. // Vector3 pos = GameSession.Instance.gameHead.position;
  448. // pos.y = transform.position.y;
  449. // Vector3 forward = (transform.position - pos).normalized;
  450. // pos.y = 0;
  451. // if (forward != Vector3.zero)
  452. // {
  453. // transform.forward = forward;
  454. // }
  455. //}
  456. DragTransfer?.Invoke(TransferState.Doing);
  457. }
  458. /// <summary>
  459. /// 添加一个后退的节点
  460. /// 当退出一个前进的节点或者编辑物体的时候添加
  461. /// </summary>
  462. /// <param name="posture"></param>
  463. public void AddBackNode(Posture posture)
  464. {
  465. if (PostureBack != null)
  466. {
  467. PostureBack.Push(posture);
  468. }
  469. }
  470. /// <summary>
  471. /// 退出一个后退的节点
  472. /// </summary>
  473. public bool PeekBackNode()
  474. {
  475. if (PostureBack != null && PostureBack.Count > 0)
  476. {
  477. AddAheadNode(UnityUtil.GetPosture(transform));
  478. Posture posture = PostureBack.Pop();
  479. DelaySyn(posture, 0.2f);
  480. return true;
  481. }
  482. return false;
  483. }
  484. /// <summary>
  485. /// 延时控制
  486. /// </summary>
  487. /// <param name="posture"></param>
  488. /// <param name="time"></param>
  489. public void DelaySyn(Posture posture, float time)
  490. {
  491. ClearTime();
  492. editType = -1;
  493. //EditTransfer?.Invoke(TransferState.Start);
  494. editType = 2;
  495. UnityUtil.SetPosture(transform, posture, time);
  496. ///同步3次位置,保持位移的实时性
  497. TimerMgr.Instance.CreateTimer(() =>
  498. {
  499. EditTransfer?.Invoke(TransferState.Doing);
  500. }, time / 4f, 3);
  501. TimerMgr.Instance.CreateTimer(() =>
  502. {
  503. EditTransfer?.Invoke(TransferState.Stop);
  504. editType = -1;
  505. }, time + 0.01f);
  506. }
  507. /// <summary>
  508. /// 添加一个前进的节点,仅当回退的时候添加
  509. /// </summary>
  510. /// <param name="posture"></param>
  511. public void AddAheadNode(Posture posture)
  512. {
  513. if (PostureAhead != null)
  514. {
  515. PostureAhead.Push(posture);
  516. }
  517. }
  518. /// <summary>
  519. /// 退出一个前进的节点
  520. /// </summary>
  521. public bool PeekAheadNode()
  522. {
  523. if (PostureAhead != null && PostureAhead.Count > 0)
  524. {
  525. AddBackNode(UnityUtil.GetPosture(transform));
  526. Posture posture = PostureAhead.Pop();
  527. DelaySyn(posture, 0.2f);
  528. return true;
  529. }
  530. return false;
  531. }
  532. /// <summary>
  533. /// 清理此计时器
  534. /// </summary>
  535. public void ClearTime()
  536. {
  537. if (dragTimer != null && TimerMgr.Instance)
  538. {
  539. TimerMgr.Instance.DestroyTimer(dragTimer);
  540. }
  541. dragTimer = null;
  542. }
  543. /// <summary>
  544. /// 重构包围盒
  545. /// </summary>
  546. /// <param name="isReStart"></param>
  547. public void RemoveBound()
  548. {
  549. if (BoundCell)
  550. {
  551. Destroy(BoundCell);
  552. }
  553. BoundBox.Redraw();
  554. isInitBound = true;
  555. }
  556. /// <summary>
  557. /// 设置编辑框的颜色
  558. /// 用来判定颜色无法被选中的效果
  559. /// </summary>
  560. /// <param name="color"></param>
  561. public void SetBoundColor(bool isLock, Color color)
  562. {
  563. if (BoundBox)
  564. {
  565. BoundBox.SetZoomColor(isLock, color);
  566. }
  567. }
  568. /// <summary>
  569. /// 获得距离最近的低啊
  570. /// </summary>
  571. /// <param name="point"></param>
  572. /// <returns></returns>
  573. public Vector3 GetCastPoint(Vector3 point)
  574. {
  575. if (BoundBox)
  576. {
  577. return BoundBox.GetCastPoint(point);
  578. }
  579. return transform.position;
  580. }
  581. /// <summary>
  582. /// 设置位移编辑激活和失活
  583. /// </summary>
  584. /// <param name="isActive"></param>
  585. public void SetTransferActive(bool isActive)
  586. {
  587. if (BoundBox && BoundBox.isActiveAndEnabled != isActive)
  588. {
  589. BoundBox.enabled = isActive;
  590. }
  591. if (DragCell && DragCell.isActiveAndEnabled != isActive)
  592. {
  593. DragCell.enabled = isActive;
  594. }
  595. SetBoundActive(isActive);
  596. if (!isActive)
  597. {
  598. DisActiveCell();
  599. }
  600. }
  601. private void OnDisable()
  602. {
  603. ClearTime();
  604. TransferDisEnable?.Invoke();
  605. //IsDragEnable = false;
  606. }
  607. /// <summary>
  608. /// 设置可操作性
  609. /// </summary>
  610. public bool IsDragEnable
  611. {
  612. get => isDragEnable;
  613. set
  614. {
  615. isDragEnable = value;
  616. SetTransferActive(isDragEnable);
  617. if (!IsLock)
  618. {
  619. if (isDragEnable)
  620. {
  621. ChangeActive();
  622. }
  623. IsSelect = value;
  624. }
  625. }
  626. }
  627. public bool IsSelect { get => isSelect; set => isSelect = value; }
  628. public TransferState TransferState
  629. {
  630. get => transferState; set
  631. {
  632. if (transferState != value)
  633. {
  634. transferState = value;
  635. TransferStateChange?.Invoke(transferState);
  636. }
  637. }
  638. }
  639. public bool IsInit { get => isInit; set => isInit = value; }
  640. public void ChangeActive()
  641. {
  642. if (curActiveCell && curActiveCell != this)
  643. {
  644. if (!curActiveCell.IsLock)
  645. {
  646. curActiveCell.IsDragEnable = false;
  647. }
  648. }
  649. if (!IsLock)
  650. {
  651. if (curActiveCell != this)
  652. {
  653. curActiveCell = this;
  654. //TransferSelect?.Invoke(this, true);
  655. }
  656. if (curActiveCell)
  657. {
  658. curActiveCell.SetBoundActive(true);
  659. }
  660. }
  661. }
  662. public void DisActiveCell()
  663. {
  664. if (curActiveCell == this)
  665. {
  666. EditTransfer?.Invoke(TransferState.Over);
  667. //TransferSelect?.Invoke(this, false);
  668. curActiveCell = null;
  669. }
  670. }
  671. public static void CancelCell()
  672. {
  673. if (curActiveCell)
  674. {
  675. curActiveCell.DisActiveCell();
  676. }
  677. }
  678. /// <summary>
  679. /// 切换畸形缩放和非畸形缩放
  680. /// </summary>
  681. /// <param name="isJiXing"></param>
  682. public void SetScaleModel(bool isJiXing = true)
  683. {
  684. if (BoundBox)
  685. {
  686. BoundBox.SetScaleModel(isJiXing);
  687. }
  688. }
  689. /// <summary>
  690. /// 畸形设置面板
  691. /// </summary>
  692. public bool IsJiXingScale
  693. {
  694. get => BoundBox.IsJiXingScale;
  695. set
  696. {
  697. BoundBox.IsJiXingScale = value;
  698. if (value)
  699. {
  700. BoundBox.ActiveHandle = editorHandType;
  701. }
  702. else
  703. {
  704. BoundBox.ActiveHandle = normalHandType;
  705. }
  706. }
  707. }
  708. public bool IsCanEditor { get => isCanEditor; set => isCanEditor = value; }
  709. }
  710. }