HomeView.cs 12 KB

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