HomeView.cs 14 KB

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