DrawingManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using SC.InputSystem;
  5. public class DrawingManager : PointerDelegate
  6. {
  7. [SerializeField]
  8. private Game3DButton SelectSizeButton;
  9. [SerializeField]
  10. private Game3DButton SelectColorButton;
  11. [SerializeField]
  12. private Game3DButton SelectCleanButton;
  13. [SerializeField]
  14. private Game3DButton StartButton;
  15. [SerializeField]
  16. private Game3DButton ExitButton;
  17. [SerializeField]
  18. private DrawingMap MDrawingMap;
  19. [SerializeField]
  20. private GameObject PenModel;//笔的模型
  21. [SerializeField]
  22. private GameObject SizeModel;//笔的模型
  23. [SerializeField]
  24. private GameObject EffectModel;//笔轨迹的特效
  25. [SerializeField]
  26. private GameObject BoardModel;//画板 挡板
  27. [SerializeField]
  28. private Transform MLineParent;//写字的父节点
  29. [SerializeField]
  30. private GameObject BgModel;//背景版 画板
  31. public Color TestColor;
  32. public static DrawingManager _instance;
  33. private float dis = 3.0f;
  34. public static DrawingManager Instance
  35. {
  36. get {
  37. return _instance;
  38. }
  39. }
  40. private DrawData mdata;
  41. private void Awake()
  42. {
  43. _instance = this;
  44. mdata = new DrawData();
  45. //设置默认值
  46. mdata.size = 1;
  47. mdata.SetColor(Color.white);
  48. }
  49. void Start()
  50. {
  51. MDrawingMap.gameObject.SetActive(false);
  52. SizeModel.gameObject.SetActive(false);
  53. PenModel.gameObject.SetActive(false);
  54. EffectModel.gameObject.SetActive(false);
  55. BoardModel.gameObject.SetActive(false);
  56. BgModel.gameObject.SetActive(false);
  57. SelectColorButton.onClick.AddListener(SelectColorClick);
  58. SelectSizeButton.onClick.AddListener(DrawEnter);
  59. SelectCleanButton.onClick.AddListener(DrawCleanALL);
  60. StartButton.onClick.AddListener(DrawReady);
  61. ExitButton.onClick.AddListener(DrawExit);
  62. MessageCenterController.Instance.Register(GameEnum.MESSAGE_ACCEPT_DRAW_SOCKET_DATA, TongbuDraw);
  63. MessageCenterController.Instance.Register(GameEnum.MESSAGE_ENTER_COURSE, EnterCourse);//进入房间
  64. MessageCenterController.Instance.Register(GameEnum.MESSAGE_EXIT_COURSE, ExitCourse);//关闭房间
  65. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_CHANGE_ROOM_OWNER_REFRESH, OwnerChange);//有房主的变动
  66. this.gameObject.SetActive(false);
  67. }
  68. private void OnDestroy()
  69. {
  70. SelectColorButton.onClick.RemoveListener(SelectColorClick);
  71. SelectSizeButton.onClick.RemoveListener(DrawEnter);
  72. SelectCleanButton.onClick.RemoveListener(DrawCleanALL);
  73. StartButton.onClick.RemoveListener(DrawReady);
  74. ExitButton.onClick.RemoveListener(DrawExit);
  75. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_ACCEPT_DRAW_SOCKET_DATA, TongbuDraw);
  76. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_ENTER_COURSE, EnterCourse);//进入房间
  77. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_EXIT_COURSE, ExitCourse);//关闭房间
  78. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_CHANGE_ROOM_OWNER_REFRESH, OwnerChange);//有房主的变动
  79. }
  80. private void EnterCourse(System.Object datas = null)
  81. {
  82. if (GamePlayerData.Instance.IsFangzhu())
  83. {
  84. this.gameObject.SetActive(true);
  85. }
  86. BgModel.gameObject.SetActive(true);
  87. }
  88. private void ExitCourse(System.Object datas = null)
  89. {
  90. this.gameObject.SetActive(false);
  91. this.mdata.ChangeType(DrawData.DrawDataType.CleanALL);
  92. RefreshData();
  93. BgModel.gameObject.SetActive(false);
  94. }
  95. private void TongbuDraw(System.Object data = null)
  96. {
  97. if (GamePlayerData.Instance.room_data.IsReconnectOwner())
  98. {
  99. return;
  100. }
  101. this.mdata = data as DrawData;
  102. RefreshData();
  103. }
  104. private void LateUpdate()
  105. {
  106. if (Input.GetKeyDown(KeyCode.T))
  107. {
  108. DrawStart();
  109. }
  110. else if (Input.GetKeyUp(KeyCode.T))
  111. {
  112. DrawEnd();
  113. }
  114. if (API_InputSystem.IsISInitialized())
  115. {
  116. if (PenModel.activeInHierarchy)
  117. {
  118. PenModel.transform.position = API_InputSystem.Position;
  119. PenModel.transform.eulerAngles = API_InputSystem.Normal;
  120. }
  121. if (EffectModel.activeInHierarchy)
  122. {
  123. EffectModel.transform.position = DrawPoint();
  124. //PenModel.transform.eulerAngles = API_InputSystem.Normal;
  125. }
  126. CheckAutoEnd();
  127. }
  128. }
  129. private void DrawReady()
  130. {
  131. if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
  132. {
  133. return;
  134. }
  135. this.mdata.ChangeType(DrawData.DrawDataType.Ready);
  136. this.mdata.point = DrawPoint();
  137. RefreshData();
  138. Debug.Log("发送准备画画");
  139. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
  140. }
  141. private void DrawClean()
  142. {
  143. if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
  144. {
  145. return;
  146. }
  147. this.mdata.ChangeType(DrawData.DrawDataType.CleanLast);
  148. RefreshData();
  149. Debug.Log("发送撤销上一步");
  150. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
  151. }
  152. private void DrawCleanALL()
  153. {
  154. if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
  155. {
  156. return;
  157. }
  158. this.mdata.ChangeType(DrawData.DrawDataType.CleanALL);
  159. RefreshData();
  160. Debug.Log("发送撤销上一步");
  161. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
  162. }
  163. private void DrawStart()
  164. {
  165. if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
  166. {
  167. return;
  168. }
  169. this.mdata.ChangeType(DrawData.DrawDataType.Start);
  170. RefreshData();
  171. }
  172. private void DrawEnd()
  173. {
  174. if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
  175. {
  176. return;
  177. }
  178. lastHitGameObj = null;
  179. this.mdata.ChangeType(DrawData.DrawDataType.End);
  180. RefreshData();
  181. Debug.Log("发送停止画画");
  182. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
  183. }
  184. private void DrawEnter()
  185. {
  186. if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
  187. {
  188. return;
  189. }
  190. this.mdata.ChangeType(DrawData.DrawDataType.Select);
  191. RefreshData();
  192. SizeModel.gameObject.SetActive(true);
  193. Debug.Log("发送进入画画模块");
  194. //先这么写把 实际上不在这里的
  195. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
  196. }
  197. private void DrawExit()
  198. {
  199. if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
  200. {
  201. return;
  202. }
  203. this.mdata.ChangeType(DrawData.DrawDataType.Exit);
  204. RefreshData();
  205. Debug.Log("发送终止画画");
  206. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
  207. }
  208. private void SelectColorClick()
  209. {
  210. if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
  211. {
  212. return;
  213. }
  214. MDrawingMap.gameObject.SetActive(true);
  215. }
  216. public Vector3 DrawPoint()
  217. {
  218. /*
  219. if (API_InputSystem.Gazer != null)
  220. {
  221. Ray mRay = new Ray(API_InputSystem.Gazer.transform.position, API_InputSystem.Normal);
  222. RaycastHit hit;
  223. if (Physics.Raycast(mRay, out hit, 1 << 0))
  224. {
  225. return hit.point - API_InputSystem.Normal * 0.05f;
  226. }
  227. return API_InputSystem.Gazer.transform.position + API_InputSystem.Normal * dis;
  228. }
  229. */
  230. return API_InputSystem.Position - API_InputSystem.Normal * 0.05f;
  231. }
  232. //如果当前检测到的面板和上一次检测到的面板不一样 那就自动停止画画
  233. private GameObject lastHitGameObj;//上一个检测到的面板
  234. private void CheckAutoEnd()
  235. {
  236. if(this.mdata.CurType == DrawData.DrawDataType.Ready || this.mdata.CurType == DrawData.DrawDataType.End)
  237. {
  238. //重新设置 画板的位置
  239. this.BoardModel.transform.position = API_InputSystem.Gazer.transform.position + API_InputSystem.Normal * dis;
  240. this.BoardModel.transform.eulerAngles = Quaternion.FromToRotation(Vector3.forward, API_InputSystem.Normal).eulerAngles;
  241. }
  242. if (this.mdata.CurType != DrawData.DrawDataType.Start)
  243. {
  244. return;
  245. }
  246. if (lastHitGameObj == null && API_InputSystem.Target != null)
  247. {
  248. lastHitGameObj = API_InputSystem.Target;
  249. Debug.Log("发送开始画画");
  250. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
  251. }
  252. if (lastHitGameObj != null)
  253. {
  254. if (API_InputSystem.Target != null)
  255. {
  256. if (BoardModel.Equals(lastHitGameObj))//如果之前检测到的是 默认画板
  257. {
  258. if (!API_InputSystem.Target.Equals(lastHitGameObj))//新检测到的是其他的碰撞框
  259. {
  260. DrawEnd();
  261. }
  262. }
  263. else if (API_InputSystem.Target.Equals(BoardModel)) //如果之前检测到的不是默认画板
  264. {
  265. //新检测到的是默认的碰撞框
  266. DrawEnd();
  267. }
  268. else if(Vector3.Distance(this.mdata.point, DrawPoint()) >0.05f)
  269. {
  270. //CDebug.Log("2个相邻点距离超过预期值" + Vector3.Distance(this.mdata.point, DrawPoint()));
  271. //DrawEnd();//2个相邻点距离超过预期值
  272. }
  273. }
  274. else
  275. {
  276. DrawEnd();
  277. }
  278. }
  279. }
  280. private void ChangeSize()
  281. {
  282. }
  283. private void ChangeColor()
  284. {
  285. }
  286. private void RefreshData()
  287. {
  288. if (this.mdata.CurType == DrawData.DrawDataType.Ready)
  289. {
  290. BoardModel.gameObject.SetActive(true);
  291. EffectModel.gameObject.SetActive(true);
  292. }
  293. else if (this.mdata.CurType == DrawData.DrawDataType.Select)
  294. {
  295. }
  296. else if (this.mdata.CurType == DrawData.DrawDataType.Exit)
  297. {
  298. PenModel.gameObject.SetActive(false);
  299. BoardModel.gameObject.SetActive(false);
  300. EffectModel.gameObject.SetActive(false);
  301. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_DRAW_END, this.mdata);
  302. }
  303. else if (this.mdata.CurType == DrawData.DrawDataType.Start)
  304. {
  305. Debug.Log("广播开始画画");
  306. BoardModel.gameObject.SetActive(true);
  307. PenModel.gameObject.SetActive(false);
  308. EffectModel.gameObject.SetActive(false);
  309. // this.mdata.point = this.BoardModel.transform.position;
  310. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_DRAW_START, this.mdata);
  311. }
  312. else if (this.mdata.CurType == DrawData.DrawDataType.End)
  313. {
  314. CDebug.Log("广播结束画画");
  315. PenModel.gameObject.SetActive(false);
  316. BoardModel.gameObject.SetActive(false);
  317. EffectModel.gameObject.SetActive(true);
  318. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_DRAW_END, this.mdata);
  319. }
  320. else if (this.mdata.CurType == DrawData.DrawDataType.CleanLast)
  321. {
  322. CDebug.Log("准备撤销上一笔的字迹");
  323. BoardModel.gameObject.SetActive(false);
  324. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_DRAW_UNDO_PREVIOUS, this.mdata);
  325. }
  326. else if (this.mdata.CurType == DrawData.DrawDataType.CleanALL)
  327. {
  328. CDebug.Log("准备撤销全部字迹");
  329. BoardModel.gameObject.SetActive(false);
  330. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_DRAW_UNDO_ALL, this.mdata);
  331. }
  332. }
  333. protected override void partAnyKeyUpDelegate(InputKeyCode keyCode, InputDevicePartBase part)
  334. {
  335. if(this.mdata.CurType == DrawData.DrawDataType.Start)
  336. {
  337. DrawEnd();
  338. }
  339. }
  340. protected override void partAnyKeyDownDelegate(InputKeyCode keyCode, InputDevicePartBase part)
  341. {
  342. if (!GamePlayerData.Instance.room_data.IsReconnectOwner())
  343. {
  344. return;//过滤掉不是老师点击的情况
  345. }
  346. if (API_InputSystem.IsISInitialized() && API_InputSystem.Target != null)
  347. {
  348. if (API_InputSystem.Target.Equals(MDrawingMap.gameObject))
  349. {
  350. var rect = API_InputSystem.Target.transform as RectTransform;
  351. Vector3 point = rect.InverseTransformPoint(API_InputSystem.Position);
  352. var size2 = rect.sizeDelta / 2;
  353. var pos = Vector2.zero;
  354. pos.x = Mathf.Clamp(point.x + size2.x, 0, 2 * size2.x);
  355. pos.y = Mathf.Clamp(point.y + size2.y, 0, 2 * size2.y);
  356. CDebug.Log(pos.ToString());
  357. this.TestColor = MDrawingMap.GetColorByPoint(pos);
  358. this.mdata.SetColor(this.TestColor);
  359. this.mdata.ChangeType(DrawData.DrawDataType.Select);
  360. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, this.mdata);
  361. }
  362. }
  363. if (true)
  364. {
  365. if (this.mdata.CurType == DrawData.DrawDataType.Ready || this.mdata.CurType == DrawData.DrawDataType.End)
  366. {
  367. DrawStart();
  368. }
  369. }
  370. }
  371. public Transform LineParent()
  372. {
  373. return this.MLineParent;
  374. }
  375. private void OwnerChange(System.Object data)
  376. {
  377. if (GamePlayerData.Instance.IsFangzhu())
  378. {
  379. this.gameObject.SetActive(true);
  380. }
  381. else
  382. {
  383. this.gameObject.SetActive(false);
  384. }
  385. if(this.mdata.CurType == DrawData.DrawDataType.Start)
  386. {
  387. //本地自动补发一个停止画的功能
  388. lastHitGameObj = null;
  389. this.mdata.ChangeType(DrawData.DrawDataType.End);
  390. RefreshData();
  391. }
  392. }
  393. }