123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using SC.InputSystem;
- public class DrawingManager : PointerDelegate
- {
- [SerializeField]
- private Game3DButton SelectSizeButton;
- [SerializeField]
- private Game3DButton SelectColorButton;
- [SerializeField]
- private Game3DButton SelectCleanButton;
- [SerializeField]
- private Game3DButton StartButton;
- [SerializeField]
- private Game3DButton ExitButton;
- [SerializeField]
- private DrawingMap MDrawingMap;
- [SerializeField]
- private GameObject PenModel;//笔的模型
- [SerializeField]
- private GameObject SizeModel;//笔的模型
- [SerializeField]
- private GameObject EffectModel;//笔轨迹的特效
- [SerializeField]
- private GameObject BoardModel;//画板 挡板
- [SerializeField]
- private Transform MLineParent;//写字的父节点
- [SerializeField]
- private GameObject BgModel;//背景版 画板
- public Color TestColor;
- public static DrawingManager _instance;
- private float dis = 3.0f;
- public static DrawingManager Instance
- {
- get {
- return _instance;
- }
- }
- private DrawData mdata;
- private void Awake()
- {
- _instance = this;
- mdata = new DrawData();
- //设置默认值
- mdata.size = 1;
- mdata.SetColor(Color.white);
- }
- void Start()
- {
- MDrawingMap.gameObject.SetActive(false);
- SizeModel.gameObject.SetActive(false);
- PenModel.gameObject.SetActive(false);
- EffectModel.gameObject.SetActive(false);
- BoardModel.gameObject.SetActive(false);
- BgModel.gameObject.SetActive(false);
- SelectColorButton.onClick.AddListener(SelectColorClick);
- SelectSizeButton.onClick.AddListener(DrawEnter);
- SelectCleanButton.onClick.AddListener(DrawCleanALL);
- StartButton.onClick.AddListener(DrawReady);
- ExitButton.onClick.AddListener(DrawExit);
- MessageCenterController.Instance.Register(GameEnum.MESSAGE_ACCEPT_DRAW_SOCKET_DATA, TongbuDraw);
- MessageCenterController.Instance.Register(GameEnum.MESSAGE_ENTER_COURSE, EnterCourse);//进入房间
- MessageCenterController.Instance.Register(GameEnum.MESSAGE_EXIT_COURSE, ExitCourse);//关闭房间
- MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_CHANGE_ROOM_OWNER_REFRESH, OwnerChange);//有房主的变动
- this.gameObject.SetActive(false);
- }
- private void OnDestroy()
- {
- SelectColorButton.onClick.RemoveListener(SelectColorClick);
- SelectSizeButton.onClick.RemoveListener(DrawEnter);
- SelectCleanButton.onClick.RemoveListener(DrawCleanALL);
- StartButton.onClick.RemoveListener(DrawReady);
- ExitButton.onClick.RemoveListener(DrawExit);
- MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_ACCEPT_DRAW_SOCKET_DATA, TongbuDraw);
- MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_ENTER_COURSE, EnterCourse);//进入房间
- MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_EXIT_COURSE, ExitCourse);//关闭房间
- MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_CHANGE_ROOM_OWNER_REFRESH, OwnerChange);//有房主的变动
- }
- private void EnterCourse(System.Object datas = null)
- {
- if (GamePlayerData.Instance.IsFangzhu())
- {
- this.gameObject.SetActive(true);
- }
- BgModel.gameObject.SetActive(true);
- }
- private void ExitCourse(System.Object datas = null)
- {
- this.gameObject.SetActive(false);
- this.mdata.ChangeType(DrawData.DrawDataType.CleanALL);
- RefreshData();
- BgModel.gameObject.SetActive(false);
- }
- private void TongbuDraw(System.Object data = null)
- {
- if (GamePlayerData.Instance.room_data.IsReconnectOwner())
- {
- return;
- }
- this.mdata = data as DrawData;
- RefreshData();
- }
- private void LateUpdate()
- {
- if (Input.GetKeyDown(KeyCode.T))
- {
- DrawStart();
- }
- else if (Input.GetKeyUp(KeyCode.T))
- {
- DrawEnd();
- }
- if (API_InputSystem.IsISInitialized())
- {
- if (PenModel.activeInHierarchy)
- {
- PenModel.transform.position = API_InputSystem.Position;
- PenModel.transform.eulerAngles = API_InputSystem.Normal;
- }
- if (EffectModel.activeInHierarchy)
- {
- EffectModel.transform.position = DrawPoint();
- //PenModel.transform.eulerAngles = API_InputSystem.Normal;
- }
- CheckAutoEnd();
- }
- }
- private void DrawReady()
- {
- if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
- {
- return;
- }
- this.mdata.ChangeType(DrawData.DrawDataType.Ready);
- this.mdata.point = DrawPoint();
- RefreshData();
- Debug.Log("发送准备画画");
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
- }
- private void DrawClean()
- {
- if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
- {
- return;
- }
- this.mdata.ChangeType(DrawData.DrawDataType.CleanLast);
- RefreshData();
- Debug.Log("发送撤销上一步");
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
- }
- private void DrawCleanALL()
- {
- if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
- {
- return;
- }
- this.mdata.ChangeType(DrawData.DrawDataType.CleanALL);
- RefreshData();
- Debug.Log("发送撤销上一步");
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
- }
- private void DrawStart()
- {
- if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
- {
- return;
- }
-
- this.mdata.ChangeType(DrawData.DrawDataType.Start);
- RefreshData();
- }
- private void DrawEnd()
- {
- if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
- {
- return;
- }
- lastHitGameObj = null;
- this.mdata.ChangeType(DrawData.DrawDataType.End);
- RefreshData();
- Debug.Log("发送停止画画");
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
- }
- private void DrawEnter()
- {
- if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
- {
- return;
- }
- this.mdata.ChangeType(DrawData.DrawDataType.Select);
- RefreshData();
- SizeModel.gameObject.SetActive(true);
- Debug.Log("发送进入画画模块");
- //先这么写把 实际上不在这里的
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
- }
- private void DrawExit()
- {
- if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
- {
- return;
- }
- this.mdata.ChangeType(DrawData.DrawDataType.Exit);
- RefreshData();
- Debug.Log("发送终止画画");
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
- }
- private void SelectColorClick()
- {
- if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
- {
- return;
- }
- MDrawingMap.gameObject.SetActive(true);
- }
- public Vector3 DrawPoint()
- {
- /*
- if (API_InputSystem.Gazer != null)
- {
- Ray mRay = new Ray(API_InputSystem.Gazer.transform.position, API_InputSystem.Normal);
- RaycastHit hit;
- if (Physics.Raycast(mRay, out hit, 1 << 0))
- {
- return hit.point - API_InputSystem.Normal * 0.05f;
- }
- return API_InputSystem.Gazer.transform.position + API_InputSystem.Normal * dis;
- }
- */
- return API_InputSystem.Position - API_InputSystem.Normal * 0.05f;
- }
- //如果当前检测到的面板和上一次检测到的面板不一样 那就自动停止画画
- private GameObject lastHitGameObj;//上一个检测到的面板
- private void CheckAutoEnd()
- {
- if(this.mdata.CurType == DrawData.DrawDataType.Ready || this.mdata.CurType == DrawData.DrawDataType.End)
- {
- //重新设置 画板的位置
- this.BoardModel.transform.position = API_InputSystem.Gazer.transform.position + API_InputSystem.Normal * dis;
- this.BoardModel.transform.eulerAngles = Quaternion.FromToRotation(Vector3.forward, API_InputSystem.Normal).eulerAngles;
- }
- if (this.mdata.CurType != DrawData.DrawDataType.Start)
- {
- return;
- }
- if (lastHitGameObj == null && API_InputSystem.Target != null)
- {
- lastHitGameObj = API_InputSystem.Target;
- Debug.Log("发送开始画画");
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
- }
- if (lastHitGameObj != null)
- {
- if (API_InputSystem.Target != null)
- {
- if (BoardModel.Equals(lastHitGameObj))//如果之前检测到的是 默认画板
- {
- if (!API_InputSystem.Target.Equals(lastHitGameObj))//新检测到的是其他的碰撞框
- {
- DrawEnd();
- }
- }
- else if (API_InputSystem.Target.Equals(BoardModel)) //如果之前检测到的不是默认画板
- {
- //新检测到的是默认的碰撞框
- DrawEnd();
- }
- else if(Vector3.Distance(this.mdata.point, DrawPoint()) >0.05f)
- {
- //CDebug.Log("2个相邻点距离超过预期值" + Vector3.Distance(this.mdata.point, DrawPoint()));
- //DrawEnd();//2个相邻点距离超过预期值
- }
- }
- else
- {
- DrawEnd();
- }
- }
-
- }
- private void ChangeSize()
- {
- }
- private void ChangeColor()
- {
- }
- private void RefreshData()
- {
- if (this.mdata.CurType == DrawData.DrawDataType.Ready)
- {
- BoardModel.gameObject.SetActive(true);
- EffectModel.gameObject.SetActive(true);
- }
- else if (this.mdata.CurType == DrawData.DrawDataType.Select)
- {
-
- }
- else if (this.mdata.CurType == DrawData.DrawDataType.Exit)
- {
- PenModel.gameObject.SetActive(false);
- BoardModel.gameObject.SetActive(false);
- EffectModel.gameObject.SetActive(false);
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_DRAW_END, this.mdata);
- }
- else if (this.mdata.CurType == DrawData.DrawDataType.Start)
- {
- Debug.Log("广播开始画画");
- BoardModel.gameObject.SetActive(true);
- PenModel.gameObject.SetActive(false);
- EffectModel.gameObject.SetActive(false);
- // this.mdata.point = this.BoardModel.transform.position;
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_DRAW_START, this.mdata);
- }
- else if (this.mdata.CurType == DrawData.DrawDataType.End)
- {
- CDebug.Log("广播结束画画");
- PenModel.gameObject.SetActive(false);
- BoardModel.gameObject.SetActive(false);
- EffectModel.gameObject.SetActive(true);
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_DRAW_END, this.mdata);
- }
- else if (this.mdata.CurType == DrawData.DrawDataType.CleanLast)
- {
- CDebug.Log("准备撤销上一笔的字迹");
- BoardModel.gameObject.SetActive(false);
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_DRAW_UNDO_PREVIOUS, this.mdata);
- }
- else if (this.mdata.CurType == DrawData.DrawDataType.CleanALL)
- {
- CDebug.Log("准备撤销全部字迹");
- BoardModel.gameObject.SetActive(false);
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_DRAW_UNDO_ALL, this.mdata);
- }
- }
- protected override void partAnyKeyUpDelegate(InputKeyCode keyCode, InputDevicePartBase part)
- {
- if(this.mdata.CurType == DrawData.DrawDataType.Start)
- {
- DrawEnd();
- }
- }
- protected override void partAnyKeyDownDelegate(InputKeyCode keyCode, InputDevicePartBase part)
- {
- if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
- {
- return;//过滤掉不是老师点击的情况
- }
- if (API_InputSystem.IsISInitialized() && API_InputSystem.Target != null)
- {
- if (API_InputSystem.Target.Equals(MDrawingMap.gameObject))
- {
- var rect = API_InputSystem.Target.transform as RectTransform;
- Vector3 point = rect.InverseTransformPoint(API_InputSystem.Position);
- var size2 = rect.sizeDelta / 2;
- var pos = Vector2.zero;
- pos.x = Mathf.Clamp(point.x + size2.x, 0, 2 * size2.x);
- pos.y = Mathf.Clamp(point.y + size2.y, 0, 2 * size2.y);
- CDebug.Log(pos.ToString());
- this.TestColor = MDrawingMap.GetColorByPoint(pos);
- this.mdata.SetColor(this.TestColor);
- this.mdata.ChangeType(DrawData.DrawDataType.Select);
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
- }
- }
- if (true)
- {
- if (this.mdata.CurType == DrawData.DrawDataType.Ready || this.mdata.CurType == DrawData.DrawDataType.End)
- {
- DrawStart();
- }
- }
- }
- public Transform LineParent()
- {
- return this.MLineParent;
- }
- private void OwnerChange(System.Object data)
- {
- if (GamePlayerData.Instance.IsFangzhu())
- {
- this.gameObject.SetActive(true);
- }
- else
- {
- this.gameObject.SetActive(false);
- }
- if(this.mdata.CurType == DrawData.DrawDataType.Start)
- {
- //本地自动补发一个停止画的功能
- lastHitGameObj = null;
- this.mdata.ChangeType(DrawData.DrawDataType.End);
- RefreshData();
- }
- }
- }
|