using Assets.Scripts.Devices.Ant; using System.Collections; using System.Collections.Generic; using System; using System.Linq; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using Assets.Scripts.Devices.Ant.Interfaces; using Assets.Scripts.UI.Control; using Assets.Scripts; using Assets.Scripts.UI.Prefab.Device; using Assets.Scripts.Devices; public class ConnectDeviceModal : PFUIPanel { private float timer = 0f; private SensorType mSensorType = SensorType.None; public SensorType SensorType { get { return mSensorType; } set { mSensorType = value; var deviceInfoStr = Utils.deviceDict[value.ToString()]; if (deviceInfoStr != null) { var deviceInfo = deviceInfoStr.Split('|'); if (deviceInfo.Length == 2) { Title.text = App.GetLocalString(deviceInfo[0]); Logo.sprite = Resources.Load(deviceInfo[1]); } } } } private Image Logo; private UnityEngine.Object deviceItem; private VerticalLayoutGroup content; private PfUIButton connectBtn; private Text connectBtnText; private Dictionary deviceList; private Text noDevice,Title; private RectTransform searchIconRect; private GameObject deviceNotFound; protected override void Awake() { base.Awake(); deviceList = new Dictionary(); var container = this.transform.Find("GameObject"); deviceNotFound = container.Find("Panel/Error").gameObject; var closeBtn = container.Find("CloseBtn"); Logo = container.Find("Logo").GetComponent(); Title = container.Find("Title").GetComponent(); var panel = container.Find("Panel"); content = panel.Find("Scroll View").Find("Viewport").Find("Content").GetComponent(); noDevice = container.Find("NoDevice").GetComponent(); noDevice.text = App.GetLocalString("NO DEVICE"); searchIconRect = container.Find("SearchIcon").GetComponent(); _x = searchIconRect.localPosition.x; _y = searchIconRect.localPosition.y; base.SetRounded(panel.GetComponent().transform, 20f); UIManager.AddEvent(closeBtn.gameObject, EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction(e => { this.Close(); //DestroyImmediate(this.gameObject); })); #if UNITY_IOS || UNITY_ANDROID deviceItem = Resources.Load("UI/Prefab/Device/Mobile/DeviceItem"); #else deviceItem = Resources.Load("UI/Prefab/Device/DeviceItem"); #endif connectBtn = container.Find("ConnectBtn").GetComponent(); connectBtnText = connectBtn.transform.Find("Text").GetComponent(); UIManager.AddEvent(connectBtn.gameObject, EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction(e => { if (!connectBtnText.text.Equals(App.GetLocalString("DISCONNECT"))) { foreach (var item in deviceList) { if (item.Value.GetStatus() == false) { if (SensorType == SensorType.SpeedCadence) { if (item.Value.DeviceInfo.Sensor == SensorType.SpeedCadence || item.Value.DeviceInfo.Sensor == SensorType.Cadence) { Debug.Log("断开设备" + item.Value.DeviceInfo.Name); item.Value.DeviceInfo.Disconnect(); DeviceCache.Remove(item.Value.DeviceInfo); } } else if (item.Value.DeviceInfo.Sensor == SensorType) { item.Value.DeviceInfo.Disconnect(); DeviceCache.Remove(item.Value.DeviceInfo); } } } var dd = deviceList.Select(d => d.Value).Where(d => d.GetStatus()).FirstOrDefault(); if (dd != null) { var modal = this.gameObject; dd.DeviceInfo.Connect((error)=> { Utils.showToast(modal, error); }); DeviceCache.Add(dd.DeviceInfo); } } else { foreach (var item in deviceList) { Debug.Log("116" + item.Value.DeviceInfo.Name+ item.Value.GetStatus().ToString()); if (item.Value.GetStatus()) { if (item.Value.DeviceInfo.Sensor == SensorType) { item.Value.DeviceInfo.Disconnect(); Debug.Log("121" + item.Value.DeviceInfo.Name + "断开"); DeviceCache.Remove(item.Value.DeviceInfo); } } } } this.Close(); })); } // Start is called before the first frame update protected override void Start() { SearchDevice(); } // Update is called once per frame void Update() { timer -= Time.deltaTime; if(timer <= 0) { SearchDevice(); timer += 1.0f; } Move(); } private void SearchDevice() { if (SensorType != SensorType.None) { //Debug.Log(AntConnector.Instance().discoveredDevices.Count(d => d.Sensor == SensorType)); var devices = new List(); //var a = AntConnector.Instance().discoveredDevices; switch (SensorType) { case SensorType.SpeedCadence: case SensorType.Cadence: devices = App.MainDeviceAdapter.GetDevices().Where(d => (d.Sensor == SensorType.Cadence || d.Sensor == SensorType.SpeedCadence) || (d is ICadenceDevice && d.State == DeviceState.Connected)).OrderBy(d=>d.Priority).ToList(); break; case SensorType.HeartRate: devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType || (d is IHeartRateDevice && d.State == DeviceState.Connected)).OrderBy(d => d.Priority).ToList(); break; case SensorType.Power: devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType || (d is IPowerDevice && d.State == DeviceState.Connected)).OrderBy(d=>d.Priority).ToList(); break; case SensorType.Speed: devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType || (d is ISpeedDevice && d.State == DeviceState.Connected)).OrderBy(d => d.Priority).ToList(); break; case SensorType.Trainer: devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType && d is ITrainerDevice).ToList(); break; case SensorType.Rower: devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType && d is IRowerDevice).ToList(); break; case SensorType.VirtualPower: break; } foreach (var device in devices) { if (deviceList.ContainsKey(device.Id)) { continue; } var gameObject = (GameObject)Instantiate(deviceItem); gameObject.transform.SetParent(content.transform); gameObject.transform.localScale = new Vector3(1, 1, 1); var deviceItemObj = gameObject.GetComponent(); deviceItemObj.DeviceInfo = device; UIManager.AddEvent(deviceItemObj.gameObject, EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction(e => { deviceItemObj.Set(true); })); //UIManager.AddEvent(deviceItemObj.gameObject, EventTriggerType.Deselect, new UnityEngine.Events.UnityAction(e => //{ // //Debug.Log("de select"); // if(deviceItemObj.DeviceInfo.Sensor == SensorType) // { // deviceItemObj.DeviceInfo.Disconnect(); // } //})); deviceList.Add(device.Id, deviceItemObj); } //删除扫描不到的设备 var needRemove = deviceList.Where(q => !devices.Select(n => n.Id).Contains(q.Key)).ToList(); if (needRemove.Count > 0) { var currentlist = FindObjectsOfType(); foreach (var item in needRemove) { deviceList.Remove(item.Key); var current = currentlist.Where(e => e.Id == item.Key).FirstOrDefault(); if (current != null) { current.gameObject.Destroy(true); } } } if(deviceList.All(d=>d.Value.GetStatus() == false)) { var firstDevice = deviceList.Select(d => d.Value).Where(d => d.DeviceInfo.State == DeviceState.Connected).OrderByDescending(d => d.DeviceInfo.Priority).FirstOrDefault(); if (firstDevice != null) { firstDevice.Set(true); } else { var defaultDevice = deviceList.FirstOrDefault(); if (defaultDevice.Value != null) { defaultDevice.Value.Set(true); } } //DeviceItem firstDevice; //var devicesTemp = deviceList.Select(d => d.Value).Where(d => d.DeviceInfo.State == DeviceState.Connected); //if(SensorType == SensorType.Cadence || SensorType == SensorType.SpeedCadence) //{ // firstDevice = devicesTemp.FirstOrDefault(d=>d) //} } //如果没有可连接的设备按钮只读 connectBtn.mButton.enabled = deviceList.Count > 0; connectBtn.mButton.interactable = deviceList.Count > 0; deviceNotFound.SetActive(deviceList.Count <= 0); } } //选中需要连接的设备 public void SelectDevice(DeviceItem deviceItemObj) { var trainer = SensorType == SensorType.Trainer && deviceItemObj.DeviceInfo.State == DeviceState.Connected; var nontrainer = deviceItemObj.DeviceInfo.Sensor != SensorType.Trainer && SensorType != SensorType.Trainer && deviceItemObj.DeviceInfo.State == DeviceState.Connected; if (trainer || nontrainer) { connectBtnText.text = App.GetLocalString("DISCONNECT"); connectBtnText.GetComponent().key = "DISCONNECT"; } else { connectBtnText.text = App.GetLocalString("CONNECT"); connectBtnText.GetComponent().key = "CONNECT"; } foreach (var item in deviceList) { if (item.Key != deviceItemObj.DeviceInfo.Id) { item.Value.Set(false); } } noDevice.text = deviceItemObj.DeviceInfo.Name; if (deviceItemObj.DeviceInfo.State == DeviceState.Connected) { noDevice.color = Color.white; } else { noDevice.color = Utils.HexToColor("5C5C6E"); } } public override void Show() { base.Show(); } public override void Close() { base.Close(); //DestroyImmediate(this.gameObject); Destroy(this.gameObject); } float time = 0; float _x = 0; float _y = 0; /// /// 做search动画 /// void Move() { var hudu = (2 * Mathf.PI / 360) * 1 * time; var x = _x + Mathf.Sin(hudu) * 8f; var y = _y + Mathf.Cos(hudu) * 8f; var rect = searchIconRect.localPosition; //rect.x = x; //rect.y = y; rect.x = x; rect.y = y; searchIconRect.localPosition = rect; time += 1.8f; //Debug.Log(x + ", " + y); //yield return null; } }