DeviceViewInfrared.cs 14 KB

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