HomeView.cs 18 KB

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