HomeView.cs 13 KB

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