HomeView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 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. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/DeviceView 1"));
  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. GameObject.Instantiate(SceneResMgr.ins.GetPrefab("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. GameObject.Instantiate(SceneResMgr.ins.GetPrefab("PKGameOptionView"));
  132. break;
  133. case "好友":
  134. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/FriendView"));
  135. break;
  136. case "排行磅":
  137. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/RankView"));
  138. break;
  139. case "对战":
  140. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/RoleSelectView"));
  141. break;
  142. case "教程":
  143. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/CourseView"), Vector3.zero, new Quaternion());
  144. break;
  145. case "我的":
  146. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/MeView"), Vector3.zero, new Quaternion());
  147. break;
  148. case "设备":
  149. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/DeviceView"), Vector3.zero, new Quaternion());
  150. break;
  151. case "商城":
  152. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/ShopView"), Vector3.zero, new Quaternion());
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. /* FriendBar */
  159. GameObject friendItemPrefab = null;
  160. Transform friendBarContent = null;
  161. IEnumerator RefreshFriendBar() {
  162. if (friendItemPrefab == null) {
  163. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  164. Destroy(friendItemPrefab.GetComponent<Image>());
  165. friendItemPrefab.SetActive(false);
  166. }
  167. if (friendBarContent == null) {
  168. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  169. }
  170. while (LoginMgr.myUserInfo.id < 1) {
  171. yield return null;
  172. }
  173. Action<JArray> cb = RenderFriendList;
  174. UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
  175. }
  176. public void RenderFriendList(JArray list) {
  177. if (flag_SwapFriendAndRank != 0) return;
  178. for (int i = 1; i < friendBarContent.childCount; i++) {
  179. Destroy(friendBarContent.GetChild(i).gameObject);
  180. }
  181. if (list.Count > 0) {
  182. Color outColor;
  183. foreach (var itemInfo in list) {
  184. int friendID = itemInfo.Value<int>("friendID");
  185. int avatarID = itemInfo.Value<int>("avatarID");
  186. string nickname = itemInfo.Value<string>("nickname");
  187. bool online = itemInfo.Value<bool>("online");
  188. long offlineTime = itemInfo.Value<long>("offlineTime");
  189. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  190. o.SetActive(true);
  191. o.name = friendID.ToString();
  192. o.transform.Find("Avatar").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  193. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  194. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  195. TimeUtil.SetOfflineTimeTextKey(offlineTime, online, timeTip);
  196. if (online) {
  197. ColorUtility.TryParseHtmlString("#12B525", out outColor);
  198. } else {
  199. ColorUtility.TryParseHtmlString("#A8B2BD", out outColor);
  200. }
  201. timeTip.GetComponent<Text>().color = outColor;
  202. }
  203. }
  204. }
  205. IEnumerator RefreshFriendRankBar() {
  206. if (friendItemPrefab == null) {
  207. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  208. Destroy(friendItemPrefab.GetComponent<Image>());
  209. friendItemPrefab.SetActive(false);
  210. }
  211. if (friendBarContent == null) {
  212. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  213. }
  214. while (LoginMgr.myUserInfo.id < 1) {
  215. yield return null;
  216. }
  217. Action<JArray> cb = RenderFriendRankList;
  218. UserPlayer.ins.call("rankComp.getFriendRankList", null, cb);
  219. }
  220. void RenderFriendRankList(JArray list) {
  221. if (flag_SwapFriendAndRank != 1) return;
  222. for (int i = 1; i < friendBarContent.childCount; i++) {
  223. Destroy(friendBarContent.GetChild(i).gameObject);
  224. }
  225. if (list.Count > 0) {
  226. foreach (var itemInfo in list) {
  227. int userID = itemInfo.Value<int>("id");
  228. int avatarID = itemInfo.Value<int>("avatarID");
  229. string nickname = itemInfo.Value<string>("nickname");
  230. bool online = itemInfo.Value<bool>("online");
  231. long offlineTime = itemInfo.Value<long>("offlineTime");
  232. int rankNum = itemInfo.Value<int>("rankNum");
  233. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  234. o.SetActive(true);
  235. o.name = userID.ToString();
  236. o.transform.Find("Avatar").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  237. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  238. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  239. Action<Text> onApplyToNext = (t) => {
  240. if (TextAutoLanguage2.GetLanguage() == 0) {
  241. t.text = $"第{rankNum}名";
  242. } else {
  243. string str = rankNum.ToString();
  244. if (str.EndsWith("1")) t.text = $"{rankNum}st";
  245. else if (str.EndsWith("2")) t.text = $"{rankNum}nd";
  246. else if (str.EndsWith("3")) t.text = $"{rankNum}rd";
  247. else t.text = $"{rankNum}th";
  248. }
  249. Color outColor;
  250. if (rankNum == 1) ColorUtility.TryParseHtmlString("#F0E68C", out outColor);
  251. else if (rankNum == 2) ColorUtility.TryParseHtmlString("#E0FFFF", out outColor);
  252. else if (rankNum == 3) ColorUtility.TryParseHtmlString("#CD8162", out outColor);
  253. else ColorUtility.TryParseHtmlString("#EEE5DE", out outColor);
  254. t.color = outColor;
  255. };
  256. timeTip.onApplyToNext += onApplyToNext;
  257. }
  258. }
  259. }
  260. int flag_SwapFriendAndRank = 0;
  261. public void OnClick_SwapFriendAndRank() {
  262. AudioMgr.ins.PlayBtn();
  263. if (flag_SwapFriendAndRank == 0) {
  264. flag_SwapFriendAndRank = 1;
  265. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("rank_title");
  266. StartCoroutine(RefreshFriendRankBar());
  267. } else if (flag_SwapFriendAndRank == 1) {
  268. flag_SwapFriendAndRank = 0;
  269. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("friend_title");
  270. StartCoroutine(RefreshFriendBar());
  271. }
  272. }
  273. public void OnClick_ShowDetail() {
  274. AudioMgr.ins.PlayBtn();
  275. if (flag_SwapFriendAndRank == 0) {
  276. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/FriendView"));
  277. } else if (flag_SwapFriendAndRank == 1) {
  278. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/RankView"));
  279. }
  280. }
  281. }