HomeView.cs 16 KB

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