HomeView.cs 19 KB

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