DeviceViewInfrared.cs 12 KB

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