BoardDlg.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. using BeinLab.Util;
  2. using DG.Tweening;
  3. using LitJson;
  4. using PublicTools.XMLDataBase;
  5. using ShadowStudio.Model;
  6. using ShadowStudio.UI;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using UnityEngine;
  11. using UnityEngine.SceneManagement;
  12. using UnityEngine.UI;
  13. using XRTool.UI;
  14. using XRTool.Util;
  15. using XRTool.WorldUI;
  16. namespace ShadowStudio.Tool
  17. {
  18. /// <summary>
  19. /// 黑板类
  20. /// </summary>
  21. [RequireComponent(typeof(WorldDlg))]
  22. public class BoardDlg : UnitySingleton<BoardDlg>
  23. {
  24. public WorldDlg dlg;
  25. public BoardConf boardConf;
  26. private RectTransform container;
  27. /// <summary>
  28. /// 存储窗口的起点和终点
  29. /// </summary>
  30. private Dictionary<Vector2, Vector2> cellMap;
  31. private List<WorldDlg> dlgMap = new List<WorldDlg>();
  32. protected override void OnDestroy()
  33. {
  34. base.OnDestroy();
  35. if (cellMap != null)
  36. {
  37. cellMap.Clear();
  38. }
  39. cellMap = null;
  40. }
  41. /// <summary>
  42. /// 初始化窗口
  43. /// </summary>
  44. private void InitConf()
  45. {
  46. #if UNITY_EDITOR || !UNITY_ANDROID
  47. TableInterface table = XSql.Instance.OpenTable(ResourcesManager.LocalPath, typeof(BoardConf).Name, ".xml", true);
  48. if (!table.Open())
  49. {
  50. table.Create(typeof(BoardConf).Name);
  51. }
  52. var boards = table.FindAllData<BoardConf>();
  53. if (boards == null || boards.Count < 1)
  54. {
  55. boardConf = new BoardConf();
  56. table.InsertData(boardConf);
  57. table.Close();
  58. }
  59. else
  60. {
  61. boardConf = boards[0];
  62. }
  63. #else
  64. string fullPath = Path.Combine(ResourcesManager.LocalPath, typeof(BoardConf).Name+".xml");
  65. GameNode.Instance. StartCoroutine( XSql.Instance.ReadServerData(fullPath,(List<BoardConf> boards) => {
  66. if (boards != null && boards.Count > 0)
  67. {
  68. boardConf = boards[0];
  69. dlg.DlgTrans.sizeDelta = boardConf.Boundary.Trans();
  70. dlg.SetScale(dlg.DlgTrans.sizeDelta);
  71. }
  72. }));
  73. #endif
  74. }
  75. private GameObject blackGroud;
  76. public GameObject BlackGroud
  77. {
  78. get
  79. {
  80. /*
  81. if (!blackGroud)
  82. {
  83. var dlg = Resources.Load("BackGround");
  84. if (dlg)
  85. {
  86. blackGroud = GameObject.Instantiate(dlg) as GameObject;
  87. GameNode.Instance.SetParent(ObjNode.WorldCanvas, blackGroud.transform, blackGroud.transform.localPosition, blackGroud.transform.localEulerAngles, blackGroud.transform.localScale);
  88. }
  89. }*/
  90. blackGroud = dlg.GetBreadthChild<Transform>("FrontBG").gameObject;
  91. return blackGroud;
  92. }
  93. }
  94. protected override void Awake()
  95. {
  96. base.Awake();
  97. InitConf();
  98. dlg = GetComponent<WorldDlg>();
  99. }
  100. private static Vector3 initBoard = Vector3.zero;
  101. //private Button loginOutBtn;
  102. private Button outRoomBtn;
  103. private TextMesh roomName;
  104. private TextMesh roomType;
  105. public TextMesh roomNum;
  106. private TextMesh roomPass;
  107. private TextMesh roomCreator;
  108. private TextMesh pubRoomType;
  109. private TextMesh roomExplain;
  110. private Button createSenceBtn;
  111. private Button showInfoBtn;
  112. private Button heidInfoBtn;
  113. private Button showUserListBtn;
  114. private Button heidUserListBtn;
  115. private GameObject UIRoot;
  116. private GameObject FullUIRoot;
  117. public GameObject AllInfoObj;
  118. public GameObject UserListObj;
  119. private void Start()
  120. {
  121. container = dlg.GetBreadthChild<RectTransform>("Container");
  122. //loginOutBtn = dlg.GetBreadthChild<Button>("LoginOutBtn");
  123. outRoomBtn = dlg.GetBreadthChild<Button>("OutRoomBtn");
  124. showInfoBtn = dlg.GetBreadthChild<Button>("ShowInfoBtn");
  125. heidInfoBtn = dlg.GetBreadthChild<Button>("HeidInfoBtn");
  126. showInfoBtn.onClick.AddListener(ShowAllInfo);
  127. heidInfoBtn.onClick.AddListener(HeidAllInfo);
  128. roomName = dlg.GetBreadthChild<TextMesh>("RoomName");
  129. roomType = dlg.GetBreadthChild<TextMesh>("RoomType");
  130. roomNum = dlg.GetBreadthChild<TextMesh>("RoomNum");
  131. roomPass = dlg.GetBreadthChild<TextMesh>("RoomPass");
  132. roomCreator = dlg.GetBreadthChild<TextMesh>("RoomCreator");
  133. pubRoomType = dlg.GetBreadthChild<TextMesh>("PubRoomType");
  134. roomExplain = dlg.GetBreadthChild<TextMesh>("RoomExplain");
  135. createSenceBtn = dlg.GetBreadthChild<Button>("CreateSenceBtn");
  136. showUserListBtn = dlg.GetBreadthChild<Button>("ShowUserListBtn");
  137. heidUserListBtn = dlg.GetBreadthChild<Button>("HeidUserListBtn");
  138. if (CommonMethod.roomConfig != null)
  139. {
  140. #if UNITY_EDITOR
  141. roomName.text = CommonMethod.roomConfig.Room_name + " (" + CommonMethod.roomConfig.Id + ")";
  142. #else
  143. roomName.text = CommonMethod.roomConfig.Room_name ;
  144. #endif
  145. Debug.Log("房间密码"+CommonMethod.roomConfig.Room_password);
  146. if (string.IsNullOrEmpty(CommonMethod.roomConfig.Room_password))
  147. {
  148. roomPass.text = LanguageMgr.Instance.GetMessage("1090").Message;
  149. }
  150. else
  151. {
  152. if (!string.IsNullOrEmpty(CommonMethod.roomPassWord))
  153. {
  154. roomPass.text = CommonMethod.roomPassWord;
  155. CommonMethod.roomPassWord = "";
  156. }
  157. }
  158. roomCreator.text = CommonMethod.roomConfig.Nick_name;
  159. if (CommonMethod.roomConfig.Room_explain == "")
  160. {
  161. roomExplain.text = LanguageMgr.Instance.GetMessage("1090").Message;
  162. }
  163. else
  164. {
  165. roomExplain.text = CommonMethod.roomConfig.Room_explain;
  166. }
  167. //roomExplain.text = CommonMethod.roomConfig.Room_explain;
  168. if (int.Parse(CommonMethod.roomConfig.Max_member) < 1)
  169. {
  170. CommonMethod.roomConfig.Max_member = "1";
  171. }
  172. switch (CommonMethod.roomConfig.Type)
  173. {
  174. case "1":
  175. roomType.text = LanguageMgr.Instance.GetMessage("1036").Message;
  176. roomNum.text = "1/1";
  177. pubRoomType.text = LanguageMgr.Instance.GetMessage("1090").Message;
  178. break;
  179. case "2":
  180. roomType.text = LanguageMgr.Instance.GetMessage("1037").Message;
  181. roomNum.text = (int.Parse(CommonMethod.roomConfig.Number) + 1) + "/" + CommonMethod.roomConfig.Max_member;
  182. switch (CommonMethod.roomConfig.Room_type)
  183. {
  184. case "1":
  185. pubRoomType.text = LanguageMgr.Instance.GetMessage("1045").Message;
  186. break;
  187. case "2":
  188. pubRoomType.text = LanguageMgr.Instance.GetMessage("1046").Message;
  189. break;
  190. default:
  191. break;
  192. }
  193. break;
  194. }
  195. }
  196. createSenceBtn.onClick.AddListener(OnClickCreateSence);
  197. outRoomBtn.onClick.AddListener(OnClickOutRoom);
  198. showUserListBtn.onClick.AddListener(ShowUserList);
  199. heidUserListBtn.onClick.AddListener(HeidUserList);
  200. if (boardConf != null)
  201. {
  202. dlg.DlgTrans.sizeDelta = boardConf.Boundary.Trans();
  203. dlg.SetScale(dlg.DlgTrans.sizeDelta);
  204. }
  205. if (initBoard == Vector3.zero)
  206. {
  207. initBoard = transform.localPosition;
  208. }
  209. AllInfoObj.SetActive(false);
  210. UserListObj.SetActive(true);
  211. showUserListBtn.gameObject.SetActive(false);
  212. heidUserListBtn.gameObject.SetActive(true);
  213. Vector3 realPos = new Vector3(0,0,0);
  214. //if (CScript.App.AppManager.Instance.isRunHolographic)
  215. //{
  216. // realPos = OpenXRCamera.Instance.head.TransformPoint(new Vector3(-initBoard.x, -initBoard.y, -initBoard.z / 3f));
  217. //}
  218. //else
  219. //{
  220. // realPos = OpenXRCamera.Instance.head.TransformPoint(initBoard);
  221. //}
  222. realPos = OpenXRCamera.Instance.head.TransformPoint(initBoard);
  223. transform.position = realPos;
  224. Vector3 pos = GameSession.Instance.gameHead.position;
  225. Vector3 forward = (realPos - pos).normalized;
  226. forward.y = 0;
  227. transform.forward = forward;
  228. GetComponent<TransferCell>().DragTransfer += OnDragTransfer;
  229. OnDragTransfer();
  230. //selectTarget.gameObject.SetActive(false);
  231. BlackGroud.SetActive(true);
  232. WorldDlgContainer.BackGD = BlackGroud;
  233. WorldDlgContainer.BackGD.GetComponent<MeshRenderer>().material.SetFloat("_Int", 10);
  234. UIRoot = UnityUtil.GetBreadthChild(this.transform, "UIRoot");
  235. // UIRoot.SetActive(false);
  236. FullUIRoot = UnityUtil.GetBreadthChild(this.transform, "FullUIRoot");
  237. // FullUIRoot.SetActive(false);
  238. //被zby替换的代码
  239. //if (ItemView.itemPos != Vector3.zero)
  240. if (MenuIcon.itemPos != Vector3.zero)
  241. {
  242. GameEffect.setAlpha(FullUIRoot, 0, 0.1f);
  243. GameEffect.setAlpha(UIRoot, 0, 0.1f);
  244. initScale = dlg.transform.localScale;
  245. initPos = dlg.transform.position;
  246. this.gameObject.SetActive(false);
  247. TimerMgr.Instance.CreateTimer(() =>
  248. {
  249. //被zby替换的代码
  250. //dlg.transform.position = ItemView.itemPos;
  251. dlg.transform.position = MenuIcon.itemPos;
  252. dlg.transform.localScale = dlg.transform.localScale / 35;
  253. this.gameObject.SetActive(true);
  254. MeshRenderer mr = WorldDlgContainer.BackGD.GetComponent<MeshRenderer>();
  255. mr.material.DOTiling(new Vector2(1, 1), "_TextureSample0", 1f);
  256. mr.material.DOOffset(new Vector2(0, 0), "_TextureSample0", 1f);
  257. dlg.transform.DOMove(initPos, 1f);
  258. GameEffect.setAlpha(FullUIRoot, 1, 1);
  259. GameEffect.setAlpha(UIRoot, 1, 1);
  260. mr.material.DOFloat(4, "_Int", 1f);
  261. dlg.transform.DOScale(initScale, 1f).OnComplete(() =>
  262. {
  263. });
  264. /*
  265. dlg.transform.DOScale(dlg.transform.localScale*4, 0.5f).OnComplete(()=> {
  266. dlg.transform.DOScale(initScale, 1f).OnComplete(()=> {
  267. GameEffect.setAlpha(FullUIRoot, 1, 1);
  268. GameEffect.setAlpha(UIRoot, 1, 1);
  269. PictrueContainer.BackGD.GetComponent<MeshRenderer>().material.DOFloat(4, "_Int", 1f);
  270. });
  271. });*/
  272. }, 0.2f);
  273. }
  274. }
  275. private void OnClickCreateSence()
  276. {
  277. if (ItemSenceList.Instance)
  278. {
  279. string scenename = "场景" + ItemSenceList.Instance.GetSceneCount();
  280. WSHandler.Room.CreateScene(scenename);
  281. }
  282. }
  283. private void HeidUserList()
  284. {
  285. UserListObj.SetActive(false);
  286. heidUserListBtn.gameObject.SetActive(false);
  287. showUserListBtn.gameObject.SetActive(true);
  288. }
  289. private void ShowUserList()
  290. {
  291. UserListObj.SetActive(true);
  292. showUserListBtn.gameObject.SetActive(false);
  293. heidUserListBtn.gameObject.SetActive(true);
  294. }
  295. private void HeidAllInfo()
  296. {
  297. AllInfoObj.SetActive(false);
  298. }
  299. private void ShowAllInfo()
  300. {
  301. AllInfoObj.SetActive(true);
  302. }
  303. private void OnClickOutRoom()
  304. {
  305. TransferCell.CancelCell();
  306. if (ConsoleDlg.Instance)
  307. {
  308. ConsoleDlg.Instance.CloseScan();
  309. }
  310. AgoraRTCManager.Instance.LeaveChannel();
  311. AgoraRTCManager.Instance.ClearDatas();
  312. // AppManager.Instance.MeshPoint.SetActive(false);
  313. Debug.Log("OutRoom 1");
  314. DestoryARcamera();
  315. // WSHandler.Room.ExitRoom();
  316. ShowViewMgr.Instance.SelfLeaveRoom();
  317. WSHandler.roomCloes();
  318. Vector3 posRoom = outRoomBtn.transform.position;
  319. Debug.Log("OutRoom 2");
  320. //被zby替换的代码
  321. //ItemView.itemPos = posRoom;
  322. MenuIcon.itemPos = posRoom;
  323. if (WorldDlgContainer.BackGD!= null&& WorldDlgContainer.BackGD.GetComponent<MeshRenderer>() == null)
  324. Debug.Log("Not BackGD");
  325. Debug.Log("OutRoom 3");
  326. // SceneManager.LoadSceneAsync("Home", LoadSceneMode.Additive);
  327. if (posRoom != Vector3.zero)
  328. {
  329. // MeshRenderer mr = WorldDlgContainer.BackGD.GetComponent<MeshRenderer>();
  330. MeshRenderer mr = BlackGroud.GetComponent<MeshRenderer>();
  331. Debug.Log("OutRoom 4");
  332. mr.material.DOTiling(new Vector2(0.205f, 0.23f), "_TextureSample0", 0.5f).SetDelay<Tween>(0.2f);
  333. mr.material.DOOffset(new Vector2(0.3F, 0.3F), "_TextureSample0", 0.5f).SetDelay<Tween>(0.2f);
  334. mr.material.DOFloat(10, "_Int", 0.5f).SetDelay<Tween>(0.2f);
  335. Debug.Log("OutRoom 5");
  336. dlg.transform.DOMove(posRoom, 0.5f).SetDelay<Tween>(0.2f).OnComplete<Tween>(() =>
  337. {
  338. ShowViewMgr.Instance.LoadHomeSence();
  339. }).SetDelay<Tween>(0.3f);
  340. Debug.Log("OutRoom 6");
  341. dlg.transform.DOScale(initScale / 30, 0.5f).SetDelay<Tween>(0.3f);
  342. Debug.Log("OutRoom 7");
  343. if (GameNode.Instance)
  344. {
  345. if (GameNode.Instance.GoodsContainerMap != null && GameNode.Instance.GoodsContainerMap.Count > 0)
  346. {
  347. foreach (int index in GameNode.Instance.GoodsContainerMap.Keys)
  348. {
  349. if (GameNode.Instance.GoodsContainerMap[index] != null&& GameNode.Instance.GoodsContainerMap[index].GetInstace())
  350. {
  351. GameNode.Instance.GoodsContainerMap[index].GetInstace().transform.DOMove(posRoom, 0.5f).SetDelay<Tween>(0.2f).OnComplete<Tween>(() =>
  352. {
  353. });
  354. GameNode.Instance.GoodsContainerMap[index].GetInstace().transform.DOScale(initScale / 30, 0.5f);
  355. }
  356. }
  357. Debug.Log("OutRoom 8");
  358. }
  359. }
  360. else
  361. {
  362. if (GameNode.Instance.GoodsContainerMap != null && GameNode.Instance.GoodsContainerMap.Count > 0)
  363. {
  364. foreach (int index in GameNode.Instance.GoodsContainerMap.Keys)
  365. {
  366. if (GameNode.Instance.GoodsContainerMap[index] != null)
  367. {
  368. GameNode.Instance.GoodsContainerMap[index].GetInstace().transform.DOMove(posRoom, 0.5f).SetDelay<Tween>(0.2f).OnComplete<Tween>(() =>
  369. {
  370. });
  371. GameNode.Instance.GoodsContainerMap[index].GetInstace().transform.DOScale(initScale / 30, 0.5f);
  372. }
  373. }
  374. Debug.Log("OutRoom 9");
  375. }
  376. }
  377. }
  378. else
  379. {
  380. ShowViewMgr.Instance.LoadHomeSence();
  381. Debug.Log("OutRoom 10");
  382. }
  383. }
  384. public void DestoryARcamera()
  385. {
  386. if (CheckDeviceType.checkIsPhone())
  387. {
  388. return;
  389. }
  390. if (ConsoleDlg.Instance!=null && ConsoleDlg.Instance.vufora_camera != null)
  391. {
  392. if (DeviceType.type == "Phone")
  393. {
  394. }
  395. else
  396. {
  397. ConsoleDlg.Instance.vufora_camera.enabled = false;
  398. }
  399. }
  400. if (ConsoleDlg.Instance != null && ConsoleDlg.Instance.vufora_camera != null)
  401. {
  402. Destroy(ConsoleDlg.Instance.vufora_camera.gameObject);
  403. }
  404. }
  405. private void OnClickLoginOut()
  406. {
  407. TransferCell.CancelCell();
  408. DestoryARcamera();
  409. WSHandler.Room.ExitRoom();
  410. ShowViewMgr.Instance.SelfLeaveRoom();
  411. WSHandler.roomCloes();
  412. WSHandler.clientClosed();
  413. ShowViewMgr.Instance.LoadLoginSence();
  414. }
  415. public void OnDragTransfer(TransferState state = TransferState.Doing)
  416. {
  417. if (GameNode.Instance)
  418. {
  419. GameNode.Instance.transform.position = transform.position;
  420. GameNode.Instance.transform.rotation = transform.rotation;
  421. transform.localPosition = Vector3.zero;
  422. transform.localRotation = Quaternion.identity;
  423. }
  424. else
  425. {
  426. GameNode.InitComplte += () =>
  427. {
  428. OnDragTransfer();
  429. };
  430. }
  431. }
  432. public void EnterCreatSceneBtn()
  433. {
  434. GetComponent<TransferCell>().DragStop();
  435. }
  436. /// <summary>
  437. /// 添加此窗口到黑板的合适位置
  438. /// 判断是否和已在窗口上的存在交叉
  439. /// 如果没有任何交叉,则加入
  440. /// 如果有交叉,移动至合适的位置
  441. /// Vector2 dis = b.center - a.center;
  442. /// if (((int) a.width + (int) b.width) / 2 > Math.Abs(dis.x) && ((int) a.height + (int) b.height) / 2 > Math.Abs(dis.y))
  443. /// </summary>
  444. /// <param name="dlg"></param>
  445. public void AddDlg(WorldDlg dlg, Vector3 prefectPos, bool isUser)
  446. {
  447. if (!dlgMap.Contains(dlg))
  448. {
  449. //dlgMap.Sort((dlg1, dlg2) => { return SortDlg(dlg1, dlg2, prefectPos); });
  450. bool isNeedMove = false;
  451. for (int i = dlgMap.Count - 1; i >= 0; i--)
  452. {
  453. var oldDlg = dlgMap[i];
  454. if (!oldDlg)
  455. {
  456. dlgMap.RemoveAt(i);
  457. continue;
  458. }
  459. Vector2 offsize = dlg.DlgTrans.anchoredPosition - oldDlg.DlgTrans.anchoredPosition;
  460. if ((oldDlg.DlgSize.x + dlg.DlgSize.x) / 2 > Math.Abs(offsize.x) &&
  461. (oldDlg.DlgSize.y + dlg.DlgSize.y) / 2 > Math.Abs(offsize.y))
  462. {
  463. isNeedMove = true;
  464. break;
  465. }
  466. }
  467. //for (int i = 0; i < dlgMap.Count; i++)
  468. //{
  469. // var oldDlg = dlgMap[i];
  470. // Vector2 offsize = dlg.DlgTrans.anchoredPosition - oldDlg.DlgTrans.anchoredPosition;
  471. // if ((oldDlg.DlgSize.x + dlg.DlgSize.x) / 2 > Math.Abs(offsize.x) &&
  472. // (oldDlg.DlgSize.y + dlg.DlgSize.y) / 2 > Math.Abs(offsize.y))
  473. // {
  474. // isNeedMove = true;
  475. // break;
  476. // }
  477. //}
  478. ///如果存在重叠的情况,需要移动
  479. ///计算合适的空余位置,并移动至合适的位置中去
  480. if (isNeedMove)
  481. {
  482. prefectPos.z = -(boardConf.Adsorption - boardConf.ShowDis) / 2;
  483. dlg.ContainerState = ContainerState.Free;
  484. }
  485. else
  486. {
  487. prefectPos.z = boardConf.ShowDis;
  488. dlgMap.Add(dlg);
  489. dlg.isOnBoard = true;
  490. }
  491. dlg.DlgTrans.DOKill();
  492. dlg.DlgTrans.DOAnchorPos3D(prefectPos, 0.3f).OnComplete(() =>
  493. {
  494. if (isUser && dlg.Transfer)
  495. {
  496. dlg.Transfer.DisActiveCell();
  497. }
  498. });
  499. }
  500. }
  501. private Vector3 initScale;
  502. private Vector3 initPos;
  503. /// <summary>
  504. /// 对窗口进行排序,排序规则:按照距离center点的远近进行排序
  505. /// </summary>
  506. /// <param name="dlg1"></param>
  507. /// <param name="dlg2"></param>
  508. /// <returns></returns>
  509. private int SortDlg(WorldDlg dlg1, WorldDlg dlg2, Vector2 center)
  510. {
  511. float dis1 = Vector2.Distance(dlg1.DlgTrans.anchoredPosition, center);
  512. float dis2 = Vector2.Distance(dlg2.DlgTrans.anchoredPosition, center);
  513. return dis1 > dis2 ? 1 : dis1 < dis2 ? -1 : 0;
  514. }
  515. public void RemoveDlg(WorldDlg dlg)
  516. {
  517. if (dlgMap.Contains(dlg))
  518. {
  519. dlgMap.Remove(dlg);
  520. dlg.isOnBoard = false;
  521. }
  522. }
  523. /// <summary>
  524. /// 尝试申请一个空间
  525. /// </summary>
  526. /// <param name="startPos"></param>
  527. /// <param name="endPos"></param>
  528. /// <param name="offset"></param>
  529. /// <returns></returns>
  530. public bool TryGetSpace(Vector2 startPos, Vector2 endPos, out Vector2 offset)
  531. {
  532. offset = Vector2.zero;
  533. if ((endPos.x - startPos.x) > container.rect.size.x || (endPos.y - startPos.y) > container.rect.size.y)
  534. {
  535. UnityLog.Instance.LogError("图片尺寸超过黑板大小!" + startPos.ToString() + "_" + endPos.ToString());
  536. return false;
  537. }
  538. if (cellMap == null)
  539. {
  540. cellMap = new Dictionary<Vector2, Vector2>();
  541. Vector2 tmpStart = startPos;
  542. while (tmpStart.x < 0)
  543. {
  544. tmpStart.x += boardConf.CellSize.x / 2;
  545. }
  546. while (tmpStart.y > 0)
  547. {
  548. tmpStart.y -= boardConf.CellSize.y / 2;
  549. }
  550. tmpStart.x = UnityUtil.GetNearst(tmpStart.x, boardConf.CellSize.x);
  551. tmpStart.y = UnityUtil.GetNearst(tmpStart.y, boardConf.CellSize.y);
  552. offset = tmpStart - startPos;
  553. return true;
  554. }
  555. else
  556. {
  557. Vector2 tmpStart = startPos;
  558. while (tmpStart.x < 0)
  559. {
  560. tmpStart.x += boardConf.CellSize.x / 2;
  561. }
  562. while (tmpStart.x > container.rect.size.x - (endPos.x - startPos.x))
  563. {
  564. tmpStart.x -= boardConf.CellSize.x / 2;
  565. }
  566. while (tmpStart.y > 0)
  567. {
  568. tmpStart.y -= boardConf.CellSize.y / 2;
  569. }
  570. while (tmpStart.y < (endPos.y - startPos.y) - container.rect.size.y)
  571. {
  572. tmpStart.y += boardConf.CellSize.y / 2;
  573. }
  574. tmpStart.x = UnityUtil.GetNearst(tmpStart.x, boardConf.CellSize.x);
  575. tmpStart.y = UnityUtil.GetNearst(tmpStart.y, boardConf.CellSize.y);
  576. if (cellMap.ContainsKey(tmpStart))
  577. {
  578. return false;
  579. }
  580. foreach (var item in cellMap)
  581. {
  582. Rect a = new Rect(item.Key, item.Value - item.Key);
  583. Rect b = new Rect(tmpStart, endPos - startPos);
  584. if (UnityUtil.IsCrossLine(a, b))
  585. {
  586. //UnityLog.Instance.Log("相交:" + a.ToString() + b.ToString(), 3);
  587. return false;
  588. }
  589. }
  590. offset = tmpStart - startPos;
  591. return true;
  592. }
  593. }
  594. /// <summary>
  595. /// 处理一个窗口是否可以放置到黑板上
  596. /// </summary>
  597. /// <param name="pic"></param>
  598. /// <param name="v"></param>
  599. public void ExecutePicture(WorldDlg pic, bool isDraging, bool isUser)
  600. {
  601. if (boardConf == null)
  602. {
  603. return;
  604. }
  605. ///相对位置
  606. Vector3 anchor3D = pic.DlgTrans.anchoredPosition3D;
  607. Vector3 rpos = anchor3D;
  608. if (pic.ContainerState == ContainerState.BoardMove)
  609. {
  610. pic.transform.forward = Vector3.Lerp(pic.transform.forward, transform.forward, Time.deltaTime * 6.66f);
  611. Vector3 pos = pic.DlgTrans.anchoredPosition3D;
  612. if (pos.z > boardConf.PressPonit)
  613. {
  614. pos.z = boardConf.PressPonit;
  615. }
  616. rpos = pos;
  617. pic.DlgTrans.anchoredPosition3D = pos;
  618. }
  619. if ((Math.Abs(rpos.z) <= boardConf.Adsorption && Vector3.Angle(transform.forward, pic.transform.forward) < boardConf.Angle) ||
  620. dlg.IsIntersect(pic))
  621. {
  622. if (isDraging)
  623. {
  624. pic.ContainerState = ContainerState.BoardMove;
  625. }
  626. else
  627. {
  628. pic.ContainerState = ContainerState.OnBoard;
  629. }
  630. }
  631. else if (dlg.IsIntersect(pic))
  632. {
  633. if (isDraging)
  634. {
  635. pic.transform.forward = Vector3.Lerp(pic.transform.forward, transform.forward, Time.deltaTime * 6.66f);
  636. }
  637. else
  638. {
  639. pic.ContainerState = ContainerState.OnBoard;
  640. }
  641. }
  642. else
  643. {
  644. if (!isDraging)
  645. {
  646. pic.ContainerState = ContainerState.Free;
  647. }
  648. else
  649. {
  650. pic.ContainerState = ContainerState.FreeMove;
  651. }
  652. }
  653. if (pic.ContainerState == ContainerState.OnBoard)
  654. {
  655. pic.transform.DORotate(transform.eulerAngles, 0.3f);
  656. if (!pic.isOnBoard)
  657. {
  658. AddDlg(pic, pic.DlgTrans.anchoredPosition3D, isUser);
  659. }
  660. else
  661. {
  662. pic.DlgTrans.DOKill();
  663. Vector3 pos = pic.DlgTrans.anchoredPosition3D;
  664. pos.z = boardConf.ShowDis;
  665. pic.DlgTrans.DOAnchorPos3D(pos, 0.3f).OnComplete(() =>
  666. {
  667. if (isUser&& pic.Transfer)
  668. {
  669. pic.Transfer.DisActiveCell();
  670. }
  671. });
  672. }
  673. }
  674. else
  675. {
  676. if (pic.isOnBoard)
  677. {
  678. RemoveDlg(pic);
  679. }
  680. }
  681. }
  682. }
  683. }