HomeView.cs 13 KB

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