123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731 |
- using DG.Tweening;
- using SC.XR.Unity.Module_InputSystem;
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using XRTool.Util;
- using XRTool.WorldUI;
- using System.Collections;
- //using UnityEditor;
- using ShadowStudio.Model;
- using UnityEngine.UIElements;
- using static BoundingBox;
- namespace ShadowStudio.Tool
- {
- /// <summary>
- /// 编辑状态
- /// </summary>
- public enum TransferState
- {
- /// <summary>
- /// 克隆状态,此状态不同,仅克隆物体时使用此作为参数
- /// 克隆完成后,状态自动切换
- /// </summary>
- Clone = -1,
- /// <summary>
- /// 同步时不存在此状态,仅做初始化处理
- /// </summary>
- Init = 0,
- /// <summary>
- /// 状态同步开始
- /// </summary>
- Start = 1,
- /// <summary>
- /// 状态同步执行中
- /// </summary>
- Doing = 2,
- /// <summary>
- /// 状态同步结束,但是同步并没有完成
- /// </summary>
- Stop = 3,
- /// <summary>
- /// 状态同步结束,不再对此数据进行同步
- /// </summary>
- Over = 4,
- }
- [RequireComponent(typeof(XBoundingBox))]
- [RequireComponent(typeof(XDragComponent))]
- /// <summary>
- /// 物体姿态控制器
- /// 选中状态,非选中状态
- /// 默认所有物体都是非选中状态
- /// 当处于选中状态时,此物体只有选中人可编辑,同时将此物体的状态进行同步,其他人能看到选中的状态
- /// </summary>
- public class TransferCell : MonoBehaviour, IPointerDownHandler
- {
- [SerializeField]
- /// <summary>
- /// 是否垂直水平面
- /// </summary>
- private bool isLookHead;
- /// <summary>
- /// 拖拽事件
- /// </summary>
- public event Action<TransferState> DragTransfer;
- /// <summary>
- /// 缩放事件
- /// </summary>
- public event Action<TransferState> ZoomTransfer;
- /// <summary>
- /// 旋转事件
- /// </summary>
- public event Action<TransferState> RoteTransfer;
- /// <summary>
- /// 编辑事件
- /// 当物体进行拖拽,缩放,旋转时都会触发
- /// </summary>
- public event Action<TransferState> EditTransfer;
- public event Action<TransferState> TransferStateChange;
- public HandleType normalHandType = HandleType.Rotation | HandleType.Scale;
- public HandleType editorHandType = HandleType.Rotation | HandleType.Scale | HandleType.AxisScale;
- /// <summary>
- /// 选中和非选中的事件
- /// </summary>
- //public event Action<TransferCell, bool> TransferSelect;
- public static TransferCell curActiveCell;
- private XDragComponent dragCell;
- private XBoundingBox boundBox;
- private Coroutine editCoroutine;
- private bool isLock;
- private TransferState transferState = TransferState.Init;
- private bool isCanEditor = true;
- private bool isInitBound = false;
- /// <summary>
- /// 编辑模式
- /// -1代表未编辑
- /// 0代表拖拽
- /// 1代表旋转
- /// 2代表缩放
- /// 开始前/结束后要设置为-1
- /// 开始后到执行中设置对应的状态
- /// </summary>
- private int editType = -1;
- private Timer dragTimer;
- public float autoDisDrag = 5f;
- private Stack<Posture> postureHistroy;
- private Stack<Posture> postureFuture;
- private GameObject boundCell;
- private bool isSelect;
- [SerializeField]
- private bool isDragEnable;
- private bool isInit = false;
- public event Action TransferDisEnable;
- /// <summary>
- /// 后退的栈
- /// </summary>
- public Stack<Posture> PostureBack
- {
- get
- {
- if (postureHistroy == null)
- {
- postureHistroy = new Stack<Posture>();
- }
- return postureHistroy;
- }
- }
- /// <summary>
- /// 前进的栈
- /// </summary>
- public Stack<Posture> PostureAhead
- {
- get
- {
- if (postureFuture == null)
- {
- postureFuture = new Stack<Posture>();
- }
- return postureFuture;
- }
- }
- /// <summary>
- /// 锁定
- /// </summary>
- public bool IsLock
- {
- get
- {
- return isLock;
- }
- set
- {
- isLock = value;
- if (isLock)
- {
- PostureAhead.Clear();
- PostureBack.Clear();
- IsDragEnable = false;
- SetBoundActive(true);
- SetBoundColor(value, Color.yellow);
- }
- else
- {
- SetBoundColor(value, Color.white);
- if (curActiveCell != this)
- {
- SetBoundActive(false);
- }
- }
- }
- }
- /// <summary>
- /// 拖拽组件
- /// </summary>
- public XDragComponent DragCell
- {
- get
- {
- if (!dragCell)
- {
- dragCell = GetComponent<XDragComponent>();
- dragCell.PointerDrag.AddListener(OnPointerDrag);
- //dragCell.PointerDown.AddListener(OnPointerDown);
- dragCell.PointerUp.AddListener(OnPointerUp);
- }
- return dragCell;
- }
- }
- /// <summary>
- /// 包围盒显示对象
- /// </summary>
- public GameObject BoundCell
- {
- get
- {
- if (!boundCell)
- {
- boundCell = UnityUtil.GetBreadthChild(transform, "XRTool.Util.XBoundingBox");
- ChangeLayerRecursively(boundCell.transform);
- }
- return boundCell;
- }
- }
- /// <summary>
- /// 更改层级
- /// </summary>
- /// <param name="parentTransform"></param>
- public void ChangeLayerRecursively(Transform parentTransform)
- {
- // 修改父物体的层级
- parentTransform.gameObject.layer = LayerMask.NameToLayer("Arrow");
- // 遍历所有子物体,并递归调用ChangeLayerRecursively方法
- for (int i = 0; i < parentTransform.childCount; i++)
- {
- Transform childTransform = parentTransform.GetChild(i);
- ChangeLayerRecursively(childTransform);
- }
- }
- /// <summary>
- /// 包围盒算法组件
- /// </summary>
- public XBoundingBox BoundBox
- {
- get
- {
- if (!boundBox)
- {
- boundBox = GetComponent<XBoundingBox>();
- if (!boundBox)
- {
- UnityLog.LogError(gameObject.name + "this is no XBoundingBox");
- }
- else
- {
- DragTransfer += state => { EditTransfer?.Invoke(state); };
- ZoomTransfer += state => { EditTransfer?.Invoke(state); };
- RoteTransfer += state => { EditTransfer?.Invoke(state); };
- EditTransfer += OnEditTransfer;
- boundBox.ScaleStarted.AddListener(OnScaleStarted);
- boundBox.Scaling.AddListener(OnScaling);
- boundBox.ScaleStopped.AddListener(OnScaleStopped);
- boundBox.RotateStarted.AddListener(OnRotateStarted);
- boundBox.Rotating.AddListener(OnRotating);
- boundBox.RotateStopped.AddListener(OnRotateStopped);
- }
- }
- return boundBox;
- }
- }
- private void OnRotating()
- {
- RoteTransfer?.Invoke(TransferState.Doing);
- }
- private void OnScaling()
- {
- ZoomTransfer?.Invoke(TransferState.Doing);
- }
- public void SetBoundActive(bool isShow)
- {
- if (BoundCell)
- {
- BoundCell.SetActive(isShow);
- }
- }
- /// <summary>
- /// 初始化
- /// </summary>
- private void Start()
- {
- if (BoundBox)
- {
- TimerMgr.Instance.CreateTimer(() =>
- {
- IsDragEnable = false;
- IsInit = true;
- }, 0);
- }
- }
- /// <summary>
- /// 当编辑物体的时候
- /// </summary>
- /// <param name="state"></param>
- public void OnEditTransfer(TransferState state)
- {
- if (state == TransferState.Start && editType != -1)
- {
- ClearTime();
- AddBackNode(UnityUtil.GetPosture(transform));
- }
- if (state == TransferState.Stop)
- {
- ClearTime();
- if (autoDisDrag > 0)
- {
- dragTimer = TimerMgr.Instance.CreateTimer(() => { IsDragEnable = false; }, autoDisDrag);
- }
- }
- TransferState = state;
- }
- /// <summary>
- /// 清除协程
- /// </summary>
- private void ClearEditor()
- {
- if (editCoroutine != null)
- {
- StopCoroutine(editCoroutine);
- editCoroutine = null;
- }
- }
- /// <summary>
- /// 旋转结束
- /// </summary>
- public void OnRotateStopped()
- {
- ClearEditor();
- editType = 1;
- RoteTransfer?.Invoke(TransferState.Stop);
- editType = -1;
- }
- /// <summary>
- /// 旋转开始
- /// </summary>
- private void OnRotateStarted()
- {
- ClearEditor();
- editType = 1;
- RoteTransfer?.Invoke(TransferState.Start);
- //editCoroutine = StartCoroutine(EditorCoroutine());
- }
- /// <summary>
- /// 缩放结束
- /// </summary>
- public void OnScaleStopped()
- {
- ClearEditor();
- editType = 2;
- ZoomTransfer?.Invoke(TransferState.Stop);
- editType = -1;
- TimerMgr.Instance.CreateTimer(RemoveBound, 0.01f);
- }
- /// <summary>
- /// 缩放开始
- /// </summary>
- private void OnScaleStarted()
- {
- ClearEditor();
- editType = 2;
- ZoomTransfer?.Invoke(TransferState.Start);
- //editCoroutine = StartCoroutine(EditorCoroutine());
- }
- /// <summary>
- /// 编辑的协程
- /// </summary>
- /// <returns></returns>
- //private IEnumerator EditorCoroutine()
- //{
- // while (editType != -1)
- // {
- // yield return new WaitForFixedUpdate();
- // yield return null;
- // if (editType == 1)
- // {
- // RoteTransfer?.Invoke(TransferState.Doing);
- // }
- // else if (editType == 2)
- // {
- // ZoomTransfer?.Invoke(TransferState.Doing);
- // }
- // }
- //}
- /// <summary>
- /// 当按下的时候
- /// 如果物体没有被锁定,处于可操作的状态时,显示操作框
- /// </summary>
- /// <param name="eventData"></param>
- public void OnPointerDown(PointerEventData eventData)
- {
- if (IsLock || (SCInputModule.Instance && !SCInputModule.Instance.CanDrag) || !IsCanEditor)
- {
- return;
- }
- transform.DOKill();
- IsDragEnable = true;
- if (!isInitBound)
- {
- RemoveBound();
- }
- if (BoundBox)
- {
- BoundBox.OnPointerDown(eventData);
- }
- if (DragCell)
- {
- DragCell.OnPointerDown(eventData);
- }
- editType = -1;
- EditTransfer?.Invoke(TransferState.Start);
- }
- /// <summary>
- /// 当抬起的时候
- /// </summary>
- /// <param name="eventData"></param>
- public virtual void OnPointerUp(PointerEventData eventData)
- {
- if (SCInputModule.Instance && !SCInputModule.Instance.CanDrag || !IsCanEditor)
- {
- return;
- }
- if (editType != -1)
- {
- DragTransfer?.Invoke(TransferState.Stop);
- editType = -1;
- }
- else
- {
- EditTransfer?.Invoke(TransferState.Stop);
- }
- ClearTime();
- if (autoDisDrag > 0)
- {
- dragTimer = TimerMgr.Instance.CreateTimer(() => { IsDragEnable = false; }, autoDisDrag);
- }
- }
- /// <summary>
- /// 拖拽事件
- /// </summary>
- /// <param name="eventData"></param>
- private void OnPointerDrag(PointerEventData eventData)
- {
- if (editType == -1)
- {
- editType = 0;
- DragTransfer?.Invoke(TransferState.Start);
- //EditorTransfer?.Invoke(state, false);
- }
- if (isLookHead)
- {
- editType = 1;
- }
- else
- {
- editType = 0;
- }
- //if (isLookHead && GameSession.Instance && GameSession.Instance.gameHead)
- //{
- // Vector3 pos = GameSession.Instance.gameHead.position;
- // pos.y = transform.position.y;
- // Vector3 forward = (transform.position - pos).normalized;
- // pos.y = 0;
- // if (forward != Vector3.zero)
- // {
- // transform.forward = forward;
- // }
- //}
- DragTransfer?.Invoke(TransferState.Doing);
- }
- /// <summary>
- /// 添加一个后退的节点
- /// 当退出一个前进的节点或者编辑物体的时候添加
- /// </summary>
- /// <param name="posture"></param>
- public void AddBackNode(Posture posture)
- {
- if (PostureBack != null)
- {
- PostureBack.Push(posture);
- }
- }
- /// <summary>
- /// 退出一个后退的节点
- /// </summary>
- public bool PeekBackNode()
- {
- if (PostureBack != null && PostureBack.Count > 0)
- {
- AddAheadNode(UnityUtil.GetPosture(transform));
- Posture posture = PostureBack.Pop();
- DelaySyn(posture, 0.2f);
- return true;
- }
- return false;
- }
- /// <summary>
- /// 延时控制
- /// </summary>
- /// <param name="posture"></param>
- /// <param name="time"></param>
- public void DelaySyn(Posture posture, float time)
- {
- ClearTime();
- editType = -1;
- //EditTransfer?.Invoke(TransferState.Start);
- editType = 2;
- UnityUtil.SetPosture(transform, posture, time);
- ///同步3次位置,保持位移的实时性
- TimerMgr.Instance.CreateTimer(() =>
- {
- EditTransfer?.Invoke(TransferState.Doing);
- }, time / 4f, 3);
- TimerMgr.Instance.CreateTimer(() =>
- {
- EditTransfer?.Invoke(TransferState.Stop);
- editType = -1;
- }, time + 0.01f);
- }
- /// <summary>
- /// 添加一个前进的节点,仅当回退的时候添加
- /// </summary>
- /// <param name="posture"></param>
- public void AddAheadNode(Posture posture)
- {
- if (PostureAhead != null)
- {
- PostureAhead.Push(posture);
- }
- }
- /// <summary>
- /// 退出一个前进的节点
- /// </summary>
- public bool PeekAheadNode()
- {
- if (PostureAhead != null && PostureAhead.Count > 0)
- {
- AddBackNode(UnityUtil.GetPosture(transform));
- Posture posture = PostureAhead.Pop();
- DelaySyn(posture, 0.2f);
- return true;
- }
- return false;
- }
- /// <summary>
- /// 清理此计时器
- /// </summary>
- public void ClearTime()
- {
- if (dragTimer != null && TimerMgr.Instance)
- {
- TimerMgr.Instance.DestroyTimer(dragTimer);
- }
- dragTimer = null;
- }
- /// <summary>
- /// 重构包围盒
- /// </summary>
- /// <param name="isReStart"></param>
- public void RemoveBound()
- {
- if (BoundCell)
- {
- Destroy(BoundCell);
- }
- BoundBox.Redraw();
- isInitBound = true;
- }
- /// <summary>
- /// 设置编辑框的颜色
- /// 用来判定颜色无法被选中的效果
- /// </summary>
- /// <param name="color"></param>
- public void SetBoundColor(bool isLock, Color color)
- {
- if (BoundBox)
- {
- BoundBox.SetZoomColor(isLock, color);
- }
- }
- /// <summary>
- /// 获得距离最近的低啊
- /// </summary>
- /// <param name="point"></param>
- /// <returns></returns>
- public Vector3 GetCastPoint(Vector3 point)
- {
- if (BoundBox)
- {
- return BoundBox.GetCastPoint(point);
- }
- return transform.position;
- }
- /// <summary>
- /// 设置位移编辑激活和失活
- /// </summary>
- /// <param name="isActive"></param>
- public void SetTransferActive(bool isActive)
- {
- if (BoundBox && BoundBox.isActiveAndEnabled != isActive)
- {
- BoundBox.enabled = isActive;
- }
- if (DragCell && DragCell.isActiveAndEnabled != isActive)
- {
- DragCell.enabled = isActive;
- }
- SetBoundActive(isActive);
- if (!isActive)
- {
- DisActiveCell();
- }
- }
- private void OnDisable()
- {
- ClearTime();
- TransferDisEnable?.Invoke();
- //IsDragEnable = false;
- }
- /// <summary>
- /// 设置可操作性
- /// </summary>
- public bool IsDragEnable
- {
- get => isDragEnable;
- set
- {
- isDragEnable = value;
- SetTransferActive(isDragEnable);
- if (!IsLock)
- {
- if (isDragEnable)
- {
- ChangeActive();
- }
- IsSelect = value;
- }
- }
- }
- public bool IsSelect { get => isSelect; set => isSelect = value; }
- public TransferState TransferState
- {
- get => transferState; set
- {
- if (transferState != value)
- {
- transferState = value;
- TransferStateChange?.Invoke(transferState);
- }
- }
- }
- public bool IsInit { get => isInit; set => isInit = value; }
- public void ChangeActive()
- {
- if (curActiveCell && curActiveCell != this)
- {
- if (!curActiveCell.IsLock)
- {
- curActiveCell.IsDragEnable = false;
- }
- }
- if (!IsLock)
- {
- if (curActiveCell != this)
- {
- curActiveCell = this;
- //TransferSelect?.Invoke(this, true);
- }
- if (curActiveCell)
- {
- curActiveCell.SetBoundActive(true);
- }
- }
- }
- public void DisActiveCell()
- {
- if (curActiveCell == this)
- {
- EditTransfer?.Invoke(TransferState.Over);
- //TransferSelect?.Invoke(this, false);
- curActiveCell = null;
- }
- }
- public static void CancelCell()
- {
- if (curActiveCell)
- {
- curActiveCell.DisActiveCell();
- }
- }
- /// <summary>
- /// 切换畸形缩放和非畸形缩放
- /// </summary>
- /// <param name="isJiXing"></param>
- public void SetScaleModel(bool isJiXing = true)
- {
- if (BoundBox)
- {
- BoundBox.SetScaleModel(isJiXing);
- }
- }
- /// <summary>
- /// 畸形设置面板
- /// </summary>
- public bool IsJiXingScale
- {
- get => BoundBox.IsJiXingScale;
- set
- {
- BoundBox.IsJiXingScale = value;
- if (value)
- {
- BoundBox.ActiveHandle = editorHandType;
- }
- else
- {
- BoundBox.ActiveHandle = normalHandType;
- }
- }
- }
- public bool IsCanEditor { get => isCanEditor; set => isCanEditor = value; }
- }
- }
|