DeviceViewInfrared.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. bool isInfraredDevices = selectDeviceViewItem.bInfraredDevices();
  113. if (smartConnect2Buttons[0].gameObject.activeSelf != isInfraredDevices)
  114. smartConnect2Buttons[0].gameObject.SetActive(isInfraredDevices);
  115. }
  116. else {
  117. if (!Connect1Parent.activeSelf) Connect1Parent.SetActive(true);
  118. if (Connect2Parent.activeSelf) Connect2Parent.SetActive(false);
  119. }
  120. //if (!selectDeviceViewItem.bInfraredDevices())
  121. DeviceTypeHelper.DeviceType detectedType = DeviceTypeHelper.DetectDeviceType();
  122. if (CommonConfig.bDisplayTwoPlayerGames && detectedType == DeviceTypeHelper.DeviceType.iPhone && !selectDeviceViewItem.bInfraredDevices())
  123. {
  124. //2p 按钮 ,两个同时蓝牙连接后。隐藏
  125. if (deviceViewItems[1].gameObject.activeSelf || deviceViewItems[0].getBLEConnectState() && deviceViewItems[1].getBLEConnectState())
  126. {
  127. if (smartConnect2Buttons[3].gameObject.activeSelf)
  128. {
  129. smartConnect2Buttons[3].gameObject.SetActive(false);
  130. }
  131. }
  132. else if (!smartConnect2Buttons[3].gameObject.activeSelf)
  133. {
  134. smartConnect2Buttons[3].gameObject.SetActive(true);
  135. }
  136. }
  137. else
  138. {
  139. //bDisplayTwoPlayerGames控制和ipad暂时隐藏2p
  140. if (smartConnect2Buttons[3].gameObject.activeSelf)
  141. {
  142. smartConnect2Buttons[3].gameObject.SetActive(false);
  143. }
  144. }
  145. }
  146. }
  147. void ShowDeviceViewItemsTwo(bool bShow) {
  148. //显示第二个设备,如果第二个玩家,才走true这里
  149. if (bShow)
  150. {
  151. if (!deviceViewItems[1].gameObject.activeSelf)
  152. {
  153. deviceViewItems[1].gameObject.SetActive(true);
  154. deviceViewItems[1].onShowDeviceInfo();//手动刷新一下信息
  155. horizontalLayoutGroup.spacing = 40;
  156. //显示2p时候。默认选择中
  157. deviceViewItems[1].OnSelectEvent();
  158. }
  159. }
  160. else
  161. {
  162. if (deviceViewItems[1].gameObject.activeSelf)
  163. {
  164. deviceViewItems[1].gameObject.SetActive(false);
  165. horizontalLayoutGroup.spacing = 76;
  166. deviceViewItems[0].OnSelectEvent();
  167. }
  168. }
  169. }
  170. public bool OnMenuBack() {
  171. ViewMgr.Instance.DestroyView<DeviceViewInfrared>();
  172. return true;
  173. }
  174. public void OnClick_Back() {
  175. AudioMgr.ins.PlayBtn();
  176. ViewMgr.Instance.DestroyView<DeviceViewInfrared>();
  177. }
  178. /// <summary>
  179. /// 第一次进入页面选择的按钮的下标
  180. /// </summary>
  181. /// <param name="index"></param>
  182. public void OnChangeSmartConnect1Button(int index)
  183. {
  184. bool _selected = false;
  185. for (int i = 0; i < smartConnect1Buttons.Count; i++)
  186. {
  187. Button _button = smartConnect1Buttons[i];
  188. Color32 _white;
  189. if (index == i)
  190. {
  191. _white = new Color32(255, 255, 255, 255);
  192. _selected = true;
  193. _button.GetComponent<Image>().sprite = smartArcheryBg[1];
  194. }
  195. else
  196. {
  197. _white = new Color32(59, 59, 59, 255);
  198. //_buttonBg = new Color32(255, 255, 255, 255);
  199. _button.GetComponent<Image>().sprite = smartArcheryBg[0];
  200. }
  201. _button.transform.Find("icon").GetComponent<Image>().color = _white;
  202. _button.transform.Find("title").GetComponent<Text>().color = _white;
  203. _button.transform.Find("arrow").GetComponent<Image>().color = _white;
  204. }
  205. if (_selected)
  206. {
  207. //进入选中的页面
  208. AudioMgr.ins.PlayBtn();
  209. //设置一次 GlobalData.MyDeviceMode
  210. if (getEnabelPanelStatus())//是否处于选中状态
  211. {
  212. //取消正在连接的连接
  213. BluetoothAim.ins.onCancelAllConnecting(BluetoothStatusEnum.Connect);
  214. if (index == 0)
  215. {
  216. ViewMgr.Instance.ShowView<SmartArcheryView>();
  217. }
  218. else if (index == 1)
  219. {
  220. Debug.Log("Gun");
  221. ViewMgr.Instance.ShowView<SmartGunView>();
  222. }
  223. bSwitchDevice = false;
  224. AimHandler.ins.onCreateTempAimDeviceInfo();
  225. }
  226. }
  227. }
  228. /// <summary>
  229. /// 第二次进入页面时候。
  230. /// </summary>
  231. /// <param name="index"></param>
  232. public void OnChangeSmartConnect2Button(int index)
  233. {
  234. //Debug.Log("OnChangeSmartConnect1Button按钮:" + index);
  235. bool _selected = false;
  236. for (int i = 0; i < smartConnect2Buttons.Count; i++)
  237. {
  238. Button _button = smartConnect2Buttons[i];
  239. Color32 _white;
  240. //Color32 _buttonBg;
  241. if (index == i)
  242. {
  243. _white = new Color32(255, 255, 255, 255);
  244. //_buttonBg = new Color32(16, 194, 198, 255);
  245. _selected = true;
  246. _button.GetComponent<Image>().sprite = smartArcheryBg[1];
  247. }
  248. else
  249. {
  250. _white = new Color32(59, 59, 59, 255);
  251. //_buttonBg = new Color32(255, 255, 255, 255);
  252. _button.GetComponent<Image>().sprite = smartArcheryBg[0];
  253. }
  254. _button.transform.Find("icon").GetComponent<Image>().color = _white;
  255. _button.transform.Find("title").GetComponent<Text>().color = _white;
  256. _button.transform.Find("arrow").GetComponent<Image>().color = _white;
  257. }
  258. if (_selected)
  259. {
  260. //进入选中的页面
  261. AudioMgr.ins.PlayBtn();
  262. //设置一次 GlobalData.MyDeviceMode
  263. switch (index)
  264. {
  265. case 0:
  266. if (selectDeviceViewItem.getBLEConnectState())
  267. {
  268. if (BluetoothAim.ins.isMainConnectToInfraredDevice() || BluetoothAim.ins.isMainConnectToGun())
  269. {
  270. //进入屏幕定位
  271. ViewManager2.ShowView(ViewManager2.Path_InfraredView);
  272. }
  273. else {
  274. //只有红外设备才能进行屏幕识别
  275. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("Connect_Tip_Infrared"));
  276. }
  277. }
  278. else {
  279. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("Connect_Tip"));
  280. }
  281. break;
  282. case 1:
  283. //连接设备
  284. if (getEnabelPanelStatus())//是否处于选中状态
  285. {
  286. if (selectDeviceViewItem)
  287. {
  288. //有信息的情况下直接连接
  289. selectDeviceViewItem.OnConnectedEvent();
  290. }
  291. else {
  292. //取消正在连接的连接
  293. BluetoothAim.ins.onCancelAllConnecting(BluetoothStatusEnum.Connect);
  294. //进入弓箭选择页面
  295. ViewMgr.Instance.ShowView<SmartArcheryView>();
  296. AimHandler.ins.onCreateTempAimDeviceInfo();
  297. }
  298. }
  299. break;
  300. case 2:
  301. //断开连接设备
  302. if (selectDeviceViewItem)
  303. {
  304. selectDeviceViewItem.OnDisConnectedEvent();
  305. }
  306. break;
  307. case 3:
  308. // if (!(BluetoothAim.ins && (BluetoothAim.ins.isMainConnectToInfraredDevice() || BluetoothAim.ins.isMainConnectToGun())))
  309. //目前枪不能增加2p
  310. if (!(BluetoothAim.ins && BluetoothAim.ins.isMainConnectToGun()))
  311. {
  312. ShowDeviceViewItemsTwo(true);
  313. //重置一些按钮状态和参数
  314. OnChangeSmartConnect1Button(-1);
  315. OnChangeSmartConnect2Button(-1);
  316. }
  317. break;
  318. case 4:
  319. //切换设备前先断开蓝牙连接
  320. if (selectDeviceViewItem)
  321. {
  322. selectDeviceViewItem.OnDisConnectedEvent();
  323. }
  324. bSwitchDevice = true;
  325. //重置一些按钮状态和参数
  326. OnChangeSmartConnect1Button(-1);
  327. OnChangeSmartConnect2Button(-1);
  328. break;
  329. }
  330. }
  331. }
  332. //暂时先更新一个
  333. public void RenderBattery(int deviceID, int value)
  334. {
  335. //smartConnect1Buttons[0].GetComponent<DeviceView_ItemShow>().RenderBattery(deviceID,value);
  336. }
  337. public void OnChangeSmartArcheryButtonState(int index)
  338. {
  339. for (int i = 0; i < smartConnect1Buttons.Count; i++)
  340. {
  341. Button _button = smartConnect1Buttons[i];
  342. Color32 _white;
  343. //Color32 _buttonBg;
  344. if (index == i)
  345. {
  346. _white = new Color32(255, 255, 255, 255);
  347. _button.GetComponent<Image>().sprite = smartArcheryBg[1];
  348. }
  349. else
  350. {
  351. _white = new Color32(59, 59, 59, 255);
  352. _button.GetComponent<Image>().sprite = smartArcheryBg[0];
  353. }
  354. _button.transform.Find("icon").GetComponent<Image>().color = _white;
  355. _button.transform.Find("title").GetComponent<Text>().color = _white;
  356. _button.transform.Find("arrow").GetComponent<Image>().color = _white;
  357. }
  358. }
  359. public void OnChangeSmartConnect2ButtonState(int index)
  360. {
  361. for (int i = 0; i < smartConnect2Buttons.Count; i++)
  362. {
  363. Button _button = smartConnect2Buttons[i];
  364. Color32 _white;
  365. //Color32 _buttonBg;
  366. if (index == i)
  367. {
  368. _white = new Color32(255, 255, 255, 255);
  369. _button.GetComponent<Image>().sprite = smartArcheryBg[1];
  370. }
  371. else
  372. {
  373. _white = new Color32(59, 59, 59, 255);
  374. _button.GetComponent<Image>().sprite = smartArcheryBg[0];
  375. }
  376. _button.transform.Find("icon").GetComponent<Image>().color = _white;
  377. _button.transform.Find("title").GetComponent<Text>().color = _white;
  378. _button.transform.Find("arrow").GetComponent<Image>().color = _white;
  379. }
  380. }
  381. /// <summary>
  382. /// 一般是切换选择硬件时调用
  383. /// </summary>
  384. public void OnCloseAllPanelStatus() {
  385. bSwitchDevice = false;
  386. for (int i = 0; i < deviceViewItems.Count; i++)
  387. {
  388. deviceViewItems[i].setPanelStatus(false);
  389. }
  390. }
  391. //检测是否选择一个设备情况。用选择框判断
  392. public bool getEnabelPanelStatus() {
  393. bool bHasEnabel = false;
  394. for (int i = 0; i < deviceViewItems.Count; i++)
  395. {
  396. if (deviceViewItems[i].getCurrentPanelEnable()) {
  397. bHasEnabel = true;
  398. break;
  399. }
  400. }
  401. return bHasEnabel;
  402. }
  403. public void onTestClear() {
  404. AimHandler.ins.onClearAimDeviceInfosNew();
  405. }
  406. }