HomeView.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. using System;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. /* 主界面 */
  10. public class HomeView : JCUnityLib.ViewBase
  11. {
  12. public static bool bOpenOtherView = false;
  13. public static string openName = "";
  14. [SerializeField] Image myAvatarSprite;
  15. [SerializeField] Text nickNameText;
  16. [SerializeField] GameObject[] genders;
  17. [SerializeField] GameObject btnConnectBow;
  18. [SerializeField] Text gameVersion;
  19. [SerializeField] GameObject friendBarTipTop;
  20. [SerializeField] GameObject friendBarTipBottom;
  21. [SerializeField] GameObject bettery;
  22. [SerializeField] GameObject betteryBar;
  23. [SerializeField] GameObject bettery2;
  24. [SerializeField] GameObject betteryBar2;
  25. [SerializeField] RawImage QRImage;
  26. [SerializeField] Text CoinCount;
  27. [SerializeField] HomeView_ChallengeOption challengeOption;
  28. public HomeView_ChallengeOption MyChallengeOption => challengeOption;
  29. [SerializeField] HomeView_TopBarView topBarView;
  30. public HomeView_TopBarView MyTopBarView => topBarView;
  31. public static HomeView ins;
  32. void Awake() {
  33. gameVersion.text = "V" + Application.version;
  34. gameVersion.gameObject.SetActive(false); //隐藏版本号
  35. if (CommonConfig.needToExamine) {
  36. transform.Find("LeftPanel/Item (1)").gameObject.SetActive(false);
  37. transform.Find("RightPanel/Item/Text").GetComponent<TextAutoLanguage>().SetText(1234562);
  38. transform.Find("RightPanel/Item (1)/Text").GetComponent<TextAutoLanguage>().SetText(1234563);
  39. transform.Find("RightPanel/Item (3)").gameObject.SetActive(false);
  40. }
  41. if (topBarView && challengeOption) topBarView.onChangeTypeEvent.AddListener(challengeOption.refreshList);
  42. FlushUrlQR();
  43. }
  44. public void FlushUrlQR()
  45. {
  46. QRImage.texture = StandaloneAPI.GetQR();
  47. }
  48. public void selectGame(int _startGameType)
  49. {
  50. // 获取父对象上的 HomeView_ChallengeOption 脚本
  51. HomeView_ChallengeOption challengeOption = GetComponentInParent<HomeView_ChallengeOption>();
  52. if (challengeOption != null)
  53. {
  54. Debug.Log("Found HomeView_ChallengeOption script on object: " + challengeOption.gameObject.name);
  55. }
  56. else
  57. {
  58. Debug.LogError("HomeView_ChallengeOption script not found on the parent object.");
  59. }
  60. ModeSelectView modeSelectView = GetComponentInParent<ModeSelectView>();
  61. if (modeSelectView != null)
  62. {
  63. Debug.Log("Found ModeSelectView script on object: " + modeSelectView.gameObject.name);
  64. }
  65. else
  66. {
  67. Debug.LogError("ModeSelectView script not found on the parent object.");
  68. }
  69. switch (_startGameType)
  70. {
  71. case 1: //奥运射箭
  72. {
  73. GameMgr.judgmentGameType = 1;
  74. modeSelectView.GoToLocalByName("SINGLE_PLYAER");
  75. }
  76. break;
  77. case 2: //打鸭子
  78. {
  79. challengeOption.StartGame(13);
  80. }
  81. break;
  82. case 3: //水果达人
  83. {
  84. challengeOption.StartGame(15);
  85. }
  86. break;
  87. case 4: //荒野射击
  88. {
  89. challengeOption.StartGame(14);
  90. }
  91. break;
  92. case 5: //打野狼
  93. {
  94. GameMgr.judgmentGameType = 5;
  95. modeSelectView.GoToLocalByName("SINGLE_PLYAER");
  96. }
  97. break;
  98. case 6: //打野鸡
  99. {
  100. GameMgr.judgmentGameType = 4;
  101. modeSelectView.GoToLocalByName("SINGLE_PLYAER");
  102. }
  103. break;
  104. case 7: //打野兔
  105. {
  106. GameMgr.judgmentGameType = 3;
  107. modeSelectView.GoToLocalByName("SINGLE_PLYAER");
  108. }
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. void Start()
  115. {
  116. ins = this;
  117. transform.SetSiblingIndex(0); //放在ui最底层
  118. BluetoothHolder.Init();
  119. AudioMgr.Init();
  120. TopBarView.NeedShowIt(this);
  121. // if (ShootCheck.ins) ShootCheck.ins.AdjustNormalOrHightMode();
  122. if (LoginMgr.myUserInfo.id > 0 || (CommonConfig.StandaloneMode && UserPlayer.ins != null && UserPlayer.ins.hasGetUserInfo)) {
  123. RenderNameOrGender();
  124. RenderMyAvatarSprite();
  125. RenderDeviceNames();
  126. }
  127. if (CommonConfig.StandaloneMode)
  128. {
  129. transform.Find("FriendBar").gameObject.SetActive(false);
  130. transform.Find("RightPanel/Line (1)").gameObject.SetActive(false);
  131. transform.Find("RightPanel/Item (1)").gameObject.SetActive(false);
  132. }
  133. else StartCoroutine(RefreshFriendBar());
  134. //如果需要初始化跳转页面,这里处理
  135. Debug.Log("bOpenOtherView:"+ bOpenOtherView);
  136. if (bOpenOtherView) {
  137. bOpenOtherView = false;
  138. switch (openName)
  139. {
  140. case "DeviceView":
  141. // ViewMgr.Instance.ShowView<DeviceView>();
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. //StartCoroutine(testTip());
  148. }
  149. IEnumerator testTip() {
  150. yield return new WaitForSeconds(1.0f);
  151. GameObject settingsViewObj = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_HomeViewTip);
  152. settingsViewObj.GetComponent<HomeView_Tip>().setHomeTip(0);
  153. }
  154. void OnDestroy()
  155. {
  156. if (ins == this) ins = null;
  157. TopBarView.DontNeedShowIt(this);
  158. }
  159. void Update() {
  160. UpdateCheckRenderFriendTip();
  161. UpdateBtnForConnect();
  162. //更新电池显示
  163. updateBatteryStatus();
  164. }
  165. // float countingTime1 = 0;
  166. BluetoothStatusEnum bowStatus;
  167. void UpdateBtnForConnect() {
  168. if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status) {
  169. bowStatus = BluetoothAim.ins.status;
  170. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
  171. //btnConnectBow.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  172. //btnConnectBow.GetComponentInChildren<Text>().color = color;
  173. //btnConnectBow.transform.Find("Check").gameObject.SetActive(bowStatus == BluetoothStatusEnum.ConnectSuccess);
  174. }
  175. }
  176. public static bool ShowProminentBeforeConnectBLE(Action onAgree = null)
  177. {
  178. if (CommonConfig.StandaloneMode) return false;
  179. if (SceneManager.GetActiveScene().name.Equals("HeartRateBand")) return false;
  180. if (PlayerPrefs.GetInt("Location-Prominent", 0) == 0) {
  181. ModalView m = ModalView.Show();
  182. string pName = Application.productName;
  183. m.textKey = "Model_Location-Prominent";
  184. m.onAgreeTextKey = "common_next";
  185. m.onRejectTextKey = "common_cancel";
  186. m.textFormatArgs = new object[] {pName};
  187. m.onAgree = () => {
  188. PlayerPrefs.SetInt("Location-Prominent", 1);
  189. try {
  190. onAgree?.Invoke();
  191. } catch (System.Exception e) {
  192. Debug.LogError(e.Message + "\n" + e.StackTrace);
  193. }
  194. };
  195. return true;
  196. }
  197. return false;
  198. }
  199. public static bool ShowProminentBeforeConnectBLE_Event(Action onAgree = null,Action onReject = null)
  200. {
  201. if (CommonConfig.StandaloneMode) return false;
  202. if (SceneManager.GetActiveScene().name.Equals("HeartRateBand")) return false;
  203. if (PlayerPrefs.GetInt("Location-Prominent", 0) == 0)
  204. {
  205. ModalView m = ModalView.Show();
  206. string pName = Application.productName;
  207. m.textKey = "Model_Location-Prominent";
  208. m.onAgreeTextKey = "common_next";
  209. m.onRejectTextKey = "common_cancel";
  210. m.textFormatArgs = new object[] { pName };
  211. m.onAgree = () => {
  212. PlayerPrefs.SetInt("Location-Prominent", 1);
  213. try
  214. {
  215. onAgree?.Invoke();
  216. }
  217. catch (System.Exception e)
  218. {
  219. Debug.LogError(e.Message + "\n" + e.StackTrace);
  220. }
  221. };
  222. m.onReject = () =>
  223. {
  224. try
  225. {
  226. onReject?.Invoke();
  227. }
  228. catch (System.Exception e)
  229. {
  230. Debug.LogError(e.Message + "\n" + e.StackTrace);
  231. }
  232. };
  233. return true;
  234. }
  235. return false;
  236. }
  237. public void OnClick_ConnectBLE() {
  238. if (ShowProminentBeforeConnectBLE()) return;
  239. BluetoothAim.ins.DoConnect();
  240. }
  241. public void OnClick_ShowDeviceView() {
  242. AudioMgr.ins.PlayBtn();
  243. ViewMgr.Instance.ShowView<DeviceViewInfrared>();
  244. }
  245. public void RenderNameOrGender() {
  246. nickNameText.text = LoginMgr.myUserInfo.nickname;
  247. genders[LoginMgr.myUserInfo.gender == 2 ? 1 : 0].SetActive(true);
  248. genders[LoginMgr.myUserInfo.gender == 2 ? 0 : 1].SetActive(false);
  249. LayoutRebuilder.ForceRebuildLayoutImmediate(nickNameText.transform.parent.GetComponent<RectTransform>());
  250. }
  251. public void RenderMyAvatarSprite() {
  252. RoleMgr.SetAvatarToImage(myAvatarSprite, LoginMgr.myUserInfo.avatarID, LoginMgr.myUserInfo.avatarUrl);
  253. }
  254. public void RenderDeviceNames()
  255. {
  256. try {
  257. (DeviceInfo bowInfo, DeviceInfo arrowInfo) = DeviceMgr.ins.GetCurrentBowArrowInfo();
  258. // this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(bowInfo.config.name);
  259. this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(200000);
  260. this.transform.Find("ShowArrow/Text").GetComponent<TextAutoLanguage>().SetText(arrowInfo.config.name);
  261. } catch (System.Exception) {}
  262. }
  263. public Action action_OnClickStartGame;
  264. public void GoTo(string target) {
  265. AudioMgr.ins.PlayBtn();
  266. switch (target)
  267. {
  268. case "开始游戏":
  269. #if !UNITY_EDITOR
  270. if ((CommonConfig.isReleaseVersion || CommonConfig.StandaloneMode) && !BluetoothStatus.IsAllConnected())
  271. {
  272. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
  273. return;
  274. }
  275. #endif
  276. ViewMgr.Instance.ShowView<GameStartView>();
  277. action_OnClickStartGame?.Invoke();
  278. break;
  279. case "联机游戏":
  280. if (CommonConfig.isReleaseVersion && !BluetoothStatus.IsAllConnected()) {
  281. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
  282. return;
  283. }
  284. GlobalDataTemp.pkMatchType = PKMatchType.OnlinePK;
  285. ViewMgr.Instance.ShowView<PKGameOptionView>();
  286. break;
  287. case "我的":
  288. //ViewMgr.Instance.ShowView<MeView>();
  289. if (!CommonConfig.StandaloneMode)
  290. ViewManager2.ShowView(ViewManager2.Path_PersonalView);
  291. break;
  292. default:
  293. break;
  294. }
  295. }
  296. /* FriendBar */
  297. GameObject friendItemPrefab = null;
  298. Transform friendBarContent = null;
  299. IEnumerator RefreshFriendBar() {
  300. if (friendItemPrefab == null) {
  301. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  302. Destroy(friendItemPrefab.GetComponent<Image>());
  303. friendItemPrefab.SetActive(false);
  304. }
  305. if (friendBarContent == null) {
  306. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  307. }
  308. while (UserPlayer.ins == null || !UserPlayer.ins.loginAuthed || !UserPlayer.ins.hasGetUserInfo) {
  309. yield return null;
  310. }
  311. Action<JArray> cb = RenderFriendList;
  312. UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
  313. }
  314. public void RenderFriendList(JArray list) {
  315. if (flag_SwapFriendAndRank != 0) return;
  316. for (int i = 1; i < friendBarContent.childCount; i++) {
  317. Destroy(friendBarContent.GetChild(i).gameObject);
  318. }
  319. if (list.Count > 0) {
  320. Color outColor;
  321. foreach (var itemInfo in list) {
  322. int friendID = itemInfo.Value<int>("friendID");
  323. int avatarID = itemInfo.Value<int>("avatarID");
  324. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  325. string nickname = itemInfo.Value<string>("nickname");
  326. bool online = itemInfo.Value<bool>("online");
  327. long offlineTime = itemInfo.Value<long>("offlineTime");
  328. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  329. o.SetActive(true);
  330. o.name = friendID.ToString();
  331. Image avatarImage = o.transform.Find("Avatar").GetComponent<Image>();
  332. RoleMgr.SetAvatarToImage(avatarImage, avatarID, avatarUrl);
  333. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  334. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  335. TimeUtil.SetOfflineTimeTextKey(offlineTime, online, timeTip);
  336. if (online) {
  337. ColorUtility.TryParseHtmlString("#12B525", out outColor);
  338. } else {
  339. ColorUtility.TryParseHtmlString("#A8B2BD", out outColor);
  340. }
  341. timeTip.GetComponent<Text>().color = outColor;
  342. }
  343. }
  344. }
  345. IEnumerator RefreshFriendRankBar() {
  346. if (friendItemPrefab == null) {
  347. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  348. Destroy(friendItemPrefab.GetComponent<Image>());
  349. friendItemPrefab.SetActive(false);
  350. }
  351. if (friendBarContent == null) {
  352. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  353. }
  354. while (UserPlayer.ins == null || !UserPlayer.ins.loginAuthed || !UserPlayer.ins.hasGetUserInfo) {
  355. yield return null;
  356. }
  357. Action<JArray> cb = RenderFriendRankList;
  358. UserPlayer.ins.call("rankComp.getFriendRankList", null, cb);
  359. }
  360. void RenderFriendRankList(JArray list) {
  361. if (flag_SwapFriendAndRank != 1) return;
  362. for (int i = 1; i < friendBarContent.childCount; i++) {
  363. Destroy(friendBarContent.GetChild(i).gameObject);
  364. }
  365. if (list.Count > 0) {
  366. foreach (var itemInfo in list) {
  367. int userID = itemInfo.Value<int>("id");
  368. int avatarID = itemInfo.Value<int>("avatarID");
  369. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  370. string nickname = itemInfo.Value<string>("nickname");
  371. bool online = itemInfo.Value<bool>("online");
  372. long offlineTime = itemInfo.Value<long>("offlineTime");
  373. int rankNum = itemInfo.Value<int>("rankNum");
  374. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  375. o.SetActive(true);
  376. o.name = userID.ToString();
  377. Image avatarImage = o.transform.Find("Avatar").GetComponent<Image>();
  378. RoleMgr.SetAvatarToImage(avatarImage, avatarID, avatarUrl);
  379. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  380. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  381. Action<Text> onApplyToNext = (t) => {
  382. if (TextAutoLanguage2.GetLanguage() == 0) {
  383. t.text = $"第{rankNum}名";
  384. } else {
  385. string str = rankNum.ToString();
  386. if (str.EndsWith("1")) t.text = $"{rankNum}st";
  387. else if (str.EndsWith("2")) t.text = $"{rankNum}nd";
  388. else if (str.EndsWith("3")) t.text = $"{rankNum}rd";
  389. else t.text = $"{rankNum}th";
  390. }
  391. Color outColor;
  392. if (rankNum == 1) ColorUtility.TryParseHtmlString("#F0E68C", out outColor);
  393. else if (rankNum == 2) ColorUtility.TryParseHtmlString("#E0FFFF", out outColor);
  394. else if (rankNum == 3) ColorUtility.TryParseHtmlString("#CD8162", out outColor);
  395. else ColorUtility.TryParseHtmlString("#EEE5DE", out outColor);
  396. t.color = outColor;
  397. };
  398. timeTip.onApplyToNext += onApplyToNext;
  399. }
  400. }
  401. }
  402. int flag_SwapFriendAndRank = 0;
  403. public void OnClick_SwapFriendAndRank() {
  404. AudioMgr.ins.PlayBtn();
  405. if (flag_SwapFriendAndRank == 0) {
  406. flag_SwapFriendAndRank = 1;
  407. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("rank_title");
  408. } else if (flag_SwapFriendAndRank == 1) {
  409. flag_SwapFriendAndRank = 0;
  410. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("friend_title");
  411. }
  412. RefreshFriendBarAccordingCurrentBar();
  413. }
  414. //根据栏目类型,调用对应的接口请求
  415. public void RefreshFriendBarAccordingCurrentBar()
  416. {
  417. if (flag_SwapFriendAndRank == 0)
  418. StartCoroutine(RefreshFriendBar());
  419. else if (flag_SwapFriendAndRank == 1)
  420. StartCoroutine(RefreshFriendRankBar());
  421. }
  422. public void OnClick_ShowDetail() {
  423. AudioMgr.ins.PlayBtn();
  424. if (flag_SwapFriendAndRank == 0) {
  425. ViewMgr.Instance.ShowView<FriendView>();
  426. } else if (flag_SwapFriendAndRank == 1) {
  427. ViewMgr.Instance.ShowView<RankView>();
  428. }
  429. }
  430. //检测是否需要提示有人加我好友
  431. bool _old_hasFriendRequest = false;
  432. int _old_flag_SwapFriendAndRank = 0;
  433. void UpdateCheckRenderFriendTip()
  434. {
  435. if (UserPlayer.ins == null) return;
  436. if (
  437. UserPlayer.ins.tempData.hasFriendRequest == _old_hasFriendRequest &&
  438. flag_SwapFriendAndRank == _old_flag_SwapFriendAndRank
  439. ) return;
  440. _old_hasFriendRequest = UserPlayer.ins.tempData.hasFriendRequest;
  441. _old_flag_SwapFriendAndRank = flag_SwapFriendAndRank;
  442. friendBarTipTop.SetActive(
  443. flag_SwapFriendAndRank == 1 &&
  444. UserPlayer.ins.tempData.hasFriendRequest);
  445. friendBarTipBottom.SetActive(
  446. flag_SwapFriendAndRank == 0 &&
  447. UserPlayer.ins.tempData.hasFriendRequest);
  448. }
  449. public void RenderBattery(int deviceID, int value)
  450. {
  451. Image img = betteryBar.GetComponent<Image>();
  452. img.fillAmount = value / 100f;
  453. }
  454. public void RenderBattery2() {
  455. int value = BluetoothAim.ins.get2PBattery();
  456. Image img = betteryBar2.GetComponent<Image>();
  457. img.fillAmount = value / 100f;
  458. }
  459. public void UpdateParent() {
  460. TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo();
  461. if (bettery != null)
  462. {
  463. bettery.transform.SetParent(topBarButtonInfo.container.transform);
  464. }
  465. if (bettery2 != null)
  466. {
  467. bettery2.transform.SetParent(topBarButtonInfo.container.transform);
  468. }
  469. }
  470. void updateBatteryStatus() {
  471. if (BluetoothAim.ins == null) return;
  472. //更新电池图标显示
  473. if (BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  474. {
  475. if (!bettery.activeSelf)
  476. {
  477. TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo();
  478. bettery.transform.SetParent(topBarButtonInfo.container.transform);
  479. bettery.SetActive(true);
  480. }
  481. }
  482. else
  483. {
  484. if (bettery.activeSelf)
  485. {
  486. bettery.SetActive(false);
  487. }
  488. }
  489. //更新设备2电池图标显示
  490. if (BluetoothAim.ins.getSmartBowHelper2P() != null &&
  491. BluetoothAim.ins.getSmartBowHelper2P().GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected)
  492. {
  493. if (!bettery2.activeSelf)
  494. {
  495. TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo();
  496. bettery2.transform.SetParent(topBarButtonInfo.container.transform);
  497. bettery2.SetActive(true);
  498. }
  499. }
  500. else
  501. {
  502. if (bettery2.activeSelf)
  503. {
  504. bettery2.SetActive(false);
  505. }
  506. }
  507. }
  508. }