HomeView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 : MonoBehaviour
  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 void OnClick_ConnectBLE() {
  68. BluetoothAim.ins.DoConnect();
  69. }
  70. public void OnClick_ShowDeviceView() {
  71. AudioMgr.ins.PlayBtn();
  72. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/DeviceView 1"));
  73. }
  74. public void RenderNameOrGender() {
  75. nickNameText.text = LoginMgr.myUserInfo.nickname;
  76. genders[LoginMgr.myUserInfo.gender == 2 ? 1 : 0].SetActive(true);
  77. genders[LoginMgr.myUserInfo.gender == 2 ? 0 : 1].SetActive(false);
  78. LayoutRebuilder.ForceRebuildLayoutImmediate(nickNameText.transform.parent.GetComponent<RectTransform>());
  79. }
  80. public void RenderMyAvatarSprite() {
  81. myAvatarSprite.sprite = RoleMgr.GetAvatar(LoginMgr.myUserInfo.avatarID);
  82. }
  83. public void RenderDeviceNames()
  84. {
  85. try {
  86. (DeviceInfo bowInfo, DeviceInfo arrowInfo) = DeviceMgr.ins.GetCurrentBowArrowInfo();
  87. // this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(bowInfo.config.name);
  88. this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(200000);
  89. this.transform.Find("ShowArrow/Text").GetComponent<TextAutoLanguage>().SetText(arrowInfo.config.name);
  90. } catch (System.Exception) {}
  91. }
  92. public Action action_OnClickStartGame;
  93. public void GoTo(string target) {
  94. AudioMgr.ins.PlayBtn();
  95. switch (target)
  96. {
  97. case "开始游戏":
  98. if (CommonConfig.isReleaseVersion && !BluetoothStatus.IsAllConnected()) {
  99. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
  100. return;
  101. }
  102. GameObject.Instantiate(SceneResMgr.ins.GetPrefab("GameStartView"));
  103. action_OnClickStartGame?.Invoke();
  104. break;
  105. case "联机游戏":
  106. if (CommonConfig.isReleaseVersion && !BluetoothStatus.IsAllConnected()) {
  107. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
  108. return;
  109. }
  110. GlobalDataTemp.pkMatchType = PKMatchType.OnlinePK;
  111. GameObject.Instantiate(SceneResMgr.ins.GetPrefab("PKGameOptionView"));
  112. break;
  113. case "好友":
  114. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/FriendView"));
  115. break;
  116. case "排行磅":
  117. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/RankView"));
  118. break;
  119. case "对战":
  120. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/RoleSelectView"));
  121. break;
  122. case "教程":
  123. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/CourseView"), Vector3.zero, new Quaternion());
  124. break;
  125. case "我的":
  126. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/MeView"), Vector3.zero, new Quaternion());
  127. break;
  128. case "设备":
  129. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/DeviceView"), Vector3.zero, new Quaternion());
  130. break;
  131. case "商城":
  132. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/ShopView"), Vector3.zero, new Quaternion());
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. /* FriendBar */
  139. GameObject friendItemPrefab = null;
  140. Transform friendBarContent = null;
  141. IEnumerator RefreshFriendBar() {
  142. if (friendItemPrefab == null) {
  143. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  144. Destroy(friendItemPrefab.GetComponent<Image>());
  145. friendItemPrefab.SetActive(false);
  146. }
  147. if (friendBarContent == null) {
  148. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  149. }
  150. while (LoginMgr.myUserInfo.id < 1) {
  151. yield return null;
  152. }
  153. Action<JArray> cb = RenderFriendList;
  154. UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
  155. }
  156. public void RenderFriendList(JArray list) {
  157. if (flag_SwapFriendAndRank != 0) return;
  158. for (int i = 1; i < friendBarContent.childCount; i++) {
  159. Destroy(friendBarContent.GetChild(i).gameObject);
  160. }
  161. if (list.Count > 0) {
  162. Color outColor;
  163. foreach (var itemInfo in list) {
  164. int friendID = itemInfo.Value<int>("friendID");
  165. int avatarID = itemInfo.Value<int>("avatarID");
  166. string nickname = itemInfo.Value<string>("nickname");
  167. bool online = itemInfo.Value<bool>("online");
  168. long offlineTime = itemInfo.Value<long>("offlineTime");
  169. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  170. o.SetActive(true);
  171. o.name = friendID.ToString();
  172. o.transform.Find("Avatar").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  173. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  174. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  175. TimeUtil.SetOfflineTimeTextKey(offlineTime, online, timeTip);
  176. if (online) {
  177. ColorUtility.TryParseHtmlString("#12B525", out outColor);
  178. } else {
  179. ColorUtility.TryParseHtmlString("#A8B2BD", out outColor);
  180. }
  181. timeTip.GetComponent<Text>().color = outColor;
  182. }
  183. }
  184. }
  185. IEnumerator RefreshFriendRankBar() {
  186. if (friendItemPrefab == null) {
  187. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  188. Destroy(friendItemPrefab.GetComponent<Image>());
  189. friendItemPrefab.SetActive(false);
  190. }
  191. if (friendBarContent == null) {
  192. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  193. }
  194. while (LoginMgr.myUserInfo.id < 1) {
  195. yield return null;
  196. }
  197. Action<JArray> cb = RenderFriendRankList;
  198. UserPlayer.ins.call("rankComp.getFriendRankList", null, cb);
  199. }
  200. void RenderFriendRankList(JArray list) {
  201. if (flag_SwapFriendAndRank != 1) return;
  202. for (int i = 1; i < friendBarContent.childCount; i++) {
  203. Destroy(friendBarContent.GetChild(i).gameObject);
  204. }
  205. if (list.Count > 0) {
  206. foreach (var itemInfo in list) {
  207. int userID = itemInfo.Value<int>("id");
  208. int avatarID = itemInfo.Value<int>("avatarID");
  209. string nickname = itemInfo.Value<string>("nickname");
  210. bool online = itemInfo.Value<bool>("online");
  211. long offlineTime = itemInfo.Value<long>("offlineTime");
  212. int rankNum = itemInfo.Value<int>("rankNum");
  213. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  214. o.SetActive(true);
  215. o.name = userID.ToString();
  216. o.transform.Find("Avatar").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  217. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  218. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  219. Action<Text> onApplyToNext = (t) => {
  220. if (TextAutoLanguage2.GetLanguage() == 0) {
  221. t.text = $"第{rankNum}名";
  222. } else {
  223. string str = rankNum.ToString();
  224. if (str.EndsWith("1")) t.text = $"{rankNum}st";
  225. else if (str.EndsWith("2")) t.text = $"{rankNum}nd";
  226. else if (str.EndsWith("3")) t.text = $"{rankNum}rd";
  227. else t.text = $"{rankNum}th";
  228. }
  229. Color outColor;
  230. if (rankNum == 1) ColorUtility.TryParseHtmlString("#F0E68C", out outColor);
  231. else if (rankNum == 2) ColorUtility.TryParseHtmlString("#E0FFFF", out outColor);
  232. else if (rankNum == 3) ColorUtility.TryParseHtmlString("#CD8162", out outColor);
  233. else ColorUtility.TryParseHtmlString("#EEE5DE", out outColor);
  234. t.color = outColor;
  235. };
  236. timeTip.onApplyToNext += onApplyToNext;
  237. }
  238. }
  239. }
  240. int flag_SwapFriendAndRank = 0;
  241. public void OnClick_SwapFriendAndRank() {
  242. AudioMgr.ins.PlayBtn();
  243. if (flag_SwapFriendAndRank == 0) {
  244. flag_SwapFriendAndRank = 1;
  245. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("rank_title");
  246. StartCoroutine(RefreshFriendRankBar());
  247. } else if (flag_SwapFriendAndRank == 1) {
  248. flag_SwapFriendAndRank = 0;
  249. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("friend_title");
  250. StartCoroutine(RefreshFriendBar());
  251. }
  252. }
  253. public void OnClick_ShowDetail() {
  254. AudioMgr.ins.PlayBtn();
  255. if (flag_SwapFriendAndRank == 0) {
  256. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/FriendView"));
  257. } else if (flag_SwapFriendAndRank == 1) {
  258. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/RankView"));
  259. }
  260. }
  261. }