HomeView.cs 12 KB

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