TransferCell.cs 23 KB

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