DeviceViewInfrared.cs 14 KB

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