HomeView.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. if (!CommonConfig.StandaloneMode)
  217. ViewManager2.ShowView(ViewManager2.Path_PersonalView);
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. /* FriendBar */
  224. GameObject friendItemPrefab = null;
  225. Transform friendBarContent = null;
  226. IEnumerator RefreshFriendBar() {
  227. if (friendItemPrefab == null) {
  228. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  229. Destroy(friendItemPrefab.GetComponent<Image>());
  230. friendItemPrefab.SetActive(false);
  231. }
  232. if (friendBarContent == null) {
  233. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  234. }
  235. while (UserPlayer.ins == null || !UserPlayer.ins.loginAuthed || !UserPlayer.ins.hasGetUserInfo) {
  236. yield return null;
  237. }
  238. Action<JArray> cb = RenderFriendList;
  239. UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
  240. }
  241. public void RenderFriendList(JArray list) {
  242. if (flag_SwapFriendAndRank != 0) return;
  243. for (int i = 1; i < friendBarContent.childCount; i++) {
  244. Destroy(friendBarContent.GetChild(i).gameObject);
  245. }
  246. if (list.Count > 0) {
  247. Color outColor;
  248. foreach (var itemInfo in list) {
  249. int friendID = itemInfo.Value<int>("friendID");
  250. int avatarID = itemInfo.Value<int>("avatarID");
  251. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  252. string nickname = itemInfo.Value<string>("nickname");
  253. bool online = itemInfo.Value<bool>("online");
  254. long offlineTime = itemInfo.Value<long>("offlineTime");
  255. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  256. o.SetActive(true);
  257. o.name = friendID.ToString();
  258. Image avatarImage = o.transform.Find("Avatar").GetComponent<Image>();
  259. RoleMgr.SetAvatarToImage(avatarImage, avatarID, avatarUrl);
  260. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  261. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  262. TimeUtil.SetOfflineTimeTextKey(offlineTime, online, timeTip);
  263. if (online) {
  264. ColorUtility.TryParseHtmlString("#12B525", out outColor);
  265. } else {
  266. ColorUtility.TryParseHtmlString("#A8B2BD", out outColor);
  267. }
  268. timeTip.GetComponent<Text>().color = outColor;
  269. }
  270. }
  271. }
  272. IEnumerator RefreshFriendRankBar() {
  273. if (friendItemPrefab == null) {
  274. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  275. Destroy(friendItemPrefab.GetComponent<Image>());
  276. friendItemPrefab.SetActive(false);
  277. }
  278. if (friendBarContent == null) {
  279. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  280. }
  281. while (UserPlayer.ins == null || !UserPlayer.ins.loginAuthed || !UserPlayer.ins.hasGetUserInfo) {
  282. yield return null;
  283. }
  284. Action<JArray> cb = RenderFriendRankList;
  285. UserPlayer.ins.call("rankComp.getFriendRankList", null, cb);
  286. }
  287. void RenderFriendRankList(JArray list) {
  288. if (flag_SwapFriendAndRank != 1) return;
  289. for (int i = 1; i < friendBarContent.childCount; i++) {
  290. Destroy(friendBarContent.GetChild(i).gameObject);
  291. }
  292. if (list.Count > 0) {
  293. foreach (var itemInfo in list) {
  294. int userID = itemInfo.Value<int>("id");
  295. int avatarID = itemInfo.Value<int>("avatarID");
  296. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  297. string nickname = itemInfo.Value<string>("nickname");
  298. bool online = itemInfo.Value<bool>("online");
  299. long offlineTime = itemInfo.Value<long>("offlineTime");
  300. int rankNum = itemInfo.Value<int>("rankNum");
  301. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  302. o.SetActive(true);
  303. o.name = userID.ToString();
  304. Image avatarImage = o.transform.Find("Avatar").GetComponent<Image>();
  305. RoleMgr.SetAvatarToImage(avatarImage, avatarID, avatarUrl);
  306. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  307. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  308. Action<Text> onApplyToNext = (t) => {
  309. if (TextAutoLanguage2.GetLanguage() == 0) {
  310. t.text = $"第{rankNum}名";
  311. } else {
  312. string str = rankNum.ToString();
  313. if (str.EndsWith("1")) t.text = $"{rankNum}st";
  314. else if (str.EndsWith("2")) t.text = $"{rankNum}nd";
  315. else if (str.EndsWith("3")) t.text = $"{rankNum}rd";
  316. else t.text = $"{rankNum}th";
  317. }
  318. Color outColor;
  319. if (rankNum == 1) ColorUtility.TryParseHtmlString("#F0E68C", out outColor);
  320. else if (rankNum == 2) ColorUtility.TryParseHtmlString("#E0FFFF", out outColor);
  321. else if (rankNum == 3) ColorUtility.TryParseHtmlString("#CD8162", out outColor);
  322. else ColorUtility.TryParseHtmlString("#EEE5DE", out outColor);
  323. t.color = outColor;
  324. };
  325. timeTip.onApplyToNext += onApplyToNext;
  326. }
  327. }
  328. }
  329. int flag_SwapFriendAndRank = 0;
  330. public void OnClick_SwapFriendAndRank() {
  331. AudioMgr.ins.PlayBtn();
  332. if (flag_SwapFriendAndRank == 0) {
  333. flag_SwapFriendAndRank = 1;
  334. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("rank_title");
  335. } else if (flag_SwapFriendAndRank == 1) {
  336. flag_SwapFriendAndRank = 0;
  337. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("friend_title");
  338. }
  339. RefreshFriendBarAccordingCurrentBar();
  340. }
  341. //根据栏目类型,调用对应的接口请求
  342. public void RefreshFriendBarAccordingCurrentBar()
  343. {
  344. if (flag_SwapFriendAndRank == 0)
  345. StartCoroutine(RefreshFriendBar());
  346. else if (flag_SwapFriendAndRank == 1)
  347. StartCoroutine(RefreshFriendRankBar());
  348. }
  349. public void OnClick_ShowDetail() {
  350. AudioMgr.ins.PlayBtn();
  351. if (flag_SwapFriendAndRank == 0) {
  352. ViewMgr.Instance.ShowView<FriendView>();
  353. } else if (flag_SwapFriendAndRank == 1) {
  354. ViewMgr.Instance.ShowView<RankView>();
  355. }
  356. }
  357. //检测是否需要提示有人加我好友
  358. bool _old_hasFriendRequest = false;
  359. int _old_flag_SwapFriendAndRank = 0;
  360. void UpdateCheckRenderFriendTip()
  361. {
  362. if (UserPlayer.ins == null) return;
  363. if (
  364. UserPlayer.ins.tempData.hasFriendRequest == _old_hasFriendRequest &&
  365. flag_SwapFriendAndRank == _old_flag_SwapFriendAndRank
  366. ) return;
  367. _old_hasFriendRequest = UserPlayer.ins.tempData.hasFriendRequest;
  368. _old_flag_SwapFriendAndRank = flag_SwapFriendAndRank;
  369. friendBarTipTop.SetActive(
  370. flag_SwapFriendAndRank == 1 &&
  371. UserPlayer.ins.tempData.hasFriendRequest);
  372. friendBarTipBottom.SetActive(
  373. flag_SwapFriendAndRank == 0 &&
  374. UserPlayer.ins.tempData.hasFriendRequest);
  375. }
  376. public void RenderBattery(int deviceID, int value)
  377. {
  378. Image img = betteryBar.GetComponent<Image>();
  379. img.fillAmount = value / 100f;
  380. }
  381. public void RenderBattery2() {
  382. int value = BluetoothAim.ins.get2PBattery();
  383. Image img = betteryBar2.GetComponent<Image>();
  384. img.fillAmount = value / 100f;
  385. }
  386. public void UpdateParent() {
  387. TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo();
  388. if (bettery != null)
  389. {
  390. bettery.transform.SetParent(topBarButtonInfo.container.transform);
  391. }
  392. if (bettery2 != null)
  393. {
  394. bettery2.transform.SetParent(topBarButtonInfo.container.transform);
  395. }
  396. }
  397. void updateBatteryStatus() {
  398. if (BluetoothAim.ins == null) return;
  399. //更新电池图标显示
  400. if (BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  401. {
  402. if (!bettery.activeSelf)
  403. {
  404. TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo();
  405. bettery.transform.SetParent(topBarButtonInfo.container.transform);
  406. bettery.SetActive(true);
  407. }
  408. }
  409. else
  410. {
  411. if (bettery.activeSelf)
  412. {
  413. bettery.SetActive(false);
  414. }
  415. }
  416. //更新设备2电池图标显示
  417. if (BluetoothAim.ins.getSmartBowHelper2P() != null &&
  418. BluetoothAim.ins.getSmartBowHelper2P().GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected)
  419. {
  420. if (!bettery2.activeSelf)
  421. {
  422. TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo();
  423. bettery2.transform.SetParent(topBarButtonInfo.container.transform);
  424. bettery2.SetActive(true);
  425. }
  426. }
  427. else
  428. {
  429. if (bettery2.activeSelf)
  430. {
  431. bettery2.SetActive(false);
  432. }
  433. }
  434. }
  435. }