DeviceViewInfrared.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using AdaptUI;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. /* 设备界面 */
  8. public class DeviceViewInfrared : JCUnityLib.ViewBase, MenuBackInterface
  9. {
  10. //GameObject bowOptions;
  11. [SerializeField] GameObject Connect1Parent;
  12. [SerializeField] List<Button> smartConnect1Buttons;
  13. [SerializeField] GameObject Connect2Parent;
  14. [SerializeField] List<Button> smartConnect2Buttons;
  15. [SerializeField] List<Sprite> smartArcheryBg;
  16. [SerializeField] List<DeviceView_ItemShow> deviceViewItems;
  17. //当前选择的对象类
  18. [HideInInspector] public DeviceView_ItemShow selectDeviceViewItem;
  19. public static DeviceViewInfrared ins;
  20. public Action action_OnClickGyr;
  21. public Action action_OnClickMag;
  22. public HorizontalLayoutGroup horizontalLayoutGroup;
  23. bool bSwitchDevice = false;
  24. void Start()
  25. {
  26. ins = this;
  27. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  28. //Button1 事件绑定
  29. for (int i = 0; i < smartConnect1Buttons.Count; i++)
  30. {
  31. int temp = i;
  32. smartConnect1Buttons[i].onClick.AddListener(()=>{
  33. OnChangeSmartConnect1Button(temp);
  34. });
  35. }
  36. OnChangeSmartArcheryButtonState((int)GlobalData.MyDeviceMode);
  37. //Button2 事件绑定
  38. for (int i = 0; i < smartConnect2Buttons.Count; i++)
  39. {
  40. int temp = i;
  41. smartConnect2Buttons[i].onClick.AddListener(() => {
  42. OnChangeSmartConnect2Button(temp);
  43. });
  44. }
  45. AimHandler.ins.onCreateAimDeviceInfoById();
  46. //默认选择第一个设备
  47. deviceViewItems[0].OnSelectEvent();
  48. //选择中的自动连接选择的对象
  49. if (selectDeviceViewItem.getCurrentItemVisible() && ! selectDeviceViewItem.getBLEConnectState()) {
  50. selectDeviceViewItem.OnConnectedEvent();
  51. }
  52. //先隐藏一个设备。如果连接之后,再显示2p,如果已经连接到蓝牙,直接显示
  53. if (deviceViewItems[1].getBLEConnectState())
  54. {
  55. ShowDeviceViewItemsTwo(true);
  56. }
  57. //else
  58. //{
  59. // ShowDeviceViewItemsTwo(false);
  60. //}
  61. }
  62. void OnDestroy()
  63. {
  64. if (ins == this) ins = null;
  65. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  66. //退出面板时候,取消正在连接的连接
  67. BluetoothAim.ins.onCancelAllConnecting();
  68. //如果没有连接
  69. if (!deviceViewItems[1].getBLEConnectState()) {
  70. //隐藏时候。直接清空这个记录数据
  71. AimHandler.ins.onClear2PAimDeviceInfosNew();
  72. }
  73. }
  74. void Update()
  75. {
  76. //刷新按钮显示状态2P
  77. if (BluetoothAim.ins)
  78. {
  79. if (selectDeviceViewItem != null)
  80. {
  81. if (selectDeviceViewItem.getCurrentItemVisible() && !bSwitchDevice)
  82. {
  83. if (Connect1Parent.activeSelf) Connect1Parent.SetActive(false);
  84. if (!Connect2Parent.activeSelf) Connect2Parent.SetActive(true);
  85. if (Connect2Parent.activeSelf)
  86. {
  87. //判断是否显示连接还是断开按钮
  88. if (selectDeviceViewItem.getBLEConnectState())
  89. {
  90. if (smartConnect2Buttons[1].gameObject.activeSelf)
  91. {
  92. smartConnect2Buttons[1].gameObject.SetActive(false);
  93. smartConnect2Buttons[2].gameObject.SetActive(true);
  94. }
  95. }
  96. else
  97. {
  98. if (!smartConnect2Buttons[1].gameObject.activeSelf)
  99. {
  100. smartConnect2Buttons[1].gameObject.SetActive(true);
  101. smartConnect2Buttons[2].gameObject.SetActive(false);
  102. }
  103. }
  104. }
  105. }
  106. else
  107. {
  108. if (!Connect1Parent.activeSelf) Connect1Parent.SetActive(true);
  109. if (Connect2Parent.activeSelf) Connect2Parent.SetActive(false);
  110. }
  111. }
  112. else {
  113. if (!Connect1Parent.activeSelf) Connect1Parent.SetActive(true);
  114. if (Connect2Parent.activeSelf) Connect2Parent.SetActive(false);
  115. }
  116. //if (!selectDeviceViewItem.bInfraredDevices())
  117. DeviceTypeHelper.DeviceType detectedType = DeviceTypeHelper.DetectDeviceType();
  118. if (detectedType == DeviceTypeHelper.DeviceType.iPhone)
  119. {
  120. //2p 按钮 ,两个同时蓝牙连接后。隐藏
  121. if (deviceViewItems[1].gameObject.activeSelf || deviceViewItems[0].getBLEConnectState() && deviceViewItems[1].getBLEConnectState())
  122. {
  123. if (smartConnect2Buttons[3].gameObject.activeSelf)
  124. {
  125. smartConnect2Buttons[3].gameObject.SetActive(false);
  126. }
  127. }
  128. else if (!smartConnect2Buttons[3].gameObject.activeSelf)
  129. {
  130. smartConnect2Buttons[3].gameObject.SetActive(true);
  131. }
  132. }
  133. else
  134. {
  135. //ipad暂时隐藏2p
  136. if (smartConnect2Buttons[3].gameObject.activeSelf)
  137. {
  138. smartConnect2Buttons[3].gameObject.SetActive(false);
  139. }
  140. }
  141. }
  142. }
  143. void ShowDeviceViewItemsTwo(bool bShow) {
  144. if (bShow)
  145. {
  146. if (!deviceViewItems[1].gameObject.activeSelf)
  147. {
  148. deviceViewItems[1].gameObject.SetActive(true);
  149. deviceViewItems[1].onShowDeviceInfo();//手动刷新一下信息
  150. horizontalLayoutGroup.spacing = 40;
  151. //显示2p时候。默认选择中
  152. deviceViewItems[1].OnSelectEvent();
  153. }
  154. }
  155. else
  156. {
  157. if (deviceViewItems[1].gameObject.activeSelf)
  158. {
  159. deviceViewItems[1].gameObject.SetActive(false);
  160. horizontalLayoutGroup.spacing = 76;
  161. deviceViewItems[0].OnSelectEvent();
  162. }
  163. }
  164. }
  165. public bool OnMenuBack() {
  166. ViewMgr.Instance.DestroyView<DeviceViewInfrared>();
  167. return true;
  168. }
  169. public void OnClick_Back() {
  170. AudioMgr.ins.PlayBtn();
  171. ViewMgr.Instance.DestroyView<DeviceViewInfrared>();
  172. }
  173. /// <summary>
  174. /// 第一次进入页面选择的按钮的下标
  175. /// </summary>
  176. /// <param name="index"></param>
  177. public void OnChangeSmartConnect1Button(int index)
  178. {
  179. bool _selected = false;
  180. for (int i = 0; i < smartConnect1Buttons.Count; i++)
  181. {
  182. Button _button = smartConnect1Buttons[i];
  183. Color32 _white;
  184. if (index == i)
  185. {
  186. _white = new Color32(255, 255, 255, 255);
  187. _selected = true;
  188. _button.GetComponent<Image>().sprite = smartArcheryBg[1];
  189. }
  190. else
  191. {
  192. _white = new Color32(59, 59, 59, 255);
  193. //_buttonBg = new Color32(255, 255, 255, 255);
  194. _button.GetComponent<Image>().sprite = smartArcheryBg[0];
  195. }
  196. _button.transform.Find("icon").GetComponent<Image>().color = _white;
  197. _button.transform.Find("title").GetComponent<Text>().color = _white;
  198. _button.transform.Find("arrow").GetComponent<Image>().color = _white;
  199. }
  200. if (_selected)
  201. {
  202. //进入选中的页面
  203. AudioMgr.ins.PlayBtn();
  204. //设置一次 GlobalData.MyDeviceMode
  205. if (getEnabelPanelStatus())//是否处于选中状态
  206. {
  207. //取消正在连接的连接
  208. BluetoothAim.ins.onCancelAllConnecting(BluetoothStatusEnum.Connect);
  209. if (index == 0)
  210. {
  211. ViewMgr.Instance.ShowView<SmartArcheryView>();
  212. }
  213. else if (index == 1)
  214. {
  215. Debug.Log("Gun");
  216. ViewMgr.Instance.ShowView<SmartGunView>();
  217. }
  218. bSwitchDevice = false;
  219. AimHandler.ins.onCreateTempAimDeviceInfo();
  220. }
  221. }
  222. }
  223. /// <summary>
  224. /// 第二次进入页面时候。
  225. /// </summary>
  226. /// <param name="index"></param>
  227. public void OnChangeSmartConnect2Button(int index)
  228. {
  229. //Debug.Log("OnChangeSmartConnect1Button按钮:" + index);
  230. bool _selected = false;
  231. for (int i = 0; i < smartConnect2Buttons.Count; i++)
  232. {
  233. Button _button = smartConnect2Buttons[i];
  234. Color32 _white;
  235. //Color32 _buttonBg;
  236. if (index == i)
  237. {
  238. _white = new Color32(255, 255, 255, 255);
  239. //_buttonBg = new Color32(16, 194, 198, 255);
  240. _selected = true;
  241. _button.GetComponent<Image>().sprite = smartArcheryBg[1];
  242. }
  243. else
  244. {
  245. _white = new Color32(59, 59, 59, 255);
  246. //_buttonBg = new Color32(255, 255, 255, 255);
  247. _button.GetComponent<Image>().sprite = smartArcheryBg[0];
  248. }
  249. _button.transform.Find("icon").GetComponent<Image>().color = _white;
  250. _button.transform.Find("title").GetComponent<Text>().color = _white;
  251. _button.transform.Find("arrow").GetComponent<Image>().color = _white;
  252. }
  253. if (_selected)
  254. {
  255. //进入选中的页面
  256. AudioMgr.ins.PlayBtn();
  257. //设置一次 GlobalData.MyDeviceMode
  258. switch (index)
  259. {
  260. case 0:
  261. if (selectDeviceViewItem.getBLEConnectState())
  262. {
  263. if (BluetoothAim.ins.isMainConnectToInfraredDevice() || BluetoothAim.ins.isMainConnectToGun())
  264. {
  265. //进入屏幕定位
  266. ViewManager2.ShowView(ViewManager2.Path_InfraredView);
  267. }
  268. else {
  269. //只有红外设备才能进行屏幕识别
  270. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("Connect_Tip_Infrared"));
  271. }
  272. }
  273. else {
  274. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("Connect_Tip"));
  275. }
  276. break;
  277. case 1:
  278. //连接设备
  279. if (getEnabelPanelStatus())//是否处于选中状态
  280. {
  281. if (selectDeviceViewItem)
  282. {
  283. //有信息的情况下直接连接
  284. selectDeviceViewItem.OnConnectedEvent();
  285. }
  286. else {
  287. //取消正在连接的连接
  288. BluetoothAim.ins.onCancelAllConnecting(BluetoothStatusEnum.Connect);
  289. //进入弓箭选择页面
  290. ViewMgr.Instance.ShowView<SmartArcheryView>();
  291. AimHandler.ins.onCreateTempAimDeviceInfo();
  292. }
  293. }
  294. break;
  295. case 2:
  296. //断开连接设备
  297. if (selectDeviceViewItem)
  298. {
  299. selectDeviceViewItem.OnDisConnectedEvent();
  300. }
  301. break;
  302. case 3:
  303. //增加2p todo HOUYI Pro/ARTEMIS Pro时,先不能增加2P;(先隐藏,待后续增加2P后,再显示)
  304. // if (!(BluetoothAim.ins && (BluetoothAim.ins.isMainConnectToInfraredDevice() || BluetoothAim.ins.isMainConnectToGun())))
  305. //if (BluetoothAim.ins)
  306. if (!(BluetoothAim.ins && BluetoothAim.ins.isMainConnectToGun()))
  307. {
  308. ShowDeviceViewItemsTwo(true);
  309. //重置一些按钮状态和参数
  310. OnChangeSmartConnect1Button(-1);
  311. OnChangeSmartConnect2Button(-1);
  312. }
  313. break;
  314. case 4:
  315. //切换设备前先断开蓝牙连接
  316. if (selectDeviceViewItem)
  317. {
  318. selectDeviceViewItem.OnDisConnectedEvent();
  319. }
  320. bSwitchDevice = true;
  321. //重置一些按钮状态和参数
  322. OnChangeSmartConnect1Button(-1);
  323. OnChangeSmartConnect2Button(-1);
  324. break;
  325. }
  326. }
  327. }
  328. //暂时先更新一个
  329. public void RenderBattery(int deviceID, int value)
  330. {
  331. //smartConnect1Buttons[0].GetComponent<DeviceView_ItemShow>().RenderBattery(deviceID,value);
  332. }
  333. public void OnChangeSmartArcheryButtonState(int index)
  334. {
  335. for (int i = 0; i < smartConnect1Buttons.Count; i++)
  336. {
  337. Button _button = smartConnect1Buttons[i];
  338. Color32 _white;
  339. //Color32 _buttonBg;
  340. if (index == i)
  341. {
  342. _white = new Color32(255, 255, 255, 255);
  343. _button.GetComponent<Image>().sprite = smartArcheryBg[1];
  344. }
  345. else
  346. {
  347. _white = new Color32(59, 59, 59, 255);
  348. _button.GetComponent<Image>().sprite = smartArcheryBg[0];
  349. }
  350. _button.transform.Find("icon").GetComponent<Image>().color = _white;
  351. _button.transform.Find("title").GetComponent<Text>().color = _white;
  352. _button.transform.Find("arrow").GetComponent<Image>().color = _white;
  353. }
  354. }
  355. public void OnChangeSmartConnect2ButtonState(int index)
  356. {
  357. for (int i = 0; i < smartConnect2Buttons.Count; i++)
  358. {
  359. Button _button = smartConnect2Buttons[i];
  360. Color32 _white;
  361. //Color32 _buttonBg;
  362. if (index == i)
  363. {
  364. _white = new Color32(255, 255, 255, 255);
  365. _button.GetComponent<Image>().sprite = smartArcheryBg[1];
  366. }
  367. else
  368. {
  369. _white = new Color32(59, 59, 59, 255);
  370. _button.GetComponent<Image>().sprite = smartArcheryBg[0];
  371. }
  372. _button.transform.Find("icon").GetComponent<Image>().color = _white;
  373. _button.transform.Find("title").GetComponent<Text>().color = _white;
  374. _button.transform.Find("arrow").GetComponent<Image>().color = _white;
  375. }
  376. }
  377. /// <summary>
  378. /// 一般是切换选择硬件时调用
  379. /// </summary>
  380. public void OnCloseAllPanelStatus() {
  381. bSwitchDevice = false;
  382. for (int i = 0; i < deviceViewItems.Count; i++)
  383. {
  384. deviceViewItems[i].setPanelStatus(false);
  385. }
  386. }
  387. //检测是否选择一个设备情况。用选择框判断
  388. public bool getEnabelPanelStatus() {
  389. bool bHasEnabel = false;
  390. for (int i = 0; i < deviceViewItems.Count; i++)
  391. {
  392. if (deviceViewItems[i].getCurrentPanelEnable()) {
  393. bHasEnabel = true;
  394. break;
  395. }
  396. }
  397. return bHasEnabel;
  398. }
  399. public void onTestClear() {
  400. AimHandler.ins.onClearAimDeviceInfosNew();
  401. }
  402. }