CreateRoomDlg.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XRTool.Util;
  5. using XRTool.WorldUI;
  6. using SC.XR.Unity;
  7. using UnityEngine.UI;
  8. using System;
  9. using LitJson;
  10. using System.Text.RegularExpressions;
  11. using ShadowStudio.Mgr;
  12. using ShadowStudio.Model;
  13. using Studio.Scripts;
  14. using Studio.WebSocket.Message;
  15. using static SC.XR.Unity.SCInputField;
  16. namespace ShadowStudio.UI
  17. {
  18. [RequireComponent(typeof(WorldDlg))]
  19. public class CreateRoomDlg : UnitySingleton<CreateRoomDlg>
  20. {
  21. private SCInputField roomNameInputField;
  22. private Button createBtn;
  23. // private bool isLock = false;
  24. private Text roomNameRemind;
  25. private Button closeBtn;
  26. private string roomType;
  27. private string id;
  28. private GameObject privateRoomUI;
  29. private SCInputField roomExplainInputField;
  30. private GameObject publicRoomUI;
  31. private SCInputField roomPassInputField;
  32. private SCInputField roomNumInputField;
  33. private SCInputField roomExplainInputField1;
  34. private Text roomPassRemind;
  35. private Text roomNumRemind;
  36. private Transform toggleBtnTran;
  37. private ToggleGroup toggleBtnGroup;
  38. private ToggleGroup choosePubTypeToggleGroup;
  39. private Transform choosePubTypeToggleTran;
  40. //被zby替换的代码
  41. //private RoomConfig roomConfig;
  42. private RoomConfig itemConfig;
  43. WorldDlg dlg;
  44. Toggle[] togglegroup;
  45. void Start()
  46. {
  47. HomeProxy.Instance.CreateRoomAction += CreateRoomHandler;
  48. dlg = GetComponent<WorldDlg>();
  49. dlg.showDlg();
  50. privateRoomUI = dlg.GetBreadthChild<Transform>("PrivateRoomUI").gameObject;
  51. publicRoomUI = dlg.GetBreadthChild<Transform>("PublicRoomUI").gameObject;
  52. roomNameInputField = dlg.GetBreadthChild<SCInputField>("RoomNameInputField");
  53. roomNameInputField.onEndEdit.AddListener(InputRoomNameEnd);
  54. roomNameInputField.onValueChanged.AddListener(RoomNameValueChanged);
  55. roomExplainInputField = dlg.GetBreadthChild<SCInputField>("RoomExplainInputField");
  56. createBtn = dlg.GetBreadthChild<Button>("CreateBtn");
  57. createBtn.onClick.AddListener(OnClickCreat);
  58. roomNameRemind = dlg.GetBreadthChild<Text>("RoomNameRemind");
  59. roomPassRemind = dlg.GetBreadthChild<Text>("RoomPassRemind");
  60. roomNumRemind = dlg.GetBreadthChild<Text>("RoomNumRemind");
  61. closeBtn = dlg.GetBreadthChild<Button>("CloseBtn");//广度优先查找 切记不要重名
  62. closeBtn.onClick.AddListener(OnClickClose);
  63. roomPassInputField = dlg.GetBreadthChild<SCInputField>("RoomPassInputField");
  64. roomPassInputField.onEndEdit.AddListener(InputPassNameEnd);
  65. roomPassInputField.onValueChanged.AddListener(RoomPassValueChanged);
  66. roomNumInputField = dlg.GetBreadthChild<SCInputField>("RoomNumInputField");
  67. roomNumInputField.onEndEdit.AddListener(InputNumNameEnd);
  68. roomNumInputField.onValueChanged.AddListener(RoomNumValueChanged);
  69. roomExplainInputField1 = dlg.GetBreadthChild<SCInputField>("RoomExplainInputField1");
  70. toggleBtnTran = dlg.GetBreadthChild<Transform>("ToggleBtnGroup");
  71. Toggle[] toggles = toggleBtnTran.GetComponentsInChildren<Toggle>(true);
  72. for (int i = 0; i < toggles.Length; i++)
  73. {
  74. Toggle toggle = toggles[i];
  75. toggles[i].onValueChanged.AddListener((bool isOn) => { OnToggle(isOn, toggle); });
  76. if (i == 0)
  77. {
  78. toggles[i].isOn = HomeProxy.Instance.IsCreatePrivateRoom;
  79. }
  80. }
  81. choosePubTypeToggleTran = dlg.GetBreadthChild<Transform>("ChoosePubTypeToggleGroup");
  82. roomNameRemind.gameObject.SetActive(false);
  83. roomPassRemind.gameObject.SetActive(false);
  84. roomNumRemind.gameObject.SetActive(false);
  85. privateRoomUI.SetActive(HomeProxy.Instance.IsCreatePrivateRoom);
  86. publicRoomUI.SetActive(!HomeProxy.Instance.IsCreatePrivateRoom);
  87. if (UserInfoMgr.Instance.userConfig.Nick_name.Length > 9)
  88. {
  89. string str = UserInfoMgr.Instance.userConfig.Nick_name.Substring(0, 6);
  90. roomNameInputField.text = str + "..." + "的房间";
  91. }
  92. else
  93. {
  94. roomNameInputField.text = UserInfoMgr.Instance.userConfig.Nick_name + "的房间";
  95. }
  96. if (roomNumInputField.text == "")
  97. {
  98. switch (UserInfoMgr.Instance.userConfig.Is_activate)
  99. {
  100. case "0":
  101. roomNumInputField.text = 2 + "";
  102. break;
  103. case "1":
  104. roomNumInputField.text = 10 + "";
  105. break;
  106. default:
  107. roomNumInputField.text = 2 + "";
  108. break;
  109. }
  110. }
  111. roomExplainInputField.lineType = LineType.MultiLineSubmit;
  112. roomExplainInputField1.lineType = LineType.MultiLineSubmit;
  113. }
  114. public void Init()
  115. {
  116. if (roomExplainInputField != null)
  117. {
  118. roomExplainInputField.text = "";
  119. roomExplainInputField1.text = "";
  120. roomPassInputField.text = "";
  121. }
  122. }
  123. private void OnEnable()
  124. {
  125. if (dlg != null)
  126. dlg.showDlg();
  127. }
  128. private void OnClickClose()
  129. {
  130. dlg.hideDlg(() => {
  131. Debug.Log("关闭结束");
  132. MenuListDlg.Instance.UpdateData();
  133. HomeMgr.Instance.ShowView(HomeMgr.CREATESEARCHRROOMDLG);
  134. HomeMgr.Instance.roomListDlg.SetActive(true);
  135. });
  136. }
  137. private void RoomNameValueChanged(string roomname)
  138. {
  139. roomNameRemind.gameObject.SetActive(false);
  140. }
  141. private void InputRoomNameEnd(string roomname)
  142. {
  143. if (roomname == "")
  144. {
  145. roomNameRemind.gameObject.SetActive(true);
  146. roomNameRemind.text = LanguageMgr.Instance.GetMessage("1048").Message;
  147. }
  148. else
  149. {
  150. if (roomname.Length < 3)
  151. {
  152. roomNameRemind.gameObject.SetActive(true);
  153. roomNameRemind.text = LanguageMgr.Instance.GetMessage("1049").Message;
  154. }
  155. else
  156. {
  157. if (roomname.Contains(" "))
  158. {
  159. roomNameRemind.gameObject.SetActive(true);
  160. roomNameRemind.text = LanguageMgr.Instance.GetMessage("1050").Message;
  161. }
  162. }
  163. }
  164. }
  165. private void OnToggle(bool isOn, Toggle toggle)
  166. {
  167. if (isOn)
  168. {
  169. HomeProxy.Instance.IsOpenPrivateOrPublicRoom(toggle.name == "ChoosePriBtn");
  170. privateRoomUI.SetActive(HomeProxy.Instance.IsCreatePrivateRoom);
  171. publicRoomUI.SetActive(!HomeProxy.Instance.IsCreatePrivateRoom);
  172. switch (toggle.name)
  173. {
  174. case "ChoosePriBtn":
  175. if (roomExplainInputField1.text != "")
  176. {
  177. roomExplainInputField.text = roomExplainInputField1.text;
  178. }
  179. break;
  180. case "ChoosePubBtn":
  181. if (roomExplainInputField.text != "")
  182. {
  183. roomExplainInputField1.text = roomExplainInputField.text;
  184. }
  185. if (togglegroup == null)
  186. {
  187. togglegroup = choosePubTypeToggleTran.GetComponentsInChildren<Toggle>(true);
  188. for (int i = 0; i < togglegroup.Length; i++)
  189. {
  190. Toggle toggle1 = togglegroup[i];
  191. togglegroup[i].onValueChanged.AddListener((bool isOn1) => { OnClickCreatPub(isOn1, toggle1); });
  192. if (i == 0)
  193. {
  194. togglegroup[i].isOn = true;
  195. roomType = "1";
  196. }
  197. }
  198. }
  199. break;
  200. }
  201. }
  202. }
  203. private void RoomPassValueChanged(string roompass)
  204. {
  205. roomPassRemind.gameObject.SetActive(false);
  206. }
  207. private void InputPassNameEnd(string roompass)
  208. {
  209. if (roompass != "")
  210. {
  211. if (roompass.Contains(" "))
  212. {
  213. roomPassRemind.gameObject.SetActive(true);
  214. roomPassRemind.text = LanguageMgr.Instance.GetMessage("1052").Message;
  215. }
  216. else
  217. {
  218. Regex regPass = new Regex(@"^[0-9]*$");
  219. bool ismatchpass = regPass.IsMatch(roompass);
  220. if (!ismatchpass)
  221. {
  222. roomPassRemind.gameObject.SetActive(true);
  223. roomPassRemind.text = LanguageMgr.Instance.GetMessage("1051").Message;
  224. }
  225. }
  226. }
  227. }
  228. private void RoomNumValueChanged(string roomnum)
  229. {
  230. roomNumRemind.gameObject.SetActive(false);
  231. }
  232. private void InputNumNameEnd(string roomnum)
  233. {
  234. if (roomnum != "")
  235. {
  236. if (roomnum.Contains(" "))
  237. {
  238. roomNumRemind.gameObject.SetActive(true);
  239. roomNumRemind.text = LanguageMgr.Instance.GetMessage("1086").Message;
  240. }
  241. else
  242. {
  243. Regex regPass = new Regex(@"^[0-9]*$");
  244. bool ismatchpass = regPass.IsMatch(roomnum);
  245. if (!ismatchpass)
  246. {
  247. roomNumRemind.gameObject.SetActive(true);
  248. roomNumRemind.text = LanguageMgr.Instance.GetMessage("1087").Message;
  249. }
  250. else
  251. {
  252. int num = int.Parse(roomnum);
  253. if (UserInfoMgr.Instance.userConfig.Is_activate == "0")
  254. {
  255. if (num <= 0 || num > 2)
  256. {
  257. roomNumRemind.gameObject.SetActive(true);
  258. roomNumRemind.text = "当前账号创建房间的人数上限为2";
  259. }
  260. else
  261. {
  262. if (roomnum.Length >= 2)
  263. {
  264. string str = roomnum.Substring(roomnum.Length - 1, 1);
  265. roomNumInputField.text = str;
  266. }
  267. }
  268. }
  269. else
  270. {
  271. if (num <= 0 || num > 10)
  272. {
  273. roomNumRemind.gameObject.SetActive(true);
  274. roomNumRemind.text = "当前账号创建房间的人数上限为10";
  275. }
  276. else
  277. {
  278. if (num < 10)
  279. {
  280. if (roomnum.Length >= 2)
  281. {
  282. string str = roomnum.Substring(roomnum.Length - 1, 1);
  283. roomNumInputField.text = str;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. }
  290. }
  291. }
  292. private void OnClickCreat()
  293. {
  294. if (roomNameInputField.text == "")
  295. {
  296. roomNameRemind.gameObject.SetActive(true);
  297. roomNameRemind.text = LanguageMgr.Instance.GetMessage("1048").Message;
  298. }
  299. else if (roomNameRemind.gameObject.activeSelf)
  300. {
  301. return;
  302. }
  303. else
  304. {
  305. if (privateRoomUI.activeSelf)
  306. {
  307. OnClickCreatPri();
  308. }
  309. else
  310. {
  311. OnClickCreatPub();
  312. }
  313. }
  314. }
  315. private void OnClickCreatPri()
  316. {
  317. HomeProxy.Instance.RequestCreateRoom(roomNameInputField.text, roomExplainInputField.text);
  318. }
  319. private void OnClickCreatPub()
  320. {
  321. if (roomNumRemind.gameObject.activeSelf)
  322. {
  323. return;
  324. }
  325. else
  326. {
  327. if (roomNumInputField.text == "")
  328. {
  329. switch (UserInfoMgr.Instance.userConfig.Is_activate)
  330. {
  331. case "0":
  332. roomNumInputField.text = 2 + "";
  333. break;
  334. case "1":
  335. roomNumInputField.text = 10 + "";
  336. break;
  337. }
  338. }
  339. Regex regPass = new Regex(@"^[0-9]*[1-9][0-9]*$");
  340. bool ismatchpass = regPass.IsMatch(roomNumInputField.text);
  341. if (!ismatchpass)
  342. {
  343. CommonMethod.ShowTextTusi();
  344. if (TuSi.Instance)
  345. {
  346. TuSi.Instance.ShowTextContent("请正确输入房间人数!");
  347. //LanguageMgr.Instance.GetMessage("1056").Message
  348. }
  349. }
  350. else
  351. {
  352. if (int.Parse(roomNumInputField.text) > 0)
  353. {
  354. HomeProxy.Instance.RequestCreateRoom(roomNameInputField.text, roomExplainInputField1.text, roomPassInputField.text, int.Parse(roomType), int.Parse(roomNumInputField.text));
  355. }
  356. else
  357. {
  358. CommonMethod.ShowTextTusi();
  359. if (TuSi.Instance)
  360. {
  361. TuSi.Instance.ShowTextContent("请正确输入房间人数!");
  362. //LanguageMgr.Instance.GetMessage("1056").Message
  363. }
  364. }
  365. }
  366. }
  367. }
  368. private void OnClickCreatPub(bool isOn, Toggle toggle)
  369. {
  370. if (isOn)
  371. {
  372. switch (toggle.name)
  373. {
  374. case "ChooseViewToggle_CheckBox":
  375. roomType = "1";
  376. break;
  377. case "ChooseIntToggle_CheckBox":
  378. roomType = "2";
  379. break;
  380. }
  381. }
  382. }
  383. private void CreateRoomHandler(int roomID)
  384. {
  385. id = roomID.ToString();
  386. MenuIcon.itemPos = createBtn.transform.position;
  387. MenuIcon.inID = roomID.ToString();
  388. CommonMethod.ShowLoading();
  389. WSHandler.Room.OnInit -= onInit;
  390. WSHandler.Room.OnJoinRoom -= onJoinRoom;
  391. if (WSHandler._roomClient != null)
  392. {
  393. WSHandler._roomClient.OnWebErrorClose -= onWebErrorClose;
  394. }
  395. WSHandler.Room.OnInit += onInit;
  396. WSHandler.Room.OnJoinRoom += onJoinRoom;
  397. WSHandler.roominit(id);
  398. WSHandler._roomClient.OnWebErrorClose += onWebErrorClose;
  399. if (HomeProxy.Instance.IsCreatePrivateRoom)
  400. {
  401. itemConfig = new RoomConfig(id, "1", roomNameInputField.text, roomExplainInputField.text, "", "0", "0", "0", UserInfoMgr.Instance.userConfig.Nick_name, "1");
  402. }
  403. else
  404. {
  405. itemConfig = new RoomConfig(id, "2", roomNameInputField.text, roomExplainInputField1.text, roomPassInputField.text, roomNumInputField.text, roomType, "0", UserInfoMgr.Instance.userConfig.Nick_name, "1");
  406. }
  407. AgoraRTCManager.Instance.ChannelName = id;
  408. }
  409. private static void onWebErrorClose(string data)
  410. {
  411. }
  412. private void onInit(JsonData data)
  413. {
  414. CommonMethod.SelfPeer(data["data"]["peerId"].ToString(), UserInfoMgr.Instance.userConfig.Nick_name);
  415. if (privateRoomUI.activeSelf)
  416. {
  417. WSHandler.Room.JoinRoom(id, "");
  418. }
  419. else
  420. {
  421. WSHandler.Room.JoinRoom(id, roomPassInputField.text);
  422. }
  423. AgoraRTCManager.Instance.ChannelName = id;
  424. }
  425. private void onJoinRoom(List<GoodsInfo> goods, List<Peer> peers, List<SceneConfig> scenes, SceneConfig currentScene)
  426. {
  427. CommonMethod.IsReceive = true;
  428. CommonMethod.HideLoading();
  429. //被zby替换的代码
  430. //HomeViewMgr.Instance.roomList.hide();
  431. HomeMgr.Instance._menuListDlg.Hide();
  432. CreateSearchRoomDlg.Instance.dlg.hideDlg();
  433. CommonMethod.GoodsInfoList = goods;
  434. CommonMethod.PeerList = peers;
  435. CommonMethod.scenes = scenes;
  436. CommonMethod.currentScene = currentScene;
  437. //被zby替换的代码
  438. //CommonMethod.roomConfig = this.roomConfig;
  439. //if (roomConfig.Room_password != "")
  440. //{
  441. // CommonMethod.roomPassWord = roomConfig.Room_password;
  442. //}
  443. CommonMethod.roomConfig = this.itemConfig;
  444. if (!string.IsNullOrEmpty(itemConfig.Room_password))
  445. {
  446. CommonMethod.roomPassWord = itemConfig.Room_password;
  447. }
  448. else
  449. {
  450. CommonMethod.roomPassWord = "";
  451. }
  452. CommonMethod.ShowNetLoading("NetWork", "正在创建房间....");
  453. Invoke("gotoLoadSence", 1.3f);
  454. }
  455. void gotoLoadSence()
  456. {
  457. CommonMethod.HideNetLoading("NetWork");
  458. //被zby替换的代码
  459. //HomeViewMgr.Instance.LoadSence();
  460. HomeMgr.Instance.LoadSence();
  461. }
  462. protected override void OnDestroy()
  463. {
  464. WSHandler.Room.OnInit -= onInit;
  465. WSHandler.Room.OnJoinRoom -= onJoinRoom;
  466. if (HomeProxy.Instance)
  467. {
  468. HomeProxy.Instance.CreateRoomAction -= CreateRoomHandler;
  469. }
  470. }
  471. }
  472. }