HomeView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using AdaptUI;
  2. using AppUI.Bluetooth;
  3. using AppUI.Manager.Role;
  4. using AppUI.Manager.View;
  5. using AppUI.View.Home.DeviceView;
  6. using BestHTTP.SignalR.Hubs;
  7. using InfraredManager;
  8. using SmartBowSDK;
  9. using System;
  10. using System.Collections;
  11. using SdkAimDeviceInfo = SmartBowSDK.AimDeviceInfo;
  12. using TMPro;
  13. using UnityEngine;
  14. using UnityEngine.SceneManagement;
  15. using UnityEngine.UI;
  16. namespace AppUI.View.Home.Main
  17. {
  18. /**
  19. * 新处理HomeView,包含主页的主要功能,如显示用户信息、连接蓝牙设备、显示挑战选项等。
  20. 1. 显示用户头像和昵称。
  21. 2. 显示游戏版本号(目前隐藏)。
  22. 3. 提供连接蓝牙设备的按钮,并根据连接状态更新按钮显示。
  23. 4. 显示好友请求提示。
  24. 5. 显示电池状态图标。
  25. 6. 包含挑战选项和顶部栏视图,并处理它们之间的交互。
  26. */
  27. public class HomeView : MonoBehaviour
  28. {
  29. public static bool bOpenOtherView = false;
  30. public static string openName = "";
  31. [SerializeField] Image myAvatarSprite;
  32. [SerializeField] TMP_Text nickNameText;
  33. [SerializeField] Text gameVersion;
  34. //[SerializeField] GameObject friendBarTipTop;
  35. [SerializeField] GameObject friendBarTipBottom;
  36. [SerializeField] HomeViewChallengeOption challengeOption;
  37. public HomeViewChallengeOption MyChallengeOption => challengeOption;
  38. [SerializeField] HomeViewTopBar topBarView;
  39. public HomeViewTopBar MyTopBarView => topBarView;
  40. public static HomeView ins;
  41. SmartBowDeviceHub Hub => SmartBowDeviceHub.ins;
  42. [SerializeField] TMP_Text curConnectStatus;
  43. [SerializeField] TMP_Text curScreenPositioningStatus;
  44. void Awake()
  45. {
  46. ins = this;
  47. gameVersion.text = "V" + Application.version;
  48. gameVersion.gameObject.SetActive(false); //隐藏版本号
  49. if (CommonConfig.needToExamine)
  50. {
  51. transform.Find("LeftPanel/Item (1)").gameObject.SetActive(false);
  52. transform.Find("RightPanel/Item/Text").GetComponent<TextAutoLanguage>().SetText(1234562);
  53. transform.Find("RightPanel/Item (1)/Text").GetComponent<TextAutoLanguage>().SetText(1234563);
  54. transform.Find("RightPanel/Item (3)").gameObject.SetActive(false);
  55. }
  56. //动态添加信息
  57. challengeOption.AddLevelInfo();
  58. if (topBarView && challengeOption) topBarView.onChangeTypeEvent.AddListener(challengeOption.refreshList);
  59. //设置相机视野角度,根据设备类型
  60. // 25 iPhone 40 iPad
  61. var deviceType = DeviceTypeHelper.DetectDeviceType();
  62. switch (deviceType)
  63. {
  64. case DeviceTypeHelper.DeviceType.iPhone:
  65. UserSettings.ins.bowRotateConvert.fieldOfView = 25;
  66. break;
  67. case DeviceTypeHelper.DeviceType.iPad:
  68. UserSettings.ins.bowRotateConvert.fieldOfView = 40;
  69. break;
  70. default:
  71. UserSettings.ins.bowRotateConvert.fieldOfView = 25;
  72. break;
  73. }
  74. }
  75. IEnumerator Start()
  76. {
  77. transform.SetSiblingIndex(0); //放在ui最底层
  78. SmartBowDeviceHub.EnsureReady();
  79. if (SmartBowDeviceHub.ins?.IsConnected(BluetoothPlayer.FIRST_PLAYER) == true)
  80. SmartBowDeviceHub.ins.SetMainConnectDeviceType();
  81. AudioMgr.Init();
  82. if (LoginMgr.myUserInfo.id > 0 || (CommonConfig.StandaloneMode && UserPlayer.ins != null && UserPlayer.ins.hasGetUserInfo))
  83. {
  84. RenderNameOrGender();
  85. RenderMyAvatarSprite();
  86. }
  87. //如果需要初始化跳转页面,这里处理
  88. Debug.Log("bOpenOtherView:" + bOpenOtherView);
  89. if (bOpenOtherView)
  90. {
  91. bOpenOtherView = false;
  92. switch (openName)
  93. {
  94. case "DeviceView":
  95. // ViewMgr.Instance.ShowView<DeviceView>();
  96. break;
  97. default:
  98. break;
  99. }
  100. }
  101. yield return new WaitForSeconds(1.0f);
  102. // 登录/认证成功后再自动连蓝牙(与 AuthLoginMask 重连互不干扰)
  103. UserPlayer.TryHomeAutoConnectBle();
  104. }
  105. void OnDestroy()
  106. {
  107. if (ins == this) ins = null;
  108. }
  109. void Update()
  110. {
  111. UpdateCheckRenderFriendTip();
  112. onUpdateStatusInfo();
  113. RefreshScreenPositioningStatusText();
  114. }
  115. public static bool ShowProminentBeforeConnectBLE(Action onAgree = null)
  116. {
  117. if (CommonConfig.StandaloneMode) return false;
  118. if (SceneManager.GetActiveScene().name.Equals("HeartRateBand")) return false;
  119. if (PlayerPrefs.GetInt("Location-Prominent", 0) == 0)
  120. {
  121. //ModalView m = ModalView.Show();
  122. ModalConfirmView m = ModalConfirmView.Show();
  123. string pName = Application.productName;
  124. m.textKey = "Model_Location-Prominent";
  125. //m.onAgreeTextKey = "common_next";
  126. m.onConfirmTextKey = "common_next";
  127. //m.onRejectTextKey = "common_cancel";
  128. m.textFormatArgs = new object[] { pName };
  129. //m.onAgree = () => {
  130. m.onConfirm = () =>
  131. {
  132. PlayerPrefs.SetInt("Location-Prominent", 1);
  133. try
  134. {
  135. onAgree?.Invoke();
  136. }
  137. catch (System.Exception e)
  138. {
  139. Debug.LogError(e.Message + "\n" + e.StackTrace);
  140. }
  141. };
  142. return true;
  143. }
  144. return false;
  145. }
  146. public void OnClick_ConnectBLE()
  147. {
  148. if (ShowProminentBeforeConnectBLE()) return;
  149. SmartBowDeviceHub.ins?.Connect1P();
  150. }
  151. public void OnClick_ShowDeviceView()
  152. {
  153. AudioMgr.ins.PlayBtn();
  154. //ViewManager.ShowView<DeviceView>();
  155. }
  156. public void RenderNameOrGender()
  157. {
  158. nickNameText.text = LoginMgr.myUserInfo.nickname;
  159. //genders[LoginMgr.myUserInfo.gender == 2 ? 1 : 0].SetActive(true);
  160. //genders[LoginMgr.myUserInfo.gender == 2 ? 0 : 1].SetActive(false);
  161. LayoutRebuilder.ForceRebuildLayoutImmediate(nickNameText.transform.parent.GetComponent<RectTransform>());
  162. }
  163. public void RenderMyAvatarSprite()
  164. {
  165. RoleManager.SetAvatarToImage(myAvatarSprite, LoginMgr.myUserInfo.avatarID, LoginMgr.myUserInfo.avatarUrl);
  166. }
  167. int flag_SwapFriendAndRank = 0;
  168. //检测是否需要提示有人加我好友
  169. bool _old_hasFriendRequest = false;
  170. int _old_flag_SwapFriendAndRank = 0;
  171. void UpdateCheckRenderFriendTip()
  172. {
  173. if (UserPlayer.ins == null) return;
  174. if (
  175. UserPlayer.ins.tempData.hasFriendRequest == _old_hasFriendRequest &&
  176. flag_SwapFriendAndRank == _old_flag_SwapFriendAndRank
  177. ) return;
  178. _old_hasFriendRequest = UserPlayer.ins.tempData.hasFriendRequest;
  179. _old_flag_SwapFriendAndRank = flag_SwapFriendAndRank;
  180. //friendBarTipTop.SetActive(
  181. // flag_SwapFriendAndRank == 1 &&
  182. // UserPlayer.ins.tempData.hasFriendRequest);
  183. friendBarTipBottom.SetActive(
  184. flag_SwapFriendAndRank == 0 &&
  185. UserPlayer.ins.tempData.hasFriendRequest);
  186. }
  187. [SerializeField] BluetoothPlayer bluetoothPlayer = BluetoothPlayer.FIRST_PLAYER;
  188. readonly Color32 StatusCompleteColor = new Color32(0, 0, 0, 255);
  189. readonly Color32 StatusIncompleteColor = new Color32(145, 144, 144, 255);
  190. bool IsConnecting()
  191. {
  192. return Hub != null && Hub.IsConnecting(bluetoothPlayer);
  193. }
  194. public bool IsConnectSuccess()
  195. {
  196. return Hub != null && Hub.IsConnected(bluetoothPlayer);
  197. }
  198. public bool IsScreenPositioned()
  199. {
  200. InfraredCameraHelper helper = InfraredCameraHelper.GetInstance();
  201. return helper != null && helper.IsScreenLoateOK();
  202. }
  203. bool TryGetDeviceInfo(out int deviceType, out bool bInitMac)
  204. {
  205. deviceType = -1;
  206. bInitMac = false;
  207. var aim = Hub?.Aim;
  208. if (aim == null) return false;
  209. SdkAimDeviceInfo info = aim.GetAimDeviceInfo(bluetoothPlayer);
  210. if (info == null) return false;
  211. deviceType = aim.GetDeviceType(bluetoothPlayer);
  212. bInitMac = info.bInitMac;
  213. return true;
  214. }
  215. bool HasRecordedDeviceInfo()
  216. {
  217. return TryGetDeviceInfo(out int deviceType, out bool bInitMac)
  218. && deviceType != -1
  219. && bInitMac;
  220. }
  221. bool TryGetDeviceDisplayName(out string displayName)
  222. {
  223. displayName = null;
  224. if (!TryGetDeviceInfo(out int deviceType, out _) || deviceType == -1)
  225. return false;
  226. return Hub != null
  227. && Hub.TryGetDeviceDisplay((AimDeviceType)deviceType, out _, out displayName)
  228. && !string.IsNullOrEmpty(displayName);
  229. }
  230. bool IsInfraredDevice()
  231. {
  232. if (!TryGetDeviceInfo(out int deviceType, out bool bInitMac) || deviceType == -1 || !bInitMac)
  233. return false;
  234. switch ((AimDeviceType)deviceType)
  235. {
  236. case AimDeviceType.HOUYIPRO:
  237. case AimDeviceType.Gun:
  238. case AimDeviceType.ARTEMISPRO:
  239. case AimDeviceType.PistolM17:
  240. case AimDeviceType.RifleM416:
  241. return true;
  242. default:
  243. return false;
  244. }
  245. }
  246. bool IsInfraredCameraReady() => InfraredDemo.IsUVCReady;
  247. public void onUpdateStatusInfo()
  248. {
  249. if (Hub == null || curConnectStatus == null) return;
  250. if (!HasRecordedDeviceInfo())
  251. {
  252. if (curConnectStatus.gameObject.activeSelf)
  253. curConnectStatus.gameObject.SetActive(false);
  254. return;
  255. }
  256. if (!curConnectStatus.gameObject.activeSelf)
  257. curConnectStatus.gameObject.SetActive(true);
  258. string statusText = TextAutoLanguage2.GetTextByKey(Hub.GetStatusTextKey(bluetoothPlayer));
  259. if (TryGetDeviceDisplayName(out string displayName))
  260. curConnectStatus.text = $"{displayName} {statusText}";
  261. else
  262. curConnectStatus.text = statusText;
  263. curConnectStatus.color = IsConnectSuccess() ? StatusCompleteColor : StatusIncompleteColor;
  264. }
  265. void RefreshScreenPositioningStatusText()
  266. {
  267. if (curScreenPositioningStatus == null) return;
  268. if (!IsInfraredDevice() || !IsInfraredCameraReady())
  269. {
  270. if (curScreenPositioningStatus.gameObject.activeSelf)
  271. curScreenPositioningStatus.gameObject.SetActive(false);
  272. return;
  273. }
  274. if (!curScreenPositioningStatus.gameObject.activeSelf)
  275. curScreenPositioningStatus.gameObject.SetActive(true);
  276. bool positioned = IsScreenPositioned();
  277. curScreenPositioningStatus.text = positioned ? "屏幕定位已完成" : "屏幕定位未完成";
  278. curScreenPositioningStatus.color = positioned ? StatusCompleteColor : StatusIncompleteColor;
  279. }
  280. }
  281. }