HomeView.cs 17 KB

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