DeviceView.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /* 设备界面 */
  6. public class DeviceView : MonoBehaviour, MenuBackInterface
  7. {
  8. Transform scrollContent;
  9. Transform itemPrefab;
  10. GameObject bowOptions;
  11. GameObject arrowOptions;
  12. Transform btnConnect;
  13. List<DeviceInfo> deviceList;
  14. void Start()
  15. {
  16. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  17. scrollContent = this.transform.Find("ScrollView/Viewport/Content");
  18. itemPrefab = scrollContent.GetChild(0);
  19. itemPrefab.gameObject.SetActive(false);
  20. bowOptions = this.transform.Find("ItemInfo/BowOptions").gameObject;
  21. arrowOptions = this.transform.Find("ItemInfo/ArrowOptions").gameObject;
  22. btnConnect = this.transform.Find("ItemShow/Connect");
  23. deviceList = DeviceMgr.ins.GetMyDeviceList();
  24. //初始化设备列表-并默认点开第一个设备
  25. foreach (DeviceInfo deviceInfo in deviceList)
  26. {
  27. DeviceConfig deviceConfig = deviceInfo.config;
  28. GameObject item = GameObject.Instantiate(itemPrefab.gameObject, scrollContent);
  29. Image modelImage = item.transform.Find("ModelBG/Model").GetComponent<Image>();
  30. modelImage.sprite = Resources.Load<Sprite>("Textures/Device/" + deviceConfig.model);
  31. modelImage.SetNativeSize();
  32. item.transform.Find("TextBG/Text").GetComponent<TextAutoLanguage>().SetText(deviceConfig.name);
  33. item.gameObject.SetActive(true);
  34. item.GetComponent<Button>().onClick.AddListener(delegate() {
  35. if (currentDeviceInfo == deviceInfo) return;
  36. AudioMgr.ins.PlayBtn();
  37. SelectItem(deviceInfo);
  38. });
  39. }
  40. SelectItem(deviceList[0]);
  41. //添加连接按钮监听
  42. btnConnect.GetComponent<Button>().onClick.AddListener(delegate() {
  43. bool changeDevice = !currentDeviceInfo.inuse;
  44. DeviceMgr.ins.UseDevice(currentDeviceInfo);
  45. if (changeDevice) {
  46. HomeView.ins.RenderDeviceNames();
  47. }
  48. if (currentDeviceInfo.config.type == 1) {
  49. BluetoothAim.ins.DoConnect();
  50. }
  51. if (currentDeviceInfo.config.type == 2) {
  52. BluetoothShoot.ins.DoConnect();
  53. }
  54. });
  55. //初始化弓的校准按钮
  56. Button[] bowOptionBtns = bowOptions.GetComponentsInChildren<Button>();
  57. for (int i = 0; i < bowOptionBtns.Length; i++)
  58. {
  59. int optionID = i;
  60. bowOptionBtns[i].onClick.AddListener(delegate() {
  61. AudioMgr.ins.PlayBtn();
  62. if (optionID == 0) {
  63. DeviceCalibrateView.Create(DeviceCalibrateItem.Gyr);
  64. } else if (optionID == 1) {
  65. DeviceCalibrateView.Create(DeviceCalibrateItem.Mag);
  66. } else if (optionID == 2) {
  67. AimHandler.ins.DoIdentity();
  68. }
  69. });
  70. }
  71. //初始化箭的加速计按钮选项
  72. // Button[] arrowOptionBtns = arrowOptions.GetComponentsInChildren<Button>();
  73. // for (int i = 0; i < arrowOptionBtns.Length; i++)
  74. // {
  75. // int optionID = i;
  76. // arrowOptionBtns[i].onClick.AddListener(delegate() {
  77. // AudioMgr.ins.PlayBtn();
  78. // //select
  79. // int acc = 16;
  80. // if (optionID == 1) {
  81. // acc = 64;
  82. // }
  83. // SelectAccForArrow(acc);
  84. // //save
  85. // LoginMgr.myUserInfo.arrowAccValue = acc;
  86. // LoginMgr.myUserInfo.Save();
  87. // //关闭设备连接,用于提醒更换设备
  88. // if (BluetoothShoot.ins.status == BluetoothStatusEnum.ConnectSuccess) {
  89. // BluetoothShoot.ins.DoConnect();
  90. // }
  91. // });
  92. // }
  93. // SelectAccForArrow(LoginMgr.myUserInfo.arrowAccValue);
  94. }
  95. void OnDestroy()
  96. {
  97. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  98. }
  99. void FixedUpdate()
  100. {
  101. UpdateBtnForConnect();
  102. }
  103. public bool OnMenuBack() {
  104. Destroy(gameObject);
  105. return true;
  106. }
  107. BluetoothStatusEnum bowStatus;
  108. // BluetoothStatusEnum arrowStatus;
  109. void UpdateBtnForConnect() {
  110. if (currentDeviceInfo == null) return;
  111. if (currentDeviceInfo.inuse && currentDeviceInfo.config.type == 1) {
  112. if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status) {
  113. bowStatus = BluetoothAim.ins.status;
  114. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
  115. btnConnect.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  116. btnConnect.GetComponentInChildren<Text>().color = color;
  117. if (BluetoothAim.ins.status == BluetoothStatusEnum.Connect) {
  118. btnConnect.GetComponent<Button>().enabled = true;
  119. } else {
  120. btnConnect.GetComponent<Button>().enabled = false;
  121. }
  122. }
  123. }
  124. // if (currentDeviceInfo.inuse && currentDeviceInfo.config.type == 2) {
  125. // if (BluetoothShoot.ins && arrowStatus != BluetoothShoot.ins.status) {
  126. // arrowStatus = BluetoothShoot.ins.status;
  127. // (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothShoot.ins.status);
  128. // btnConnect.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  129. // btnConnect.GetComponentInChildren<Text>().color = color;
  130. // if (BluetoothShoot.ins.status == BluetoothStatusEnum.Connect) {
  131. // btnConnect.GetComponent<Button>().enabled = true;
  132. // } else {
  133. // btnConnect.GetComponent<Button>().enabled = false;
  134. // }
  135. // }
  136. // }
  137. }
  138. void SelectAccForArrow(float acc)
  139. {
  140. for (int i = 0; i < arrowOptions.transform.childCount; i++)
  141. {
  142. Transform t = arrowOptions.transform.GetChild(i);
  143. t.GetComponent<Button>().enabled = true;
  144. t.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonGray");
  145. t.GetComponentInChildren<Text>().color = Color.gray;
  146. }
  147. Transform optionTransform;
  148. Color outColor;
  149. if (acc == 16)
  150. {
  151. optionTransform = arrowOptions.transform.GetChild(0);
  152. optionTransform.GetComponent<Button>().enabled = false;
  153. ColorUtility.TryParseHtmlString("#3D6B03", out outColor);
  154. optionTransform.GetComponentInChildren<Text>().color = outColor;
  155. optionTransform.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonGreen");
  156. }
  157. else if (acc == 64)
  158. {
  159. optionTransform = arrowOptions.transform.GetChild(1);
  160. optionTransform.GetComponent<Button>().enabled = false;
  161. ColorUtility.TryParseHtmlString("#B65A00", out outColor);
  162. optionTransform.GetComponentInChildren<Text>().color = outColor;
  163. optionTransform.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonYellow");
  164. }
  165. }
  166. DeviceInfo currentDeviceInfo;
  167. void SelectItem(DeviceInfo deviceInfo)
  168. {
  169. currentDeviceInfo = deviceInfo;
  170. Image modelImage = this.transform.Find("ItemShow/Model").GetComponent<Image>();
  171. modelImage.sprite = Resources.Load<Sprite>("Textures/Device/" + deviceInfo.config.model);
  172. modelImage.SetNativeSize();
  173. this.transform.Find("ItemInfo/Name").GetComponent<TextAutoLanguage>().SetText(deviceInfo.config.name);
  174. this.transform.Find("ItemInfo/Detail").GetComponent<TextAutoLanguage>().SetText(deviceInfo.config.detail);
  175. Transform stars = this.transform.Find("ItemInfo/Stars");
  176. Image starLight = stars.GetChild(0).GetComponent<Image>();
  177. Image starDark = stars.GetChild(1).GetComponent<Image>();
  178. starLight.gameObject.SetActive(false);
  179. starDark.gameObject.SetActive(false);
  180. for (int i = 1; i <= 5; i++) {
  181. stars.GetChild(i + 2).GetComponent<Image>().sprite =
  182. i <= deviceInfo.config.difficulty ? starLight.sprite : starDark.sprite;
  183. }
  184. bowOptions.SetActive(deviceInfo.config.type == 1);
  185. // arrowOptions.SetActive(deviceInfo.config.type == 2);
  186. //重置连接按钮
  187. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothStatusEnum.Connect);
  188. btnConnect.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  189. btnConnect.GetComponentInChildren<Text>().color = color;
  190. btnConnect.GetComponent<Button>().enabled = true;
  191. bowStatus = BluetoothStatusEnum.None;
  192. // arrowStatus = BluetoothStatusEnum.None;
  193. //是弓才激活连接按钮
  194. btnConnect.gameObject.SetActive(currentDeviceInfo.config.type == 1);
  195. }
  196. public void LeftPointer()
  197. {
  198. AudioMgr.ins.PlayBtn();
  199. int index = deviceList.IndexOf(currentDeviceInfo) - 1;
  200. if (index < 0) {
  201. index = deviceList.Count - 1;
  202. }
  203. SelectItem(deviceList[index]);
  204. }
  205. public void RightPointer()
  206. {
  207. AudioMgr.ins.PlayBtn();
  208. int index = deviceList.IndexOf(currentDeviceInfo) + 1;
  209. if (index >= deviceList.Count) {
  210. index = 0;
  211. }
  212. SelectItem(deviceList[index]);
  213. }
  214. public void Back() {
  215. AudioMgr.ins.PlayBtn();
  216. Destroy(this.gameObject);
  217. }
  218. }