HomeView.cs 12 KB

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