DeviceViewInfrared.cs 16 KB

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