HomeView.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. using AdaptUI;
  10. /* 主界面 */
  11. public class HomeView : JCUnityLib.ViewBase
  12. {
  13. public static bool bOpenOtherView = false;
  14. public static string openName = "";
  15. [SerializeField] Image myAvatarSprite;
  16. [SerializeField] Text nickNameText;
  17. [SerializeField] GameObject[] genders;
  18. [SerializeField] GameObject btnConnectBow;
  19. [SerializeField] Text gameVersion;
  20. [SerializeField] GameObject friendBarTipTop;
  21. [SerializeField] GameObject friendBarTipBottom;
  22. [SerializeField] GameObject bettery;
  23. [SerializeField] GameObject betteryBar;
  24. [SerializeField] GameObject bettery2;
  25. [SerializeField] GameObject betteryBar2;
  26. [SerializeField] HomeView_ChallengeOption challengeOption;
  27. public HomeView_ChallengeOption MyChallengeOption => challengeOption;
  28. [SerializeField] HomeView_TopBarView topBarView;
  29. public HomeView_TopBarView MyTopBarView => topBarView;
  30. public static HomeView ins;
  31. void Awake() {
  32. gameVersion.text = "V" + Application.version;
  33. gameVersion.gameObject.SetActive(false); //隐藏版本号
  34. if (CommonConfig.needToExamine) {
  35. transform.Find("LeftPanel/Item (1)").gameObject.SetActive(false);
  36. transform.Find("RightPanel/Item/Text").GetComponent<TextAutoLanguage>().SetText(1234562);
  37. transform.Find("RightPanel/Item (1)/Text").GetComponent<TextAutoLanguage>().SetText(1234563);
  38. transform.Find("RightPanel/Item (3)").gameObject.SetActive(false);
  39. }
  40. if (topBarView && challengeOption) topBarView.onChangeTypeEvent.AddListener(challengeOption.refreshList);
  41. //设置相机视野角度,根据设备类型
  42. // 25 iPhone 40 iPad
  43. var deviceType = DeviceTypeHelper.DetectDeviceType();
  44. switch (deviceType)
  45. {
  46. case DeviceTypeHelper.DeviceType.iPhone:
  47. UserSettings.ins.bowRotateConvert.fieldOfView = 25;
  48. break;
  49. case DeviceTypeHelper.DeviceType.iPad:
  50. UserSettings.ins.bowRotateConvert.fieldOfView = 40;
  51. break;
  52. default:
  53. UserSettings.ins.bowRotateConvert.fieldOfView = 25;
  54. break;
  55. }
  56. }
  57. IEnumerator Start()
  58. {
  59. ins = this;
  60. transform.SetSiblingIndex(0); //放在ui最底层
  61. BluetoothHolder.Init();
  62. AudioMgr.Init();
  63. TopBarView.NeedShowIt(this);
  64. // if (ShootCheck.ins) ShootCheck.ins.AdjustNormalOrHightMode();
  65. if (LoginMgr.myUserInfo.id > 0 || (CommonConfig.StandaloneMode && UserPlayer.ins != null && UserPlayer.ins.hasGetUserInfo)) {
  66. RenderNameOrGender();
  67. RenderMyAvatarSprite();
  68. RenderDeviceNames();
  69. }
  70. if (CommonConfig.StandaloneMode)
  71. {
  72. transform.Find("FriendBar").gameObject.SetActive(false);
  73. transform.Find("RightPanel/Line (1)").gameObject.SetActive(false);
  74. transform.Find("RightPanel/Item (1)").gameObject.SetActive(false);
  75. }
  76. else StartCoroutine(RefreshFriendBar());
  77. //如果需要初始化跳转页面,这里处理
  78. Debug.Log("bOpenOtherView:"+ bOpenOtherView);
  79. if (bOpenOtherView) {
  80. bOpenOtherView = false;
  81. switch (openName)
  82. {
  83. case "DeviceView":
  84. // ViewMgr.Instance.ShowView<DeviceView>();
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. //StartCoroutine(testTip());
  91. yield return new WaitForSeconds(1.0f);
  92. //不是B 端进行蓝牙连接
  93. if (!CommonConfig.bDisableBluetooth)
  94. {
  95. //进行一次蓝牙连接
  96. BluetoothAim.ins.HomeViewConnectBLE();
  97. }
  98. }
  99. IEnumerator testTip() {
  100. yield return new WaitForSeconds(1.0f);
  101. GameObject settingsViewObj = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_HomeViewTip);
  102. settingsViewObj.GetComponent<HomeView_Tip>().setHomeTip(0);
  103. }
  104. void OnDestroy()
  105. {
  106. if (ins == this) ins = null;
  107. TopBarView.DontNeedShowIt(this);
  108. }
  109. void Update() {
  110. UpdateCheckRenderFriendTip();
  111. UpdateBtnForConnect();
  112. //更新电池显示
  113. updateBatteryStatus();
  114. }
  115. // float countingTime1 = 0;
  116. BluetoothStatusEnum bowStatus;
  117. void UpdateBtnForConnect() {
  118. if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status) {
  119. bowStatus = BluetoothAim.ins.status;
  120. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
  121. //btnConnectBow.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  122. //btnConnectBow.GetComponentInChildren<Text>().color = color;
  123. //btnConnectBow.transform.Find("Check").gameObject.SetActive(bowStatus == BluetoothStatusEnum.ConnectSuccess);
  124. }
  125. }
  126. public static bool ShowProminentBeforeConnectBLE(Action onAgree = null)
  127. {
  128. if (CommonConfig.StandaloneMode) return false;
  129. if (SceneManager.GetActiveScene().name.Equals("HeartRateBand")) return false;
  130. if (PlayerPrefs.GetInt("Location-Prominent", 0) == 0) {
  131. ModalView m = ModalView.Show();
  132. string pName = Application.productName;
  133. m.textKey = "Model_Location-Prominent";
  134. m.onAgreeTextKey = "common_next";
  135. m.onRejectTextKey = "common_cancel";
  136. m.textFormatArgs = new object[] {pName};
  137. m.onAgree = () => {
  138. PlayerPrefs.SetInt("Location-Prominent", 1);
  139. try {
  140. onAgree?.Invoke();
  141. } catch (System.Exception e) {
  142. Debug.LogError(e.Message + "\n" + e.StackTrace);
  143. }
  144. };
  145. return true;
  146. }
  147. return false;
  148. }
  149. public static bool ShowProminentBeforeConnectBLE_Event(Action onAgree = null,Action onReject = null)
  150. {
  151. if (CommonConfig.StandaloneMode) return false;
  152. if (SceneManager.GetActiveScene().name.Equals("HeartRateBand")) return false;
  153. if (PlayerPrefs.GetInt("Location-Prominent", 0) == 0)
  154. {
  155. ModalView m = ModalView.Show();
  156. string pName = Application.productName;
  157. m.textKey = "Model_Location-Prominent";
  158. m.onAgreeTextKey = "common_next";
  159. m.onRejectTextKey = "common_cancel";
  160. m.textFormatArgs = new object[] { pName };
  161. m.onAgree = () => {
  162. PlayerPrefs.SetInt("Location-Prominent", 1);
  163. try
  164. {
  165. onAgree?.Invoke();
  166. }
  167. catch (System.Exception e)
  168. {
  169. Debug.LogError(e.Message + "\n" + e.StackTrace);
  170. }
  171. };
  172. m.onReject = () =>
  173. {
  174. try
  175. {
  176. onReject?.Invoke();
  177. }
  178. catch (System.Exception e)
  179. {
  180. Debug.LogError(e.Message + "\n" + e.StackTrace);
  181. }
  182. };
  183. return true;
  184. }
  185. return false;
  186. }
  187. public void OnClick_ConnectBLE() {
  188. if (ShowProminentBeforeConnectBLE()) return;
  189. BluetoothAim.ins.DoConnect();
  190. }
  191. public void OnClick_ShowDeviceView() {
  192. AudioMgr.ins.PlayBtn();
  193. ViewMgr.Instance.ShowView<DeviceViewInfrared>();
  194. }
  195. public void RenderNameOrGender() {
  196. nickNameText.text = LoginMgr.myUserInfo.nickname;
  197. genders[LoginMgr.myUserInfo.gender == 2 ? 1 : 0].SetActive(true);
  198. genders[LoginMgr.myUserInfo.gender == 2 ? 0 : 1].SetActive(false);
  199. LayoutRebuilder.ForceRebuildLayoutImmediate(nickNameText.transform.parent.GetComponent<RectTransform>());
  200. }
  201. public void RenderMyAvatarSprite() {
  202. RoleMgr.SetAvatarToImage(myAvatarSprite, LoginMgr.myUserInfo.avatarID, LoginMgr.myUserInfo.avatarUrl);
  203. }
  204. public void RenderDeviceNames()
  205. {
  206. try {
  207. (DeviceInfo bowInfo, DeviceInfo arrowInfo) = DeviceMgr.ins.GetCurrentBowArrowInfo();
  208. // this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(bowInfo.config.name);
  209. this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(200000);
  210. this.transform.Find("ShowArrow/Text").GetComponent<TextAutoLanguage>().SetText(arrowInfo.config.name);
  211. } catch (System.Exception) {}
  212. }
  213. public Action action_OnClickStartGame;
  214. public void GoTo(string target) {
  215. AudioMgr.ins.PlayBtn();
  216. switch (target)
  217. {
  218. case "开始游戏":
  219. #if !UNITY_EDITOR
  220. if ((CommonConfig.isReleaseVersion || CommonConfig.StandaloneMode) && !BluetoothStatus.IsAllConnected())
  221. {
  222. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
  223. return;
  224. }
  225. #endif
  226. ViewMgr.Instance.ShowView<GameStartView>();
  227. action_OnClickStartGame?.Invoke();
  228. break;
  229. case "联机游戏":
  230. if (CommonConfig.isReleaseVersion && !BluetoothStatus.IsAllConnected()) {
  231. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
  232. return;
  233. }
  234. GlobalDataTemp.pkMatchType = PKMatchType.OnlinePK;
  235. ViewMgr.Instance.ShowView<PKGameOptionView>();
  236. break;
  237. case "我的":
  238. //ViewMgr.Instance.ShowView<MeView>();
  239. if (!CommonConfig.StandaloneMode)
  240. ViewManager2.ShowView(ViewManager2.Path_PersonalView);
  241. break;
  242. default:
  243. break;
  244. }
  245. }
  246. /* FriendBar */
  247. GameObject friendItemPrefab = null;
  248. Transform friendBarContent = null;
  249. IEnumerator RefreshFriendBar() {
  250. yield return null;
  251. //if (friendItemPrefab == null) {
  252. // friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  253. // Destroy(friendItemPrefab.GetComponent<Image>());
  254. // friendItemPrefab.SetActive(false);
  255. //}
  256. //if (friendBarContent == null) {
  257. // friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  258. //}
  259. //while (UserPlayer.ins == null || !UserPlayer.ins.loginAuthed || !UserPlayer.ins.hasGetUserInfo) {
  260. // yield return null;
  261. //}
  262. //Action<JArray> cb = RenderFriendList;
  263. //UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
  264. }
  265. public void RenderFriendList(JArray list) {
  266. if (flag_SwapFriendAndRank != 0) return;
  267. for (int i = 1; i < friendBarContent.childCount; i++) {
  268. Destroy(friendBarContent.GetChild(i).gameObject);
  269. }
  270. if (list.Count > 0) {
  271. Color outColor;
  272. foreach (var itemInfo in list) {
  273. int friendID = itemInfo.Value<int>("friendID");
  274. int avatarID = itemInfo.Value<int>("avatarID");
  275. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  276. string nickname = itemInfo.Value<string>("nickname");
  277. bool online = itemInfo.Value<bool>("online");
  278. long offlineTime = itemInfo.Value<long>("offlineTime");
  279. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  280. o.SetActive(true);
  281. o.name = friendID.ToString();
  282. Image avatarImage = o.transform.Find("Avatar").GetComponent<Image>();
  283. RoleMgr.SetAvatarToImage(avatarImage, avatarID, avatarUrl);
  284. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  285. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  286. TimeUtil.SetOfflineTimeTextKey(offlineTime, online, timeTip);
  287. if (online) {
  288. ColorUtility.TryParseHtmlString("#12B525", out outColor);
  289. } else {
  290. ColorUtility.TryParseHtmlString("#A8B2BD", out outColor);
  291. }
  292. timeTip.GetComponent<Text>().color = outColor;
  293. }
  294. }
  295. }
  296. IEnumerator RefreshFriendRankBar() {
  297. if (friendItemPrefab == null) {
  298. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  299. Destroy(friendItemPrefab.GetComponent<Image>());
  300. friendItemPrefab.SetActive(false);
  301. }
  302. if (friendBarContent == null) {
  303. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  304. }
  305. while (UserPlayer.ins == null || !UserPlayer.ins.loginAuthed || !UserPlayer.ins.hasGetUserInfo) {
  306. yield return null;
  307. }
  308. Action<JArray> cb = RenderFriendRankList;
  309. UserPlayer.ins.call("rankComp.getFriendRankList", null, cb);
  310. }
  311. void RenderFriendRankList(JArray list) {
  312. if (flag_SwapFriendAndRank != 1) return;
  313. for (int i = 1; i < friendBarContent.childCount; i++) {
  314. Destroy(friendBarContent.GetChild(i).gameObject);
  315. }
  316. if (list.Count > 0) {
  317. foreach (var itemInfo in list) {
  318. int userID = itemInfo.Value<int>("id");
  319. int avatarID = itemInfo.Value<int>("avatarID");
  320. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  321. string nickname = itemInfo.Value<string>("nickname");
  322. bool online = itemInfo.Value<bool>("online");
  323. long offlineTime = itemInfo.Value<long>("offlineTime");
  324. int rankNum = itemInfo.Value<int>("rankNum");
  325. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  326. o.SetActive(true);
  327. o.name = userID.ToString();
  328. Image avatarImage = o.transform.Find("Avatar").GetComponent<Image>();
  329. RoleMgr.SetAvatarToImage(avatarImage, avatarID, avatarUrl);
  330. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  331. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  332. Action<Text> onApplyToNext = (t) => {
  333. if (TextAutoLanguage2.GetLanguage() == 0) {
  334. t.text = $"第{rankNum}名";
  335. } else {
  336. string str = rankNum.ToString();
  337. if (str.EndsWith("1")) t.text = $"{rankNum}st";
  338. else if (str.EndsWith("2")) t.text = $"{rankNum}nd";
  339. else if (str.EndsWith("3")) t.text = $"{rankNum}rd";
  340. else t.text = $"{rankNum}th";
  341. }
  342. Color outColor;
  343. if (rankNum == 1) ColorUtility.TryParseHtmlString("#F0E68C", out outColor);
  344. else if (rankNum == 2) ColorUtility.TryParseHtmlString("#E0FFFF", out outColor);
  345. else if (rankNum == 3) ColorUtility.TryParseHtmlString("#CD8162", out outColor);
  346. else ColorUtility.TryParseHtmlString("#EEE5DE", out outColor);
  347. t.color = outColor;
  348. };
  349. timeTip.onApplyToNext += onApplyToNext;
  350. }
  351. }
  352. }
  353. int flag_SwapFriendAndRank = 0;
  354. public void OnClick_SwapFriendAndRank() {
  355. AudioMgr.ins.PlayBtn();
  356. if (flag_SwapFriendAndRank == 0) {
  357. flag_SwapFriendAndRank = 1;
  358. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("rank_title");
  359. } else if (flag_SwapFriendAndRank == 1) {
  360. flag_SwapFriendAndRank = 0;
  361. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("friend_title");
  362. }
  363. RefreshFriendBarAccordingCurrentBar();
  364. }
  365. //根据栏目类型,调用对应的接口请求
  366. public void RefreshFriendBarAccordingCurrentBar()
  367. {
  368. if (flag_SwapFriendAndRank == 0)
  369. StartCoroutine(RefreshFriendBar());
  370. else if (flag_SwapFriendAndRank == 1)
  371. StartCoroutine(RefreshFriendRankBar());
  372. }
  373. public void OnClick_ShowDetail() {
  374. AudioMgr.ins.PlayBtn();
  375. if (flag_SwapFriendAndRank == 0) {
  376. ViewMgr.Instance.ShowView<FriendView>();
  377. } else if (flag_SwapFriendAndRank == 1) {
  378. ViewMgr.Instance.ShowView<RankView>();
  379. }
  380. }
  381. //检测是否需要提示有人加我好友
  382. bool _old_hasFriendRequest = false;
  383. int _old_flag_SwapFriendAndRank = 0;
  384. void UpdateCheckRenderFriendTip()
  385. {
  386. if (UserPlayer.ins == null) return;
  387. if (
  388. UserPlayer.ins.tempData.hasFriendRequest == _old_hasFriendRequest &&
  389. flag_SwapFriendAndRank == _old_flag_SwapFriendAndRank
  390. ) return;
  391. _old_hasFriendRequest = UserPlayer.ins.tempData.hasFriendRequest;
  392. _old_flag_SwapFriendAndRank = flag_SwapFriendAndRank;
  393. friendBarTipTop.SetActive(
  394. flag_SwapFriendAndRank == 1 &&
  395. UserPlayer.ins.tempData.hasFriendRequest);
  396. friendBarTipBottom.SetActive(
  397. flag_SwapFriendAndRank == 0 &&
  398. UserPlayer.ins.tempData.hasFriendRequest);
  399. }
  400. public void RenderBattery(int deviceID, int value)
  401. {
  402. Image img = betteryBar.GetComponent<Image>();
  403. img.fillAmount = value / 100f;
  404. }
  405. public void RenderBattery2() {
  406. int value = BluetoothAim.ins.get2PBattery();
  407. Image img = betteryBar2.GetComponent<Image>();
  408. img.fillAmount = value / 100f;
  409. }
  410. public void UpdateParent() {
  411. TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo();
  412. if (bettery != null)
  413. {
  414. bettery.transform.SetParent(topBarButtonInfo.container.transform);
  415. }
  416. if (bettery2 != null)
  417. {
  418. bettery2.transform.SetParent(topBarButtonInfo.container.transform);
  419. }
  420. }
  421. void updateBatteryStatus() {
  422. if (BluetoothAim.ins == null) return;
  423. //更新电池图标显示
  424. if (BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  425. {
  426. if (!bettery.activeSelf)
  427. {
  428. TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo();
  429. bettery.transform.SetParent(topBarButtonInfo.container.transform);
  430. bettery.SetActive(true);
  431. }
  432. }
  433. else
  434. {
  435. if (bettery.activeSelf)
  436. {
  437. bettery.SetActive(false);
  438. }
  439. }
  440. //更新设备2电池图标显示
  441. if (BluetoothAim.ins.getSmartBowHelper2P() != null &&
  442. BluetoothAim.ins.getSmartBowHelper2P().GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected)
  443. {
  444. if (!bettery2.activeSelf)
  445. {
  446. TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo();
  447. bettery2.transform.SetParent(topBarButtonInfo.container.transform);
  448. bettery2.SetActive(true);
  449. }
  450. }
  451. else
  452. {
  453. if (bettery2.activeSelf)
  454. {
  455. bettery2.SetActive(false);
  456. }
  457. }
  458. }
  459. }