HomeView.cs 20 KB

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